Skip to content

Commit

Permalink
fix: move announcements stuff to APSystem subclass, fix stuff
Browse files Browse the repository at this point in the history
* announce to announcements actor, not to newly created actor
* only update required parts of announcements actor info
  • Loading branch information
catdevnull committed Feb 29, 2024
1 parent 073b699 commit e06ea59
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
60 changes: 60 additions & 0 deletions src/server/announcements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { nanoid } from 'nanoid'
import { ActorInfo } from '../schemas'
import ActivityPubSystem, { DEFAULT_PUBLIC_KEY_FIELD } from './apsystem'
import { generateKeypair } from 'http-signed-fetch'

export class Announcements {
apsystem: ActivityPubSystem
publicURL: string

constructor (apsystem: ActivityPubSystem, publicURL: string) {
this.apsystem = apsystem
this.publicURL = publicURL
}

async init (): Promise<void> {
const actorUrl = `${this.publicURL}/v1/announcements`

try {
const prev = await this.apsystem.store.announcements.getInfo()
if (prev.actorUrl !== actorUrl) {
await this.apsystem.store.announcements.setInfo({
...prev,
actorUrl
})
}
} catch {
const { privateKeyPem, publicKeyPem } = generateKeypair()
await this.apsystem.store.announcements.setInfo({
actorUrl,
publicKeyId: `${actorUrl}#${DEFAULT_PUBLIC_KEY_FIELD}`,
keypair: {
privateKeyPem,
publicKeyPem
},
announce: false
})
}
}

async announce (actor: string, info: ActorInfo): Promise<void> {
const existedAlready = (await this.apsystem.store.actorsDb.keys().all()).some(k => k === actor)

if (!existedAlready && info.announce) {
const activity = {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Note',
id: `${info.actorUrl}/outbox/${nanoid()}`,
actor: info.actorUrl,
attributedTo: info.actorUrl,
published: new Date().toUTCString(),
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: ['https://social.distributed.press/v1/announcements/followers'],
// TODO: add a template in config
content: `a wild site appears! ${actor}`
}
await this.apsystem.store.announcements.outbox.add(activity)
await this.apsystem.notifyFollowers('announcements', activity)
}
}
}
20 changes: 1 addition & 19 deletions src/server/api/creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,14 @@ export const creationRoutes = (cfg: APIConfig, store: Store, apsystem: ActivityP
}, async (request, reply) => {
const { actor } = request.params

const existedAlready = (await store.actorsDb.keys().all()).some(k => k === actor)

const allowed = await apsystem.hasPermissionActorRequest(actor, request, false)
if (!allowed) {
return await reply.code(403).send('Not Allowed')
}

const info = request.body
await store.forActor(actor).setInfo(info)

if (!existedAlready && info.announce) {
const activity = {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Note',
id: `${info.actorUrl}/outbox/${nanoid()}`,
actor: info.actorUrl,
attributedTo: info.actorUrl,
published: new Date().toUTCString(),
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: ['https://social.distributed.press/v1/announcements/followers'],
// TODO: add a template in config
content: `a wild site appears! ${actor}`
}
await store.announcements.outbox.add(activity)
await apsystem.notifyFollowers(actor, activity)
}
await apsystem.announcements.announce(actor, info)

return await reply.send(info)
})
Expand Down
19 changes: 1 addition & 18 deletions src/server/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,7 @@ async function apiBuilder (cfg: APIConfig): Promise<FastifyTypebox> {
return 'ok\n'
})

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

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

Expand Down
3 changes: 3 additions & 0 deletions src/server/apsystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

import type Store from './store/index.js'
import { makeSigner } from '../keypair.js'
import { Announcements } from './announcements.js'

export const DEFAULT_PUBLIC_KEY_FIELD = 'publicKey'

Expand Down Expand Up @@ -45,6 +46,7 @@ export default class ActivityPubSystem {
modCheck: ModerationChecker
fetch: FetchLike
hookSystem: HookSystem
announcements: Announcements

constructor (
publicURL: string,
Expand All @@ -58,6 +60,7 @@ export default class ActivityPubSystem {
this.modCheck = modCheck
this.fetch = fetch
this.hookSystem = hookSystem
this.announcements = new Announcements(this, publicURL)
}

makeURL (path: string): string {
Expand Down

0 comments on commit e06ea59

Please sign in to comment.