Merge pull request #3037 from Bilb/fix/ses-1278-quote-msg-not-found

fix: add quoted msg to redux quotes when receiving quoted msg
pull/3057/head
Audric Ackermann 2 months ago committed by GitHub
commit 290cb554e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -14,7 +14,11 @@ import { DisappearingMessages } from '../session/disappearing_messages';
import { ProfileManager } from '../session/profile_manager/ProfileManager';
import { PubKey } from '../session/types';
import { UserUtils } from '../session/utils';
import { PropsForMessageWithoutConvoProps, lookupQuote } from '../state/ducks/conversations';
import {
MessageModelPropsWithoutConvoProps,
lookupQuote,
pushQuotedMessageDetails,
} from '../state/ducks/conversations';
import { showMessageRequestBannerOutsideRedux } from '../state/ducks/userConfig';
import { getHideMessageRequestBannerOutsideRedux } from '../state/selectors/userConfig';
import { GoogleChrome } from '../util';
@ -25,6 +29,12 @@ function contentTypeSupported(type: string): boolean {
return Chrome.isImageTypeSupported(type) || Chrome.isVideoTypeSupported(type);
}
function isMessageModel(
msg: MessageModel | MessageModelPropsWithoutConvoProps
): msg is MessageModel {
return (msg as MessageModel).get !== undefined;
}
/**
* Note: this function does not trigger a write to the db nor trigger redux update.
* You have to call msg.commit() once you are done with the handling of this message
@ -53,12 +63,12 @@ async function copyFromQuotedMessage(
// First we try to look for the quote in memory
const stateConversations = window.inboxStore?.getState().conversations;
const { messages, quotes } = stateConversations;
let quotedMessage: PropsForMessageWithoutConvoProps | MessageModel | undefined = lookupQuote(
let quotedMessage: MessageModelPropsWithoutConvoProps | MessageModel | undefined = lookupQuote(
quotes,
messages,
id,
quote.author
)?.propsForMessage;
);
// If the quote is not found in memory, we try to find it in the DB
if (!quotedMessage) {
@ -83,15 +93,19 @@ async function copyFromQuotedMessage(
return;
}
const isMessageModelType = Boolean((quotedMessage as MessageModel).get !== undefined);
window?.log?.info(`Found quoted message id: ${id}`);
quoteLocal.referencedMessageNotFound = false;
// NOTE we send the entire body to be consistent with the other platforms
quoteLocal.text =
(isMessageModelType
? (quotedMessage as MessageModel).get('body')
: (quotedMessage as PropsForMessageWithoutConvoProps).text) || '';
(isMessageModel(quotedMessage)
? quotedMessage.get('body')
: quotedMessage.propsForMessage.text) || '';
if (isMessageModel(quotedMessage)) {
window.inboxStore.dispatch(pushQuotedMessageDetails(quotedMessage.getMessageModelProps()));
} else {
window.inboxStore.dispatch(pushQuotedMessageDetails(quotedMessage));
}
// no attachments, just save the quote with the body
if (
@ -106,9 +120,9 @@ async function copyFromQuotedMessage(
firstAttachment.thumbnail = null;
const queryAttachments =
(isMessageModelType
? (quotedMessage as MessageModel).get('attachments')
: (quotedMessage as PropsForMessageWithoutConvoProps).attachments) || [];
(isMessageModel(quotedMessage)
? quotedMessage.get('attachments')
: quotedMessage.propsForMessage.attachments) || [];
if (queryAttachments.length > 0) {
const queryFirst = queryAttachments[0];
@ -123,9 +137,9 @@ async function copyFromQuotedMessage(
}
const queryPreview =
(isMessageModelType
? (quotedMessage as MessageModel).get('preview')
: (quotedMessage as PropsForMessageWithoutConvoProps).previews) || [];
(isMessageModel(quotedMessage)
? quotedMessage.get('preview')
: quotedMessage.propsForMessage.previews) || [];
if (queryPreview.length > 0) {
const queryFirst = queryPreview[0];
const { image } = queryFirst;

@ -353,6 +353,10 @@ export type MentionsMembersType = Array<{
authorProfileName: string;
}>;
function buildQuoteId(sender: string, timestamp: number) {
return `${timestamp}-${sender}`;
}
/**
* Fetches the messages for a conversation to put into redux.
* @param conversationKey - the id of the conversation
@ -407,7 +411,7 @@ async function getMessages({
const timestamp = quotedMessage.propsForMessage.timestamp;
const sender = quotedMessage.propsForMessage.sender;
if (timestamp && sender) {
quotesProps[`${timestamp}-${sender}`] = quotedMessage;
quotesProps[buildQuoteId(sender, timestamp)] = quotedMessage;
}
}
}
@ -609,10 +613,10 @@ function handleMessageExpiredOrDeleted(
if (timestamp && sender) {
const message2Delete = lookupQuote(editedQuotes, editedMessages, timestamp, sender);
window.log.debug(
`Deleting quote {${timestamp}-${sender}} ${JSON.stringify(message2Delete)}`
`Deleting quote {${buildQuoteId(sender, timestamp)}} ${JSON.stringify(message2Delete)}`
);
delete editedQuotes[`${timestamp}-${sender}`];
delete editedQuotes[buildQuoteId(sender, timestamp)];
}
return {
@ -905,6 +909,23 @@ const conversationsSlice = createSlice({
oldBottomMessageId: null,
};
},
pushQuotedMessageDetails(
state: ConversationsStateType,
action: PayloadAction<MessageModelPropsWithoutConvoProps>
) {
const { payload } = action;
if (state.selectedConversation === payload.propsForMessage.convoId) {
const builtId = buildQuoteId(
payload.propsForMessage.sender,
payload.propsForMessage.timestamp
);
if (state.quotes[builtId]) {
return state;
}
state.quotes[builtId] = payload;
}
return state;
},
resetOldTopMessageId(state: ConversationsStateType) {
state.oldTopMessageId = null;
return state;
@ -1111,6 +1132,7 @@ export const {
resetOldTopMessageId,
resetOldBottomMessageId,
markConversationFullyRead,
pushQuotedMessageDetails,
// layout stuff
showMessageInfoView,
openRightPanel,
@ -1207,23 +1229,17 @@ export function lookupQuote(
timestamp: number,
author: string
): MessageModelPropsWithoutConvoProps | undefined {
let sourceMessage = quotes[`${timestamp}-${author}`];
const sourceMessage = quotes[buildQuoteId(author, timestamp)];
// NOTE If a quote is processed but we haven't triggered a render, the quote might not be in the lookup map yet so we check the messages in memory.
if (!sourceMessage) {
const quotedMessages = messages.filter(message => {
const msgProps = message.propsForMessage;
return msgProps.timestamp === timestamp && msgProps.sender === author;
});
if (quotedMessages?.length) {
for (const quotedMessage of quotedMessages) {
if (quotedMessage) {
sourceMessage = quotedMessage;
}
}
}
if (sourceMessage) {
return sourceMessage;
}
return sourceMessage;
// NOTE If a quote is processed but we haven't triggered a render, the quote might not be in the lookup map yet so we check the messages in memory.
const foundMessageToQuote = messages.find(message => {
const msgProps = message.propsForMessage;
return msgProps.timestamp === timestamp && msgProps.sender === author;
});
return foundMessageToQuote;
}

Loading…
Cancel
Save