Skip to content

Commit

Permalink
Move updateNote to job
Browse files Browse the repository at this point in the history
  • Loading branch information
llun committed Sep 13, 2024
1 parent aaa8d61 commit 16aa5a2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
42 changes: 1 addition & 41 deletions lib/actions/updateNote.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
import { Note } from '@llun/activities.schema'

import { compact } from '@/lib/utils/jsonld'
import {
ACTIVITY_STREAM_PUBLIC,
ACTIVITY_STREAM_PUBLIC_COMACT,
ACTIVITY_STREAM_URL
ACTIVITY_STREAM_PUBLIC_COMACT
} from '@/lib/utils/jsonld/activitystream'

import { sendUpdateNote } from '../activities'
import { getContent, getSummary } from '../activities/entities/note'
import { Actor } from '../models/actor'
import { StatusType } from '../models/status'
import { Storage } from '../storage/types'
import { logger } from '../utils/logger'
import { getSpan } from '../utils/trace'

interface UpdateNoteParams {
note: Note
storage: Storage
}
export const updateNote = async ({ note, storage }: UpdateNoteParams) => {
const span = getSpan('actions', 'updateNote', { status: note.id })
const existingStatus = await storage.getStatus({
statusId: note.id,
withReplies: false
})
if (!existingStatus || existingStatus.type !== StatusType.enum.Note) {
span.end()
return note
}

const compactNote = (await compact({
'@context': ACTIVITY_STREAM_URL,
...note
})) as Note
if (compactNote.type !== 'Note') {
span.end()
return null
}

const text = getContent(compactNote)
const summary = getSummary(compactNote)
await storage.updateNote({
statusId: compactNote.id,
summary,
text
})
span.end()
return note
}

interface UpdateNoteFromUserInput {
statusId: string
currentActor: Actor
Expand Down
2 changes: 2 additions & 0 deletions lib/jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
} from './createAnnounceJob'
import { CREATE_NOTE_JOB_NAME, createNoteJob } from './createNoteJob'
import { CREATE_POLL_JOB_NAME, createPollJob } from './createPollJob'
import { UPDATE_NOTE_JOB_NAME, updateNoteJob } from './updateNoteJob'
import { UPDATE_POLL_JOB_NAME, updatePollJob } from './updatePollJob'

export const JOBS: Record<string, JobHandle> = {
[CREATE_NOTE_JOB_NAME]: createNoteJob,
[UPDATE_NOTE_JOB_NAME]: updateNoteJob,
[CREATE_ANNOUNCE_JOB_NAME]: createAnnounceJob,
[CREATE_POLL_JOB_NAME]: createPollJob,
[UPDATE_POLL_JOB_NAME]: updatePollJob
Expand Down
38 changes: 38 additions & 0 deletions lib/jobs/updateNoteJob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Note } from '@llun/activities.schema'

import { getContent, getSummary } from '../activities/entities/note'
import { StatusType } from '../models/status'
import { compact } from '../utils/jsonld'
import { ACTIVITY_STREAM_URL } from '../utils/jsonld/activitystream'
import { createJobHandle } from './createJobHandle'

export const UPDATE_NOTE_JOB_NAME = 'UpdateNoteJob'
export const updateNoteJob = createJobHandle(
UPDATE_NOTE_JOB_NAME,
async (storage, message) => {
const note = Note.parse(message.data)
const existingStatus = await storage.getStatus({
statusId: note.id,
withReplies: false
})
if (!existingStatus || existingStatus.type !== StatusType.enum.Note) {
return
}

const compactNote = (await compact({
'@context': ACTIVITY_STREAM_URL,
...note
})) as Note
if (compactNote.type !== 'Note') {
return
}

const text = getContent(compactNote)
const summary = getSummary(compactNote)
await storage.updateNote({
statusId: compactNote.id,
summary,
text
})
}
)

0 comments on commit 16aa5a2

Please sign in to comment.