diff --git a/ts/models/messages.ts b/ts/models/messages.ts index 893137f57fd..5709123c6a2 100644 --- a/ts/models/messages.ts +++ b/ts/models/messages.ts @@ -132,7 +132,6 @@ import { getMessageIdForLogging } from '../util/idForLogging'; import { hasAttachmentDownloads } from '../util/hasAttachmentDownloads'; import { queueAttachmentDownloads } from '../util/queueAttachmentDownloads'; import { findStoryMessages } from '../util/findStoryMessage'; -import { getStoryDataFromMessageAttributes } from '../services/storyLoader'; import type { ConversationQueueJobData } from '../jobs/conversationJobQueue'; import { shouldDownloadStory } from '../util/shouldDownloadStory'; import type { EmbeddedContactWithHydratedAvatar } from '../types/EmbeddedContact'; @@ -249,44 +248,15 @@ export class MessageModel extends window.Backbone.Model { this.CURRENT_PROTOCOL_VERSION = Proto.DataMessage.ProtocolVersion.CURRENT; this.INITIAL_PROTOCOL_VERSION = Proto.DataMessage.ProtocolVersion.INITIAL; - this.on('change', this.notifyRedux); + this.on('change', this.updateMessageCache); } - notifyRedux(): void { - if (!window.reduxActions) { - return; - } - + updateMessageCache(): void { window.MessageCache.setAttributes({ messageId: this.id, messageAttributes: this.attributes, skipSaveToDatabase: true, }); - - const { storyChanged } = window.reduxActions.stories; - - if (isStory(this.attributes)) { - const storyData = getStoryDataFromMessageAttributes({ - ...this.attributes, - }); - - if (!storyData) { - return; - } - - storyChanged(storyData); - - // We don't want messageChanged to run - return; - } - - const { messageChanged } = window.reduxActions.conversations; - - if (messageChanged) { - const conversationId = this.get('conversationId'); - // Note: The clone is important for triggering a re-run of selectors - messageChanged(this.id, conversationId, { ...this.attributes }); - } } getSenderIdentifier(): string { diff --git a/ts/services/MessageCache.ts b/ts/services/MessageCache.ts index 8a053a83674..887727fc1ae 100644 --- a/ts/services/MessageCache.ts +++ b/ts/services/MessageCache.ts @@ -14,6 +14,8 @@ import { getSenderIdentifier } from '../util/getSenderIdentifier'; import { isNotNil } from '../util/isNotNil'; import { map } from '../util/iterables'; import { softAssert, strictAssert } from '../util/assert'; +import { isStory } from '../messages/helpers'; +import { getStoryDataFromMessageAttributes } from './storyLoader'; export class MessageCache { private state = { @@ -197,6 +199,21 @@ export class MessageCache { this.markModelStale(nextMessageAttributes); if (window.reduxActions) { + if (isStory(nextMessageAttributes)) { + const storyData = getStoryDataFromMessageAttributes({ + ...nextMessageAttributes, + }); + + if (!storyData) { + return; + } + + window.reduxActions.stories.storyChanged(storyData); + + // We don't want messageChanged to run + return; + } + window.reduxActions.conversations.messageChanged( messageId, nextMessageAttributes.conversationId,