From 331a4e1e121ff0fb9033f2b0faec2c0a647ec97c Mon Sep 17 00:00:00 2001 From: William Grant Date: Tue, 2 May 2023 12:06:54 +1000 Subject: [PATCH] fix: resolved object is not extensible bug forEach and async still don't play nice so used a regular for loop, added QuoteLookupType --- ts/models/conversation.ts | 2 +- ts/state/ducks/conversations.ts | 79 +++++++++++++++++---------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/ts/models/conversation.ts b/ts/models/conversation.ts index a6f058892..845c5daa6 100644 --- a/ts/models/conversation.ts +++ b/ts/models/conversation.ts @@ -22,7 +22,7 @@ import { ToastUtils, UserUtils } from '../session/utils'; import { BlockedNumberController } from '../util'; import { leaveClosedGroup } from '../session/group/closed-group'; import { SignalService } from '../protobuf'; -import { MessageModel, sliceQuoteText } from './message'; +import { MessageModel } from './message'; import { MessageAttributesOptionals, MessageDirection } from './messageType'; import autoBind from 'auto-bind'; diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts index f27f9d694..ac6b73721 100644 --- a/ts/state/ducks/conversations.ts +++ b/ts/state/ducks/conversations.ts @@ -284,6 +284,10 @@ export type ConversationLookupType = { [key: string]: ReduxConversationType; }; +export type QuoteLookupType = { + [key: string]: MessageModelPropsWithoutConvoProps; +}; + export type ConversationsStateType = { conversationLookup: ConversationLookupType; selectedConversation?: string; @@ -291,7 +295,7 @@ export type ConversationsStateType = { messages: Array; // NOTE the quotes that are in view // key is message [timestamp]-[author-pubkey] - quotes: Record; + quotes: QuoteLookupType; firstUnreadMessageId: string | undefined; messageDetailProps?: MessagePropsDetails; showRightPanel: boolean; @@ -345,7 +349,7 @@ async function getMessages({ messageId: string | null; }): Promise<{ messagesProps: Array; - quotesProps: Record; + quotesProps: QuoteLookupType; }> { const beforeTimestamp = Date.now(); @@ -366,42 +370,41 @@ async function getMessages({ const time = Date.now() - beforeTimestamp; window?.log?.info(`Loading ${messagesProps.length} messages took ${time}ms to load.`); - const quotesProps: Record = {}; - messagesProps - .filter( - message => message.propsForMessage?.quote?.messageId && message.propsForMessage.quote?.sender - ) - .forEach(async message => { - const id = message.propsForMessage?.quote?.messageId; - const sender = message.propsForMessage.quote?.sender; - - // TODO use this is the renderering process - // const contact = message.findAndFormatContact(author); - // const authorName = contact?.profileName || contact?.name || ''; - - if (id && sender) { - const timestamp = Number(id); - // See if message is already in memory if not lookup in db - let results = []; - results = messagesProps.filter( - message => - message.propsForMessage.timestamp === timestamp && - message.propsForMessage.sender === sender - ); - - if (results.length) { - message = results[0]; - } else { - const dbResult = ( - await Data.getMessageBySenderAndTimestamp({ source: sender, timestamp }) - )?.getMessageModelProps(); - if (dbResult) { - message = dbResult; - } + const quotesProps: QuoteLookupType = {}; + const quotes = messagesProps.filter( + message => message.propsForMessage?.quote?.messageId && message.propsForMessage.quote?.sender + ); + + for (let i = 0; i < quotes.length; i++) { + const id = quotes[i].propsForMessage?.quote?.messageId; + const sender = quotes[i].propsForMessage.quote?.sender; + + // TODO use this is the renderering process + // const contact = message.findAndFormatContact(author); + // const authorName = contact?.profileName || contact?.name || ''; + + if (id && sender) { + const timestamp = Number(id); + // See if a quoted message is already in memory if not lookup in db + let results = messagesProps.filter( + message => + message.propsForMessage.timestamp === timestamp && + message.propsForMessage.sender === sender + ); + + if (!results.length) { + const dbResult = ( + await Data.getMessageBySenderAndTimestamp({ source: sender, timestamp }) + )?.getMessageModelProps(); + if (dbResult) { + results = [dbResult]; } - quotesProps[`${timestamp}-${sender}`] = message; } - }); + quotesProps[`${timestamp}-${sender}`] = results[0]; + } + } + + window.log.debug(`WIP: quoteProps`, quotesProps); return { messagesProps, quotesProps }; } @@ -798,7 +801,7 @@ const conversationsSlice = createSlice({ firstUnreadIdOnOpen: string | undefined; mostRecentMessageIdOnOpen: string | null; initialMessages: Array; - initialQuotes: Record; + initialQuotes: QuoteLookupType; }> ) { // this is quite hacky, but we don't want to show the showScrollButton if we have only a small amount of messages, @@ -850,7 +853,7 @@ const conversationsSlice = createSlice({ mostRecentMessageIdOnOpen: string | null; initialMessages: Array; - initialQuotes: Record; + initialQuotes: QuoteLookupType; }> ) { return {