UNPKG

@cocalc/server

Version:

CoCalc server functionality: functions used by either the hub and the next.js server

52 lines 2.14 kB
"use strict"; /* Returns users of the sandbox that are idle. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const pool_1 = __importDefault(require("@cocalc/database/pool")); const misc_1 = require("@cocalc/util/misc"); async function idleSandboxUsers(project_id, idleTimeoutSeconds = 60 * 10) { if (!(0, misc_1.is_valid_uuid_string)(project_id)) { throw Error("invalid project_id"); } const pool = (0, pool_1.default)("long"); // long cache makes sense here. const { rows } = await pool.query(`SELECT users, last_active, sandbox FROM projects WHERE project_id=$1`, [project_id]); if (rows.length == 0) { // no such project -- nothing to do return []; } if (!rows[0].sandbox) { // not a sandbox, so nothing to do return []; } const idleUsers = []; const { users, last_active } = rows[0]; const now = new Date().valueOf(); const cutoff = now - idleTimeoutSeconds * 1000; const addToLastActive = []; // these got added to sandbox but no activity being tracked yet, so we initialize it. for (const account_id in users ?? {}) { const active = new Date(last_active?.[account_id] ?? 0).valueOf(); if (!active) { addToLastActive.push(account_id); } else if (active <= cutoff && users[account_id]?.["group"] != "owner") { idleUsers.push(account_id); } } if (addToLastActive.length > 0) { await updateLastActive(project_id, addToLastActive); } return idleUsers; } exports.default = idleSandboxUsers; async function updateLastActive(project_id, users) { const pool = (0, pool_1.default)(); const now = new Date(); const X = {}; for (const account_id of users) { X[account_id] = now; } await pool.query(`UPDATE projects SET last_active = COALESCE(last_active, '{}') || '${JSON.stringify(X)}' WHERE project_id=$1`, [project_id]); } //# sourceMappingURL=idle-sandbox-users.js.map