Skip to content

Commit

Permalink
wip: setup announcement actor, add endpoint for actor
Browse files Browse the repository at this point in the history
  • Loading branch information
catdevnull committed Feb 13, 2024
1 parent 893a428 commit 24db4e6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/server/api/announcements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { APActor } from 'activitypub-types'

import type { APIConfig, FastifyTypebox } from '.'
import Store from '../store'
import type ActivityPubSystem from '../apsystem'

type APActorNonStandard = APActor & {
publicKey: {
id: string
owner: string
publicKeyPem: string
}
}

export const announcementsRoutes = (cfg: APIConfig, store: Store, apsystem: ActivityPubSystem) => async (server: FastifyTypebox): Promise<void> => {
server.get<{
Reply: APActorNonStandard
}>('/announcements', {
schema: {
params: {},
// XXX: even with Type.Any(), the endpoint returns `{}` :/
// response: {
// // TODO: typebox APActor
// 200: Type.Any()
// },
description: 'Announcements ActivityPub actor',
tags: ['ActivityPub']
}
}, async (request, reply) => {
// return await reply.send({ prueba: 'asdfasd' })
const actor = await store.announcements.getInfo()

return await reply.send({
'@context': [
// TODO: I copied this from Mastodon, is this correct?
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
// https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
type: 'Service',
name: 'Announcements',
inbox: `${actor.actorUrl}/inbox`,
outbox: `${actor.actorUrl}/outbox`,
publicKey: {
// TODO: copied from Mastodon
id: `${actor.actorUrl}#main-key`,

owner: actor.actorUrl,
publicKeyPem: actor.keypair.publicKeyPem
}
})
})
}
20 changes: 20 additions & 0 deletions src/server/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { blockAllowListRoutes } from './blockallowlist.js'
import { adminRoutes } from './admins.js'
import { followerRoutes } from './followers.js'
import { hookRoutes } from './hooks.js'
import { generateKeypair } from 'http-signed-fetch'
import { announcementsRoutes } from './announcements.js'

export const paths = envPaths('distributed-press')

Expand Down Expand Up @@ -94,6 +96,23 @@ async function apiBuilder (cfg: APIConfig): Promise<FastifyTypebox> {
return 'ok\n'
})

// Setup announcements actor
let keys
try {
const prev = await store.announcements.getInfo()
keys = { ...prev.keypair, publicKeyId: prev.publicKeyId }
} catch {
keys = generateKeypair()
}
await store.announcements.setInfo({
actorUrl: `${cfg.publicURL}/v1/announcements`,
publicKeyId: keys.publicKeyId,
keypair: {
privateKeyPem: keys.privateKeyPem,
publicKeyPem: keys.publicKeyPem
}
})

await server.register(v1Routes(cfg, store, apsystem, hookSystem), { prefix: '/v1' })

await server.ready()
Expand Down Expand Up @@ -123,6 +142,7 @@ const v1Routes = (cfg: APIConfig, store: Store, apsystem: ActivityPubSystem, hoo
})
}

await server.register(announcementsRoutes(cfg, store, apsystem))
await server.register(creationRoutes(cfg, store, apsystem))
await server.register(inboxRoutes(cfg, store, apsystem))
await server.register(outboxRoutes(cfg, store, apsystem))
Expand Down
3 changes: 3 additions & 0 deletions src/server/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class Store {
db: AbstractLevel<any, string, any>
actorCache: Map<string, ActorStore>
actorsDb: AbstractLevel<any, string, any>
announcements: ActorStore
blocklist: AccountListStore
allowlist: AccountListStore
admins: AccountListStore
Expand All @@ -19,6 +20,8 @@ export default class Store {
this.db = db
this.actorCache = new Map()
this.actorsDb = this.db.sublevel('actorCache', { valueEncoding: 'json' })
const announcementsDb = this.db.sublevel('announcements', { valueEncoding: 'json' })
this.announcements = new ActorStore(announcementsDb)
const blocklistDb = this.db.sublevel('blocklist', {
valueEncoding: 'json'
})
Expand Down

0 comments on commit 24db4e6

Please sign in to comment.