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,81 @@ +// @flow + +import uuid from 'uuid'; + +import type { DMOperationSpec } 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, date, text } = dmOperation; + return { + type: messageTypes.CREATE_ENTRY, + threadID, + creatorID, + time, + entryID, + date, + 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, + date: dateString, + text, + messageID, + } = dmOperation; + + const messageData = createMessageDataFromDMOperation(dmOperation); + const rawMessageInfos = [ + rawMessageInfoFromMessageData(messageData, messageID), + ]; + + const date = dateFromString(dateString); + 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() { + return { isProcessingPossible: true }; + }, + supportsAutoRetry: true, +}); + +export { 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 @@ -38,6 +38,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; @@ -346,6 +347,29 @@ unread: t.Boolean, }); +export type DMCreateEntryOperation = { + +type: 'create_entry', + +threadID: string, + +creatorID: string, + +time: number, + +entryID: string, + +date: 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, + date: t.String, + text: t.String, + messageID: t.String, + }); + export type DMOperation = | DMCreateThreadOperation | DMCreateSidebarOperation @@ -359,7 +383,8 @@ | DMRemoveMembersOperation | DMChangeThreadSettingsOperation | DMChangeThreadSubscriptionOperation - | DMChangeThreadReadStatusOperation; + | DMChangeThreadReadStatusOperation + | DMCreateEntryOperation; export const dmOperationValidator: TUnion = t.union([ dmCreateThreadOperationValidator, dmCreateSidebarOperationValidator, @@ -374,6 +399,7 @@ dmChangeThreadSettingsOperationValidator, dmChangeThreadSubscriptionOperationValidator, dmChangeThreadReadStatusOperationValidator, + dmCreateEntryOperationValidator, ]); export type DMOperationResult = {