CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/database/postgres/delete-projects.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Code related to permanently deleting projects.
8
*/
9
10
import { callback2 } from "@cocalc/util/async-utils";
11
import { PostgreSQL } from "./types";
12
13
/*
14
Permanently delete from the database all project records, where the
15
project is explicitly deleted already (so the deleted field is true).
16
Call this function to setup projects for permanent deletion. This blanks
17
the user field so the user no longer can access the project, and we don't
18
know that the user had anything to do with the project. A separate phase
19
later then purges these projects from disk as well as the database.
20
*/
21
export async function permanently_unlink_all_deleted_projects_of_user(
22
db: PostgreSQL,
23
account_id_or_email_address: string
24
): Promise<void> {
25
// Get the account_id if necessary.
26
const account_id = await get_account_id(db, account_id_or_email_address);
27
28
// Get all of the projects for that user that are marked deleted and
29
// permanently "unlink" them, i.e., set them up for permanent delete.
30
await callback2(db._query, {
31
query: "UPDATE projects",
32
set: { users: null },
33
where: ["deleted = true", `users#>'{${account_id}}' IS NOT NULL`],
34
});
35
}
36
37
async function get_account_id(
38
db: PostgreSQL,
39
account_id_or_email_address: string
40
): Promise<string> {
41
if (account_id_or_email_address.indexOf("@") == -1) {
42
return account_id_or_email_address;
43
}
44
45
// It is an email address
46
return (
47
await callback2(db.get_account, {
48
email_address: account_id_or_email_address,
49
columns: ["account_id"],
50
})
51
).account_id;
52
}
53
54
/*
55
This deletes all projects older than the given number of days, from the perspective of a user.
56
Another task has to run to actually get rid of the data, etc.
57
*/
58
export async function unlink_old_deleted_projects(
59
db: PostgreSQL,
60
age_d = 30
61
): Promise<void> {
62
await callback2(db._query, {
63
query: "UPDATE projects",
64
set: { users: null },
65
where: [
66
"deleted = true",
67
"users IS NOT NULL",
68
`last_edited <= NOW() - '${age_d} days'::INTERVAL`,
69
],
70
});
71
}
72
73