import { env } from "cloudflare:workers"; import { randomId } from "../crypto"; import { run } from "../db"; export type { WebhookEvent } from "~/lib/webhook-events"; import type { WebhookEvent } from "~/lib/webhook-events"; export type NotifyMessage = { kind: "notify"; repoId: string; issueId: string | null; actorId: string | null; reason: "comment" | "mention" | "review_requested" | "assigned" | "state_change" | "ci" | "review"; title: string; url: string; mentionText: string | null; directRecipients: string[]; }; export type QueueMessage = | { kind: "webhook.dispatch"; repoId: string; event: WebhookEvent; payload: Record } | { kind: "webhook.deliver"; webhookId: string; event: WebhookEvent; deliveryId: string; payload: Record; } | NotifyMessage; export async function emitWebhook( repoId: string, event: WebhookEvent, payload: Record, ): Promise { const message: QueueMessage = { kind: "webhook.dispatch", repoId, event, payload }; await env.EVENTS.send(message); } export async function emitNotification(message: Omit): Promise { const body: QueueMessage = { kind: "notify", ...message }; await env.EVENTS.send(body); } export async function recordActivity( actorId: string | null, repoId: string | null, kind: string, data: Record, ): Promise { await run( "INSERT INTO activity (id, actor_id, repo_id, kind, data, created_at) VALUES (?, ?, ?, ?, ?, ?)", randomId(), actorId, repoId, kind, JSON.stringify(data), Date.now(), ); } export function publicUrl(path: string): string { return new URL(path, env.PUBLIC_URL).toString(); }