diff --git a/lib/shared/dm-ops/process-dm-ops.js b/lib/shared/dm-ops/process-dm-ops.js --- a/lib/shared/dm-ops/process-dm-ops.js +++ b/lib/shared/dm-ops/process-dm-ops.js @@ -15,8 +15,13 @@ processDMOpsActionType, queueDMOpsActionType, } from '../../types/dm-ops.js'; +import type { RawThreadInfo } from '../../types/minimally-encoded-thread-permissions-types.js'; +import { threadTypes } from '../../types/thread-types-enum.js'; +import type { LegacyRawThreadInfo } from '../../types/thread-types.js'; import { updateTypes } from '../../types/update-types-enum.js'; import { useSelector } from '../../utils/redux-utils.js'; +import { messageSpecs } from '../messages/message-specs.js'; +import { updateSpecs } from '../updates/update-specs.js'; function useProcessDMOperation(): ( dmOp: DMOperation, @@ -72,7 +77,43 @@ const messagesByThreadID = _groupBy(message => message.threadID)( allNewMessageInfos, ); + + const updatedThickSidebars = updateInfos + .map(update => updateSpecs[update.type].getUpdatedThreadInfo?.(update)) + .filter(thread => thread?.type === threadTypes.THICK_SIDEBAR); + const updatedThreadInfosByThreadID: { + [string]: RawThreadInfo | LegacyRawThreadInfo, + } = {}; + for (const threadInfo of updatedThickSidebars) { + if (threadInfo?.id) { + // We can have multiple thread infos with the same ID, and we want + // to keep the last one. + updatedThreadInfosByThreadID[threadInfo.id] = threadInfo; + } + } + for (const threadID in messagesByThreadID) { + const repliesCountIncreasingMessages = messagesByThreadID[ + threadID + ].filter(message => messageSpecs[message.type].includedInRepliesCount); + if (repliesCountIncreasingMessages.length > 0) { + const threadInfo = + updatedThreadInfosByThreadID[threadID] ?? threadInfos[threadID]; + const repliesCountIncreaseTime = Math.max( + repliesCountIncreasingMessages.map(message => message.time), + ); + updateInfos.push({ + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time: repliesCountIncreaseTime, + threadInfo: { + ...threadInfo, + repliesCount: + threadInfo.repliesCount + repliesCountIncreasingMessages.length, + }, + }); + } + const messagesFromOtherPeers = messagesByThreadID[threadID].filter( message => message.creatorID !== viewerID, ); @@ -104,7 +145,7 @@ metadata, ); }, - [viewerID, utilities, dispatchWithMessageSource], + [viewerID, utilities, dispatchWithMessageSource, threadInfos], ); }