diff --git a/lib/shared/dm-ops/add-members-spec.js b/lib/shared/dm-ops/add-members-spec.js index 510adb0c4..7d45c0d3f 100644 --- a/lib/shared/dm-ops/add-members-spec.js +++ b/lib/shared/dm-ops/add-members-spec.js @@ -1,128 +1,126 @@ // @flow import invariant from 'invariant'; import uuid from 'uuid'; import { createRoleAndPermissionForThickThreads } from './create-thread-spec.js'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; -import { createRepliesCountUpdate } from './dm-op-utils.js'; import type { DMAddMembersOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import { 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'; export type AddMembersResult = { rawMessageInfos: Array, updateInfos: Array, threadInfo: ?ThickRawThreadInfo, }; function createAddNewMembersResults( dmOperation: DMAddMembersOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ): AddMembersResult { const { editorID, time, messageID, addedUserIDs, threadID } = dmOperation; const addMembersMessage = { type: messageTypes.ADD_MEMBERS, id: messageID, threadID, creatorID: editorID, time, addedUserIDs: [...addedUserIDs], }; const currentThreadInfo = utilities.threadInfos[threadID]; if (!currentThreadInfo.thick) { return { rawMessageInfos: [], updateInfos: [], threadInfo: null, }; } 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 resultThreadInfo = { ...currentThreadInfo, members: [...currentThreadInfo.members, ...newMembers], }; - const updateWithRepliesCount = createRepliesCountUpdate(resultThreadInfo, [ - addMembersMessage, - ]); - const update = updateWithRepliesCount ?? { - type: updateTypes.UPDATE_THREAD, - id: uuid.v4(), - time, - threadInfo: resultThreadInfo, - }; + const updateInfos = [ + { + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time, + threadInfo: resultThreadInfo, + }, + ]; return { rawMessageInfos: [addMembersMessage], - updateInfos: [update], + 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 }; }, canBeProcessed( dmOperation: DMAddMembersOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { if (utilities.threadInfos[dmOperation.threadID]) { return { isProcessingPossible: true }; } return { isProcessingPossible: false, reason: { type: 'missing_thread', threadID: dmOperation.threadID, }, }; }, }); export { addMembersSpec, createAddNewMembersResults }; diff --git a/lib/shared/dm-ops/add-viewer-to-thread-members-spec.js b/lib/shared/dm-ops/add-viewer-to-thread-members-spec.js index 475e9f509..4ace51e88 100644 --- a/lib/shared/dm-ops/add-viewer-to-thread-members-spec.js +++ b/lib/shared/dm-ops/add-viewer-to-thread-members-spec.js @@ -1,92 +1,80 @@ // @flow import uuid from 'uuid'; import type { AddMembersResult } from './add-members-spec.js'; import { createThickRawThreadInfo } from './create-thread-spec.js'; import type { DMOperationSpec } from './dm-op-spec.js'; -import { createRepliesCountUpdate } from './dm-op-utils.js'; import type { DMAddViewerToThreadMembersOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import { messageTruncationStatus } from '../../types/message-types.js'; import { updateTypes } from '../../types/update-types-enum.js'; -import type { ClientUpdateInfo } from '../../types/update-types.js'; function createAddViewerToThreadMembersResults( dmOperation: DMAddViewerToThreadMembersOperation, viewerID: string, ): AddMembersResult { const { editorID, time, messageID, addedUserIDs, existingThreadDetails } = dmOperation; const addMembersMessage = { type: messageTypes.ADD_MEMBERS, id: messageID, threadID: existingThreadDetails.threadID, creatorID: editorID, time, addedUserIDs: [...addedUserIDs], }; - const updateInfos: Array = []; - const resultThreadInfo = createThickRawThreadInfo( { ...existingThreadDetails, allMemberIDs: [...existingThreadDetails.allMemberIDs, ...addedUserIDs], }, viewerID, ); - updateInfos.push({ - type: updateTypes.JOIN_THREAD, - id: uuid.v4(), - time, - threadInfo: resultThreadInfo, - rawMessageInfos: [addMembersMessage], - truncationStatus: messageTruncationStatus.EXHAUSTIVE, - rawEntryInfos: [], - }); - const repliesCountUpdate = createRepliesCountUpdate(resultThreadInfo, [ - addMembersMessage, - ]); - if ( - repliesCountUpdate && - repliesCountUpdate.type === updateTypes.UPDATE_THREAD - ) { - updateInfos.push(repliesCountUpdate); - resultThreadInfo.repliesCount = repliesCountUpdate.threadInfo.repliesCount; - } + const updateInfos = [ + { + type: updateTypes.JOIN_THREAD, + id: uuid.v4(), + time, + threadInfo: resultThreadInfo, + rawMessageInfos: [addMembersMessage], + truncationStatus: messageTruncationStatus.EXHAUSTIVE, + rawEntryInfos: [], + }, + ]; return { rawMessageInfos: [], updateInfos, threadInfo: resultThreadInfo, }; } const addViewerToThreadMembersSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMAddViewerToThreadMembersOperation, viewerID: string, ) => { const { rawMessageInfos, updateInfos } = createAddViewerToThreadMembersResults(dmOperation, viewerID); return { rawMessageInfos, updateInfos }; }, canBeProcessed( dmOperation: DMAddViewerToThreadMembersOperation, viewerID: string, ) { if (dmOperation.addedUserIDs.includes(viewerID)) { return { isProcessingPossible: true }; } console.log('Invalid DM operation', dmOperation); return { isProcessingPossible: false, reason: { type: 'invalid', }, }; }, }); export { addViewerToThreadMembersSpec, createAddViewerToThreadMembersResults }; diff --git a/lib/shared/dm-ops/change-thread-settings-spec.js b/lib/shared/dm-ops/change-thread-settings-spec.js index 0b3f161b8..4b251b4ab 100644 --- a/lib/shared/dm-ops/change-thread-settings-spec.js +++ b/lib/shared/dm-ops/change-thread-settings-spec.js @@ -1,188 +1,181 @@ // @flow import invariant from 'invariant'; import uuid from 'uuid'; import { type AddMembersResult, createAddNewMembersResults, } from './add-members-spec.js'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; -import { createRepliesCountUpdate } from './dm-op-utils.js'; import type { DMChangeThreadSettingsAndAddViewerOperation, DMChangeThreadSettingsOperation, DMOperationResult, } 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'; function processAddMembersOperation( dmOperation: DMChangeThreadSettingsOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { const { editorID, time, messageIDsPrefix, changes, threadID } = dmOperation; const newMemberIDs = changes.newMemberIDs && changes.newMemberIDs.length > 0 ? [...new Set(changes.newMemberIDs)] : []; if (!changes.newMemberIDs || changes.newMemberIDs.length === 0) { return null; } const operation = { type: 'add_members', editorID, time, messageID: `${messageIDsPrefix}/add_members`, addedUserIDs: newMemberIDs, threadID, }; return createAddNewMembersResults(operation, viewerID, utilities); } function processChangeSettingsOperation( dmOperation: | DMChangeThreadSettingsOperation | DMChangeThreadSettingsAndAddViewerOperation, viewerID: string, utilities: ProcessDMOperationUtilities, addMembersResult: ?AddMembersResult, ): DMOperationResult { const { editorID, time, changes, messageIDsPrefix } = dmOperation; const { name, description, color, avatar } = changes; const threadID = dmOperation.type === 'change_thread_settings' ? dmOperation.threadID : dmOperation.existingThreadDetails.threadID; let threadInfoToUpdate: ?(RawThreadInfo | LegacyRawThreadInfo) = utilities.threadInfos[threadID]; const updateInfos: Array = []; const rawMessageInfos: Array = []; if (addMembersResult) { if (addMembersResult.threadInfo) { threadInfoToUpdate = addMembersResult.threadInfo; } updateInfos.push(...addMembersResult.updateInfos); rawMessageInfos.push(...addMembersResult.rawMessageInfos); } invariant(threadInfoToUpdate?.thick, 'Thread should be thick'); 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}`, }); } - const repliesCountUpdate = createRepliesCountUpdate( - threadInfoToUpdate, - rawMessageInfos, - ); - if (repliesCountUpdate) { - updateInfos.push(repliesCountUpdate); - } else if (values(changedFields).length > 0) { + if (values(changedFields).length > 0) { updateInfos.push({ type: updateTypes.UPDATE_THREAD, id: uuid.v4(), time, threadInfo: threadInfoToUpdate, }); } return { rawMessageInfos, updateInfos, }; } const changeThreadSettingsSpec: DMOperationSpec = Object.freeze({ processDMOperation: async ( dmOperation: DMChangeThreadSettingsOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) => { const addMembersResult = processAddMembersOperation( dmOperation, viewerID, utilities, ); return processChangeSettingsOperation( dmOperation, viewerID, utilities, addMembersResult, ); }, canBeProcessed( dmOperation: DMChangeThreadSettingsOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { if (utilities.threadInfos[dmOperation.threadID]) { return { isProcessingPossible: true }; } return { isProcessingPossible: false, reason: { type: 'missing_thread', threadID: dmOperation.threadID, }, }; }, }); export { changeThreadSettingsSpec, processChangeSettingsOperation }; diff --git a/lib/shared/dm-ops/create-sidebar-spec.js b/lib/shared/dm-ops/create-sidebar-spec.js index f18cd6e93..376be5587 100644 --- a/lib/shared/dm-ops/create-sidebar-spec.js +++ b/lib/shared/dm-ops/create-sidebar-spec.js @@ -1,113 +1,112 @@ // @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], }; }, canBeProcessed() { return { isProcessingPossible: true }; }, }); export { createSidebarSpec }; diff --git a/lib/shared/dm-ops/dm-op-utils.js b/lib/shared/dm-ops/dm-op-utils.js index d35493c42..bc7a21c2b 100644 --- a/lib/shared/dm-ops/dm-op-utils.js +++ b/lib/shared/dm-ops/dm-op-utils.js @@ -1,129 +1,90 @@ // @flow import uuid from 'uuid'; 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 { type DMOperationP2PMessage, userActionsP2PMessageTypes, } from '../../types/tunnelbroker/user-actions-peer-to-peer-message-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 { messageSpecs } from '../messages/message-specs.js'; function generateMessagesToPeers( message: DMOperation, peers: $ReadOnlyArray<{ +userID: string, +deviceID: string, }>, supportsAutoRetry: boolean, ): $ReadOnlyArray { const opMessage: DMOperationP2PMessage = { type: userActionsP2PMessageTypes.DM_OPERATION, op: message, }; const plaintext = JSON.stringify(opMessage); const outboundP2PMessages = []; for (const peer of peers) { const messageToPeer: OutboundP2PMessage = { messageID: uuid.v4(), deviceID: peer.deviceID, userID: peer.userID, timestamp: new Date().getTime().toString(), plaintext, ciphertext: '', status: outboundP2PMessageStatuses.persisted, supportsAutoRetry, }; outboundP2PMessages.push(messageToPeer); } return outboundP2PMessages; } export type DMOperationSpecification = { +op: DMOperation, +supportsAutoRetry: boolean, +recipients: | { +type: 'all_peer_devices' | 'self_devices' } | { +type: 'some_users', +userIDs: $ReadOnlyArray }, }; async function createMessagesToPeersFromDMOp( operation: DMOperationSpecification, allPeerUserIDAndDeviceIDs: $ReadOnlyArray<{ +userID: string, +deviceID: string, }>, currentUserInfo: ?CurrentUserInfo, ): Promise<$ReadOnlyArray> { if (!currentUserInfo?.id) { return []; } let peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs; if (operation.recipients.type === 'self_devices') { peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter( peer => peer.userID === currentUserInfo.id, ); } else if (operation.recipients.type === 'some_users') { const userIDs = new Set(operation.recipients.userIDs); peerUserIDAndDeviceIDs = allPeerUserIDAndDeviceIDs.filter(peer => userIDs.has(peer.userID), ); } const thisDeviceID = await getContentSigningKey(); const targetPeers = peerUserIDAndDeviceIDs.filter( peer => peer.deviceID !== thisDeviceID, ); return generateMessagesToPeers( operation.op, targetPeers, operation.supportsAutoRetry, ); } -function createRepliesCountUpdate( - 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, createRepliesCountUpdate }; +export { createMessagesToPeersFromDMOp }; diff --git a/lib/shared/dm-ops/join-thread-spec.js b/lib/shared/dm-ops/join-thread-spec.js index 4a03f6a60..f1254913f 100644 --- a/lib/shared/dm-ops/join-thread-spec.js +++ b/lib/shared/dm-ops/join-thread-spec.js @@ -1,141 +1,128 @@ // @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 { createRepliesCountUpdate } 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 } 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 { joinerID, time, messageID, existingThreadDetails } = dmOperation; const currentThreadInfo = utilities.threadInfos[existingThreadDetails.threadID]; const joinThreadMessage = { type: messageTypes.JOIN_THREAD, id: messageID, threadID: existingThreadDetails.threadID, creatorID: joinerID, time, }; if (userIsMember(currentThreadInfo, joinerID)) { return { rawMessageInfos: [joinThreadMessage], updateInfos: [], }; } const updateInfos: Array = []; const rawMessageInfos: Array = []; if (viewerID === joinerID) { const newThreadInfo = createThickRawThreadInfo( { ...existingThreadDetails, allMemberIDs: [...existingThreadDetails.allMemberIDs, joinerID], }, viewerID, ); updateInfos.push({ type: updateTypes.JOIN_THREAD, id: uuid.v4(), time, threadInfo: newThreadInfo, rawMessageInfos: [joinThreadMessage], truncationStatus: messageTruncationStatus.EXHAUSTIVE, rawEntryInfos: [], }); - const repliesCountUpdate = createRepliesCountUpdate(newThreadInfo, [ - joinThreadMessage, - ]); - if (repliesCountUpdate) { - updateInfos.push(repliesCountUpdate); - } } else { invariant(currentThreadInfo.thick, 'Thread should be thick'); 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: joinerID, role: defaultRoleID, permissions: membershipPermissions, isSender: joinerID === viewerID, subscription: joinThreadSubscription, }); const updatedThreadInfo = { ...currentThreadInfo, members: [...currentThreadInfo.members, member], }; - const updateWithRepliesCount = createRepliesCountUpdate( - updatedThreadInfo, - [joinThreadMessage], - ); - updateInfos.push( - updateWithRepliesCount ?? { - type: updateTypes.UPDATE_THREAD, - id: uuid.v4(), - time, - threadInfo: updatedThreadInfo, - }, - ); + updateInfos.push({ + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time, + threadInfo: updatedThreadInfo, + }); } return { rawMessageInfos, updateInfos, }; }, canBeProcessed( dmOperation: DMJoinThreadOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { if (utilities.threadInfos[dmOperation.existingThreadDetails.threadID]) { return { isProcessingPossible: true }; } return { isProcessingPossible: false, reason: { type: 'missing_thread', threadID: dmOperation.existingThreadDetails.threadID, }, }; }, }); export { joinThreadSpec }; diff --git a/lib/shared/dm-ops/leave-thread-spec.js b/lib/shared/dm-ops/leave-thread-spec.js index 53dcf9a2a..23d4e0907 100644 --- a/lib/shared/dm-ops/leave-thread-spec.js +++ b/lib/shared/dm-ops/leave-thread-spec.js @@ -1,93 +1,86 @@ // @flow import invariant from 'invariant'; import uuid from 'uuid'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; -import { createRepliesCountUpdate } from './dm-op-utils.js'; import type { DMLeaveThreadOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.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 threadInfo = utilities.threadInfos[threadID]; invariant(threadInfo.thick, 'Thread should be thick'); 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 = createRepliesCountUpdate( - updatedThreadInfo, - [leaveThreadMessage], - ); - updateInfos.push( - updateWithRepliesCount ?? { - type: updateTypes.UPDATE_THREAD, - id: uuid.v4(), - time, - threadInfo: updatedThreadInfo, - }, - ); + updateInfos.push({ + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time, + threadInfo: updatedThreadInfo, + }); } return { rawMessageInfos: [leaveThreadMessage], updateInfos, }; }, canBeProcessed( dmOperation: DMLeaveThreadOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { if (utilities.threadInfos[dmOperation.threadID]) { return { isProcessingPossible: true }; } return { isProcessingPossible: false, reason: { type: 'missing_thread', threadID: dmOperation.threadID, }, }; }, }); export { leaveThreadSpec }; diff --git a/lib/shared/dm-ops/remove-members-spec.js b/lib/shared/dm-ops/remove-members-spec.js index b5250aa7c..16e8430b8 100644 --- a/lib/shared/dm-ops/remove-members-spec.js +++ b/lib/shared/dm-ops/remove-members-spec.js @@ -1,97 +1,90 @@ // @flow import invariant from 'invariant'; import uuid from 'uuid'; import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; -import { createRepliesCountUpdate } from './dm-op-utils.js'; import type { DMRemoveMembersOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.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 threadInfo = utilities.threadInfos[threadID]; invariant(threadInfo.thick, 'Thread should be thick'); 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 = createRepliesCountUpdate( - updatedThreadInfo, - [removeMembersMessage], - ); - updateInfos.push( - updateWithRepliesCount ?? { - type: updateTypes.UPDATE_THREAD, - id: uuid.v4(), - time, - threadInfo: updatedThreadInfo, - }, - ); + updateInfos.push({ + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time, + threadInfo: updatedThreadInfo, + }); } return { rawMessageInfos: [removeMembersMessage], updateInfos, }; }, canBeProcessed( dmOperation: DMRemoveMembersOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { if (utilities.threadInfos[dmOperation.threadID]) { return { isProcessingPossible: true }; } return { isProcessingPossible: false, reason: { type: 'missing_thread', threadID: dmOperation.threadID, }, }; }, }); 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 9440cbe8b..9f50f2316 100644 --- a/lib/shared/dm-ops/send-edit-message-spec.js +++ b/lib/shared/dm-ops/send-edit-message-spec.js @@ -1,62 +1,48 @@ // @flow import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; -import { createRepliesCountUpdate } from './dm-op-utils.js'; import type { DMSendEditMessageOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; -import type { ClientUpdateInfo } from '../../types/update-types.js'; const sendEditMessageSpec: DMOperationSpec = Object.freeze({ - processDMOperation: async ( - dmOperation: DMSendEditMessageOperation, - viewerID: string, - utilities: ProcessDMOperationUtilities, - ) => { + processDMOperation: async (dmOperation: DMSendEditMessageOperation) => { const { threadID, creatorID, time, messageID, targetMessageID, text } = dmOperation; const editMessage = { type: messageTypes.EDIT_MESSAGE, id: messageID, threadID, creatorID, time, targetMessageID, text, }; - const updateInfos: Array = []; - const threadInfo = utilities.threadInfos[threadID]; - const repliesCountUpdate = createRepliesCountUpdate(threadInfo, [ - editMessage, - ]); - if (repliesCountUpdate) { - updateInfos.push(repliesCountUpdate); - } return { rawMessageInfos: [editMessage], - updateInfos, + updateInfos: [], }; }, canBeProcessed( dmOperation: DMSendEditMessageOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { if (utilities.threadInfos[dmOperation.threadID]) { return { isProcessingPossible: true }; } return { isProcessingPossible: false, reason: { type: 'missing_thread', threadID: dmOperation.threadID, }, }; }, }); 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 114c48c0f..608778d47 100644 --- a/lib/shared/dm-ops/send-reaction-message-spec.js +++ b/lib/shared/dm-ops/send-reaction-message-spec.js @@ -1,70 +1,56 @@ // @flow import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; -import { createRepliesCountUpdate } from './dm-op-utils.js'; import type { DMSendReactionMessageOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; -import type { ClientUpdateInfo } from '../../types/update-types.js'; const sendReactionMessageSpec: DMOperationSpec = Object.freeze({ - processDMOperation: async ( - dmOperation: DMSendReactionMessageOperation, - viewerID: string, - utilities: ProcessDMOperationUtilities, - ) => { + processDMOperation: async (dmOperation: DMSendReactionMessageOperation) => { 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 = []; - const threadInfo = utilities.threadInfos[threadID]; - const repliesCountUpdate = createRepliesCountUpdate(threadInfo, [ - reactionMessage, - ]); - if (repliesCountUpdate) { - updateInfos.push(repliesCountUpdate); - } return { rawMessageInfos: [reactionMessage], - updateInfos, + updateInfos: [], }; }, canBeProcessed( dmOperation: DMSendReactionMessageOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { if (utilities.threadInfos[dmOperation.threadID]) { return { isProcessingPossible: true }; } return { isProcessingPossible: false, reason: { type: 'missing_thread', threadID: dmOperation.threadID, }, }; }, }); 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 28cae7bef..e65e423e6 100644 --- a/lib/shared/dm-ops/send-text-message-spec.js +++ b/lib/shared/dm-ops/send-text-message-spec.js @@ -1,59 +1,47 @@ // @flow import type { DMOperationSpec, ProcessDMOperationUtilities, } from './dm-op-spec.js'; -import { createRepliesCountUpdate } from './dm-op-utils.js'; import type { DMSendTextMessageOperation } from '../../types/dm-ops.js'; import { messageTypes } from '../../types/message-types-enum.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; const sendTextMessageSpec: DMOperationSpec = Object.freeze({ - processDMOperation: async ( - dmOperation: DMSendTextMessageOperation, - viewerID: string, - utilities: ProcessDMOperationUtilities, - ) => { + processDMOperation: async (dmOperation: DMSendTextMessageOperation) => { const { threadID, creatorID, time, messageID, text } = dmOperation; const textMessage = { type: messageTypes.TEXT, id: messageID, threadID, creatorID, time, text, }; const updateInfos: Array = []; - const threadInfo = utilities.threadInfos[threadID]; - const repliesCountUpdate = createRepliesCountUpdate(threadInfo, [ - textMessage, - ]); - if (repliesCountUpdate) { - updateInfos.push(repliesCountUpdate); - } return { rawMessageInfos: [textMessage], updateInfos, }; }, canBeProcessed( dmOperation: DMSendTextMessageOperation, viewerID: string, utilities: ProcessDMOperationUtilities, ) { if (utilities.threadInfos[dmOperation.threadID]) { return { isProcessingPossible: true }; } return { isProcessingPossible: false, reason: { type: 'missing_thread', threadID: dmOperation.threadID, }, }; }, }); export { sendTextMessageSpec };