Skip to content

Commit

Permalink
[keyserver] Delete contained chats when container chat is deleted
Browse files Browse the repository at this point in the history
Summary: https://linear.app/comm/issue/ENG-4180/deleting-container-chat-doesnt-delete-contained-chats.

Test Plan:
1. Run web app
2. Create subchannel with some threads (sidebars)
3. Check if threads are searchable
4. Delete subchannel and check if threads are still searchable

Reviewers: bartek, tomek, atul, ashoat

Reviewed By: ashoat

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D8436
  • Loading branch information
pklatka committed Jul 10, 2023
1 parent 64b6154 commit c2b8b1f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
42 changes: 22 additions & 20 deletions keyserver/src/deleters/thread-deleters.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { dbQuery, SQL } from '../database/database.js';
import {
fetchThreadInfos,
fetchServerThreadInfos,
fetchContainedThreadIDs,
} from '../fetchers/thread-fetchers.js';
import { fetchThreadPermissionsBlob } from '../fetchers/thread-permission-fetchers.js';
import { fetchUpdateInfoForThreadDeletion } from '../fetchers/update-fetchers.js';
Expand All @@ -30,11 +31,7 @@ async function deleteThread(
}
const { threadID } = threadDeletionRequest;

const [permissionsBlob, { threadInfos: serverThreadInfos }] =
await Promise.all([
fetchThreadPermissionsBlob(viewer, threadID),
fetchServerThreadInfos(SQL`t.id = ${threadID}`),
]);
const permissionsBlob = await fetchThreadPermissionsBlob(viewer, threadID);

if (!permissionsBlob) {
// This should only occur if the first request goes through but the client
Expand All @@ -60,14 +57,18 @@ async function deleteThread(
throw new ServerError('invalid_credentials');
}

await rescindPushNotifs(
SQL`n.thread = ${threadID}`,
SQL`IF(m.thread = ${threadID}, NULL, m.thread)`,
);

// TODO: if org, delete all descendant threads as well. make sure to warn user
// TODO: handle descendant thread permission update correctly.
// thread-permission-updaters should be used for descendant threads.
const threadIDs = await fetchContainedThreadIDs(threadID);

const [{ threadInfos: serverThreadInfos }] = await Promise.all([
fetchServerThreadInfos(SQL`t.id IN (${threadIDs})`),
rescindPushNotifs(
SQL`n.thread IN (${threadIDs})`,
SQL`IF(m.thread IN (${threadIDs}), NULL, m.thread)`,
),
]);

const query = SQL`
DELETE t, ic, d, id, e, ie, re, ire, mm, r, ir, ms, im, up, iu, f, n, ino
FROM threads t
Expand All @@ -88,19 +89,20 @@ async function deleteThread(
LEFT JOIN focused f ON f.thread = t.id
LEFT JOIN notifications n ON n.thread = t.id
LEFT JOIN ids ino ON ino.id = n.id
WHERE t.id = ${threadID}
WHERE t.id IN (${threadIDs})
`;

const serverThreadInfo = serverThreadInfos[threadID];
const time = Date.now();
const updateDatas = [];
for (const memberInfo of serverThreadInfo.members) {
updateDatas.push({
type: updateTypes.DELETE_THREAD,
userID: memberInfo.id,
time,
threadID,
});
for (const containedThreadID of threadIDs) {
for (const memberInfo of serverThreadInfos[containedThreadID].members) {
updateDatas.push({
type: updateTypes.DELETE_THREAD,
userID: memberInfo.id,
time,
threadID: containedThreadID,
});
}
}

const [{ viewerUpdates }] = await Promise.all([
Expand Down
20 changes: 20 additions & 0 deletions keyserver/src/fetchers/thread-fetchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,25 @@ async function serverThreadInfoFromMessageInfo(
return threads.threadInfos[threadID];
}

async function fetchContainedThreadIDs(
parentThreadID: string,
): Promise<Array<string>> {
const query = SQL`
WITH RECURSIVE thread_tree AS (
SELECT id, containing_thread_id
FROM threads
WHERE id = ${parentThreadID}
UNION ALL
SELECT t.id, t.containing_thread_id
FROM threads t
JOIN thread_tree tt ON t.containing_thread_id = tt.id
)
SELECT id FROM thread_tree
`;
const [result] = await dbQuery(query);
return result.map(row => row.id.toString());
}

export {
fetchServerThreadInfos,
fetchThreadInfos,
Expand All @@ -318,4 +337,5 @@ export {
personalThreadQuery,
fetchPersonalThreadID,
serverThreadInfoFromMessageInfo,
fetchContainedThreadIDs,
};

0 comments on commit c2b8b1f

Please sign in to comment.