Skip to content

Commit

Permalink
chore: move outbox getter to Announcements class
Browse files Browse the repository at this point in the history
  • Loading branch information
catdevnull committed Mar 6, 2024
1 parent e0e863e commit d081056
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
20 changes: 20 additions & 0 deletions src/server/announcements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { nanoid } from 'nanoid'
import { ActorInfo } from '../schemas'
import ActivityPubSystem, { DEFAULT_PUBLIC_KEY_FIELD } from './apsystem'
import { generateKeypair } from 'http-signed-fetch'
import { APOrderedCollection } from 'activitypub-types'

export class Announcements {
apsystem: ActivityPubSystem
Expand Down Expand Up @@ -57,4 +58,23 @@ export class Announcements {
await this.apsystem.notifyFollowers('announcements', activity)
}
}

async getOutbox (): Promise<APOrderedCollection> {
const actor = await this.apsystem.store.announcements.getInfo()
const activities = await this.apsystem.store.announcements.outbox.list()
const orderedItems = activities
// XXX: maybe `new Date()` doesn't correctly parse possible dates?
.map(a => ({ ...a, published: typeof a.published === 'string' ? new Date(a.published) : a.published }))
.sort((a, b) => +(b.published ?? 0) - +(a.published ?? 0))
.map(a => a.id)
.filter((id): id is string => id !== undefined)

return {
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${actor.actorUrl}outbox`,
type: 'OrderedCollection',
totalItems: orderedItems.length,
orderedItems
}
}
}
15 changes: 1 addition & 14 deletions src/server/api/announcements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,7 @@ export const announcementsRoutes = (cfg: APIConfig, store: Store, apsystem: Acti
tags: ['ActivityPub']
}
}, async (request, reply) => {
const actor = await store.announcements.getInfo()
const activities = await store.announcements.outbox.list()
const orderedItems = activities
// XXX: maybe `new Date()` doesn't correctly parse possible dates?
.map(a => ({ ...a, published: typeof a.published === 'string' ? new Date(a.published) : a.published }))
.sort((a, b) => +(b.published ?? 0) - +(a.published ?? 0))
.map(a => a.id)
return await reply.send({
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${actor.actorUrl}outbox`,
type: 'OrderedCollection',
totalItems: orderedItems.length,
orderedItems
})
return await reply.send(await apsystem.announcements.getOutbox())
})

server.get<{
Expand Down

0 comments on commit d081056

Please sign in to comment.