"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
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 get_contents_1 = __importDefault(require("./get-contents"));
const get_project_1 = __importDefault(require("./get-project"));
const path_1 = require("path");
const base_path_1 = __importDefault(require("lib/base-path"));
const is_collaborator_1 = __importDefault(require("@cocalc/server/projects/is-collaborator"));
const get_account_1 = __importDefault(require("lib/account/get-account"));
const star_1 = require("@cocalc/server/public-paths/star");
const get_proxied_public_path_info_1 = __importDefault(require("lib/share/proxy/get-proxied-public-path-info"));
async function getPublicPathInfo({ id, relativePath, public_path, req, }) {
    if (typeof id != "string" || id.length != 40) {
        throw Error("invalid id");
    }
    // TODO: currently not using any caching because when editing and saving, we want info to update.
    // However, we should implement this by using a query param to prevent using cache?
    const pool = (0, pool_1.default)();
    // Get the database entry that describes the public path
    const { rows } = await pool.query(`SELECT project_id, path, description, compute_image, license, disabled, unlisted, authenticated, url,
    counter::INT,
    (SELECT COUNT(*)::INT FROM public_path_stars WHERE public_path_id=id) AS stars
    FROM public_paths WHERE vhost IS NULL AND id=$1`, [id]);
    if (rows.length == 0 || rows[0].project_id == null || rows[0].path == null) {
        throw Error("not found");
    }
    if (relativePath == null) {
        if (public_path?.[1] == "files") {
            // only files/ implemented right now; we might add other things like edit/ later?
            relativePath = public_path.slice(2).join("/");
        }
        else {
            relativePath = "";
        }
    }
    if (relativePath.includes("..") || relativePath.startsWith("/")) {
        // a security check
        throw Error("invalid relativePath");
    }
    const { disabled, authenticated } = rows[0];
    const account_id = await (0, get_account_1.default)(req);
    if (disabled) {
        // Share is disabled, so account_id must be a collaborator on the project.
        if (!account_id ||
            !(await (0, is_collaborator_1.default)({
                account_id,
                project_id: rows[0].project_id,
            }))) {
            throw Error("not found");
        }
    }
    if (authenticated) {
        // Only authenticated users are allowed to access
        if (account_id == null) {
            throw Error("not found");
        }
    }
    // if user is signed in, whether or not they stared this.
    const isStarred = account_id ? await (0, star_1.isStarred)(id, account_id) : null;
    try {
        let details;
        if (rows[0].url) {
            // only proxied public paths have url attribute
            details = await (0, get_proxied_public_path_info_1.default)(rows[0].url, public_path ?? []);
        }
        else {
            const { title, avatar_image_full } = await (0, get_project_1.default)(rows[0].project_id, ["title", "avatar_image_full"], "medium");
            details = {
                contents: await (0, get_contents_1.default)(rows[0].project_id, (0, path_1.join)(rows[0].path, relativePath)),
                projectTitle: title,
                projectAvatarImage: avatar_image_full,
            };
        }
        return {
            id,
            ...rows[0],
            relativePath,
            basePath: base_path_1.default,
            isStarred,
            ...details,
        };
    }
    catch (error) {
        return { id, ...rows[0], relativePath, isStarred, error: error.toString() };
    }
}
exports.default = getPublicPathInfo;
//# sourceMappingURL=get-public-path-info.js.map