-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move announcements stuff to APSystem subclass, fix stuff
* announce to announcements actor, not to newly created actor * only update required parts of announcements actor info
- Loading branch information
1 parent
073b699
commit e06ea59
Showing
4 changed files
with
65 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters