Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/database/postgres/delete-projects.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Code related to permanently deleting projects.7*/89import { callback2 } from "@cocalc/util/async-utils";10import { PostgreSQL } from "./types";1112/*13Permanently delete from the database all project records, where the14project is explicitly deleted already (so the deleted field is true).15Call this function to setup projects for permanent deletion. This blanks16the user field so the user no longer can access the project, and we don't17know that the user had anything to do with the project. A separate phase18later then purges these projects from disk as well as the database.19*/20export async function permanently_unlink_all_deleted_projects_of_user(21db: PostgreSQL,22account_id_or_email_address: string23): Promise<void> {24// Get the account_id if necessary.25const account_id = await get_account_id(db, account_id_or_email_address);2627// Get all of the projects for that user that are marked deleted and28// permanently "unlink" them, i.e., set them up for permanent delete.29await callback2(db._query, {30query: "UPDATE projects",31set: { users: null },32where: ["deleted = true", `users#>'{${account_id}}' IS NOT NULL`],33});34}3536async function get_account_id(37db: PostgreSQL,38account_id_or_email_address: string39): Promise<string> {40if (account_id_or_email_address.indexOf("@") == -1) {41return account_id_or_email_address;42}4344// It is an email address45return (46await callback2(db.get_account, {47email_address: account_id_or_email_address,48columns: ["account_id"],49})50).account_id;51}5253/*54This deletes all projects older than the given number of days, from the perspective of a user.55Another task has to run to actually get rid of the data, etc.56*/57export async function unlink_old_deleted_projects(58db: PostgreSQL,59age_d = 3060): Promise<void> {61await callback2(db._query, {62query: "UPDATE projects",63set: { users: null },64where: [65"deleted = true",66"users IS NOT NULL",67`last_edited <= NOW() - '${age_d} days'::INTERVAL`,68],69});70}717273