diff --git a/lib/actions/activity-actions.js b/lib/actions/activity-actions.js --- a/lib/actions/activity-actions.js +++ b/lib/actions/activity-actions.js @@ -1,6 +1,5 @@ // @flow -import invariant from 'invariant'; import * as React from 'react'; import { @@ -9,20 +8,15 @@ } from '../keyserver-conn/keyserver-call-utils.js'; import { useKeyserverCall } from '../keyserver-conn/keyserver-call.js'; import type { CallKeyserverEndpoint } from '../keyserver-conn/keyserver-conn-types.js'; -import { - type OutboundDMOperationSpecification, - dmOperationSpecificationTypes, -} from '../shared/dm-ops/dm-op-types.js'; import { useProcessAndSendDMOperation } from '../shared/dm-ops/process-dm-ops.js'; +import { threadSpecs } from '../shared/threads/thread-specs.js'; import type { ActivityUpdate, ActivityUpdateSuccessPayload, SetThreadUnreadStatusPayload, SetThreadUnreadStatusRequest, } from '../types/activity-types.js'; -import type { DMChangeThreadReadStatusOperation } from '../types/dm-ops.js'; import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types'; -import { threadTypeIsThick } from '../types/thread-types-enum.js'; import { useSelector } from '../utils/redux-utils.js'; export type UpdateActivityInput = { @@ -124,36 +118,14 @@ const keyserverCall = useKeyserverCall(setThreadUnreadStatus); return React.useCallback( - async (input: UseSetThreadUnreadStatusInput) => { - if (!threadTypeIsThick(input.threadInfo.type)) { - const { threadInfo, ...rest } = input; - return await keyserverCall({ ...rest }); - } - - invariant(viewerID, 'viewerID must be set'); - const { threadInfo } = input; - const op: DMChangeThreadReadStatusOperation = { - type: 'change_thread_read_status', - time: Date.now(), - threadID: threadInfo.id, - creatorID: viewerID, - unread: !threadInfo.currentUser.unread, - }; - - const opSpecification: OutboundDMOperationSpecification = { - type: dmOperationSpecificationTypes.OUTBOUND, - op, - recipients: { - type: 'self_devices', + async (input: UseSetThreadUnreadStatusInput) => + threadSpecs[input.threadInfo.type].protocol.setThreadUnreadStatus( + { input, viewerID }, + { + processAndSendDMOperation, + keyserverSetThreadUnreadStatus: keyserverCall, }, - }; - - await processAndSendDMOperation(opSpecification); - return { - resetToUnread: false, - threadID: threadInfo.id, - }; - }, + ), [keyserverCall, viewerID, processAndSendDMOperation], ); 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 @@ -5,6 +5,7 @@ import type { TunnelbrokerSocketState } from '../../../tunnelbroker/tunnelbroker-context.js'; import type { + DMChangeThreadReadStatusOperation, DMChangeThreadSettingsOperation, DMCreateEntryOperation, DMDeleteEntryOperation, @@ -36,6 +37,8 @@ DeleteEntryUtils, ProtocolEditEntryInput, EditEntryUtils, + ProtocolSetThreadUnreadStatusInput, + SetThreadUnreadStatusUtils, } from '../thread-spec.js'; const dmThreadProtocol: ThreadProtocol = Object.freeze({ @@ -393,6 +396,39 @@ }, }; }, + + setThreadUnreadStatus: async ( + input: ProtocolSetThreadUnreadStatusInput, + utils: SetThreadUnreadStatusUtils, + ) => { + const { + viewerID, + input: { threadInfo }, + } = input; + + invariant(viewerID, 'viewerID must be set'); + const op: DMChangeThreadReadStatusOperation = { + type: 'change_thread_read_status', + time: Date.now(), + threadID: threadInfo.id, + creatorID: viewerID, + unread: !threadInfo.currentUser.unread, + }; + + const opSpecification: OutboundDMOperationSpecification = { + type: dmOperationSpecificationTypes.OUTBOUND, + op, + recipients: { + type: 'self_devices', + }, + }; + + await utils.processAndSendDMOperation(opSpecification); + return { + resetToUnread: false, + threadID: threadInfo.id, + }; + }, }); 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 @@ -47,6 +47,8 @@ DeleteEntryUtils, ProtocolEditEntryInput, EditEntryUtils, + ProtocolSetThreadUnreadStatusInput, + SetThreadUnreadStatusUtils, } from '../thread-spec.js'; const keyserverThreadProtocol: ThreadProtocol = Object.freeze({ @@ -200,6 +202,16 @@ protocolInput: ProtocolEditEntryInput, utils: EditEntryUtils, ) => utils.keyserverEditEntry(protocolInput.input.saveEntryInfo), + + setThreadUnreadStatus: ( + input: ProtocolSetThreadUnreadStatusInput, + utils: SetThreadUnreadStatusUtils, + ) => { + const { + input: { threadInfo, ...rest }, + } = input; + return utils.keyserverSetThreadUnreadStatus(rest); + }, }); 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 @@ -1,5 +1,6 @@ // @flow +import type { UseSetThreadUnreadStatusInput } from '../../actions/activity-actions.js'; import type { UseCreateEntryInput, UseDeleteEntryInput, @@ -15,6 +16,10 @@ import { type MediaMetadataReassignmentAction } from '../../actions/upload-actions.js'; import type { ProcessOutboundP2PMessagesResult } from '../../tunnelbroker/peer-to-peer-context.js'; import type { TunnelbrokerSocketState } from '../../tunnelbroker/tunnelbroker-context.js'; +import type { + SetThreadUnreadStatusPayload, + SetThreadUnreadStatusRequest, +} from '../../types/activity-types.js'; import type { CreateEntryInfo, CreateEntryPayload, @@ -132,6 +137,15 @@ +keyserverEditEntry: SaveEntryInfo => Promise, }; +export type ProtocolSetThreadUnreadStatusInput = { + +input: UseSetThreadUnreadStatusInput, + +viewerID: ?string, +}; +export type SetThreadUnreadStatusUtils = { + +processAndSendDMOperation: OutboundDMOperationSpecification => Promise, + +keyserverSetThreadUnreadStatus: SetThreadUnreadStatusRequest => Promise, +}; + export type ThreadProtocol = { +sendTextMessage: ( message: ProtocolSendTextMessageInput, @@ -166,6 +180,10 @@ input: ProtocolEditEntryInput, utils: EditEntryUtils, ) => Promise, + +setThreadUnreadStatus: ( + input: ProtocolSetThreadUnreadStatusInput, + utils: SetThreadUnreadStatusUtils, + ) => Promise, }; export type ThreadSpec = {