Skip to content

Commit

Permalink
Add delete object job
Browse files Browse the repository at this point in the history
  • Loading branch information
llun committed Sep 21, 2024
1 parent bc7dce1 commit f9bb928
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 84 deletions.
23 changes: 12 additions & 11 deletions app/api/inbox/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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'
Expand All @@ -31,8 +32,7 @@ const CORS_HEADERS = [HttpMethod.enum.OPTIONS, HttpMethod.enum.POST]

export const OPTIONS = defaultOptions(CORS_HEADERS)

export const POST = ActivityPubVerifySenderGuard(async (request, context) => {
const { storage } = context
export const POST = ActivityPubVerifySenderGuard(async (request) => {
const body = await request.json()
const activity = (await compact(body)) as StatusActivity
const deduplicationId = crypto
Expand Down Expand Up @@ -93,21 +93,22 @@ export const POST = ActivityPubVerifySenderGuard(async (request, context) => {
case UndoAction: {
switch (activity.object.type) {
case AnnounceAction: {
const statusId = activity.object.id
await storage.deleteStatus({ statusId })
await getQueue().publish({
id: deduplicationId,
name: DELETE_OBJECT_JOB_NAME,
data: activity.object
})
break
}
}
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
}
case DeleteAction: {
// TODO: Handle delete object type string
if (typeof activity.object === 'string') {
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
}

const id = activity.object.id
await storage.deleteStatus({ statusId: id })
await getQueue().publish({
id: deduplicationId,
name: DELETE_OBJECT_JOB_NAME,
data: activity.object
})
return apiResponse(request, CORS_HEADERS, DEFAULT_202, 202)
}
default:
Expand Down
1 change: 1 addition & 0 deletions lib/activities/actions/createStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface CreateStatus extends BaseActivity, ContextEntity {
interface CompactParams {
status: Status
}

export const compact = ({ status }: CompactParams) => {
const published = getISOTimeUTC(status.data.createdAt)
const context = { '@context': 'https://www.w3.org/ns/activitystreams' }
Expand Down
29 changes: 29 additions & 0 deletions lib/jobs/deleteObjectJob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Tombstone } from '@llun/activities.schema'

import { getTracer } from '../utils/trace'
import { createJobHandle } from './createJobHandle'
import { DELETE_OBJECT_JOB_NAME } from './names'

export const deleteObjectJob = createJobHandle(
DELETE_OBJECT_JOB_NAME,
async (storage, message) => {
const data = message.data
if (typeof data === 'string') {
await getTracer().startActiveSpan('deleteUser', async (span) => {
span.setAttribute('actorId', data)
await storage.deleteActor({
actorId: data
})
})
return
}

const tombStone = Tombstone.parse(data)
await getTracer().startActiveSpan('deleteStatus', async (span) => {
span.setAttribute('statusId', tombStone.id)
await storage.deleteStatus({
statusId: tombStone.id
})
})
}
)
5 changes: 4 additions & 1 deletion lib/jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { JobHandle } from '../services/queue/type'
import { createAnnounceJob } from './createAnnounceJob'
import { createNoteJob } from './createNoteJob'
import { createPollJob } from './createPollJob'
import { deleteObjectJob } from './deleteObjectJob'
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 './names'
Expand All @@ -17,5 +19,6 @@ export const JOBS: Record<string, JobHandle> = {
[UPDATE_NOTE_JOB_NAME]: updateNoteJob,
[CREATE_ANNOUNCE_JOB_NAME]: createAnnounceJob,
[CREATE_POLL_JOB_NAME]: createPollJob,
[UPDATE_POLL_JOB_NAME]: updatePollJob
[UPDATE_POLL_JOB_NAME]: updatePollJob,
[DELETE_OBJECT_JOB_NAME]: deleteObjectJob
}
1 change: 1 addition & 0 deletions lib/jobs/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export const CREATE_NOTE_JOB_NAME = 'CreateNoteJob'
export const CREATE_POLL_JOB_NAME = 'CreatePollJob'
export const UPDATE_NOTE_JOB_NAME = 'UpdateNoteJob'
export const UPDATE_POLL_JOB_NAME = 'UpdatePollJob'
export const DELETE_OBJECT_JOB_NAME = 'DeleteObjectJob'
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
"migrate:make": "knex migrate:make"
},
"dependencies": {
"@aws-sdk/client-lambda": "^3.655.0",
"@aws-sdk/client-lambda": "^3.656.0",
"@aws-sdk/client-s3": "^3.654.0",
"@aws-sdk/s3-presigned-post": "^3.654.0",
"@aws-sdk/util-utf8-node": "^3.259.0",
"@date-fns/utc": "^2.1.0",
"@google-cloud/firestore": "^7.10.0",
"@jmondi/oauth2-server": "^4.0.2",
"@llun/activities.schema": "^0.2.12",
"@llun/activities.schema": "^0.2.13",
"@opentelemetry/api": "^1.9.0",
"@upstash/qstash": "2.7.8",
"@upstash/qstash": "2.7.9",
"bcrypt": "^5.1.1",
"better-sqlite3": "^11.3.0",
"bootstrap": "^5.3.3",
Expand All @@ -52,7 +52,7 @@
"react-modal": "^3.16.1",
"resend": "^4.0.0",
"sanitize-html": "^2.13.0",
"sass": "^1.79.2",
"sass": "^1.79.3",
"sharp": "0.33.5",
"zod": "^3.23.8"
},
Expand Down Expand Up @@ -82,7 +82,7 @@
"@typescript-eslint/eslint-plugin": "^8.6.0",
"@typescript-eslint/parser": "^8.6.0",
"dotenv-flow": "^4.1.0",
"eslint": "^9.10.0",
"eslint": "^9.11.0",
"eslint-config-next": "14.2.13",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
Expand Down
Loading

0 comments on commit f9bb928

Please sign in to comment.