@cocalc/database
Version:
CoCalc: code for working with our PostgreSQL database
58 lines • 2.54 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.unlink_old_deleted_projects = exports.permanently_unlink_all_deleted_projects_of_user = void 0;
/*
Code related to permanently deleting projects.
*/
const async_utils_1 = require("@cocalc/util/async-utils");
/*
Permanently delete from the database all project records, where the
project is explicitly deleted already (so the deleted field is true).
Call this function to setup projects for permanent deletion. This blanks
the user field so the user no longer can access the project, and we don't
know that the user had anything to do with the project. A separate phase
later then purges these projects from disk as well as the database.
*/
async function permanently_unlink_all_deleted_projects_of_user(db, account_id_or_email_address) {
// Get the account_id if necessary.
const account_id = await get_account_id(db, account_id_or_email_address);
// Get all of the projects for that user that are marked deleted and
// permanently "unlink" them, i.e., set them up for permanent delete.
await (0, async_utils_1.callback2)(db._query, {
query: "UPDATE projects",
set: { users: null },
where: ["deleted = true", `users#>'{${account_id}}' IS NOT NULL`],
});
}
exports.permanently_unlink_all_deleted_projects_of_user = permanently_unlink_all_deleted_projects_of_user;
async function get_account_id(db, account_id_or_email_address) {
if (account_id_or_email_address.indexOf("@") == -1) {
return account_id_or_email_address;
}
// It is an email address
return (await (0, async_utils_1.callback2)(db.get_account, {
email_address: account_id_or_email_address,
columns: ["account_id"],
})).account_id;
}
/*
This deletes all projects older than the given number of days, from the perspective of a user.
Another task has to run to actually get rid of the data, etc.
*/
async function unlink_old_deleted_projects(db, age_d = 30) {
await (0, async_utils_1.callback2)(db._query, {
query: "UPDATE projects",
set: { users: null },
where: [
"deleted = true",
"users IS NOT NULL",
`last_edited <= NOW() - '${age_d} days'::INTERVAL`,
],
});
}
exports.unlink_old_deleted_projects = unlink_old_deleted_projects;
//# sourceMappingURL=delete-projects.js.map