diff --git a/lib/shared/dm-ops/delete-entry-spec.js b/lib/shared/dm-ops/delete-entry-spec.js new file mode 100644 --- /dev/null +++ b/lib/shared/dm-ops/delete-entry-spec.js @@ -0,0 +1,97 @@ +// @flow + +import uuid from 'uuid'; + +import type { + DMOperationSpec, + ProcessDMOperationUtilities, +} from './dm-op-spec.js'; +import type { DMDeleteEntryOperation } 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: DMDeleteEntryOperation) { + const { threadID, creatorID, time, entryID, entryDate, text } = dmOperation; + return { + type: messageTypes.DELETE_ENTRY, + threadID, + creatorID, + time, + entryID, + date: entryDate, + text, + }; +} + +const deleteEntrySpec: DMOperationSpec = Object.freeze({ + notificationsCreationData: async (dmOperation: DMDeleteEntryOperation) => { + const messageData = createMessageDataFromDMOperation(dmOperation); + return { messageDatas: [messageData] }; + }, + processDMOperation: async (dmOperation: DMDeleteEntryOperation) => { + const { + threadID, + creatorID, + time, + entryID, + entryDate: 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: true, + lastUpdatedTime: time, + }; + + const entryUpdateInfo: EntryUpdateInfo = { + entryInfo: rawEntryInfo, + type: updateTypes.UPDATE_ENTRY, + id: uuid.v4(), + time, + }; + + return { + rawMessageInfos, + updateInfos: [entryUpdateInfo], + }; + }, + canBeProcessed( + dmOperation: DMDeleteEntryOperation, + 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 { deleteEntrySpec }; 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 @@ -8,6 +8,7 @@ import { createEntrySpec } from './create-entry-spec.js'; import { createSidebarSpec } from './create-sidebar-spec.js'; import { createThreadSpec } from './create-thread-spec.js'; +import { deleteEntrySpec } from './delete-entry-spec.js'; import type { DMOperationSpec } from './dm-op-spec.js'; import { joinThreadSpec } from './join-thread-spec.js'; import { leaveThreadSpec } from './leave-thread-spec.js'; @@ -36,4 +37,5 @@ [dmOperationTypes.CHANGE_THREAD_SUBSCRIPTION]: changeThreadSubscriptionSpec, [dmOperationTypes.CHANGE_THREAD_READ_STATUS]: changeThreadReadStatusSpec, [dmOperationTypes.CREATE_ENTRY]: createEntrySpec, + [dmOperationTypes.DELETE_ENTRY]: deleteEntrySpec, }); 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 @@ -41,6 +41,7 @@ CHANGE_THREAD_SUBSCRIPTION: 'change_thread_subscription', CHANGE_THREAD_READ_STATUS: 'change_thread_read_status', CREATE_ENTRY: 'create_entry', + DELETE_ENTRY: 'delete_entry', }); export type DMOperationType = $Values; @@ -394,6 +395,29 @@ messageID: t.String, }); +export type DMDeleteEntryOperation = { + +type: 'delete_entry', + +threadID: string, + +creatorID: string, + +time: number, + +entryID: string, + +entryDate: string, + +text: string, + +messageID: string, +}; + +export const dmDeleteEntryOperationValidator: TInterface = + tShape({ + type: tString(dmOperationTypes.DELETE_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 @@ -409,7 +433,8 @@ | DMChangeThreadSettingsOperation | DMChangeThreadSubscriptionOperation | DMChangeThreadReadStatusOperation - | DMCreateEntryOperation; + | DMCreateEntryOperation + | DMDeleteEntryOperation; export const dmOperationValidator: TUnion = t.union([ dmCreateThreadOperationValidator, dmCreateSidebarOperationValidator, @@ -426,6 +451,7 @@ dmChangeThreadSubscriptionOperationValidator, dmChangeThreadReadStatusOperationValidator, dmCreateEntryOperationValidator, + dmDeleteEntryOperationValidator, ]); export type DMOperationResult = {