Skip to content

Commit

Permalink
Merge pull request #3037 from Bilb/fix/ses-1278-quote-msg-not-found
Browse files Browse the repository at this point in the history
fix: add quoted msg to redux quotes when receiving quoted msg
  • Loading branch information
Bilb committed Mar 18, 2024
2 parents 91706bf + b954b37 commit 290cb55
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
42 changes: 28 additions & 14 deletions ts/receiver/queuedJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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 (
Expand All @@ -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];
Expand All @@ -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;
Expand Down
54 changes: 35 additions & 19 deletions ts/state/ducks/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1111,6 +1132,7 @@ export const {
resetOldTopMessageId,
resetOldBottomMessageId,
markConversationFullyRead,
pushQuotedMessageDetails,
// layout stuff
showMessageInfoView,
openRightPanel,
Expand Down Expand Up @@ -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;
}

0 comments on commit 290cb55

Please sign in to comment.