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,17 +1,27 @@ // @flow +import invariant from 'invariant'; +import * as React from 'react'; + import { extractKeyserverIDFromID, extractKeyserverIDFromIDOptional, } 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 } from '../shared/dm-ops/dm-op-utils.js'; +import { dmOperationSpecificationTypes } from '../shared/dm-ops/dm-op-utils.js'; +import { useProcessAndSendDMOperation } from '../shared/dm-ops/process-dm-ops.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 = { +activityUpdates: $ReadOnlyArray, @@ -98,10 +108,49 @@ }; }; -function useSetThreadUnreadStatus(): ( +function useSetThreadUnreadStatus( + threadInfo: ThreadInfo, +): ( request: SetThreadUnreadStatusRequest, ) => Promise { - return useKeyserverCall(setThreadUnreadStatus); + const viewerID = useSelector( + state => state.currentUserInfo && state.currentUserInfo.id, + ); + const processAndSendDMOperation = useProcessAndSendDMOperation(); + const keyserverCall = useKeyserverCall(setThreadUnreadStatus); + + return React.useCallback( + async (input: SetThreadUnreadStatusRequest) => { + if (!threadTypeIsThick(threadInfo.type)) { + return await keyserverCall(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 processAndSendDMOperation(opSpecification); + return { + resetToUnread: false, + threadID: threadInfo.id, + }; + }, + + [keyserverCall, threadInfo, viewerID, processAndSendDMOperation], + ); } export { diff --git a/lib/hooks/toggle-unread-status.js b/lib/hooks/toggle-unread-status.js --- a/lib/hooks/toggle-unread-status.js +++ b/lib/hooks/toggle-unread-status.js @@ -22,7 +22,9 @@ const { currentUser } = threadInfo; const boundSetThreadUnreadStatus: ( request: SetThreadUnreadStatusRequest, - ) => Promise = useSetThreadUnreadStatus(); + ) => Promise = + useSetThreadUnreadStatus(threadInfo); + const toggleUnreadStatus = React.useCallback(() => { const request = { threadID: threadInfo.id, @@ -40,7 +42,7 @@ ); afterAction(); }, [ - threadInfo.id, + threadInfo, currentUser.unread, mostRecentNonLocalMessage, dispatchActionPromise,