diff --git a/lib/shared/edit-messages-utils.js b/lib/shared/edit-messages-utils.js --- a/lib/shared/edit-messages-utils.js +++ b/lib/shared/edit-messages-utils.js @@ -1,30 +1,18 @@ // @flow -import invariant from 'invariant'; import * as React from 'react'; -import uuid from 'uuid'; -import { - type OutboundDMOperationSpecification, - dmOperationSpecificationTypes, -} from './dm-ops/dm-op-types.js'; import { useProcessAndSendDMOperation } from './dm-ops/process-dm-ops.js'; import { threadIsPending, useThreadHasPermission } from './thread-utils.js'; -import { sendEditMessageActionTypes } from '../actions/message-actions.js'; +import { threadSpecs } from './threads/thread-specs.js'; import { useSendEditMessage } from '../hooks/message-hooks.js'; -import { type DMSendEditMessageOperation } from '../types/dm-ops.js'; import type { ComposableMessageInfo, - RawMessageInfo, RobotextMessageInfo, } from '../types/message-types'; import { messageTypes } from '../types/message-types-enum.js'; import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; import { threadPermissions } from '../types/thread-permission-types.js'; -import { - thickThreadTypes, - threadTypeIsThick, -} from '../types/thread-types-enum.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector } from '../utils/redux-utils.js'; @@ -39,59 +27,21 @@ ); return React.useCallback( - async (messageID, newText) => { - if (threadTypeIsThick(threadInfo.type)) { - invariant(viewerID, 'viewerID should be set'); - const op: DMSendEditMessageOperation = { - type: 'send_edit_message', - threadID: threadInfo.id, - creatorID: viewerID, - time: Date.now(), - messageID: uuid.v4(), - targetMessageID: messageID, - text: newText, - }; - const opSpecification: OutboundDMOperationSpecification = { - type: dmOperationSpecificationTypes.OUTBOUND, - op, - recipients: { - type: 'all_thread_members', - threadID: - threadInfo.type === thickThreadTypes.THICK_SIDEBAR && - threadInfo.parentThreadID - ? threadInfo.parentThreadID - : threadInfo.id, - }, - }; - await processAndSendDMOperation(opSpecification); - } else { - const editMessagePromise = (async () => { - const result = await callEditMessage({ - targetMessageID: messageID, - text: newText, - }); - - return ({ - newMessageInfos: result.newMessageInfos, - }: { +newMessageInfos: $ReadOnlyArray }); - })(); - - void dispatchActionPromise( - sendEditMessageActionTypes, - editMessagePromise, - ); - - await editMessagePromise; - } - }, + (messageID, newText) => + threadSpecs[threadInfo.type].protocol.editTextMessage( + { threadInfo, messageID, newText, viewerID }, + { + keyserverEditMessage: callEditMessage, + dispatchActionPromise, + processAndSendDMOperation, + }, + ), [ - threadInfo.type, - threadInfo.id, - threadInfo.parentThreadID, - viewerID, - processAndSendDMOperation, - dispatchActionPromise, callEditMessage, + dispatchActionPromise, + processAndSendDMOperation, + threadInfo, + viewerID, ], ); } diff --git a/lib/shared/threads/protocols/dm-thread-protocol.js b/lib/shared/threads/protocols/dm-thread-protocol.js --- a/lib/shared/threads/protocols/dm-thread-protocol.js +++ b/lib/shared/threads/protocols/dm-thread-protocol.js @@ -3,15 +3,21 @@ import invariant from 'invariant'; import uuid from 'uuid'; +import type { DMSendEditMessageOperation } from '../../../types/dm-ops.js'; import { thickThreadTypes } from '../../../types/thread-types-enum.js'; import { SendMessageError } from '../../../utils/errors.js'; -import { dmOperationSpecificationTypes } from '../../dm-ops/dm-op-types.js'; +import { + dmOperationSpecificationTypes, + type OutboundDMOperationSpecification, +} from '../../dm-ops/dm-op-types.js'; import { type ProtocolSendTextMessageInput, type SendMultimediaMessageUtils, type SendTextMessageUtils, type ThreadProtocol, type ProtocolSendMultimediaMessageInput, + type ProtocolEditTextMessageInput, + type EditTextMessageUtils, } from '../thread-spec.js'; const dmThreadProtocol: ThreadProtocol = Object.freeze({ @@ -127,6 +133,36 @@ }, }; }, + + editTextMessage: async ( + message: ProtocolEditTextMessageInput, + utils: EditTextMessageUtils, + ) => { + const { viewerID, threadInfo, messageID, newText } = message; + invariant(viewerID, 'viewerID should be set'); + const op: DMSendEditMessageOperation = { + type: 'send_edit_message', + threadID: threadInfo.id, + creatorID: viewerID, + time: Date.now(), + messageID: uuid.v4(), + targetMessageID: messageID, + text: newText, + }; + const opSpecification: OutboundDMOperationSpecification = { + type: dmOperationSpecificationTypes.OUTBOUND, + op, + recipients: { + type: 'all_thread_members', + threadID: + threadInfo.type === thickThreadTypes.THICK_SIDEBAR && + threadInfo.parentThreadID + ? threadInfo.parentThreadID + : threadInfo.id, + }, + }; + await utils.processAndSendDMOperation(opSpecification); + }, }); export { dmThreadProtocol }; diff --git a/lib/shared/threads/protocols/keyserver-thread-protocol.js b/lib/shared/threads/protocols/keyserver-thread-protocol.js --- a/lib/shared/threads/protocols/keyserver-thread-protocol.js +++ b/lib/shared/threads/protocols/keyserver-thread-protocol.js @@ -3,6 +3,7 @@ import invariant from 'invariant'; import type { ProcessHolders } from '../../../actions/holder-actions.js'; +import { sendEditMessageActionTypes } from '../../../actions/message-actions.js'; import { type MediaMetadataReassignmentAction, updateMultimediaMessageMediaActionType, @@ -21,6 +22,7 @@ type MediaIDUpdatePayload, type MediaIDUpdates, type RawMultimediaMessageInfo, + type RawMessageInfo, } from '../../../types/message-types.js'; import { getMediaMessageServerDBContentsFromMedia } from '../../../types/messages/media.js'; import type { Dispatch } from '../../../types/redux-types.js'; @@ -34,6 +36,8 @@ type SendTextMessageUtils, type ProtocolSendMultimediaMessageInput, type SendMultimediaMessageUtils, + type ProtocolEditTextMessageInput, + type EditTextMessageUtils, } from '../thread-spec.js'; const keyserverThreadProtocol: ThreadProtocol = Object.freeze({ @@ -133,6 +137,30 @@ mediaIDUpdates, }; }, + + editTextMessage: async ( + message: ProtocolEditTextMessageInput, + utils: EditTextMessageUtils, + ) => { + const { messageID, newText } = message; + const editMessagePromise = (async () => { + const result = await utils.keyserverEditMessage({ + targetMessageID: messageID, + text: newText, + }); + + return ({ + newMessageInfos: result.newMessageInfos, + }: { +newMessageInfos: $ReadOnlyArray }); + })(); + + void utils.dispatchActionPromise( + sendEditMessageActionTypes, + editMessagePromise, + ); + + await editMessagePromise; + }, }); function mediaIDIsKeyserverID(mediaID: string): boolean { diff --git a/lib/shared/threads/thread-spec.js b/lib/shared/threads/thread-spec.js --- a/lib/shared/threads/thread-spec.js +++ b/lib/shared/threads/thread-spec.js @@ -13,6 +13,8 @@ SendMessagePayload, RawMultimediaMessageInfo, SendMultimediaMessagePayload, + SendEditMessageRequest, + SendEditMessageResult, } from '../../types/message-types.js'; import type { RawTextMessageInfo } from '../../types/messages/text.js'; import type { @@ -20,7 +22,11 @@ ThreadInfo, } from '../../types/minimally-encoded-thread-permissions-types.js'; import type { Dispatch } from '../../types/redux-types.js'; -import type { OutboundComposableDMOperationSpecification } from '../dm-ops/dm-op-types.js'; +import type { DispatchActionPromise } from '../../utils/redux-promise-utils.js'; +import type { + OutboundComposableDMOperationSpecification, + OutboundDMOperationSpecification, +} from '../dm-ops/dm-op-types.js'; export type ThreadTrait = | 'sidebar' @@ -56,6 +62,18 @@ +dispatch: Dispatch, }; +export type ProtocolEditTextMessageInput = { + +messageID: string, + +newText: string, + +threadInfo: ThreadInfo, + +viewerID: ?string, +}; +export type EditTextMessageUtils = { + +keyserverEditMessage: SendEditMessageRequest => Promise, + +dispatchActionPromise: DispatchActionPromise, + +processAndSendDMOperation: OutboundDMOperationSpecification => Promise, +}; + export type ThreadProtocol = { +sendTextMessage: ( message: ProtocolSendTextMessageInput, @@ -65,6 +83,10 @@ message: ProtocolSendMultimediaMessageInput, utils: SendMultimediaMessageUtils, ) => Promise, + +editTextMessage: ( + message: ProtocolEditTextMessageInput, + utils: EditTextMessageUtils, + ) => Promise, }; export type ThreadSpec = {