|
| 1 | +/* |
| 2 | +Handle all mentions that haven't yet been handled. |
| 3 | +*/ |
| 4 | + |
| 5 | +import getPool from "@cocalc/backend/database"; |
| 6 | +import { delay } from "awaiting"; |
| 7 | +import { getLogger } from "@cocalc/backend/logger"; |
| 8 | +import type { Action, Key } from "./types"; |
| 9 | +import notify from "./notify"; |
| 10 | + |
| 11 | +const logger = getLogger("mentions - handle"); |
| 12 | + |
| 13 | +// TODO: should be in the database server settings; also should be |
| 14 | +// user configurable, and this is just a default |
| 15 | +const minEmailInterval = "6 hours"; |
| 16 | +const maxPerInterval = 2; // get up to 2 emails for a given chatroom every 6 hours. |
| 17 | + |
| 18 | +// We check for new notifications this frequently. |
| 19 | +const polIntervalSeconds = 15; |
| 20 | + |
| 21 | +// Handle all notification, then wait for the given time, then again |
| 22 | +// handle all unhandled notifications. |
| 23 | +export default async function init(): Promise<void> { |
| 24 | + while (true) { |
| 25 | + try { |
| 26 | + await handleAllMentions(); |
| 27 | + } catch (err) { |
| 28 | + logger.warn(`WARNING -- error handling mentions -- ${err}`); |
| 29 | + } |
| 30 | + |
| 31 | + await delay(polIntervalSeconds * 1000); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +async function handleAllMentions(): Promise<void> { |
| 36 | + const pool = getPool(); |
| 37 | + const { rows } = await pool.query( |
| 38 | + "SELECT time, project_id, path, source, target, description FROM mentions WHERE action IS null" |
| 39 | + ); |
| 40 | + for (const row of rows) { |
| 41 | + const { time, project_id, path, source, target, description } = row; |
| 42 | + try { |
| 43 | + await handleMention( |
| 44 | + { project_id, path, time, target }, |
| 45 | + source, |
| 46 | + description ?? "" |
| 47 | + ); |
| 48 | + } catch (err) { |
| 49 | + logger.warn( |
| 50 | + `WARNING -- error handling mention (will try later) -- ${err}` |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +async function handleMention( |
| 57 | + key: Key, |
| 58 | + source: string, |
| 59 | + description: string |
| 60 | +): Promise<void> { |
| 61 | + // Check that source and target are both currently |
| 62 | + // collaborators on the project. |
| 63 | + const action: Action = await determineAction(key); |
| 64 | + try { |
| 65 | + switch (action) { |
| 66 | + case "ignore": // already recently notified about this chatroom. |
| 67 | + await setAction(key, action); |
| 68 | + return; |
| 69 | + case "notify": |
| 70 | + let whatDid = await notify(key, source, description); |
| 71 | + // record what we did. |
| 72 | + await setAction(key, whatDid); |
| 73 | + return; |
| 74 | + default: |
| 75 | + throw Error(`BUG: unknown action "${action}"`); |
| 76 | + } |
| 77 | + } catch (err) { |
| 78 | + await setError(key, action, `${err}`); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +async function determineAction(key: Key): Promise<Action> { |
| 83 | + const pool = getPool(); |
| 84 | + const { rows } = await pool.query( |
| 85 | + `SELECT COUNT(*) FROM mentions WHERE project_id=$1 AND path=$2 AND target=$3 AND action = 'email' AND time >= NOW() - INTERVAL '${parseInt( |
| 86 | + minEmailInterval |
| 87 | + )}'`, |
| 88 | + [key.project_id, key.path, key.target] |
| 89 | + ); |
| 90 | + const count: number = parseInt(rows[0]?.count ?? 0); |
| 91 | + if (count >= maxPerInterval) { |
| 92 | + return "ignore"; |
| 93 | + } |
| 94 | + return "notify"; |
| 95 | +} |
| 96 | + |
| 97 | +async function setAction(key: Key, action: Action): Promise<void> { |
| 98 | + const pool = getPool(); |
| 99 | + await pool.query( |
| 100 | + "UPDATE mentions SET action=$1 WHERE project_id=$2 AND path=$3 AND time=$4 AND target=$5", |
| 101 | + [action, key.project_id, key.path, key.time, key.target] |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +async function setError( |
| 106 | + key: Key, |
| 107 | + action: Action, |
| 108 | + error: string |
| 109 | +): Promise<void> { |
| 110 | + const pool = getPool(); |
| 111 | + await pool.query( |
| 112 | + "UPDATE mentions SET action=$1, error=$2 WHERE project_id=$3 AND path=$4 AND time=$5 AND target=$6", |
| 113 | + [action, error, key.project_id, key.path, key.time, key.target] |
| 114 | + ); |
| 115 | +} |
0 commit comments