|
| 1 | +import { logger, param, query } from '@cardstack/runtime-common'; |
| 2 | +import * as Sentry from '@sentry/node'; |
| 3 | +import type { DBAdapter, PgPrimitive } from '@cardstack/runtime-common'; |
| 4 | +import type { MatrixEvent, Room } from 'matrix-js-sdk'; |
| 5 | + |
| 6 | +const log = logger('bot-runner'); |
| 7 | +export interface BotRegistration { |
| 8 | + id: string; |
| 9 | + created_at: string; |
| 10 | + username: string; |
| 11 | +} |
| 12 | + |
| 13 | +export interface TimelineHandlerOptions { |
| 14 | + authUserId: string; |
| 15 | + dbAdapter: DBAdapter; |
| 16 | +} |
| 17 | + |
| 18 | +export function onTimelineEvent({ |
| 19 | + authUserId, |
| 20 | + dbAdapter, |
| 21 | +}: TimelineHandlerOptions) { |
| 22 | + return async function handleTimelineEvent( |
| 23 | + event: MatrixEvent, |
| 24 | + room: Room | undefined, |
| 25 | + toStartOfTimeline: boolean | undefined, |
| 26 | + ) { |
| 27 | + try { |
| 28 | + if (!room || toStartOfTimeline) { |
| 29 | + return; |
| 30 | + } |
| 31 | + if (room.getMyMembership() !== 'join') { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + let senderUsername = event.getSender(); |
| 36 | + if (!senderUsername || senderUsername === authUserId) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + let registrations = await getRegistrationsForUser( |
| 41 | + dbAdapter, |
| 42 | + senderUsername, |
| 43 | + ); |
| 44 | + if (!registrations.length) { |
| 45 | + return; |
| 46 | + } |
| 47 | + log.debug( |
| 48 | + `received event from ${senderUsername} in room ${room.roomId} with ${registrations.length} registrations`, |
| 49 | + ); |
| 50 | + for (let registration of registrations) { |
| 51 | + let createdAt = Date.parse(registration.created_at); |
| 52 | + if (Number.isNaN(createdAt)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + let eventTimestamp = event.event.origin_server_ts; |
| 56 | + if (eventTimestamp == null || eventTimestamp < createdAt) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + // TODO: filter out events we want to handle based on the registration (e.g. command messages, system events) |
| 60 | + // TODO: handle the event for this registration (e.g. enqueue a job). |
| 61 | + } |
| 62 | + } catch (error) { |
| 63 | + log.error('error handling timeline event', error); |
| 64 | + Sentry.captureException(error); |
| 65 | + } |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +async function getRegistrationsForUser( |
| 70 | + dbAdapter: DBAdapter, |
| 71 | + username: string, |
| 72 | +): Promise<BotRegistration[]> { |
| 73 | + let rows = await query(dbAdapter, [ |
| 74 | + `SELECT br.id, br.username, br.created_at`, |
| 75 | + `FROM bot_registrations br`, |
| 76 | + `WHERE br.username = `, |
| 77 | + param(username), |
| 78 | + ]); |
| 79 | + |
| 80 | + let registrations: BotRegistration[] = []; |
| 81 | + for (let row of rows) { |
| 82 | + let registration = toBotRegistration(row); |
| 83 | + if (registration) { |
| 84 | + registrations.push(registration); |
| 85 | + } |
| 86 | + } |
| 87 | + return registrations; |
| 88 | +} |
| 89 | + |
| 90 | +function toBotRegistration( |
| 91 | + row: Record<string, PgPrimitive>, |
| 92 | +): BotRegistration | null { |
| 93 | + if ( |
| 94 | + typeof row.id !== 'string' || |
| 95 | + typeof row.username !== 'string' || |
| 96 | + typeof row.created_at !== 'string' |
| 97 | + ) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + return { |
| 101 | + id: row.id, |
| 102 | + username: row.username, |
| 103 | + created_at: row.created_at, |
| 104 | + }; |
| 105 | +} |
0 commit comments