From 09bd95228d660272f0fb0cdeacbd0788aec351e6 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Mon, 19 Aug 2024 13:26:05 -0700 Subject: [PATCH] Add unlink step to existing UI flow --- ts/shims/deleteAllData.ts | 11 +++++++++++ ts/textsecure/WebAPI.ts | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/ts/shims/deleteAllData.ts b/ts/shims/deleteAllData.ts index 705eae013f31..f0057b87f7da 100644 --- a/ts/shims/deleteAllData.ts +++ b/ts/shims/deleteAllData.ts @@ -7,6 +7,17 @@ import { deleteAllLogs } from '../util/deleteAllLogs'; import * as Errors from '../types/errors'; export async function deleteAllData(): Promise { + try { + // This might fail if websocket closes before we receive the response, while + // still unlinking the device on the server. + await window.textsecure.server?.unlink(); + } catch (error) { + log.error( + 'Something went wrong unlinking device:', + Errors.toLogFormat(error) + ); + } + try { await deleteAllLogs(); diff --git a/ts/textsecure/WebAPI.ts b/ts/textsecure/WebAPI.ts index 3e1431a33ccf..56d70d93e310 100644 --- a/ts/textsecure/WebAPI.ts +++ b/ts/textsecure/WebAPI.ts @@ -578,6 +578,7 @@ const URL_CALLS = { challenge: 'v1/challenge', config: 'v1/config', deliveryCert: 'v1/certificate/delivery', + devices: 'v1/devices', directoryAuthV2: 'v2/directory/auth', discovery: 'v1/discovery', getGroupAvatarUpload: 'v1/groups/avatar/form', @@ -647,6 +648,7 @@ const WEBSOCKET_CALLS = new Set([ 'getGroupCredentials', // Devices + 'devices', 'linkDevice', 'registerCapabilities', @@ -1327,6 +1329,7 @@ export type WebAPIType = { abortSignal: AbortSignal ) => Promise; linkDevice: (options: LinkDeviceOptionsType) => Promise; + unlink: () => Promise; makeProxiedRequest: ( targetUrl: string, options?: ProxiedRequestOptionsType @@ -1806,6 +1809,7 @@ export function initialize({ setBackupSignatureKey, setPhoneNumberDiscoverability, startRegistration, + unlink, unregisterRequestHandler, updateDeviceName, uploadAvatar, @@ -2697,6 +2701,19 @@ export function initialize({ ); } + async function unlink() { + if (!username) { + return; + } + + const [, deviceId] = username.split('.'); + await _ajax({ + call: 'devices', + httpType: 'DELETE', + urlParameters: `/${deviceId}`, + }); + } + async function updateDeviceName(deviceName: string) { await _ajax({ call: 'updateDeviceName',