UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

246 lines (244 loc) 12.6 kB
"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.is_a_site_license_manager = exports.manager_site_licenses = exports.matching_site_licenses = void 0; var misc_1 = require("smc-util/misc"); var query_1 = require("../query"); // This works and does the entire search in the database. // Unfortunately, it is unusably slow and I just don't have // the time to do something better right now: /* export async function matching_site_licenses( db: PostgreSQL, search: string, limit: number = 5 ): Promise<{ id: string }[]> { const where: any[] = []; const params: (string | number)[] = []; let i = 1; for (const s of search_split(search.toLowerCase())) { where.push( `(lower(site_licenses.title) LIKE $${i}::TEXT OR lower(site_licenses.description) LIKE $${i}::TEXT OR site_licenses.id::TEXT LIKE $${i}::TEXT OR lower(site_licenses.info::TEXT) LIKE $${i}::TEXT OR lower(accounts.first_name) LIKE $${i}::TEXT OR lower(accounts.last_name) LIKE $${i}::TEXT OR lower(accounts.email_address) LIKE $${i}::TEXT)` ); params.push(`%${s}%`); i += 1; } let query = "SELECT DISTINCT(site_licenses.id) AS id, site_licenses.last_used FROM site_licenses, accounts WHERE accounts.account_id::TEXT = ANY(site_licenses.managers) AND"; query += ` (${where.join(" AND ")})`; // recently active licenses are much more relevant than old ones query += " ORDER BY site_licenses.last_used DESC NULLS LAST"; query += ` LIMIT $${i}::INTEGER`; params.push(limit); i += 1; return (await db.async_query({ query, params })).rows; } */ // This is dumb but will be sufficiently fast up to probably 5K licenses. // This is not user facing functionality. We could maybe restrict to last_used // in the few months by default (optinally anything) and this would last // much longer... function matching_site_licenses(db, search, limit) { if (limit === void 0) { limit = 5; } return __awaiter(this, void 0, void 0, function () { var licenses, managers, licenses_1, licenses_1_1, x, _a, _b, account_id, accounts, _c, _d, row, e_1_1, v, matches, licenses_2, licenses_2_1, license, s, _e, _f, account_id; var e_2, _g, e_3, _h, e_1, _j, e_4, _k, e_5, _l; return __generator(this, function (_m) { switch (_m.label) { case 0: if (!misc_1.is_valid_uuid_string(search)) return [3 /*break*/, 2]; return [4 /*yield*/, db.async_query({ cache: true, query: "SELECT id FROM site_licenses WHERE id=$1", params: [search], })]; case 1: return [2 /*return*/, (_m.sent()).rows]; case 2: return [4 /*yield*/, db.async_query({ query: "SELECT id, id || ' ' || coalesce(lower(title),'') || ' ' || coalesce(lower(description),'') || ' ' || coalesce(lower(info::TEXT),'') AS info, managers FROM site_licenses ORDER BY last_used DESC NULLS LAST", })]; case 3: licenses = (_m.sent()).rows; managers = new Set(); try { for (licenses_1 = __values(licenses), licenses_1_1 = licenses_1.next(); !licenses_1_1.done; licenses_1_1 = licenses_1.next()) { x = licenses_1_1.value; if (x.managers != null) { try { for (_a = (e_3 = void 0, __values(x.managers)), _b = _a.next(); !_b.done; _b = _a.next()) { account_id = _b.value; managers.add(account_id); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_b && !_b.done && (_h = _a.return)) _h.call(_a); } finally { if (e_3) throw e_3.error; } } } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (licenses_1_1 && !licenses_1_1.done && (_g = licenses_1.return)) _g.call(licenses_1); } finally { if (e_2) throw e_2.error; } } accounts = {}; _m.label = 4; case 4: _m.trys.push([4, 9, 10, 11]); return [4 /*yield*/, db.async_query({ cache: true, query: "SELECT account_id, coalesce(lower(first_name),'') || ' ' || coalesce(lower(last_name),'') || ' ' || coalesce(lower(email_address),'') AS info FROM accounts WHERE account_id=ANY($1)", params: [Array.from(managers)], })]; case 5: _c = __values.apply(void 0, [(_m.sent()).rows]), _d = _c.next(); _m.label = 6; case 6: if (!!_d.done) return [3 /*break*/, 8]; row = _d.value; accounts[row.account_id] = row.info; _m.label = 7; case 7: _d = _c.next(); return [3 /*break*/, 6]; case 8: return [3 /*break*/, 11]; case 9: e_1_1 = _m.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 11]; case 10: try { if (_d && !_d.done && (_j = _c.return)) _j.call(_c); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 11: v = misc_1.search_split(search.toLowerCase()); matches = []; try { for (licenses_2 = __values(licenses), licenses_2_1 = licenses_2.next(); !licenses_2_1.done; licenses_2_1 = licenses_2.next()) { license = licenses_2_1.value; s = license.info; if (license.managers) { try { for (_e = (e_5 = void 0, __values(license.managers)), _f = _e.next(); !_f.done; _f = _e.next()) { account_id = _f.value; s += " " + accounts[account_id]; } } catch (e_5_1) { e_5 = { error: e_5_1 }; } finally { try { if (_f && !_f.done && (_l = _e.return)) _l.call(_e); } finally { if (e_5) throw e_5.error; } } } if (misc_1.search_match(s, v)) { matches.push({ id: license.id }); } if (matches.length >= limit) break; } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (licenses_2_1 && !licenses_2_1.done && (_k = licenses_2.return)) _k.call(licenses_2); } finally { if (e_4) throw e_4.error; } } return [2 /*return*/, matches]; } }); }); } exports.matching_site_licenses = matching_site_licenses; function manager_site_licenses(db, account_id) { return __awaiter(this, void 0, void 0, function () { var query, params; return __generator(this, function (_a) { switch (_a.label) { case 0: query = "SELECT * FROM site_licenses WHERE $1=ANY(managers)"; params = [account_id]; return [4 /*yield*/, db.async_query({ query: query, params: params })]; case 1: return [2 /*return*/, (_a.sent()).rows]; } }); }); } exports.manager_site_licenses = manager_site_licenses; // Return true if the given user is a manager of any site licenses. function is_a_site_license_manager(db, account_id) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, query_1.query({ db: db, query: "SELECT COUNT(*) FROM site_licenses WHERE $1=ANY(managers)", one: true, params: [account_id], })]; case 1: return [2 /*return*/, ((_a.sent()).count > 0)]; } }); }); } exports.is_a_site_license_manager = is_a_site_license_manager; //# sourceMappingURL=search.js.map