diff --git a/lib/shared/dm-ops/add-members-spec.js b/lib/shared/dm-ops/add-members-spec.js index 9a38c70c3..b8e0a9178 100644 --- a/lib/shared/dm-ops/add-members-spec.js +++ b/lib/shared/dm-ops/add-members-spec.js @@ -1,159 +1,175 @@ // @flow import invariant from 'invariant'; import uuid from 'uuid'; import { createRoleAndPermissionForThickThreads, createThickRawThreadInfo, } from './create-thread-spec.js'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; +import { createUpdateUnreadCountUpdate } from './dm-op-utils.js'; import type { DMAddMembersOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import { messageTruncationStatus, type RawMessageInfo, } from '../../types/message-types.js'; import { minimallyEncodeMemberInfo, type ThickRawThreadInfo, } from '../../types/minimally-encoded-thread-permissions-types.js'; import { joinThreadSubscription } from '../../types/subscription-types.js'; import type { ThickMemberInfo } from '../../types/thread-types.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; import { values } from '../../utils/objects.js'; import { roleIsDefaultRole, userIsMember } from '../thread-utils.js'; function createAddNewMembersResults( dmOperation: DMAddMembersOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ): { +rawMessageInfos: Array, +updateInfos: Array, +threadInfo: ?ThickRawThreadInfo, } { const { editorID, time, messageID, addedUserIDs, existingThreadDetails } = dmOperation; const addMembersMessage = { type: messageTypes.ADD_MEMBERS, id: messageID, threadID: existingThreadDetails.threadID, creatorID: editorID, time, addedUserIDs: [...addedUserIDs], }; const viewerIsAdded = addedUserIDs.includes(viewerID); const updateInfos: Array = []; const rawMessageInfos: Array = []; let resultThreadInfo: ?ThickRawThreadInfo; if (viewerIsAdded) { const newThread = createThickRawThreadInfo( { ...existingThreadDetails, allMemberIDs: [...existingThreadDetails.allMemberIDs, ...addedUserIDs], }, viewerID, ); resultThreadInfo = newThread; updateInfos.push( { type: updateTypes.JOIN_THREAD, id: uuid.v4(), time, threadInfo: newThread, rawMessageInfos: [addMembersMessage], truncationStatus: messageTruncationStatus.EXHAUSTIVE, rawEntryInfos: [], }, { type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID: existingThreadDetails.threadID, unread: true, }, ); + const repliesCountUpdate = createUpdateUnreadCountUpdate(newThread, [ + addMembersMessage, + ]); + if ( + repliesCountUpdate && + repliesCountUpdate.type === updateTypes.UPDATE_THREAD + ) { + updateInfos.push(repliesCountUpdate); + resultThreadInfo.repliesCount = + repliesCountUpdate.threadInfo.repliesCount; + } } else { const currentThreadInfoOptional = utilities.threadInfos[existingThreadDetails.threadID]; if (!currentThreadInfoOptional || !currentThreadInfoOptional.thick) { // We can't perform this operation now. It should be queued for later. return { rawMessageInfos: [], updateInfos: [], threadInfo: null, }; } const currentThreadInfo: ThickRawThreadInfo = currentThreadInfoOptional; const defaultRoleID = values(currentThreadInfo.roles).find(role => roleIsDefaultRole(role), )?.id; invariant(defaultRoleID, 'Default role ID must exist'); const { membershipPermissions } = createRoleAndPermissionForThickThreads( currentThreadInfo.type, currentThreadInfo.id, defaultRoleID, ); const newMembers = addedUserIDs .filter(userID => !userIsMember(currentThreadInfo, userID)) .map(userID => minimallyEncodeMemberInfo({ id: userID, role: defaultRoleID, permissions: membershipPermissions, isSender: editorID === viewerID, subscription: joinThreadSubscription, }), ); const newThreadInfo = { ...currentThreadInfo, members: [...currentThreadInfo.members, ...newMembers], }; resultThreadInfo = newThreadInfo; + const updateWithRepliesCount = createUpdateUnreadCountUpdate( + newThreadInfo, + [addMembersMessage], + ); updateInfos.push( - { + updateWithRepliesCount ?? { type: updateTypes.UPDATE_THREAD, id: uuid.v4(), time, threadInfo: newThreadInfo, }, { type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID: existingThreadDetails.threadID, unread: true, }, ); rawMessageInfos.push(addMembersMessage); } return { rawMessageInfos, updateInfos, threadInfo: resultThreadInfo, }; } const addMembersSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMAddMembersOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) => { const { rawMessageInfos, updateInfos } = createAddNewMembersResults( dmOperation, viewerID, utilities, ); return { rawMessageInfos, updateInfos }; }, }); export { addMembersSpec, createAddNewMembersResults }; diff --git a/lib/shared/dm-ops/change-thread-settings-spec.js b/lib/shared/dm-ops/change-thread-settings-spec.js index a636ac0c2..de2fe9d19 100644 --- a/lib/shared/dm-ops/change-thread-settings-spec.js +++ b/lib/shared/dm-ops/change-thread-settings-spec.js @@ -1,155 +1,162 @@ // @flow import uuid from 'uuid'; import { createAddNewMembersResults } from './add-members-spec.js'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; +import { createUpdateUnreadCountUpdate } from './dm-op-utils.js'; import type { DMChangeThreadSettingsOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import type { RawMessageInfo } from '../../types/message-types.js'; import type { RawThreadInfo } from '../../types/minimally-encoded-thread-permissions-types.js'; import type { LegacyRawThreadInfo } from '../../types/thread-types.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; import { values } from '../../utils/objects.js'; const changeThreadSettingsSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMChangeThreadSettingsOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) => { const { editorID, time, changes, messageIDsPrefix, existingThreadDetails, } = dmOperation; const { name, description, color, avatar } = changes; const threadID = existingThreadDetails.threadID; const newMemberIDs = changes.newMemberIDs && changes.newMemberIDs.length > 0 ? [...new Set(changes.newMemberIDs)] : null; let threadInfoToUpdate: ?(RawThreadInfo | LegacyRawThreadInfo) = utilities.threadInfos[threadID]; if (!threadInfoToUpdate && !newMemberIDs?.includes(viewerID)) { // We can't perform this operation now. It should be queued for later. return { rawMessageInfos: [], updateInfos: [], }; } const updateInfos: Array = []; const rawMessageInfos: Array = []; if (newMemberIDs) { const addMembersResult = createAddNewMembersResults( { type: 'add_members', editorID, time, messageID: `${messageIDsPrefix}/add_members`, addedUserIDs: newMemberIDs, existingThreadDetails, }, viewerID, utilities, ); if (addMembersResult.threadInfo) { threadInfoToUpdate = addMembersResult.threadInfo; } updateInfos.push(...addMembersResult.updateInfos); rawMessageInfos.push(...addMembersResult.rawMessageInfos); } if (!threadInfoToUpdate || !threadInfoToUpdate.thick) { // We can't perform this operation now. It should be queued for later. return { rawMessageInfos: [], updateInfos: [], }; } const changedFields: { [string]: string | number } = {}; if (name !== undefined && name !== null) { changedFields.name = name; threadInfoToUpdate = { ...threadInfoToUpdate, name, }; } if (description !== undefined && description !== null) { changedFields.description = description; threadInfoToUpdate = { ...threadInfoToUpdate, description, }; } if (color) { changedFields.color = color; threadInfoToUpdate = { ...threadInfoToUpdate, color, }; } if (avatar) { changedFields.avatar = JSON.stringify(avatar); threadInfoToUpdate = { ...threadInfoToUpdate, avatar, }; } for (const fieldName in changedFields) { const newValue = changedFields[fieldName]; rawMessageInfos.push({ type: messageTypes.CHANGE_SETTINGS, threadID, creatorID: editorID, time, field: fieldName, value: newValue, id: `${messageIDsPrefix}/${fieldName}`, }); } - if (values(changedFields).length > 0) { + const repliesCountUpdate = createUpdateUnreadCountUpdate( + threadInfoToUpdate, + rawMessageInfos, + ); + if (repliesCountUpdate) { + updateInfos.push(repliesCountUpdate); + } else if (values(changedFields).length > 0) { updateInfos.push({ type: updateTypes.UPDATE_THREAD, id: uuid.v4(), time, threadInfo: threadInfoToUpdate, }); } if (rawMessageInfos.length > 0) { updateInfos.push({ type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID, unread: true, }); } return { rawMessageInfos, updateInfos, }; }, }); export { changeThreadSettingsSpec }; diff --git a/lib/shared/dm-ops/create-sidebar-spec.js b/lib/shared/dm-ops/create-sidebar-spec.js index fd7acd46d..a588817f8 100644 --- a/lib/shared/dm-ops/create-sidebar-spec.js +++ b/lib/shared/dm-ops/create-sidebar-spec.js @@ -1,109 +1,110 @@ // @flow import uuid from 'uuid'; import { createThickRawThreadInfo } from './create-thread-spec.js'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; import type { DMCreateSidebarOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import { type RawMessageInfo, messageTruncationStatus, } from '../../types/message-types.js'; import { threadTypes } from '../../types/thread-types-enum.js'; import { updateTypes } from '../../types/update-types-enum.js'; import { isInvalidSidebarSource } from '../message-utils.js'; const createSidebarSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMCreateSidebarOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) => { const { threadID, creatorID, time, parentThreadID, memberIDs, sourceMessageID, roleID, newSidebarSourceMessageID, newCreateSidebarMessageID, } = dmOperation; const allMemberIDs = [creatorID, ...memberIDs]; const rawThreadInfo = createThickRawThreadInfo( { threadID, threadType: threadTypes.THICK_SIDEBAR, creationTime: time, parentThreadID, allMemberIDs, roleID, creatorID, sourceMessageID, containingThreadID: parentThreadID, + repliesCount: 1, }, viewerID, ); const sourceMessage = await utilities.fetchMessage(sourceMessageID); if (!sourceMessage) { throw new Error( `could not find sourceMessage ${sourceMessageID}... probably ` + 'joined thick thread ${parentThreadID} after its creation', ); } if (isInvalidSidebarSource(sourceMessage)) { throw new Error( `sourceMessage ${sourceMessageID} is an invalid sidebar source`, ); } const rawMessageInfos: Array = [ { type: messageTypes.SIDEBAR_SOURCE, id: newSidebarSourceMessageID, threadID, creatorID, time, sourceMessage, }, { type: messageTypes.CREATE_SIDEBAR, id: newCreateSidebarMessageID, threadID, creatorID, time: time + 1, sourceMessageAuthorID: sourceMessage.creatorID, initialThreadState: { parentThreadID, color: rawThreadInfo.color, memberIDs: allMemberIDs, }, }, ]; const threadJoinUpdateInfo = { type: updateTypes.JOIN_THREAD, id: uuid.v4(), time, threadInfo: rawThreadInfo, rawMessageInfos, truncationStatus: messageTruncationStatus.EXHAUSTIVE, rawEntryInfos: [], }; return { rawMessageInfos: [], // included in updateInfos below updateInfos: [threadJoinUpdateInfo], }; }, }); export { createSidebarSpec }; diff --git a/lib/shared/dm-ops/dm-op-utils.js b/lib/shared/dm-ops/dm-op-utils.js index c04d42616..4d619e85f 100644 --- a/lib/shared/dm-ops/dm-op-utils.js +++ b/lib/shared/dm-ops/dm-op-utils.js @@ -1,69 +1,108 @@ // @flow import uuid from 'uuid'; import type { AuxUserStore } from '../../types/aux-user-types.js'; import type { DMOperation } from '../../types/dm-ops.js'; +import { messageTypes } from '../../types/message-types-enum.js'; +import type { RawMessageInfo } from '../../types/message-types.js'; +import type { RawThreadInfo } from '../../types/minimally-encoded-thread-permissions-types.js'; import { outboundP2PMessageStatuses, type OutboundP2PMessage, } from '../../types/sqlite-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 type { ClientUpdateInfo } from '../../types/update-types.js'; import type { CurrentUserInfo } from '../../types/user-types.js'; import { getContentSigningKey } from '../../utils/crypto-utils.js'; import { values } from '../../utils/objects.js'; +import { messageSpecs } from '../messages/message-specs.js'; function generateMessagesToPeers( message: DMOperation, peers: $ReadOnlyArray, userID: string, supportsAutoRetry: boolean, ): $ReadOnlyArray { const outboundP2PMessages = []; for (const peerID of peers) { const messageToPeer: OutboundP2PMessage = { messageID: uuid.v4(), deviceID: peerID, userID, timestamp: new Date().getTime().toString(), plaintext: JSON.stringify(message), ciphertext: '', status: outboundP2PMessageStatuses.persisted, supportsAutoRetry, }; outboundP2PMessages.push(messageToPeer); } return outboundP2PMessages; } export type DMOperationSpecification = { +op: DMOperation, +supportsAutoRetry: boolean, +recipients: 'all_peer_devices' | 'self_devices', }; async function createMessagesToPeersFromDMOp( operation: DMOperationSpecification, auxUserStore: AuxUserStore, currentUserInfo: ?CurrentUserInfo, ): Promise<$ReadOnlyArray> { if (!currentUserInfo?.id) { return []; } const selfDevices = auxUserStore.auxUserInfos[currentUserInfo.id].deviceList?.devices ?? []; const allPeerDevices = values(auxUserStore.auxUserInfos) .map(info => info.deviceList?.devices ?? []) .flat(); const devices = operation.recipients === 'all_peer_devices' ? allPeerDevices : selfDevices; const thisDeviceID = await getContentSigningKey(); const targetDevices = devices.filter(id => id !== thisDeviceID); return generateMessagesToPeers( operation.op, targetDevices, currentUserInfo.id, operation.supportsAutoRetry, ); } -export { createMessagesToPeersFromDMOp }; +function createUpdateUnreadCountUpdate( + threadInfo: RawThreadInfo | LegacyRawThreadInfo, + newMessages: $ReadOnlyArray, +): ?ClientUpdateInfo { + if (threadInfo.type !== threadTypes.THICK_SIDEBAR) { + return null; + } + const includedMessageTypes = new Set( + Object.keys(messageTypes) + .map(key => messageTypes[key]) + .filter(type => messageSpecs[type].includedInRepliesCount), + ); + const filteredMessages = newMessages.filter(message => + includedMessageTypes.has(message.type), + ); + const countIncrease = filteredMessages.length; + if (countIncrease === 0) { + return null; + } + const time = Math.max(...filteredMessages.map(message => message.time)); + return { + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time, + threadInfo: { + ...threadInfo, + repliesCount: threadInfo.repliesCount + countIncrease, + }, + }; +} + +export { createMessagesToPeersFromDMOp, createUpdateUnreadCountUpdate }; diff --git a/lib/shared/dm-ops/join-thread-spec.js b/lib/shared/dm-ops/join-thread-spec.js index 4ae3ac99b..d842edf08 100644 --- a/lib/shared/dm-ops/join-thread-spec.js +++ b/lib/shared/dm-ops/join-thread-spec.js @@ -1,146 +1,158 @@ // @flow import invariant from 'invariant'; import uuid from 'uuid'; import { createRoleAndPermissionForThickThreads, createThickRawThreadInfo, } from './create-thread-spec.js'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; +import { createUpdateUnreadCountUpdate } from './dm-op-utils.js'; import type { DMJoinThreadOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import { messageTruncationStatus, type RawMessageInfo, } from '../../types/message-types.js'; import { minimallyEncodeMemberInfo, type ThickRawThreadInfo, } from '../../types/minimally-encoded-thread-permissions-types.js'; import { joinThreadSubscription } from '../../types/subscription-types.js'; import type { ThickMemberInfo } from '../../types/thread-types.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; import { values } from '../../utils/objects.js'; import { roleIsDefaultRole, userIsMember } from '../thread-utils.js'; const joinThreadSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMJoinThreadOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) => { const { editorID, time, messageID, existingThreadDetails } = dmOperation; const joinThreadMessage = { type: messageTypes.JOIN_THREAD, id: messageID, threadID: existingThreadDetails.threadID, creatorID: editorID, time, }; const currentThreadInfoOptional = utilities.threadInfos[existingThreadDetails.threadID]; if (userIsMember(currentThreadInfoOptional, editorID)) { return { rawMessageInfos: [joinThreadMessage], updateInfos: [ { type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID: existingThreadDetails.threadID, unread: true, }, ], }; } const updateInfos: Array = []; const rawMessageInfos: Array = []; if (viewerID === editorID) { + const newThreadInfo = createThickRawThreadInfo( + { + ...existingThreadDetails, + allMemberIDs: [...existingThreadDetails.allMemberIDs, editorID], + }, + viewerID, + ); updateInfos.push( { type: updateTypes.JOIN_THREAD, id: uuid.v4(), time, - threadInfo: createThickRawThreadInfo( - { - ...existingThreadDetails, - allMemberIDs: [...existingThreadDetails.allMemberIDs, editorID], - }, - viewerID, - ), + threadInfo: newThreadInfo, rawMessageInfos: [joinThreadMessage], truncationStatus: messageTruncationStatus.EXHAUSTIVE, rawEntryInfos: [], }, { type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID: existingThreadDetails.threadID, unread: true, }, ); + const repliesCountUpdate = createUpdateUnreadCountUpdate(newThreadInfo, [ + joinThreadMessage, + ]); + if (repliesCountUpdate) { + updateInfos.push(repliesCountUpdate); + } } else { if (!currentThreadInfoOptional || !currentThreadInfoOptional.thick) { // We can't perform this operation now. It should be queued for later. return { rawMessageInfos: [], updateInfos: [], }; } const currentThreadInfo: ThickRawThreadInfo = currentThreadInfoOptional; rawMessageInfos.push(joinThreadMessage); const defaultRoleID = values(currentThreadInfo.roles).find(role => roleIsDefaultRole(role), )?.id; invariant(defaultRoleID, 'Default role ID must exist'); const { membershipPermissions } = createRoleAndPermissionForThickThreads( currentThreadInfo.type, currentThreadInfo.id, defaultRoleID, ); const member = minimallyEncodeMemberInfo({ id: editorID, role: defaultRoleID, permissions: membershipPermissions, isSender: editorID === viewerID, subscription: joinThreadSubscription, }); const updatedThreadInfo = { ...currentThreadInfo, members: [...currentThreadInfo.members, member], }; + const updateWithRepliesCount = createUpdateUnreadCountUpdate( + updatedThreadInfo, + [joinThreadMessage], + ); updateInfos.push( - { + updateWithRepliesCount ?? { type: updateTypes.UPDATE_THREAD, id: uuid.v4(), time, threadInfo: updatedThreadInfo, }, { type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID: existingThreadDetails.threadID, unread: true, }, ); } return { rawMessageInfos, updateInfos, }; }, }); export { joinThreadSpec }; diff --git a/lib/shared/dm-ops/leave-thread-spec.js b/lib/shared/dm-ops/leave-thread-spec.js index e7a66f4dc..c7378db5c 100644 --- a/lib/shared/dm-ops/leave-thread-spec.js +++ b/lib/shared/dm-ops/leave-thread-spec.js @@ -1,86 +1,91 @@ // @flow import uuid from 'uuid'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; +import { createUpdateUnreadCountUpdate } from './dm-op-utils.js'; import type { DMLeaveThreadOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import type { ThickRawThreadInfo } from '../../types/minimally-encoded-thread-permissions-types.js'; import { threadTypes } from '../../types/thread-types-enum.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; import { userIsMember } from '../thread-utils.js'; const leaveThreadSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMLeaveThreadOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) => { const { editorID, time, messageID, threadID } = dmOperation; const threadInfoOptional = utilities.threadInfos[threadID]; if (!threadInfoOptional || !threadInfoOptional.thick) { // We can't perform this operation now. It should be queued for later. return { rawMessageInfos: [], updateInfos: [], }; } const threadInfo: ThickRawThreadInfo = threadInfoOptional; const leaveThreadMessage = { type: messageTypes.LEAVE_THREAD, id: messageID, threadID, creatorID: editorID, time, }; const updateInfos: Array = []; if ( viewerID === editorID && userIsMember(threadInfo, editorID) && (threadInfo.type !== threadTypes.THICK_SIDEBAR || (threadInfo.parentThreadID && !utilities.threadInfos[threadInfo.parentThreadID])) ) { updateInfos.push({ type: updateTypes.DELETE_THREAD, id: uuid.v4(), time, threadID, }); } else { const updatedThreadInfo = { ...threadInfo, members: threadInfo.members.filter(member => member.id !== editorID), }; + const updateWithRepliesCount = createUpdateUnreadCountUpdate( + updatedThreadInfo, + [leaveThreadMessage], + ); updateInfos.push( - { + updateWithRepliesCount ?? { type: updateTypes.UPDATE_THREAD, id: uuid.v4(), time, threadInfo: updatedThreadInfo, }, { type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID, unread: true, }, ); } return { rawMessageInfos: [leaveThreadMessage], updateInfos, }; }, }); export { leaveThreadSpec }; diff --git a/lib/shared/dm-ops/remove-members-spec.js b/lib/shared/dm-ops/remove-members-spec.js index 235ffab55..c9c706ca9 100644 --- a/lib/shared/dm-ops/remove-members-spec.js +++ b/lib/shared/dm-ops/remove-members-spec.js @@ -1,90 +1,95 @@ // @flow import uuid from 'uuid'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; +import { createUpdateUnreadCountUpdate } from './dm-op-utils.js'; import type { DMRemoveMembersOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import type { ThickRawThreadInfo } from '../../types/minimally-encoded-thread-permissions-types.js'; import { threadTypes } from '../../types/thread-types-enum.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; const removeMembersSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMRemoveMembersOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) => { const { editorID, time, messageID, threadID, removedUserIDs } = dmOperation; const threadInfoOptional = utilities.threadInfos[threadID]; if (!threadInfoOptional || !threadInfoOptional.thick) { // We can't perform this operation now. It should be queued for later. return { rawMessageInfos: [], updateInfos: [], }; } const threadInfo: ThickRawThreadInfo = threadInfoOptional; const removeMembersMessage = { type: messageTypes.REMOVE_MEMBERS, id: messageID, threadID, time, creatorID: editorID, removedUserIDs: [...removedUserIDs], }; const removedUserIDsSet = new Set(removedUserIDs); const viewerIsRemoved = removedUserIDsSet.has(viewerID); const updateInfos: Array = []; if ( viewerIsRemoved && (threadInfo.type !== threadTypes.THICK_SIDEBAR || (threadInfo.parentThreadID && !utilities.threadInfos[threadInfo.parentThreadID])) ) { updateInfos.push({ type: updateTypes.DELETE_THREAD, id: uuid.v4(), time, threadID, }); } else { const updatedThreadInfo = { ...threadInfo, members: threadInfo.members.filter( member => !removedUserIDsSet.has(member.id), ), }; + const updateWithRepliesCount = createUpdateUnreadCountUpdate( + updatedThreadInfo, + [removeMembersMessage], + ); updateInfos.push( - { + updateWithRepliesCount ?? { type: updateTypes.UPDATE_THREAD, id: uuid.v4(), time, threadInfo: updatedThreadInfo, }, { type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID, unread: true, }, ); } return { rawMessageInfos: [removeMembersMessage], updateInfos, }; }, }); export { removeMembersSpec }; diff --git a/lib/shared/dm-ops/send-edit-message-spec.js b/lib/shared/dm-ops/send-edit-message-spec.js index 51a65d241..560c85945 100644 --- a/lib/shared/dm-ops/send-edit-message-spec.js +++ b/lib/shared/dm-ops/send-edit-message-spec.js @@ -1,46 +1,60 @@ // @flow import uuid from 'uuid'; -import type { DMOperationSpec } from './dm-op-spec.js'; +import type { + DMOperationSpec, + ProcessDMOperationUtilities, +} from './dm-op-spec.js'; +import { createUpdateUnreadCountUpdate } from './dm-op-utils.js'; import type { DMSendEditMessageOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; const sendEditMessageSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMSendEditMessageOperation, viewerID: string, + utilities: ProcessDMOperationUtilities, ) => { const { threadID, creatorID, time, messageID, targetMessageID, text } = dmOperation; const editMessage = { type: messageTypes.EDIT_MESSAGE, id: messageID, threadID, creatorID, time, targetMessageID, text, }; const updateInfos: Array = []; if (creatorID !== viewerID) { updateInfos.push({ type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID, unread: true, }); } + const threadInfo = utilities.threadInfos[threadID]; + if (threadInfo) { + const repliesCountUpdate = createUpdateUnreadCountUpdate(threadInfo, [ + editMessage, + ]); + if (repliesCountUpdate) { + updateInfos.push(repliesCountUpdate); + } + } return { rawMessageInfos: [editMessage], updateInfos, }; }, }); export { sendEditMessageSpec }; diff --git a/lib/shared/dm-ops/send-reaction-message-spec.js b/lib/shared/dm-ops/send-reaction-message-spec.js index f2fd1d794..2a26e0fc6 100644 --- a/lib/shared/dm-ops/send-reaction-message-spec.js +++ b/lib/shared/dm-ops/send-reaction-message-spec.js @@ -1,54 +1,68 @@ // @flow import uuid from 'uuid'; -import type { DMOperationSpec } from './dm-op-spec.js'; +import type { + DMOperationSpec, + ProcessDMOperationUtilities, +} from './dm-op-spec.js'; +import { createUpdateUnreadCountUpdate } from './dm-op-utils.js'; import type { DMSendReactionMessageOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; const sendReactionMessageSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMSendReactionMessageOperation, viewerID: string, + utilities: ProcessDMOperationUtilities, ) => { const { threadID, creatorID, time, messageID, targetMessageID, reaction, action, } = dmOperation; const reactionMessage = { type: messageTypes.REACTION, id: messageID, threadID, creatorID, time, targetMessageID, reaction, action, }; const updateInfos: Array = []; if (creatorID !== viewerID) { updateInfos.push({ type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID, unread: true, }); } + const threadInfo = utilities.threadInfos[threadID]; + if (threadInfo) { + const repliesCountUpdate = createUpdateUnreadCountUpdate(threadInfo, [ + reactionMessage, + ]); + if (repliesCountUpdate) { + updateInfos.push(repliesCountUpdate); + } + } return { rawMessageInfos: [reactionMessage], updateInfos, }; }, }); export { sendReactionMessageSpec }; diff --git a/lib/shared/dm-ops/send-text-message-spec.js b/lib/shared/dm-ops/send-text-message-spec.js index 679564a61..cfdaa463d 100644 --- a/lib/shared/dm-ops/send-text-message-spec.js +++ b/lib/shared/dm-ops/send-text-message-spec.js @@ -1,43 +1,57 @@ // @flow import uuid from 'uuid'; -import type { DMOperationSpec } from './dm-op-spec.js'; +import type { + DMOperationSpec, + ProcessDMOperationUtilities, +} from './dm-op-spec.js'; +import { createUpdateUnreadCountUpdate } from './dm-op-utils.js'; import type { DMSendTextMessageOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import { updateTypes } from '../../types/update-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; const sendTextMessageSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMSendTextMessageOperation, viewerID: string, + utilities: ProcessDMOperationUtilities, ) => { const { threadID, creatorID, time, messageID, text } = dmOperation; const textMessage = { type: messageTypes.TEXT, id: messageID, threadID, creatorID, time, text, }; const updateInfos: Array = []; if (creatorID !== viewerID) { updateInfos.push({ type: updateTypes.UPDATE_THREAD_READ_STATUS, id: uuid.v4(), time, threadID, unread: true, }); } + const threadInfo = utilities.threadInfos[threadID]; + if (threadInfo) { + const repliesCountUpdate = createUpdateUnreadCountUpdate(threadInfo, [ + textMessage, + ]); + if (repliesCountUpdate) { + updateInfos.push(repliesCountUpdate); + } + } return { rawMessageInfos: [textMessage], updateInfos, }; }, }); export { sendTextMessageSpec };