"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 });
/*
Get the public paths associated to a given project.  Unlisted paths are NOT included.
*/
const pool_1 = __importStar(require("@cocalc/database/pool"));
const util_1 = require("./util");
const is_collaborator_1 = __importDefault(require("@cocalc/server/projects/is-collaborator"));
const get_account_1 = __importDefault(require("lib/account/get-account"));
const project_avatar_image_1 = require("./project-avatar-image");
async function getPublicPaths(project_id, req //  use to get account_id if necessary
) {
    if (!(0, util_1.isUUID)(project_id)) {
        throw Error("project_id must be a uuid");
    }
    // short: user might create a new public path then want to look at it shortly thereafter
    const pool = (0, pool_1.default)("short");
    const result = await pool.query(`SELECT id, path, description, ${(0, pool_1.timeInSeconds)("last_edited")}, disabled, unlisted, authenticated,
    counter::INT,
     (SELECT COUNT(*)::INT FROM public_path_stars WHERE public_path_id=id) AS stars
    FROM public_paths WHERE project_id=$1 ORDER BY stars DESC, last_edited DESC`, [project_id]);
    const v = await filterNonPublicAndNotAuthenticated(result.rows, project_id, req);
    const avatar_image_tiny = await (0, project_avatar_image_1.getProjectAvatarTiny)(project_id);
    if (avatar_image_tiny) {
        for (const x of v) {
            x.avatar_image_tiny = avatar_image_tiny;
        }
    }
    return v;
}
exports.default = getPublicPaths;
async function filterNonPublicAndNotAuthenticated(rows, project_id, req) {
    const v = [];
    let isCollab = undefined;
    let isAuthenticated = undefined;
    for (const row of rows) {
        if (!row.disabled && !row.unlisted && !row.authenticated) {
            v.push(row);
            continue;
        }
        if (isCollab == null) {
            const account_id = await (0, get_account_1.default)(req);
            isAuthenticated = account_id != null;
            if (account_id) {
                isCollab = await (0, is_collaborator_1.default)({
                    account_id,
                    project_id,
                });
            }
            else {
                isCollab = false;
            }
        }
        if (isCollab) {
            v.push(row);
        }
        else if (row.authenticated && isAuthenticated) {
            v.push(row);
        }
    }
    return v;
}
//# sourceMappingURL=get-public-paths.js.map