smc-hub
Version:
CoCalc: Backend webserver component
252 lines • 13.5 kB
JavaScript
"use strict";
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthorInfo = void 0;
/*
Information (from the database) about authors of shares,
and what shares were authored by a given account.
*/
var async_utils_1 = require("smc-util/async-utils");
var misc_1 = require("smc-util/misc");
var AuthorInfo = /** @class */ (function () {
function AuthorInfo(database) {
this.database = database;
}
AuthorInfo.prototype.get_authors = function (project_id,
// path can be a single path or an array of paths;
// if give a single path, also automatically includes
// known aux files (this is just for ipynb).
path) {
return __awaiter(this, void 0, void 0, function () {
var account_ids, known_account_ids, paths, collabs, _a, paths_1, paths_1_1, path_1, id, result, _b, _c, account_id, e_1_1, result, _d, _e, v, authors, names, account_id, _f, first_name, last_name, name_1;
var e_1, _g, e_2, _h, e_3, _j;
return __generator(this, function (_k) {
switch (_k.label) {
case 0:
if (!misc_1.is_valid_uuid_string(project_id)) {
throw Error("project_id=" + project_id + " must be a valid uuid string");
}
account_ids = [];
known_account_ids = new Set();
if (typeof path == "string") {
paths = [path];
if (misc_1.endswith(path, ".ipynb")) {
paths.push(misc_1.meta_file(path, "jupyter2"));
paths.push(misc_1.meta_file(path, "jupyter"));
}
}
else {
paths = path;
}
_a = Set.bind;
return [4 /*yield*/, async_utils_1.callback2(this.database.get_collaborators.bind(this.database), {
project_id: project_id,
})];
case 1:
collabs = new (_a.apply(Set, [void 0, _k.sent()]))();
_k.label = 2;
case 2:
_k.trys.push([2, 7, 8, 9]);
paths_1 = __values(paths), paths_1_1 = paths_1.next();
_k.label = 3;
case 3:
if (!!paths_1_1.done) return [3 /*break*/, 6];
path_1 = paths_1_1.value;
id = this.database.sha1(project_id, path_1);
return [4 /*yield*/, async_utils_1.callback2(this.database._query, {
query: "SELECT users FROM syncstrings WHERE string_id='" + id + "'",
})];
case 4:
result = _k.sent();
if (result == null || result.rowCount < 1)
return [3 /*break*/, 5];
try {
for (_b = (e_2 = void 0, __values(result.rows[0].users)), _c = _b.next(); !_c.done; _c = _b.next()) {
account_id = _c.value;
if (account_id != project_id &&
!known_account_ids.has(account_id) &&
collabs.has(account_id)) {
account_ids.push(account_id);
known_account_ids.add(account_id);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_h = _b.return)) _h.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
_k.label = 5;
case 5:
paths_1_1 = paths_1.next();
return [3 /*break*/, 3];
case 6: return [3 /*break*/, 9];
case 7:
e_1_1 = _k.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 9];
case 8:
try {
if (paths_1_1 && !paths_1_1.done && (_g = paths_1.return)) _g.call(paths_1);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 9:
if (!(account_ids.length === 0)) return [3 /*break*/, 11];
return [4 /*yield*/, async_utils_1.callback2(this.database._query, {
query: "SELECT jsonb_object_keys(users) FROM projects where project_id='" + project_id + "'",
})];
case 10:
result = _k.sent();
if (result != null && result.rowCount >= 1) {
try {
for (_d = __values(result.rows), _e = _d.next(); !_e.done; _e = _d.next()) {
v = _e.value;
account_ids.push(v.jsonb_object_keys);
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_e && !_e.done && (_j = _d.return)) _j.call(_d);
}
finally { if (e_3) throw e_3.error; }
}
}
_k.label = 11;
case 11:
authors = [];
return [4 /*yield*/, async_utils_1.callback2(this.database.get_usernames, {
account_ids: account_ids,
cache_time_s: 60 * 5,
})];
case 12:
names = _k.sent();
for (account_id in names) {
_f = names[account_id], first_name = _f.first_name, last_name = _f.last_name;
name_1 = first_name + " " + last_name;
authors.push({ name: name_1, account_id: account_id });
}
// Sort by last name
authors.sort(function (a, b) {
return misc_1.cmp(names[a.account_id].last_name, names[b.account_id].last_name);
});
return [2 /*return*/, authors];
}
});
});
};
AuthorInfo.prototype.get_username = function (account_id) {
return __awaiter(this, void 0, void 0, function () {
var names, _a, first_name, last_name;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, async_utils_1.callback2(this.database.get_usernames, {
account_ids: [account_id],
cache_time_s: 60 * 5,
})];
case 1:
names = _b.sent();
_a = names[account_id], first_name = _a.first_name, last_name = _a.last_name;
return [2 /*return*/, first_name + " " + last_name];
}
});
});
};
AuthorInfo.prototype.get_shares = function (account_id) {
return __awaiter(this, void 0, void 0, function () {
var query, result, ids, _a, _b, x;
var e_4, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
// Returns the id's of all public paths for which account_id
// is a collaborator on the project that has actively used the project.
// It would be more useful
// to additionally filter using the syncstrings table for documents
// that account_id actually edited, but that's a lot harder.
// We sort from most recently saved back.
if (!misc_1.is_valid_uuid_string(account_id)) {
throw Error("account_id=" + account_id + " must be a valid uuid string");
}
query = "SELECT public_paths.id FROM public_paths, projects WHERE public_paths.project_id = projects.project_id AND projects.last_active ? '" + account_id + "' AND projects.users ? '" + account_id + "' AND (public_paths.unlisted is null OR public_paths.unlisted = false) AND (public_paths.disabled is null OR public_paths.disabled = false) ORDER BY public_paths.last_edited DESC";
return [4 /*yield*/, async_utils_1.callback2(this.database._query, { query: query })];
case 1:
result = _d.sent();
ids = [];
if (result == null)
return [2 /*return*/, []];
try {
for (_a = __values(result.rows), _b = _a.next(); !_b.done; _b = _a.next()) {
x = _b.value;
ids.push(x.id);
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_4) throw e_4.error; }
}
return [2 /*return*/, ids];
}
});
});
};
return AuthorInfo;
}());
exports.AuthorInfo = AuthorInfo;
//# sourceMappingURL=authors.js.map