UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

332 lines 15.8 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.record_error = exports.handle_mention = exports.handle_all_mentions = exports.handle_mentions_loop = void 0; /* Handle all mentions that haven't yet been handled. */ var MIN_EMAIL_INTERVAL = process.env.COCALC_MENTIONS_MIN_EMAIL_INTERVAL || "8 hours"; // How long to wait between each round of handling notifications. var POLL_INTERVAL_S; if (process.env.COCALC_MENTIONS_POLL_INTERVAL_S != undefined) { POLL_INTERVAL_S = parseInt(process.env.COCALC_MENTIONS_POLL_INTERVAL_S); } else { POLL_INTERVAL_S = 15; } var async_utils_1 = require("smc-util/async-utils"); var misc_1 = require("smc-util/misc"); var awaiting_1 = require("awaiting"); var project_queries_1 = require("../postgres/project-queries"); var account_queries_1 = require("../postgres/account-queries"); var email_1 = require("../email"); var HELP_EMAIL = require("smc-util/theme").HELP_EMAIL; // TODO: should be something like notifications@cocalc.com... var NOTIFICATIONS_EMAIL = HELP_EMAIL; // Handle all notification, then wait for the given time, then again // handle all unhandled notifications. function handle_mentions_loop(db, poll_interval_s) { if (poll_interval_s === void 0) { poll_interval_s = POLL_INTERVAL_S; } return __awaiter(this, void 0, void 0, function () { var err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!true) return [3 /*break*/, 6]; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, handle_all_mentions(db)]; case 2: _a.sent(); return [3 /*break*/, 4]; case 3: err_1 = _a.sent(); console.warn("WARNING -- error handling mentions -- " + err_1); console.trace(); return [3 /*break*/, 4]; case 4: return [4 /*yield*/, awaiting_1.delay(poll_interval_s * 1000)]; case 5: _a.sent(); return [3 /*break*/, 0]; case 6: return [2 /*return*/]; } }); }); } exports.handle_mentions_loop = handle_mentions_loop; function handle_all_mentions(db) { return __awaiter(this, void 0, void 0, function () { var result, _a, _b, row, project_id, path, time, source, target, priority, description, e_1_1; var e_1, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: return [4 /*yield*/, async_utils_1.callback2(db._query, { select: ["time", "project_id", "path", "source", "target", "priority"], table: "mentions", where: "action is null", // no action taken yet. })]; case 1: result = _d.sent(); if (result == null || result.rows == null) { throw Error("invalid result"); // can't happen } _d.label = 2; case 2: _d.trys.push([2, 7, 8, 9]); _a = __values(result.rows), _b = _a.next(); _d.label = 3; case 3: if (!!_b.done) return [3 /*break*/, 6]; row = _b.value; project_id = row.project_id; path = row.path; time = row.time; source = row.source; target = row.target; priority = row.priority; description = row.description; return [4 /*yield*/, handle_mention(db, { project_id: project_id, path: path, time: time, target: target }, source, priority, description)]; case 4: _d.sent(); _d.label = 5; case 5: _b = _a.next(); return [3 /*break*/, 3]; case 6: return [3 /*break*/, 9]; case 7: e_1_1 = _d.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 9]; case 8: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 9: return [2 /*return*/]; } }); }); } exports.handle_all_mentions = handle_all_mentions; function determine_action(db, key, source) { return __awaiter(this, void 0, void 0, function () { var project_id, path, target, _a, result, count; return __generator(this, function (_b) { switch (_b.label) { case 0: project_id = key.project_id, path = key.path, target = key.target; return [4 /*yield*/, account_queries_1.is_paying_customer(db, source)]; case 1: _a = !(_b.sent()); if (!_a) return [3 /*break*/, 3]; return [4 /*yield*/, project_queries_1.project_has_network_access(db, project_id)]; case 2: _a = !(_b.sent()); _b.label = 3; case 3: if (_a) { // Mentions are ignored when sending is NOT a paying customer *and* // the project does not have network access. // Otherwise, spammers could use @mentions to send emails. // Users can still see mentions inside CoCalc itself... return [2 /*return*/, "no-network"]; } return [4 /*yield*/, async_utils_1.callback2(db._query, { query: "SELECT COUNT(*) FROM mentions WHERE project_id=$1 AND path=$2 AND target=$3 AND action = 'email' AND time >= NOW() - INTERVAL '" + MIN_EMAIL_INTERVAL + "'", params: [project_id, path, target], })]; case 4: result = _b.sent(); count = parseInt(result.rows[0].count); if (count > 0) { return [2 /*return*/, "ignore"]; } return [2 /*return*/, "email"]; } }); }); } function handle_mention(db, key, source, _priority, // ignored for now. description) { return __awaiter(this, void 0, void 0, function () { var action, _a, err_2; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, determine_action(db, key, source)]; case 1: action = _b.sent(); _b.label = 2; case 2: _b.trys.push([2, 12, , 14]); _a = action; switch (_a) { case "ignore": return [3 /*break*/, 3]; case "no-network": return [3 /*break*/, 5]; case "email": return [3 /*break*/, 7]; } return [3 /*break*/, 10]; case 3: // Mark that we ignore this. return [4 /*yield*/, set_action(db, key, "ignore")]; case 4: // Mark that we ignore this. _b.sent(); return [2 /*return*/]; case 5: // Mark that we ignore this because no network. (basically a trial user) return [4 /*yield*/, set_action(db, key, "no-network")]; case 6: // Mark that we ignore this because no network. (basically a trial user) _b.sent(); return [2 /*return*/]; case 7: return [4 /*yield*/, send_email_notification(db, key, source, description)]; case 8: _b.sent(); // Mark that we sent email. return [4 /*yield*/, set_action(db, key, "email")]; case 9: // Mark that we sent email. _b.sent(); return [2 /*return*/]; case 10: throw Error("unknown action \"" + action + "\""); case 11: return [3 /*break*/, 14]; case 12: err_2 = _b.sent(); return [4 /*yield*/, record_error(db, key, action, "" + err_2)]; case 13: _b.sent(); return [3 /*break*/, 14]; case 14: return [2 /*return*/]; } }); }); } exports.handle_mention = handle_mention; function send_email_notification(db, key, source, description) { if (description === void 0) { description = ""; } return __awaiter(this, void 0, void 0, function () { var user_names, source_name, project_title, context, subject, url, body, from, to, category, settings; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, async_utils_1.callback2(db.account_ids_to_usernames, { account_ids: [source], })]; case 1: user_names = _a.sent(); source_name = user_names[source].first_name + " " + user_names[source].last_name; return [4 /*yield*/, awaiting_1.callback(db._get_project_column, "title", key.project_id)]; case 2: project_title = _a.sent(); context = description.length > 0 ? "<br/><blockquote>" + description + "</blockquote>" : ""; subject = "[" + misc_1.trunc(project_title, 40) + "] " + key.path; url = "https://cocalc.com/projects/" + key.project_id + "/files/" + key.path; body = source_name + " mentioned you in <a href=\"" + url + "\">a chat at " + key.path + " in " + project_title + "</a>." + context; from = source_name + " <" + NOTIFICATIONS_EMAIL + ">"; return [4 /*yield*/, awaiting_1.callback(db.get_user_column, "email_address", key.target)]; case 3: to = _a.sent(); if (!to) { throw Error("no implemented way to notify target (no known email address)"); } category = "notification"; return [4 /*yield*/, async_utils_1.callback2(db.get_server_settings_cached, {})]; case 4: settings = _a.sent(); // Send email notification. return [4 /*yield*/, async_utils_1.callback2(email_1.send_email, { subject: subject, body: body, from: from, to: to, category: category, settings: settings })]; case 5: // Send email notification. _a.sent(); return [2 /*return*/]; } }); }); } function set_action(db, key, action) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, async_utils_1.callback2(db._query, { query: "UPDATE mentions SET action=$1", params: [action], where: key, })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); } function record_error(db, key, action, error) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, async_utils_1.callback2(db._query, { query: "UPDATE mentions SET action=$1,error=$2", where: key, params: [action, error], })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); } exports.record_error = record_error; //# sourceMappingURL=handle.js.map