Skip to content

Commit

Permalink
Implement maunally marking DM thread as read/unread
Browse files Browse the repository at this point in the history
Summary: This differential implements marking dm thread as read/unread manually (by toggle).

Test Plan: Play around marking a thread as read/unrad on one device and ensure that changes are reflected in the UI of another device (if app is active) or result in correct badge updates/rescinds (inactive app)

Reviewers: tomek, kamil

Reviewed By: tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D13238
  • Loading branch information
marcinwasowicz committed Sep 9, 2024
1 parent 6fdb407 commit af90fa4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
53 changes: 51 additions & 2 deletions lib/actions/activity-actions.js
Original file line number Diff line number Diff line change
@@ -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<ActivityUpdate>,
Expand Down Expand Up @@ -98,10 +108,49 @@ const setThreadUnreadStatus =
};
};

function useSetThreadUnreadStatus(): (
function useSetThreadUnreadStatus(
threadInfo: ThreadInfo,
): (
request: SetThreadUnreadStatusRequest,
) => Promise<SetThreadUnreadStatusPayload> {
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 {
Expand Down
6 changes: 4 additions & 2 deletions lib/hooks/toggle-unread-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ function useToggleUnreadStatus(
const { currentUser } = threadInfo;
const boundSetThreadUnreadStatus: (
request: SetThreadUnreadStatusRequest,
) => Promise<SetThreadUnreadStatusPayload> = useSetThreadUnreadStatus();
) => Promise<SetThreadUnreadStatusPayload> =
useSetThreadUnreadStatus(threadInfo);

const toggleUnreadStatus = React.useCallback(() => {
const request = {
threadID: threadInfo.id,
Expand All @@ -40,7 +42,7 @@ function useToggleUnreadStatus(
);
afterAction();
}, [
threadInfo.id,
threadInfo,
currentUser.unread,
mostRecentNonLocalMessage,
dispatchActionPromise,
Expand Down

0 comments on commit af90fa4

Please sign in to comment.