From b954b3746035b89eb83ae5c81580a59dae08b6bd Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 22 Feb 2024 16:48:04 +1100 Subject: [PATCH] fix: add quoted msg to redux quotes when receiving quoted msg --- ts/receiver/queuedJob.ts | 42 ++++++++++++++++--------- ts/state/ducks/conversations.ts | 54 +++++++++++++++++++++------------ 2 files changed, 63 insertions(+), 33 deletions(-) diff --git a/ts/receiver/queuedJob.ts b/ts/receiver/queuedJob.ts index f92b0ec74..9823a5a55 100644 --- a/ts/receiver/queuedJob.ts +++ b/ts/receiver/queuedJob.ts @@ -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; diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index 35040ccde..4a9d73b2d 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -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 @@ -409,7 +413,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; } } } @@ -611,10 +615,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 { @@ -907,6 +911,23 @@ const conversationsSlice = createSlice({ oldBottomMessageId: null, }; }, + pushQuotedMessageDetails( + state: ConversationsStateType, + action: PayloadAction + ) { + 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; @@ -1113,6 +1134,7 @@ export const { resetOldTopMessageId, resetOldBottomMessageId, markConversationFullyRead, + pushQuotedMessageDetails, // layout stuff showMessageInfoView, openRightPanel, @@ -1211,23 +1233,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; }