"use strict";
/*
API endpoint that makes it possible to send a message to a project
that the user is a collaborator on and get back a response.

See cocalc/src/packages/server/projects/connection/call.ts
for a list of messages.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.callProject = void 0;
const call_1 = __importDefault(require("@cocalc/server/projects/connection/call"));
const get_params_1 = __importDefault(require("lib/api/get-params"));
const get_account_1 = __importDefault(require("lib/account/get-account"));
const misc_1 = require("@cocalc/util/misc");
const is_collaborator_1 = __importDefault(require("@cocalc/server/projects/is-collaborator"));
async function handle(req, res) {
    const account_id = await (0, get_account_1.default)(req);
    try {
        const { project_id, mesg } = (0, get_params_1.default)(req);
        res.json(await callProject({ account_id, project_id, mesg }));
    }
    catch (err) {
        res.json({ error: err.message });
    }
}
exports.default = handle;
// also used by the latex api call
async function callProject({ account_id, project_id, mesg, }) {
    if (!(0, misc_1.isValidUUID)(account_id)) {
        throw Error("user must be authenticated");
    }
    if (!(0, misc_1.isValidUUID)(project_id)) {
        throw Error("must specify project_id");
    }
    if (!(await (0, is_collaborator_1.default)({ account_id, project_id }))) {
        throw Error("authenticated user must be a collaborator on the project");
    }
    return await (0, call_1.default)({ project_id, mesg });
}
exports.callProject = callProject;
//# sourceMappingURL=call.js.map