Skip to content

Commit

Permalink
Merge branch 'main' into oltp
Browse files Browse the repository at this point in the history
  • Loading branch information
llun committed Sep 21, 2024
2 parents 3a67a1b + 1dffd4d commit 3f365b7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 98 deletions.
99 changes: 99 additions & 0 deletions app/api/inbox/getJobMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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'
import {
AnnounceAction,
CreateAction,
DeleteAction,
UndoAction,
UpdateAction
} from '@/lib/activities/actions/types'
import {
CREATE_ANNOUNCE_JOB_NAME,
CREATE_NOTE_JOB_NAME,
CREATE_POLL_JOB_NAME,
DELETE_OBJECT_JOB_NAME,
UPDATE_NOTE_JOB_NAME,
UPDATE_POLL_JOB_NAME
} from '@/lib/jobs/names'

export const getJobMessage = (activity: StatusActivity) => {
const deduplicationId = crypto
.createHash('sha256')
.update(JSON.stringify(activity.id))
.digest('hex')
if (
isMatch(activity, {
type: CreateAction,
object: { type: ENTITY_TYPE_NOTE }
})
) {
return {
id: deduplicationId,
name: CREATE_NOTE_JOB_NAME,
data: activity.object
}
}

if (
isMatch(activity, {
type: CreateAction,
object: { type: ENTITY_TYPE_QUESTION }
})
) {
return {
id: deduplicationId,
name: CREATE_POLL_JOB_NAME,
data: activity.object
}
}

if (
isMatch(activity, {
type: UpdateAction,
object: { type: ENTITY_TYPE_QUESTION }
})
) {
return {
id: deduplicationId,
name: UPDATE_POLL_JOB_NAME,
data: activity.object
}
}

if (
isMatch(activity, {
type: UpdateAction,
object: { type: ENTITY_TYPE_NOTE }
})
) {
return {
id: deduplicationId,
name: UPDATE_NOTE_JOB_NAME,
data: activity.object
}
}

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

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
}
100 changes: 2 additions & 98 deletions app/api/inbox/route.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
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'
import {
AnnounceAction,
CreateAction,
DeleteAction,
UndoAction,
UpdateAction
} from '@/lib/activities/actions/types'
import {
CREATE_ANNOUNCE_JOB_NAME,
CREATE_NOTE_JOB_NAME,
CREATE_POLL_JOB_NAME,
DELETE_OBJECT_JOB_NAME,
UPDATE_NOTE_JOB_NAME,
UPDATE_POLL_JOB_NAME
} from '@/lib/jobs/names'
import { ActivityPubVerifySenderGuard } from '@/lib/services/guards/ActivityPubVerifyGuard'
import { getQueue } from '@/lib/services/queue'
import { HttpMethod } from '@/lib/utils/getCORSHeaders'
Expand All @@ -29,89 +10,12 @@ import {
defaultOptions
} from '@/lib/utils/response'

import { getJobMessage } from './getJobMessage'

const CORS_HEADERS = [HttpMethod.enum.OPTIONS, HttpMethod.enum.POST]

export const OPTIONS = defaultOptions(CORS_HEADERS)

export const getJobMessage = (activity: StatusActivity) => {
const deduplicationId = crypto
.createHash('sha256')
.update(JSON.stringify(activity.id))
.digest('hex')
if (
isMatch(activity, {
type: CreateAction,
object: { type: ENTITY_TYPE_NOTE }
})
) {
return {
id: deduplicationId,
name: CREATE_NOTE_JOB_NAME,
data: activity.object
}
}

if (
isMatch(activity, {
type: CreateAction,
object: { type: ENTITY_TYPE_QUESTION }
})
) {
return {
id: deduplicationId,
name: CREATE_POLL_JOB_NAME,
data: activity.object
}
}

if (
isMatch(activity, {
type: UpdateAction,
object: { type: ENTITY_TYPE_QUESTION }
})
) {
return {
id: deduplicationId,
name: UPDATE_POLL_JOB_NAME,
data: activity.object
}
}

if (
isMatch(activity, {
type: UpdateAction,
object: { type: ENTITY_TYPE_NOTE }
})
) {
return {
id: deduplicationId,
name: UPDATE_NOTE_JOB_NAME,
data: activity.object
}
}

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

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
Expand Down

0 comments on commit 3f365b7

Please sign in to comment.