"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.getPublicPathNames = void 0;
/* Get basic information about a user or organization
from the database.  This should be enough to render
a nice "homepage" for that user or organization.
*/
const pool_1 = __importDefault(require("@cocalc/database/pool"));
const project_1 = __importDefault(require("./project"));
const owner_1 = require("lib/names/owner");
const get_public_path_1 = __importStar(require("lib/share/proxy/get-public-path"));
const path_1 = require("path");
async function getPublicPathId(owner, project, public_path // this is the entire actual path
) {
    if ((0, get_public_path_1.shouldUseProxy)(owner)) {
        // special case -- proxy urls...
        const { id } = await (0, get_public_path_1.default)({
            url: (0, path_1.join)(owner, project, ...public_path),
        });
        return id;
    }
    const project_id = await (0, project_1.default)(owner, project);
    const pool = (0, pool_1.default)("long");
    const result = await pool.query("SELECT id FROM public_paths WHERE LOWER(name)=$1 AND project_id=$2", [public_path[0]?.toLowerCase() ?? "", project_id]);
    if (result.rows.length > 0) {
        return result.rows[0].id;
    }
    throw Error(`no public_path ${owner}/${project}/${public_path[0]}`);
}
exports.default = getPublicPathId;
// Given the id of a public path, returns owner name, project name, and public_path name
// if they are all defined and nonempty. Otherwise returns undefined.
async function getPublicPathNames(id) {
    const pool = (0, pool_1.default)("medium");
    let result = await pool.query("SELECT project_id, name FROM public_paths WHERE id=$1", [id]);
    if (result.rows.length == 0)
        return;
    const { project_id, name: public_path } = result.rows[0];
    if (!public_path)
        return;
    // Having to get users is pretty stupid -- see comment in lib/project/get-owner.ts
    result = await pool.query("SELECT name, users FROM projects WHERE project_id=$1 AND name IS NOT NULL AND name != ''", [project_id]);
    if (result.rows.length == 0)
        return;
    const { name: project, users } = result.rows[0];
    let owner_id = "";
    for (const account_id in users) {
        if (users[account_id].group == "owner") {
            owner_id = account_id;
            break;
        }
    }
    if (!owner_id)
        return; // shouldn't be possible
    const owner = await (0, owner_1.getOwnerName)(owner_id);
    if (!owner)
        return;
    return { owner, project, public_path };
}
exports.getPublicPathNames = getPublicPathNames;
//# sourceMappingURL=public-path.js.map