Skip to content

Commit

Permalink
[keyserver] Remove holders in account deleters
Browse files Browse the repository at this point in the history
Summary:
Address [[ https://linear.app/comm/issue/ENG-9353/delete-holders-when-deleting-account | ENG-9353 ]].
Before calling `DELETE FROM uploads`, we need to select the `extra` column first and remove blob holders if present in the column JSON.

Depends on D13513

Test Plan: Enabled blob-hosted user avatars. Created an account, set image avatar, then deleted the account and watched the logs. Similiarly to D13513, a blob was uploaded and then its holder was removed

Reviewers: tomek, ashoat

Reviewed By: ashoat

Differential Revision: https://phab.comm.dev/D13514
  • Loading branch information
barthap committed Oct 2, 2024
1 parent d1df2ad commit c5cb285
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions keyserver/src/deleters/account-deleters.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,38 @@ import {
fetchUsername,
} from '../fetchers/user-fetchers.js';
import { rescindPushNotifs } from '../push/rescind.js';
import { removeBlobHolders } from '../services/blob.js';
import { createNewAnonymousCookie } from '../session/cookies.js';
import type { Viewer, AnonymousViewerData } from '../session/viewer.js';
import { fetchOlmAccount } from '../updaters/olm-account-updater.js';
import { blobHoldersFromUploadRows } from '../uploads/media-utils.js';

async function deleteUploadsForUser(deletedUserID: string): Promise<void> {
try {
const [holderRows] = await dbQuery(SQL`
SELECT extra
FROM uploads
WHERE user_container = ${deletedUserID}
`);
const blobHolders = blobHoldersFromUploadRows(holderRows);
await removeBlobHolders(blobHolders);
await dbQuery(SQL`
DELETE u, i
FROM uploads u
LEFT JOIN ids i on i.id = u.id
WHERE u.user_container = ${deletedUserID};
`);
} catch (err) {
// unassign uploads so the deletion will be retried
// by the `deleteUnassignedUploads()`
await dbQuery(SQL`
UPDATE uploads
SET user_container = NULL
WHERE user_container = ${deletedUserID};
`);
throw err;
}
}

async function deleteAccount(viewer: Viewer): Promise<?LogOutResponse> {
if (!viewer.loggedIn) {
Expand All @@ -33,6 +62,8 @@ async function deleteAccount(viewer: Viewer): Promise<?LogOutResponse> {
(user: UserInfo): boolean => user.id !== deletedUserID,
);

ignorePromiseRejections(deleteUploadsForUser(deletedUserID));

// TODO: if this results in any orphaned orgs, convert them to chats
const deletionQuery = SQL`
START TRANSACTION;
Expand Down Expand Up @@ -60,10 +91,6 @@ async function deleteAccount(viewer: Viewer): Promise<?LogOutResponse> {
FROM reports r
LEFT JOIN ids i ON i.id = r.id
WHERE r.user = ${deletedUserID};
DELETE u, i
FROM uploads u
LEFT JOIN ids i on i.id = u.id
WHERE u.user_container = ${deletedUserID};
DELETE FROM relationships_undirected WHERE user1 = ${deletedUserID};
DELETE FROM relationships_undirected WHERE user2 = ${deletedUserID};
DELETE FROM relationships_directed WHERE user1 = ${deletedUserID};
Expand Down

0 comments on commit c5cb285

Please sign in to comment.