fix: when we receive and make a quote message do an in-memory lookup before looking in the db

this can fix original message not found  errors when there are lot of unread messages and one of them is quoted
pull/2817/head
William Grant 2 years ago
parent ec620d06f7
commit c08dcc7ec6

@ -64,8 +64,13 @@ export const MessageQuote = (props: Props) => {
// 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 (quoteNotFound && quote.id && quote.author) { if (quoteNotFound && quote.id && quote.author) {
// We always look for the quote by sentAt timestamp, for opengroups, closed groups and session chats
// this will return an array of sent messages by id that we have locally.
const quotedMessagesCollection = await Data.getMessagesBySenderAndSentAt([ const quotedMessagesCollection = await Data.getMessagesBySenderAndSentAt([
{ timestamp: toNumber(quote.id), source: quote.author }, {
timestamp: toNumber(quote.id),
source: quote.author,
},
]); ]);
if (quotedMessagesCollection?.length) { if (quotedMessagesCollection?.length) {

@ -16,6 +16,7 @@ import { getHideMessageRequestBannerOutsideRedux } from '../state/selectors/user
import { GoogleChrome } from '../util'; import { GoogleChrome } from '../util';
import { LinkPreviews } from '../util/linkPreviews'; import { LinkPreviews } from '../util/linkPreviews';
import { ReleasedFeatures } from '../util/releaseFeature'; import { ReleasedFeatures } from '../util/releaseFeature';
import { PropsForMessageWithoutConvoProps, lookupQuote } from '../state/ducks/conversations';
function contentTypeSupported(type: string): boolean { function contentTypeSupported(type: string): boolean {
const Chrome = GoogleChrome; const Chrome = GoogleChrome;
@ -48,26 +49,48 @@ async function copyFromQuotedMessage(
const id = _.toNumber(quoteId); const id = _.toNumber(quoteId);
// 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(
quotes,
messages,
id,
quote.author
)?.propsForMessage;
// If the quote is not found in memory, we try to find it in the DB
if (!quotedMessage) {
// We always look for the quote by sentAt timestamp, for opengroups, closed groups and session chats // We always look for the quote by sentAt timestamp, for opengroups, closed groups and session chats
// this will return an array of sent message by id we have locally. // this will return an array of sent messages by id that we have locally.
const quotedMessagesCollection = await Data.getMessagesBySenderAndSentAt([
{
timestamp: id,
source: quote.author,
},
]);
const collection = await Data.getMessagesBySentAt(id); if (quotedMessagesCollection?.length) {
// we now must make sure this is the sender we expect quotedMessage = quotedMessagesCollection.at(0);
const found = collection.find(message => { }
return Boolean(author === message.get('source')); }
});
if (!found) { if (!quotedMessage) {
window?.log?.warn(`We did not found quoted message ${id} with author ${author}.`); window?.log?.warn(`We did not found quoted message ${id} with author ${author}.`);
quoteLocal.referencedMessageNotFound = true; quoteLocal.referencedMessageNotFound = true;
msg.set({ quote: quoteLocal }); msg.set({ quote: quoteLocal });
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 = found.get('body') || ''; quoteLocal.text =
(isMessageModelType
? (quotedMessage as MessageModel).get('body')
: (quotedMessage as PropsForMessageWithoutConvoProps).text) || '';
// no attachments, just save the quote with the body // no attachments, just save the quote with the body
if ( if (
@ -81,7 +104,10 @@ async function copyFromQuotedMessage(
firstAttachment.thumbnail = null; firstAttachment.thumbnail = null;
const queryAttachments = found.get('attachments') || []; const queryAttachments =
(isMessageModelType
? (quotedMessage as MessageModel).get('attachments')
: (quotedMessage as PropsForMessageWithoutConvoProps).attachments) || [];
if (queryAttachments.length > 0) { if (queryAttachments.length > 0) {
const queryFirst = queryAttachments[0]; const queryFirst = queryAttachments[0];
@ -95,7 +121,10 @@ async function copyFromQuotedMessage(
} }
} }
const queryPreview = found.get('preview') || []; const queryPreview =
(isMessageModelType
? (quotedMessage as MessageModel).get('preview')
: (quotedMessage as PropsForMessageWithoutConvoProps).previews) || [];
if (queryPreview.length > 0) { if (queryPreview.length > 0) {
const queryFirst = queryPreview[0]; const queryFirst = queryPreview[0];
const { image } = queryFirst; const { image } = queryFirst;

@ -1161,6 +1161,7 @@ export async function openConversationToSpecificMessage(args: {
/** /**
* Look for quote matching the timestamp and author in the quote lookup map * Look for quote matching the timestamp and author in the quote lookup map
* @param quotes - the lookup map of the selected conversations quotes * @param quotes - the lookup map of the selected conversations quotes
* @param messages - the messages in memory for the selected conversation
* @param author - the pubkey of the quoted author * @param author - the pubkey of the quoted author
* @param timestamp - usually the id prop on the quote object of a message * @param timestamp - usually the id prop on the quote object of a message
* @returns - the message model if found, undefined otherwise * @returns - the message model if found, undefined otherwise

Loading…
Cancel
Save