Skip to content

Commit

Permalink
[lib] Extract hook to send logout message to primary device
Browse files Browse the repository at this point in the history
Summary:
Part of [[ https://linear.app/comm/issue/ENG-9710/v1-logout-make-sure-old-clients-send-logout-message-to-primary-device | ENG-9710 ]].
We want to send this message not only on secondary device logout, but also on legacy logout when `use_new_flow` is received

Depends on D14032

Test Plan: Performed secondary device logout. Confirmed that primary device received the message.

Reviewers: kamil, varun

Reviewed By: kamil

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D14033
  • Loading branch information
barthap committed Nov 29, 2024
1 parent 430be00 commit 4e352b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
40 changes: 30 additions & 10 deletions lib/actions/user-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import {
permissionsAndAuthRelatedRequestTimeout,
callIdentityServiceTimeout,
} from '../shared/timeouts.js';
import { usePeerToPeerCommunication } from '../tunnelbroker/peer-to-peer-context.js';
import {
PeerToPeerContext,
usePeerToPeerCommunication,
} from '../tunnelbroker/peer-to-peer-context.js';
import type {
LegacyLogInInfo,
LegacyLogInResult,
Expand Down Expand Up @@ -358,20 +361,24 @@ function usePrimaryDeviceLogOut(): () => Promise<LogOutResult> {
]);
}

const secondaryDeviceLogOutOptions = Object.freeze({
logOutType: 'secondary_device',
});

function useSecondaryDeviceLogOut(): () => Promise<LogOutResult> {
const logOut = useLogOut(secondaryDeviceLogOutOptions);

function useSendLogoutMessageToPrimaryDevice(
mandatory?: boolean = true,
): () => Promise<void> {
const identityContext = React.useContext(IdentityClientContext);
if (!identityContext) {
throw new Error('Identity service client is not initialized');
}
const { broadcastEphemeralMessage } = usePeerToPeerCommunication();
const peerToPeerContext = React.useContext(PeerToPeerContext);

return React.useCallback(async () => {
if (!peerToPeerContext) {
if (mandatory) {
throw new Error('PeerToPeerContext not set');
}
return;
}

const { broadcastEphemeralMessage } = peerToPeerContext;
const { identityClient, getAuthMetadata } = identityContext;
const authMetadata = await getAuthMetadata();
const { userID, deviceID } = authMetadata;
Expand All @@ -398,10 +405,23 @@ function useSecondaryDeviceLogOut(): () => Promise<LogOutResult> {
[recipient],
authMetadata,
);
}, [identityContext, peerToPeerContext, mandatory]);
}

const secondaryDeviceLogOutOptions = Object.freeze({
logOutType: 'secondary_device',
});

function useSecondaryDeviceLogOut(): () => Promise<LogOutResult> {
const logOut = useLogOut(secondaryDeviceLogOutOptions);
const sendLogoutMessage = useSendLogoutMessageToPrimaryDevice();

return React.useCallback(async () => {
await sendLogoutMessage();

// log out of identity service, keyserver and visually
return logOut();
}, [identityContext, broadcastEphemeralMessage, logOut]);
}, [sendLogoutMessage, logOut]);
}

const claimUsernameActionTypes = Object.freeze({
Expand Down
2 changes: 1 addition & 1 deletion lib/tunnelbroker/peer-to-peer-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,4 @@ function usePeerToPeerCommunication(): PeerToPeerContextType {
return context;
}

export { PeerToPeerProvider, usePeerToPeerCommunication };
export { PeerToPeerContext, PeerToPeerProvider, usePeerToPeerCommunication };

0 comments on commit 4e352b1

Please sign in to comment.