"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getName = void 0;
const pool_1 = __importStar(require("@cocalc/database/pool"));
const util_1 = require("./util");
const get_account_1 = __importDefault(require("lib/account/get-account"));
async function getAccountInfo(account_id, req) {
    return {
        account_id,
        ...(await getName(account_id)),
        publicPaths: await getPublicPaths(account_id, req),
    };
}
exports.default = getAccountInfo;
async function getName(account_id) {
    if (!(0, util_1.isUUID)(account_id)) {
        throw Error("invalid UUID");
    }
    const pool = (0, pool_1.default)("long");
    // Get the database entry
    const { rows } = await pool.query("SELECT name, first_name, last_name, email_address, passports FROM accounts WHERE account_id=$1", [account_id]);
    if (rows.length == 0) {
        throw Error("no such user");
    }
    const is_anonymous = !rows[0].email_address && !rows[0].passports;
    return {
        first_name: rows[0].first_name,
        last_name: rows[0].last_name,
        name: rows[0].name,
        email_address: rows[0].email_address,
        is_anonymous,
    };
}
exports.getName = getName;
async function getPublicPaths(account_id, req // used to get account_id of requester to see if we should include unlisted and disabled public_paths
) {
    if (!(0, util_1.isUUID)(account_id)) {
        // VERY important to check this because we substitute the account_id
        // into the query string directly, and this is input directly from the user!
        throw Error("invalid UUID");
    }
    const pool = (0, pool_1.default)("medium");
    // Returns public paths for which account_id is a collaborator on the project and they have
    // actively used the project at some point.
    // We sort from most recently edited.
    const query = `SELECT public_paths.id as id, public_paths.path as path, public_paths.description as description,
  public_paths.disabled as disabled, public_paths.unlisted as unlisted, public_paths.vhost as vhost, public_paths.authenticated as authenticated,
  (SELECT COUNT(*)::INT FROM public_path_stars WHERE public_path_id=public_paths.id) AS stars,
  public_paths.counter::INT AS counter,
  ${(0, pool_1.timeInSeconds)("public_paths.last_edited", "last_edited")} FROM public_paths, projects WHERE public_paths.project_id = projects.project_id AND projects.last_active ? '${account_id}' AND projects.users ? '${account_id}'  ORDER BY stars DESC, public_paths.last_edited DESC`;
    const { rows } = await pool.query(query);
    // If there are any disabled or unlisted public_paths, we also get the id of the requestor so we can filter them out.
    return await filterNonPublicAndNotAuthenticated(rows, account_id, req);
}
async function filterNonPublicAndNotAuthenticated(rows, account_id, req) {
    const v = [];
    let client_id = undefined;
    for (const row of rows) {
        if (!row.disabled && !row.unlisted && !row.authenticated) {
            v.push(row);
            continue;
        }
        if (client_id == null) {
            client_id = (await (0, get_account_1.default)(req)) ?? "";
        }
        if (client_id == account_id) {
            v.push(row);
        }
        else if (row.authenticated === true && client_id !== "") {
            v.push(row);
        }
    }
    return v;
}
//# sourceMappingURL=get-account-info.js.map