diff --git a/lib/shared/dm-ops/dm-op-specs.js b/lib/shared/dm-ops/dm-op-specs.js index 1df0e52ad..1a5450270 100644 --- a/lib/shared/dm-ops/dm-op-specs.js +++ b/lib/shared/dm-ops/dm-op-specs.js @@ -1,15 +1,17 @@ // @flow import { createSidebarSpec } from './create-sidebar-spec.js'; import { createThreadSpec } from './create-thread-spec.js'; import type { DMOperationSpec } from './dm-op-spec.js'; +import { sendReactionMessageSpec } from './send-reaction-message-spec.js'; import { sendTextMessageSpec } from './send-text-message-spec.js'; import { type DMOperationType, dmOperationTypes } from '../../types/dm-ops.js'; export const dmOpSpecs: { +[DMOperationType]: DMOperationSpec, } = Object.freeze({ [dmOperationTypes.CREATE_THREAD]: createThreadSpec, [dmOperationTypes.CREATE_SIDEBAR]: createSidebarSpec, [dmOperationTypes.SEND_TEXT_MESSAGE]: sendTextMessageSpec, + [dmOperationTypes.SEND_REACTION_MESSAGE]: sendReactionMessageSpec, }); diff --git a/lib/shared/dm-ops/send-reaction-message-spec.js b/lib/shared/dm-ops/send-reaction-message-spec.js new file mode 100644 index 000000000..f2fd1d794 --- /dev/null +++ b/lib/shared/dm-ops/send-reaction-message-spec.js @@ -0,0 +1,54 @@ +// @flow + +import uuid from 'uuid'; + +import type { DMOperationSpec } from './dm-op-spec.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, + ) => { + 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, + }); + } + return { + rawMessageInfos: [reactionMessage], + updateInfos, + }; + }, + }); + +export { sendReactionMessageSpec }; diff --git a/lib/types/dm-ops.js b/lib/types/dm-ops.js index 33fec2720..f64e74d2e 100644 --- a/lib/types/dm-ops.js +++ b/lib/types/dm-ops.js @@ -1,108 +1,133 @@ // @flow import type { TInterface, TUnion } from 'tcomb'; import t from 'tcomb'; import type { RawMessageInfo } from './message-types.js'; import { type NonSidebarThickThreadType, nonSidebarThickThreadTypes, } from './thread-types-enum.js'; import type { ClientUpdateInfo } from './update-types.js'; import { values } from '../utils/objects.js'; import { tShape, tString, tUserID } from '../utils/validation-utils.js'; export const dmOperationTypes = Object.freeze({ CREATE_THREAD: 'create_thread', CREATE_SIDEBAR: 'create_sidebar', SEND_TEXT_MESSAGE: 'send_text_message', + SEND_REACTION_MESSAGE: 'send_reaction_message', }); export type DMOperationType = $Values; export type DMCreateThreadOperation = { +type: 'create_thread', +threadID: string, +creatorID: string, +time: number, +threadType: NonSidebarThickThreadType, +memberIDs: $ReadOnlyArray, +roleID: string, +newMessageID: string, }; export const dmCreateThreadOperationValidator: TInterface = tShape({ type: tString(dmOperationTypes.CREATE_THREAD), threadID: t.String, creatorID: tUserID, time: t.Number, threadType: t.enums.of(values(nonSidebarThickThreadTypes)), memberIDs: t.list(tUserID), roleID: t.String, newMessageID: t.String, }); export type DMCreateSidebarOperation = { +type: 'create_sidebar', +threadID: string, +creatorID: string, +time: number, +parentThreadID: string, +memberIDs: $ReadOnlyArray, +sourceMessageID: string, +roleID: string, +newSidebarSourceMessageID: string, +newCreateSidebarMessageID: string, }; export const dmCreateSidebarOperationValidator: TInterface = tShape({ type: tString(dmOperationTypes.CREATE_SIDEBAR), threadID: t.String, creatorID: tUserID, time: t.Number, parentThreadID: t.String, memberIDs: t.list(tUserID), sourceMessageID: t.String, roleID: t.String, newSidebarSourceMessageID: t.String, newCreateSidebarMessageID: t.String, }); export type DMSendTextMessageOperation = { +type: 'send_text_message', +threadID: string, +creatorID: string, +time: number, +messageID: string, +text: string, }; export const dmSendTextMessageOperationValidator: TInterface = tShape({ type: tString(dmOperationTypes.SEND_TEXT_MESSAGE), threadID: t.String, creatorID: tUserID, time: t.Number, messageID: t.String, text: t.String, }); +export type DMSendReactionMessageOperation = { + +type: 'send_reaction_message', + +threadID: string, + +creatorID: string, + +time: number, + +messageID: string, + +targetMessageID: string, + +reaction: string, + +action: 'add_reaction' | 'remove_reaction', +}; +export const dmSendReactionMessageOperationValidator: TInterface = + tShape({ + type: tString(dmOperationTypes.SEND_REACTION_MESSAGE), + threadID: t.String, + creatorID: tUserID, + time: t.Number, + messageID: t.String, + targetMessageID: t.String, + reaction: t.String, + action: t.enums.of(['add_reaction', 'remove_reaction']), + }); + export type DMOperation = | DMCreateThreadOperation | DMCreateSidebarOperation - | DMSendTextMessageOperation; + | DMSendTextMessageOperation + | DMSendReactionMessageOperation; export const dmOperationValidator: TUnion = t.union([ dmCreateThreadOperationValidator, dmCreateSidebarOperationValidator, dmSendTextMessageOperationValidator, + dmSendReactionMessageOperationValidator, ]); export type DMOperationResult = { rawMessageInfos: Array, updateInfos: Array, }; export const processDMOpsActionType = 'PROCESS_DM_OPS'; export type ProcessDMOpsPayload = { +rawMessageInfos: $ReadOnlyArray, +updateInfos: $ReadOnlyArray, };