diff --git a/lib/shared/dm-ops/dm-op-specs.js b/lib/shared/dm-ops/dm-op-specs.js --- a/lib/shared/dm-ops/dm-op-specs.js +++ b/lib/shared/dm-ops/dm-op-specs.js @@ -6,6 +6,7 @@ import type { DMOperationSpec } from './dm-op-spec.js'; import { joinThreadSpec } from './join-thread-spec.js'; import { leaveThreadSpec } from './leave-thread-spec.js'; +import { removeMembersSpec } from './remove-members-spec.js'; import { sendEditMessageSpec } from './send-edit-message-spec.js'; import { sendReactionMessageSpec } from './send-reaction-message-spec.js'; import { sendTextMessageSpec } from './send-text-message-spec.js'; @@ -22,4 +23,5 @@ [dmOperationTypes.ADD_MEMBERS]: addMembersSpec, [dmOperationTypes.JOIN_THREAD]: joinThreadSpec, [dmOperationTypes.LEAVE_THREAD]: leaveThreadSpec, + [dmOperationTypes.REMOVE_MEMBERS]: removeMembersSpec, }); diff --git a/lib/shared/dm-ops/remove-members-spec.js b/lib/shared/dm-ops/remove-members-spec.js new file mode 100644 --- /dev/null +++ b/lib/shared/dm-ops/remove-members-spec.js @@ -0,0 +1,75 @@ +// @flow + +import uuid from 'uuid'; + +import type { + DMOperationSpec, + ProcessDMOperationUtilities, +} from './dm-op-spec.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 { 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.getThreadInfo(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) { + updateInfos.push({ + type: updateTypes.DELETE_THREAD, + id: uuid.v4(), + time, + threadID, + }); + } else { + const updatedThreadInfo = { + ...threadInfo, + members: threadInfo.members.filter( + member => !removedUserIDsSet.has(member.id), + ), + }; + updateInfos.push({ + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time, + threadInfo: updatedThreadInfo, + }); + } + return { + rawMessageInfos: [removeMembersMessage], + updateInfos, + }; + }, + }); + +export { removeMembersSpec }; diff --git a/lib/types/dm-ops.js b/lib/types/dm-ops.js --- a/lib/types/dm-ops.js +++ b/lib/types/dm-ops.js @@ -30,6 +30,7 @@ ADD_MEMBERS: 'add_members', JOIN_THREAD: 'join_thread', LEAVE_THREAD: 'leave_thread', + REMOVE_MEMBERS: 'remove_members', }); export type DMOperationType = $Values; @@ -203,6 +204,24 @@ threadID: t.String, }); +export type DMRemoveMembersOperation = { + +type: 'remove_members', + +editorID: string, + +time: number, + +messageID: string, + +threadID: string, + +removedUserIDs: $ReadOnlyArray, +}; +export const dmRemoveMembersOperationValidator: TInterface = + tShape({ + type: tString(dmOperationTypes.REMOVE_MEMBERS), + editorID: tUserID, + time: t.Number, + messageID: t.String, + threadID: t.String, + removedUserIDs: t.list(tUserID), + }); + export type DMOperation = | DMCreateThreadOperation | DMCreateSidebarOperation @@ -211,7 +230,8 @@ | DMSendEditMessageOperation | DMAddMembersOperation | DMJoinThreadOperation - | DMLeaveThreadOperation; + | DMLeaveThreadOperation + | DMRemoveMembersOperation; export const dmOperationValidator: TUnion = t.union([ dmCreateThreadOperationValidator, dmCreateSidebarOperationValidator, @@ -221,6 +241,7 @@ dmAddMembersOperationValidator, dmJoinThreadOperationValidator, dmLeaveThreadOperationValidator, + dmRemoveMembersOperationValidator, ]); export type DMOperationResult = {