diff --git a/lib/shared/dm-ops/create-entry-spec.js b/lib/shared/dm-ops/create-entry-spec.js new file mode 100644 --- /dev/null +++ b/lib/shared/dm-ops/create-entry-spec.js @@ -0,0 +1,90 @@ +// @flow + +import uuid from 'uuid'; + +import type { + DMOperationSpec, + ProcessDMOperationUtilities, +} from './dm-op-spec.js'; +import type { DMCreateEntryOperation } from '../../types/dm-ops.js'; +import type { ThickRawEntryInfo } from '../../types/entry-types.js'; +import { messageTypes } from '../../types/message-types-enum.js'; +import { updateTypes } from '../../types/update-types-enum.js'; +import type { EntryUpdateInfo } from '../../types/update-types.js'; +import { dateFromString } from '../../utils/date-utils.js'; +import { rawMessageInfoFromMessageData } from '../message-utils.js'; + +function createMessageDataFromDMOperation(dmOperation: DMCreateEntryOperation) { + const { threadID, creatorID, time, entryID, entryDate, text } = dmOperation; + return { + type: messageTypes.CREATE_ENTRY, + threadID, + creatorID, + time, + entryID, + date: entryDate, + text, + }; +} + +const createEntrySpec: DMOperationSpec = Object.freeze({ + notificationsCreationData: async (dmOperation: DMCreateEntryOperation) => { + const messageData = createMessageDataFromDMOperation(dmOperation); + return { messageDatas: [messageData] }; + }, + processDMOperation: async (dmOperation: DMCreateEntryOperation) => { + const { threadID, creatorID, time, entryID, entryDate, text, messageID } = + dmOperation; + + const messageData = createMessageDataFromDMOperation(dmOperation); + const rawMessageInfos = [ + rawMessageInfoFromMessageData(messageData, messageID), + ]; + + const date = dateFromString(entryDate); + const rawEntryInfo: ThickRawEntryInfo = { + id: entryID, + threadID, + text, + year: date.getFullYear(), + month: date.getMonth() + 1, + day: date.getDate(), + creationTime: time, + creatorID, + thick: true, + deleted: false, + lastUpdatedTime: time, + }; + + const entryUpdateInfo: EntryUpdateInfo = { + entryInfo: rawEntryInfo, + type: updateTypes.UPDATE_ENTRY, + id: uuid.v4(), + time, + }; + + return { + rawMessageInfos, + updateInfos: [entryUpdateInfo], + }; + }, + canBeProcessed( + dmOperation: DMCreateEntryOperation, + viewerID: string, + utilities: ProcessDMOperationUtilities, + ) { + if (utilities.threadInfos[dmOperation.threadID]) { + return { isProcessingPossible: true }; + } + return { + isProcessingPossible: false, + reason: { + type: 'missing_thread', + threadID: dmOperation.threadID, + }, + }; + }, + supportsAutoRetry: true, +}); + +export { createEntrySpec }; 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 @@ -5,6 +5,7 @@ import { changeThreadReadStatusSpec } from './change-thread-read-status-spec.js'; import { changeThreadSettingsSpec } from './change-thread-settings-spec.js'; import { changeThreadSubscriptionSpec } from './change-thread-subscription.js'; +import { createEntrySpec } from './create-entry-spec.js'; import { createSidebarSpec } from './create-sidebar-spec.js'; import { createThreadSpec } from './create-thread-spec.js'; import type { DMOperationSpec } from './dm-op-spec.js'; @@ -34,4 +35,5 @@ [dmOperationTypes.CHANGE_THREAD_SETTINGS]: changeThreadSettingsSpec, [dmOperationTypes.CHANGE_THREAD_SUBSCRIPTION]: changeThreadSubscriptionSpec, [dmOperationTypes.CHANGE_THREAD_READ_STATUS]: changeThreadReadStatusSpec, + [dmOperationTypes.CREATE_ENTRY]: createEntrySpec, }); 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 @@ -40,6 +40,7 @@ CHANGE_THREAD_SETTINGS: 'change_thread_settings', CHANGE_THREAD_SUBSCRIPTION: 'change_thread_subscription', CHANGE_THREAD_READ_STATUS: 'change_thread_read_status', + CREATE_ENTRY: 'create_entry', }); export type DMOperationType = $Values; @@ -370,6 +371,29 @@ | DMSendTextMessageOperation | DMSendMultimediaMessageOperation; +export type DMCreateEntryOperation = { + +type: 'create_entry', + +threadID: string, + +creatorID: string, + +time: number, + +entryID: string, + +entryDate: string, + +text: string, + +messageID: string, +}; + +export const dmCreateEntryOperationValidator: TInterface = + tShape({ + type: tString(dmOperationTypes.CREATE_ENTRY), + threadID: t.String, + creatorID: t.String, + time: t.Number, + entryID: t.String, + entryDate: t.String, + text: t.String, + messageID: t.String, + }); + export type DMOperation = | DMCreateThreadOperation | DMCreateSidebarOperation @@ -384,7 +408,8 @@ | DMRemoveMembersOperation | DMChangeThreadSettingsOperation | DMChangeThreadSubscriptionOperation - | DMChangeThreadReadStatusOperation; + | DMChangeThreadReadStatusOperation + | DMCreateEntryOperation; export const dmOperationValidator: TUnion = t.union([ dmCreateThreadOperationValidator, dmCreateSidebarOperationValidator, @@ -400,6 +425,7 @@ dmChangeThreadSettingsOperationValidator, dmChangeThreadSubscriptionOperationValidator, dmChangeThreadReadStatusOperationValidator, + dmCreateEntryOperationValidator, ]); export type DMOperationResult = {