Skip to content

Commit

Permalink
Update input type of useUpdateSubscription
Browse files Browse the repository at this point in the history
Summary: This differential updates input type of useUpdateSubscription.

Test Plan: Check that updating thread subscription works for both thin and thick threads

Reviewers: tomek, kamil

Reviewed By: tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D13289
  • Loading branch information
marcinwasowicz committed Sep 12, 2024
1 parent 3909f7e commit 6847b92
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
36 changes: 24 additions & 12 deletions lib/actions/user-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ import type {
SubscriptionUpdateRequest,
SubscriptionUpdateResult,
} from '../types/subscription-types.js';
import {
thickThreadTypes,
threadTypeIsThick,
} from '../types/thread-types-enum.js';
import { thickThreadTypes } from '../types/thread-types-enum.js';
import type { RawThreadInfos } from '../types/thread-types.js';
import {
userActionsP2PMessageTypes,
Expand Down Expand Up @@ -1209,26 +1206,41 @@ const updateSubscription =
};
};

function useUpdateSubscription(
threadInfo: ThreadInfo,
): (input: SubscriptionUpdateRequest) => Promise<SubscriptionUpdateResult> {
type UseUpdateSubscriptionInput = $ReadOnly<
| {
+thick: false,
...SubscriptionUpdateRequest,
}
| {
+thick: true,
+threadInfo: ThreadInfo,
...SubscriptionUpdateRequest,
},
>;
function useUpdateSubscription(): (
input: UseUpdateSubscriptionInput,
) => 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);
async (input: UseUpdateSubscriptionInput) => {
if (!input.thick) {
const { thick, ...rest } = input;
return await keyserverCall({ ...rest });
}

invariant(viewerID, 'viewerID must be set');

const { threadInfo, updatedFields } = input;
const subscription = {
...threadInfo.currentUser.subscription,
...input.updatedFields,
...updatedFields,
};

const op: DMChangeThreadSubscriptionOperation = {
type: 'change_thread_subscription',
time: Date.now(),
Expand All @@ -1253,7 +1265,7 @@ function useUpdateSubscription(
await processAndSendDMOperation(opSpecification);
return { threadID: threadInfo.id, subscription };
},
[keyserverCall, processAndSendDMOperation, viewerID, threadInfo],
[keyserverCall, processAndSendDMOperation, viewerID],
);
}

Expand Down
21 changes: 17 additions & 4 deletions lib/shared/thread-settings-notifications-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useCanPromoteSidebar } from '../hooks/promote-sidebar.react.js';
import { createLoadingStatusSelector } from '../selectors/loading-selectors.js';
import { threadInfoSelector } from '../selectors/thread-selectors.js';
import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js';
import { threadTypeIsThick } from '../types/thread-types-enum.js';
import { useDispatchActionPromise } from '../utils/redux-promise-utils.js';
import { useSelector } from '../utils/redux-utils.js';

Expand Down Expand Up @@ -102,16 +103,28 @@ function useThreadSettingsNotifications(

const dispatchActionPromise = useDispatchActionPromise();

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

const updateSubscriptionPromise = React.useCallback(async () => {
const res = await callUpdateSubscription({
const updateSubscriptionRequest = {
threadID: threadInfo.id,
updatedFields: {
home: notificationSettings !== 'muted',
pushNotifs: notificationSettings === 'home',
},
});
};
const updateSubscriptionInput = threadTypeIsThick(threadInfo.type)
? {
thick: true,
threadInfo,
...updateSubscriptionRequest,
}
: {
thick: false,
...updateSubscriptionRequest,
};

const res = await callUpdateSubscription(updateSubscriptionInput);

onSuccessCallback();

Expand All @@ -120,7 +133,7 @@ function useThreadSettingsNotifications(
callUpdateSubscription,
notificationSettings,
onSuccessCallback,
threadInfo.id,
threadInfo,
]);

const updateSubscriptionLoadingStatus = useSelector(
Expand Down

0 comments on commit 6847b92

Please sign in to comment.