Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add quoted msg to redux quotes when receiving quoted msg #3037

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
yougotwill marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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 @@ -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;
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -907,6 +911,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 @@ -1113,6 +1134,7 @@ export const {
resetOldTopMessageId,
resetOldBottomMessageId,
markConversationFullyRead,
pushQuotedMessageDetails,
// layout stuff
showMessageInfoView,
openRightPanel,
Expand Down Expand Up @@ -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;
}
Loading