Skip to content

Commit

Permalink
Implement updating DM thread subscription
Browse files Browse the repository at this point in the history
Summary: This differential implements synchronizing thread subscription in dm threads.

Test Plan:
1. Create dm thread and add another user. (Use UI button implemented by Kamil)
2. Play around with changing thread subscription and sending notifs.

Reviewers: kamil, tomek

Reviewed By: tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D13236
  • Loading branch information
marcinwasowicz committed Sep 9, 2024
1 parent 8e5753f commit a0bb600
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
59 changes: 55 additions & 4 deletions lib/actions/user-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import {
getPrekeyValueFromBlob,
} from '../shared/crypto-utils.js';
import { fetchLatestDeviceList } from '../shared/device-list-utils.js';
import type { OutboundDMOperationSpecification } from '../shared/dm-ops/dm-op-utils';
import { dmOperationSpecificationTypes } from '../shared/dm-ops/dm-op-utils.js';
import { useProcessAndSendDMOperation } from '../shared/dm-ops/process-dm-ops.js';
import { IdentityClientContext } from '../shared/identity-client-context.js';
import threadWatcher from '../shared/thread-watcher.js';
import {
Expand Down Expand Up @@ -54,6 +57,7 @@ import type {
UpdateUserAvatarRequest,
UpdateUserAvatarResponse,
} from '../types/avatar-types.js';
import type { DMChangeThreadSubscriptionOperation } from '../types/dm-ops';
import type { RawEntryInfo, CalendarQuery } from '../types/entry-types.js';
import type {
UserIdentitiesResponse,
Expand All @@ -63,6 +67,7 @@ import type {
RawMessageInfo,
MessageTruncationStatuses,
} from '../types/message-types.js';
import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types';
import type { GetOlmSessionInitializationDataResponse } from '../types/olm-session-types.js';
import type {
UserSearchResult,
Expand All @@ -73,6 +78,10 @@ import type {
SubscriptionUpdateRequest,
SubscriptionUpdateResult,
} from '../types/subscription-types.js';
import {
thickThreadTypes,
threadTypeIsThick,
} from '../types/thread-types-enum.js';
import type { RawThreadInfos } from '../types/thread-types.js';
import {
userActionsP2PMessageTypes,
Expand Down Expand Up @@ -1200,10 +1209,52 @@ const updateSubscription =
};
};

function useUpdateSubscription(): (
input: SubscriptionUpdateRequest,
) => Promise<SubscriptionUpdateResult> {
return useKeyserverCall(updateSubscription);
function useUpdateSubscription(
threadInfo: ThreadInfo,
): (input: SubscriptionUpdateRequest) => Promise<SubscriptionUpdateResult> {
const processAndSendDMOperation = useProcessAndSendDMOperation();
const viewerID = useSelector(
state => state.currentUserInfo && state.currentUserInfo.id,
);
const keyserverCall = useKeyserverCall(updateSubscription);

return React.useCallback(
async (input: SubscriptionUpdateRequest) => {
if (!threadTypeIsThick(threadInfo.type)) {
return await keyserverCall(input);
}

invariant(viewerID, 'viewerID must be set');
const subscription = {
...threadInfo.currentUser.subscription,
...input.updatedFields,
};
const op: DMChangeThreadSubscriptionOperation = {
type: 'change_thread_subscription',
time: Date.now(),
threadID: threadInfo.id,
creatorID: viewerID,
subscription,
};

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);
return { threadID: threadInfo.id, subscription };
},
[keyserverCall, processAndSendDMOperation, viewerID, threadInfo],
);
}

const setUserSettingsActionTypes = Object.freeze({
Expand Down
9 changes: 9 additions & 0 deletions lib/shared/dm-ops/change-thread-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,18 @@ const changeThreadSubscriptionSpec: DMOperationSpec<DMChangeThreadSubscriptionOp
member => member.id !== creatorID,
);
const membersUpdate = [...otherMemberInfos, updatedCreatorMemberInfo];
const currentUserUpdate =
viewerID === creatorID
? {
...threadInfo.currentUser,
subscription,
}
: threadInfo.currentUser;

const threadInfoUpdate = {
...threadInfo,
members: membersUpdate,
currentUser: currentUserUpdate,
timestamps: {
...threadInfo.timestamps,
members: {
Expand All @@ -57,6 +65,7 @@ const changeThreadSubscriptionSpec: DMOperationSpec<DMChangeThreadSubscriptionOp
},
},
};

const updateInfos: Array<ClientUpdateInfo> = [
{
type: updateTypes.UPDATE_THREAD,
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/thread-settings-notifications-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function useThreadSettingsNotifications(

const dispatchActionPromise = useDispatchActionPromise();

const callUpdateSubscription = useUpdateSubscription();
const callUpdateSubscription = useUpdateSubscription(threadInfo);

const updateSubscriptionPromise = React.useCallback(async () => {
const res = await callUpdateSubscription({
Expand Down

0 comments on commit a0bb600

Please sign in to comment.