diff --git a/ts/background.ts b/ts/background.ts index 30e5580b0..f47092371 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -2,10 +2,12 @@ // SPDX-License-Identifier: AGPL-3.0-only import { DataMessageClass } from './textsecure.d'; +import { MessageAttributesType } from './model-types.d'; import { WhatIsThis } from './window.d'; import { assert } from './util/assert'; export async function startApp(): Promise { + window.attachmentDownloadQueue = []; try { window.log.info('Initializing SQL in renderer'); await window.sqlInitializer.initialize(); @@ -2073,11 +2075,20 @@ export async function startApp(): Promise { attachmentsToDownload.length, attachmentDownloadQueue.length ); - await Promise.all( + window.attachmentDownloadQueue = undefined; + const messagesWithDownloads = await Promise.all( attachmentsToDownload.map(message => message.queueAttachmentDownloads() ) ); + const messagesToSave: Array = []; + messagesWithDownloads.forEach((shouldSave, messageKey) => { + if (shouldSave) { + const message = attachmentsToDownload[messageKey]; + messagesToSave.push(message.attributes); + } + }); + await window.Signal.Data.saveMessages(messagesToSave, {}); } }, 500); diff --git a/ts/models/messages.ts b/ts/models/messages.ts index 4f90a329b..0e54194a1 100644 --- a/ts/models/messages.ts +++ b/ts/models/messages.ts @@ -3614,8 +3614,15 @@ export class MessageModel extends window.Backbone.Model { (this.getConversation()!.getAccepted() || message.isOutgoing()) && !shouldHoldOffDownload ) { - window.attachmentDownloadQueue = window.attachmentDownloadQueue || []; - window.attachmentDownloadQueue.unshift(message); + if (window.attachmentDownloadQueue) { + window.attachmentDownloadQueue.unshift(message); + window.log.info( + 'Adding to attachmentDownloadQueue', + message.get('sent_at') + ); + } else { + await message.queueAttachmentDownloads(); + } } // Does this message have any pending, previously-received associated reactions? diff --git a/ts/state/selectors/search.ts b/ts/state/selectors/search.ts index ba6cf0337..ee9f0ba35 100644 --- a/ts/state/selectors/search.ts +++ b/ts/state/selectors/search.ts @@ -28,6 +28,8 @@ import { getConversationSelector, } from './conversations'; +import { BodyRangeType } from '../../types/Util'; + export const getSearch = (state: StateType): SearchStateType => state.search; export const getQuery = createSelector( @@ -110,29 +112,6 @@ export const getSearchResults = createSelector( } ); -export function _messageSearchResultSelector( - message: MessageSearchResultType, - from: ConversationType, - to: ConversationType, - searchConversationId?: string, - selectedMessageId?: string -): MessageSearchResultPropsDataType { - return { - from, - to, - - id: message.id, - conversationId: message.conversationId, - sentAt: message.sent_at, - snippet: message.snippet, - bodyRanges: message.bodyRanges, - body: message.body, - - isSelected: Boolean(selectedMessageId && message.id === selectedMessageId), - isSearchingInConversation: Boolean(searchConversationId), - }; -} - // A little optimization to reset our selector cache whenever high-level application data // changes: regionCode and userNumber. type CachedMessageSearchResultSelectorType = ( @@ -142,18 +121,58 @@ type CachedMessageSearchResultSelectorType = ( searchConversationId?: string, selectedMessageId?: string ) => MessageSearchResultPropsDataType; + export const getCachedSelectorForMessageSearchResult = createSelector( getUserConversationId, - (): CachedMessageSearchResultSelectorType => { + getConversationSelector, + ( + _, + conversationSelector: GetConversationByIdType + ): CachedMessageSearchResultSelectorType => { // Note: memoizee will check all parameters provided, and only run our selector // if any of them have changed. - return memoizee(_messageSearchResultSelector, { max: 500 }); + return memoizee( + ( + message: MessageSearchResultType, + from: ConversationType, + to: ConversationType, + searchConversationId?: string, + selectedMessageId?: string + ) => { + const bodyRanges = message.bodyRanges || []; + return { + from, + to, + + id: message.id, + conversationId: message.conversationId, + sentAt: message.sent_at, + snippet: message.snippet, + bodyRanges: bodyRanges.map((bodyRange: BodyRangeType) => { + const conversation = conversationSelector(bodyRange.mentionUuid); + + return { + ...bodyRange, + replacementText: conversation.title, + }; + }), + body: message.body, + + isSelected: Boolean( + selectedMessageId && message.id === selectedMessageId + ), + isSearchingInConversation: Boolean(searchConversationId), + }; + }, + { max: 500 } + ); } ); type GetMessageSearchResultByIdType = ( id: string ) => MessageSearchResultPropsDataType | undefined; + export const getMessageSearchResultSelector = createSelector( getCachedSelectorForMessageSearchResult, getMessageSearchResultLookup, diff --git a/ts/test-both/state/selectors/search_test.ts b/ts/test-both/state/selectors/search_test.ts index ac3a7fe11..2fcb62a0a 100644 --- a/ts/test-both/state/selectors/search_test.ts +++ b/ts/test-both/state/selectors/search_test.ts @@ -204,9 +204,6 @@ describe('both/state/selectors/search', () => { ...state, conversations: { ...state.conversations, - conversationLookup: { - ...state.conversations.conversationLookup, - }, }, }; const secondSelector = getMessageSearchResultSelector(secondState); diff --git a/ts/util/messageBatcher.ts b/ts/util/messageBatcher.ts index e59d91820..ec8813dd2 100644 --- a/ts/util/messageBatcher.ts +++ b/ts/util/messageBatcher.ts @@ -8,17 +8,17 @@ import { createWaitBatcher } from './waitBatcher'; export const updateMessageBatcher = createBatcher({ wait: 500, maxSize: 50, - processBatch: async (messages: Array) => { - window.log.info('updateMessageBatcher', messages.length); - await window.Signal.Data.saveMessages(messages, {}); + processBatch: async (messageAttrs: Array) => { + window.log.info('updateMessageBatcher', messageAttrs.length); + await window.Signal.Data.saveMessages(messageAttrs, {}); }, }); export const saveNewMessageBatcher = createWaitBatcher({ wait: 500, maxSize: 30, - processBatch: async (messages: Array) => { - window.log.info('saveNewMessageBatcher', messages.length); - await window.Signal.Data.saveMessages(messages, { forceSave: true }); + processBatch: async (messageAttrs: Array) => { + window.log.info('saveNewMessageBatcher', messageAttrs.length); + await window.Signal.Data.saveMessages(messageAttrs, { forceSave: true }); }, }); diff --git a/ts/window.d.ts b/ts/window.d.ts index 759a42db4..05a5967d0 100644 --- a/ts/window.d.ts +++ b/ts/window.d.ts @@ -137,7 +137,7 @@ declare global { WhatIsThis: WhatIsThis; - attachmentDownloadQueue: Array; + attachmentDownloadQueue: Array | undefined; baseAttachmentsPath: string; baseStickersPath: string; baseTempPath: string;