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

pull/3037/head
Audric Ackermann 2 years ago
parent 8019cb43a5
commit b954b37460

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

@ -353,6 +353,10 @@ export type MentionsMembersType = Array<{
authorProfileName: string; authorProfileName: string;
}>; }>;
function buildQuoteId(sender: string, timestamp: number) {
return `${timestamp}-${sender}`;
}
/** /**
* Fetches the messages for a conversation to put into redux. * Fetches the messages for a conversation to put into redux.
* @param conversationKey - the id of the conversation * @param conversationKey - the id of the conversation
@ -409,7 +413,7 @@ async function getMessages({
const timestamp = quotedMessage.propsForMessage.timestamp; const timestamp = quotedMessage.propsForMessage.timestamp;
const sender = quotedMessage.propsForMessage.sender; const sender = quotedMessage.propsForMessage.sender;
if (timestamp && sender) { if (timestamp && sender) {
quotesProps[`${timestamp}-${sender}`] = quotedMessage; quotesProps[buildQuoteId(sender, timestamp)] = quotedMessage;
} }
} }
} }
@ -611,10 +615,10 @@ function handleMessageExpiredOrDeleted(
if (timestamp && sender) { if (timestamp && sender) {
const message2Delete = lookupQuote(editedQuotes, editedMessages, timestamp, sender); const message2Delete = lookupQuote(editedQuotes, editedMessages, timestamp, sender);
window.log.debug( 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 { return {
@ -907,6 +911,23 @@ const conversationsSlice = createSlice({
oldBottomMessageId: null, 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) { resetOldTopMessageId(state: ConversationsStateType) {
state.oldTopMessageId = null; state.oldTopMessageId = null;
return state; return state;
@ -1113,6 +1134,7 @@ export const {
resetOldTopMessageId, resetOldTopMessageId,
resetOldBottomMessageId, resetOldBottomMessageId,
markConversationFullyRead, markConversationFullyRead,
pushQuotedMessageDetails,
// layout stuff // layout stuff
showMessageInfoView, showMessageInfoView,
openRightPanel, openRightPanel,
@ -1211,23 +1233,17 @@ export function lookupQuote(
timestamp: number, timestamp: number,
author: string author: string
): MessageModelPropsWithoutConvoProps | undefined { ): MessageModelPropsWithoutConvoProps | undefined {
let sourceMessage = quotes[`${timestamp}-${author}`]; const sourceMessage = quotes[buildQuoteId(author, timestamp)];
if (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. // 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 foundMessageToQuote = messages.find(message => {
const quotedMessages = messages.filter(message => {
const msgProps = message.propsForMessage; const msgProps = message.propsForMessage;
return msgProps.timestamp === timestamp && msgProps.sender === author; return msgProps.timestamp === timestamp && msgProps.sender === author;
}); });
if (quotedMessages?.length) { return foundMessageToQuote;
for (const quotedMessage of quotedMessages) {
if (quotedMessage) {
sourceMessage = quotedMessage;
}
}
}
}
return sourceMessage;
} }

Loading…
Cancel
Save