Skip to content

Commit

Permalink
Clean up inbox api
Browse files Browse the repository at this point in the history
  • Loading branch information
llun committed Sep 21, 2024
1 parent f9bb928 commit 2f75a11
Showing 1 changed file with 80 additions and 72 deletions.
152 changes: 80 additions & 72 deletions app/api/inbox/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ENTITY_TYPE_NOTE, ENTITY_TYPE_QUESTION } from '@llun/activities.schema'
import isMatch from 'lodash/isMatch'
import crypto from 'node:crypto'

import { StatusActivity } from '@/lib/activities/actions/status'
Expand Down Expand Up @@ -32,86 +33,93 @@ const CORS_HEADERS = [HttpMethod.enum.OPTIONS, HttpMethod.enum.POST]

export const OPTIONS = defaultOptions(CORS_HEADERS)

export const POST = ActivityPubVerifySenderGuard(async (request) => {
const body = await request.json()
const activity = (await compact(body)) as StatusActivity
export const getJobMessage = (activity: StatusActivity) => {
const deduplicationId = crypto
.createHash('sha256')
.update(JSON.stringify(activity.id))
.digest('hex')
switch (activity.type) {
case CreateAction: {
const jobName = ((name: string) => {
switch (name) {
case ENTITY_TYPE_NOTE:
return CREATE_NOTE_JOB_NAME
case ENTITY_TYPE_QUESTION:
return CREATE_POLL_JOB_NAME
default:
return null
}
})(activity.object.type)
if (!jobName) {
return apiErrorResponse(404)
}
await getQueue().publish({
id: deduplicationId,
name: jobName,
data: activity.object
})
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
if (
isMatch(activity, {
type: CreateAction,
object: { type: ENTITY_TYPE_NOTE }
})
) {
return {
id: deduplicationId,
name: CREATE_NOTE_JOB_NAME,
data: activity.object
}
case UpdateAction: {
switch (activity.object.type) {
case ENTITY_TYPE_QUESTION: {
await getQueue().publish({
id: deduplicationId,
name: UPDATE_POLL_JOB_NAME,
data: activity.object
})
break
}
case ENTITY_TYPE_NOTE: {
await getQueue().publish({
id: deduplicationId,
name: UPDATE_NOTE_JOB_NAME,
data: activity.object
})
break
}
}
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
}

if (
isMatch(activity, {
type: CreateAction,
object: { type: ENTITY_TYPE_QUESTION }
})
) {
return {
id: deduplicationId,
name: CREATE_POLL_JOB_NAME,
data: activity.object
}
case AnnounceAction: {
await getQueue().publish({
id: deduplicationId,
name: CREATE_ANNOUNCE_JOB_NAME,
data: activity
})
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
}

if (
isMatch(activity, {
type: UpdateAction,
object: { type: ENTITY_TYPE_QUESTION }
})
) {
return {
id: deduplicationId,
name: UPDATE_POLL_JOB_NAME,
data: activity.object
}
case UndoAction: {
switch (activity.object.type) {
case AnnounceAction: {
await getQueue().publish({
id: deduplicationId,
name: DELETE_OBJECT_JOB_NAME,
data: activity.object
})
break
}
}
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
}

if (
isMatch(activity, {
type: UpdateAction,
object: { type: ENTITY_TYPE_NOTE }
})
) {
return {
id: deduplicationId,
name: UPDATE_NOTE_JOB_NAME,
data: activity.object
}
case DeleteAction: {
await getQueue().publish({
id: deduplicationId,
name: DELETE_OBJECT_JOB_NAME,
data: activity.object
})
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
}

if (isMatch(activity, { type: AnnounceAction })) {
return {
id: deduplicationId,
name: CREATE_ANNOUNCE_JOB_NAME,
data: activity
}
default:
return apiErrorResponse(404)
}

if (
isMatch(activity, { type: UndoAction, object: { type: AnnounceAction } }) ||
isMatch(activity, { type: DeleteAction })
) {
return {
id: deduplicationId,
name: DELETE_OBJECT_JOB_NAME,
data: activity.object
}
}

return null
}

export const POST = ActivityPubVerifySenderGuard(async (request) => {
const body = await request.json()
const activity = (await compact(body)) as StatusActivity
const jobMessage = getJobMessage(activity)
if (!jobMessage) {
return apiErrorResponse(404)
}

await getQueue().publish(jobMessage)
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
})

0 comments on commit 2f75a11

Please sign in to comment.