forked from mattkenney/wildebeest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (49 loc) · 1.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { MessageBody, InboxMessageBody, DeliverMessageBody } from 'wildebeest/backend/src/types/queue'
import { type Database, getDatabase } from 'wildebeest/backend/src/database'
import * as actors from 'wildebeest/backend/src/activitypub/actors'
import { MessageType } from 'wildebeest/backend/src/types/queue'
import { initSentryQueue } from './sentry'
import { handleInboxMessage } from './inbox'
import { handleDeliverMessage } from './deliver'
export type Env = {
DATABASE: Database
DOMAIN: string
ADMIN_EMAIL: string
DO_CACHE: DurableObjectNamespace
SENTRY_DSN: string
SENTRY_ACCESS_CLIENT_ID: string
SENTRY_ACCESS_CLIENT_SECRET: string
NEON_DATABASE_URL?: string
}
export default {
async queue(batch: MessageBatch<MessageBody>, env: Env, ctx: ExecutionContext) {
const sentry = initSentryQueue(env, ctx)
const db = await getDatabase(env)
try {
for (const message of batch.messages) {
const actor = await actors.getActorById(db, new URL(message.body.actorId))
if (actor === null) {
console.warn(`actor ${message.body.actorId} is missing`)
return
}
switch (message.body.type) {
case MessageType.Inbox: {
await handleInboxMessage(env, actor, message.body as InboxMessageBody)
break
}
case MessageType.Deliver: {
await handleDeliverMessage(env, actor, message.body as DeliverMessageBody)
break
}
default:
throw new Error('unsupported message type: ' + message.body.type)
}
}
} catch (err: any) {
if (sentry !== null) {
sentry.captureException(err)
}
console.error(err.stack, err.cause)
}
},
}