@evanp/activitypub-bot 0.44.3 → 0.45.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -9,6 +9,41 @@ and this project adheres to
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.45.2] - 2026-04-24
13
+
14
+ ### Fixed
15
+
16
+ - Only send activities with `actor` property as a bare URI,
17
+ not an embedded object. Fixes interoperability with Misskey.
18
+ - Explicitly set `manuallyApprovesFollowers` to false. Fixes
19
+ (some) interoperability with Pixelfed.
20
+
21
+ ## [0.45.1] - 2026-04-23
22
+
23
+ ### Fixed
24
+
25
+ - LitePubRelayClientBot test was not clearing
26
+ queued jobs
27
+
28
+ ## [0.45.0] - 2026-04-23
29
+
30
+ ### Added
31
+
32
+ - LoggingBot class for debugging installations
33
+ - addFollowingUnsafe(), removeFollowingUnsafe(), fanoutPublic()
34
+ methods of BotContext
35
+
36
+ ### Changed
37
+
38
+ - MastodonRelayClientBot can take an array of relay ids as
39
+ `relay` option or one as a string
40
+ - MastodonRelayClientBot ignores `unsubscribe`, syncs relay
41
+ array instead
42
+ - MastodonRelayClientBot adds relay server to `following`
43
+ - MastodonRelayClientBot and LitePubRelayClientBot fan out
44
+ relay server `Announce` activities to all other bots
45
+ - BotContext constructor takes `bots` argument
46
+
12
47
  ## [0.44.3] - 2026-04-23
13
48
 
14
49
  ### Fixed
package/README.md CHANGED
@@ -204,6 +204,13 @@ An *OKBot* instance will reply to any message that it's mentioned in with the co
204
204
 
205
205
  A *DoNothingBot* instance will only do default stuff, like accepting follows.
206
206
 
207
+ #### LoggingBot
208
+
209
+ A *LoggingBot* behaves like `DoNothingBot` but logs each `Bot` interface
210
+ callback it receives (`onPublic`, `onFollow`, `onMention`, etc.) at `debug`
211
+ level. Useful as a smoke-test bot to confirm that activities are being
212
+ delivered to local bots; silent in production unless log level is lowered.
213
+
207
214
  #### FollowBackBot
208
215
 
209
216
  A *FollowBackBot* will follow back anyone who follows it. Useful for collecting
@@ -211,7 +218,15 @@ public information.
211
218
 
212
219
  #### MastodonRelayClientBot
213
220
 
214
- A *MastodonRelayClientBot* can be the client of a Mastodon relay.
221
+ A *MastodonRelayClientBot* can be the client of one or more Mastodon-style
222
+ relays. Its `relay` option accepts either a single actor URL as a string or
223
+ an array of actor URLs; the bot sends a Mastodon-style `Follow`
224
+ (`object: https://www.w3.org/ns/activitystreams#Public`) to each on
225
+ initialize. To unsubscribe from a relay, remove it from the array and
226
+ re-initialize — the bot diffs the new config against the current `following`
227
+ collection and sends an `Undo`/`Follow` for each removed relay. It also
228
+ advertises `Application` actor type, which Mastodon-style relay software
229
+ (e.g. Pleroma-Relay) requires from subscribers.
215
230
 
216
231
  #### MastodonRelayServerBot
217
232
 
@@ -475,6 +490,27 @@ Async generator that yields each actor in this bot's `following` collection, one
475
490
 
476
491
  Returns `true` if `url` is served by this activitypub-bot instance (i.e. its origin matches the configured `--origin`). Synchronous.
477
492
 
493
+ #### async addFollowingUnsafe (actor)
494
+
495
+ Forcibly adds `actor` to this bot's `following` collection without sending
496
+ a `Follow` or waiting for an `Accept`. Intended for bots (like relay
497
+ clients) whose followship is established through a protocol exchange that
498
+ sits outside the usual Follow/Accept flow. Trust that the caller knows
499
+ what they're doing.
500
+
501
+ #### async removeFollowingUnsafe (actor)
502
+
503
+ Forcibly removes `actor` from this bot's `following` collection without
504
+ sending an `Undo`/`Follow`. The protocol-level cleanup is the caller's
505
+ responsibility. Pair with `addFollowingUnsafe()` for symmetry.
506
+
507
+ #### async fanoutPublic (activity)
508
+
509
+ Invokes `onPublic(activity)` on every bot registered on the server, so one
510
+ bot can forward a public activity it received through a side-channel
511
+ (e.g. a relay-forwarded `Announce`) to all local bots. Errors in any
512
+ individual bot's `onPublic` are logged and do not interrupt the fanout.
513
+
478
514
  #### async onIdle ()
479
515
 
480
516
  Resolves when the background distribution queue has drained. Intended for test code that needs to wait for outbound activities to finish being delivered before asserting on their effects.
@@ -912,11 +912,7 @@ export class ActivityHandler {
912
912
  const activity = await as2.import({
913
913
  ...activityData,
914
914
  id: this.#formatActivityId(bot, activityData.type),
915
- actor: {
916
- id: this.#botId(bot),
917
- name: bot.fullname,
918
- type: bot.type
919
- },
915
+ actor: this.#botId(bot),
920
916
  published: now,
921
917
  updated: now
922
918
  })
@@ -406,4 +406,23 @@ as2.registerContext('https://purl.archive.org/socialweb/webfinger', {
406
406
  }
407
407
  })
408
408
 
409
+ as2.registerContext('https://purl.archive.org/miscellany', {
410
+ '@context': {
411
+ as: 'https://www.w3.org/ns/activitystreams#',
412
+ xsd: 'http://www.w3.org/2001/XMLSchema#',
413
+ Hashtag: 'as:Hashtag',
414
+ manuallyApprovesFollowers: {
415
+ '@id': 'as:manuallyApprovesFollowers',
416
+ '@type': 'xsd:boolean'
417
+ },
418
+ movedTo: {
419
+ '@id': 'as:movedTo',
420
+ '@type': '@id'
421
+ },
422
+ sensitive: {
423
+ '@id': 'as:sensitive',
424
+ '@type': 'xsd:boolean'
425
+ }
426
+ }
427
+ })
409
428
  export default as2
package/lib/app.js CHANGED
@@ -143,7 +143,8 @@ export async function makeApp ({ databaseUrl, origin, bots, logLevel = 'silent',
143
143
  distributor,
144
144
  formatter,
145
145
  transformer,
146
- logger
146
+ logger,
147
+ bots
147
148
  )
148
149
  ))
149
150
  )
@@ -164,7 +165,8 @@ export async function makeApp ({ databaseUrl, origin, bots, logLevel = 'silent',
164
165
  distributor,
165
166
  formatter,
166
167
  transformer,
167
- logger
168
+ logger,
169
+ bots
168
170
  ))
169
171
 
170
172
  const { workers: deliveryWorkers, runs: deliveryWorkerRuns } = createWorkers(logger, deliveryWorkerCount, DeliveryWorker, jobQueue, logger, { actorStorage, activityHandler, bots })
package/lib/botcontext.js CHANGED
@@ -25,6 +25,8 @@ export class BotContext {
25
25
  #formatter = null
26
26
  #transformer = null
27
27
  #logger = null
28
+ #bots
29
+
28
30
  get botId () {
29
31
  return this.#botId
30
32
  }
@@ -42,7 +44,8 @@ export class BotContext {
42
44
  distributor,
43
45
  formatter,
44
46
  transformer,
45
- logger
47
+ logger,
48
+ bots
46
49
  ) {
47
50
  this.#botId = botId
48
51
  this.#botDataStorage = botDataStorage
@@ -52,6 +55,7 @@ export class BotContext {
52
55
  this.#distributor = distributor
53
56
  this.#formatter = formatter
54
57
  this.#transformer = transformer
58
+ this.#bots = bots
55
59
  this.#logger = logger.child({ class: 'BotContext', botId })
56
60
  }
57
61
 
@@ -481,6 +485,37 @@ export class BotContext {
481
485
  yield * this.#actorStorage.items(this.#botId, 'following')
482
486
  }
483
487
 
488
+ async addFollowingUnsafe (actor) {
489
+ assert.ok(actor)
490
+ assert.equal(typeof actor, 'object')
491
+ await this.#actorStorage.addToCollection(this.#botId, 'following', actor)
492
+ this.#logger.debug({ actor: actor.id }, 'unsafe add to following')
493
+ }
494
+
495
+ async removeFollowingUnsafe (actor) {
496
+ assert.ok(actor)
497
+ assert.equal(typeof actor, 'object')
498
+ await this.#actorStorage.removeFromCollection(this.#botId, 'following', actor)
499
+ this.#logger.debug({ actor: actor.id }, 'unsafe remove from following')
500
+ }
501
+
502
+ async fanoutPublic (activity) {
503
+ assert.ok(activity)
504
+ assert.equal(typeof activity, 'object')
505
+ await Promise.all(Object.values(this.#bots).map(async (bot) => {
506
+ let result
507
+ try {
508
+ result = await bot.onPublic(activity)
509
+ } catch (err) {
510
+ this.#logger.warn(
511
+ { err, activity: activity.id, bot: bot.id },
512
+ 'Error handling public activity for bot'
513
+ )
514
+ }
515
+ return result
516
+ }))
517
+ }
518
+
484
519
  async #isInCollection (name, obj) {
485
520
  assert.ok(name)
486
521
  assert.equal(typeof name, 'string')
@@ -540,9 +575,7 @@ export class BotContext {
540
575
  type,
541
576
  nanoid: nanoid()
542
577
  }),
543
- actor: {
544
- id: this.#formatter.format({ username: this.#botId })
545
- },
578
+ actor: this.#formatter.format({ username: this.#botId }),
546
579
  published: now,
547
580
  updated: now
548
581
  }
@@ -4,6 +4,7 @@ import Bot from '../bot.js'
4
4
 
5
5
  const NS = 'https://www.w3.org/ns/activitystreams#'
6
6
  const CREATE = `${NS}Create`
7
+ const ANNOUNCE = `${NS}Announce`
7
8
 
8
9
  const DEFAULT_NAME = 'LitePubRelayClientBot'
9
10
  const DEFAULT_DESCRIPTION = 'A LitePub relay client'
@@ -75,6 +76,26 @@ export default class LitePubRelayClientBot extends Bot {
75
76
  }
76
77
  }
77
78
 
79
+ async handleActivity (activity) {
80
+ if (activity.type === ANNOUNCE) {
81
+ return await this.#handleAnnounce(activity)
82
+ } else {
83
+ return false
84
+ }
85
+ }
86
+
87
+ async #handleAnnounce (activity) {
88
+ if (this.#relay.includes(activity.actor?.first?.id)) {
89
+ this._context.logger.debug(
90
+ { activity: activity.id },
91
+ 'fanning out relay announce activity'
92
+ )
93
+ await this._context.fanoutPublic(activity)
94
+ return true
95
+ }
96
+ return false
97
+ }
98
+
78
99
  async #hasAnyFollower () {
79
100
  for await (const actor of this._context.followers()) {
80
101
  if (this.#relay.includes(actor.id)) {
@@ -0,0 +1,103 @@
1
+ import Bot from '../bot.js'
2
+
3
+ const DEFAULT_FULLNAME = 'Logging Bot'
4
+ const DEFAULT_DESCRIPTION = 'A bot that logs callback events.'
5
+
6
+ export default class LoggingBot extends Bot {
7
+ constructor (username, options = {}) {
8
+ super(username, {
9
+ fullname: DEFAULT_FULLNAME,
10
+ description: DEFAULT_DESCRIPTION,
11
+ ...options
12
+ })
13
+ }
14
+
15
+ async onMention (object, activity) {
16
+ this._context.logger.debug(
17
+ {
18
+ class: this.constructor.name,
19
+ object: await object.export(),
20
+ activity: await activity.export()
21
+ },
22
+ 'onMention'
23
+ )
24
+ }
25
+
26
+ async onFollow (actor, activity) {
27
+ this._context.logger.debug(
28
+ {
29
+ class: this.constructor.name,
30
+ actor: await actor.export(),
31
+ activity: await activity.export()
32
+ },
33
+ 'onFollow'
34
+ )
35
+ }
36
+
37
+ async onLike (object, activity) {
38
+ this._context.logger.debug(
39
+ {
40
+ class: this.constructor.name,
41
+ object: await object.export(),
42
+ activity: await activity.export()
43
+ },
44
+ 'onLike'
45
+ )
46
+ }
47
+
48
+ async onAnnounce (object, activity) {
49
+ this._context.logger.debug(
50
+ {
51
+ class: this.constructor.name,
52
+ object: await object.export(),
53
+ activity: await activity.export()
54
+ },
55
+ 'onAnnounce'
56
+ )
57
+ }
58
+
59
+ async onPublic (activity) {
60
+ this._context.logger.debug(
61
+ {
62
+ class: this.constructor.name,
63
+ activity: await activity.export()
64
+ },
65
+ 'onPublic'
66
+ )
67
+ }
68
+
69
+ async actorOK (actorId, activity) {
70
+ this._context.logger.debug(
71
+ {
72
+ class: this.constructor.name,
73
+ actorId,
74
+ activity: await activity.export()
75
+ },
76
+ 'actorOK'
77
+ )
78
+ return false
79
+ }
80
+
81
+ async handleActivity (activity) {
82
+ this._context.logger.debug(
83
+ {
84
+ class: this.constructor.name,
85
+ activity: await activity.export()
86
+ },
87
+ 'handleActivity'
88
+ )
89
+ return false
90
+ }
91
+
92
+ async onUndoFollow (actor, undoActivity, followActivity) {
93
+ this._context.logger.debug(
94
+ {
95
+ class: this.constructor.name,
96
+ actor: await actor.export(),
97
+ undoActivity: await undoActivity.export(),
98
+ followActivity: await followActivity.export()
99
+ },
100
+ 'onUndoFollow'
101
+ )
102
+ }
103
+ }
@@ -1,17 +1,34 @@
1
+ import assert from 'node:assert'
2
+
1
3
  import Bot from '../bot.js'
2
4
 
3
5
  const NS = 'https://www.w3.org/ns/activitystreams#'
4
6
  const ACCEPT = `${NS}Accept`
5
7
  const REJECT = `${NS}Reject`
8
+ const ANNOUNCE = `${NS}Announce`
6
9
 
7
10
  export default class MastodonRelayClientBot extends Bot {
8
11
  #relay
9
- #unsubscribe
12
+ #relayForwarding
10
13
 
11
14
  constructor (username, options = {}) {
15
+ if (typeof username !== 'string') {
16
+ throw new Error('username must be a string')
17
+ }
18
+ if (typeof options !== 'object') {
19
+ throw new Error('options must be an object')
20
+ }
21
+ if (typeof options.relay !== 'string' &&
22
+ !Array.isArray(options.relay)) {
23
+ throw new Error('relay option must be a string or array')
24
+ }
12
25
  super(username, options)
13
- this.#relay = options.relay
14
- this.#unsubscribe = !!options.unsubscribe
26
+ this.#relay = Array.isArray(options.relay)
27
+ ? options.relay
28
+ : [options.relay]
29
+ this.#relayForwarding = ('relayForwarding' in options)
30
+ ? options.relayForwarding
31
+ : true
15
32
  }
16
33
 
17
34
  get type () {
@@ -26,24 +43,29 @@ export default class MastodonRelayClientBot extends Bot {
26
43
  return 'A bot for subscribing to relays'
27
44
  }
28
45
 
29
- get key () {
30
- return `follow:${this.#relay}`
31
- }
32
-
33
46
  async initialize (context) {
34
- super.initialize(context)
47
+ await super.initialize(context)
35
48
  this._context.logger.info(
36
- { relay: this.#relay, unsubscribe: this.#unsubscribe },
49
+ { relay: this.#relay },
37
50
  'Initialising relay client'
38
51
  )
39
- if (this.#unsubscribe) {
40
- if (await this._context.hasData(this.key)) {
41
- await this.#unfollowRelay()
52
+
53
+ assert.ok(Array.isArray(this.#relay))
54
+
55
+ const toFollow = new Set(this.#relay)
56
+
57
+ for await (const actor of this._context.following()) {
58
+ if (toFollow.has(actor.id)) {
59
+ toFollow.delete(actor.id)
60
+ } else {
61
+ await this.#unfollowRelay(actor)
42
62
  }
43
- } else {
44
- if (!(await this._context.hasData(this.key)) ||
45
- ((await this._context.getData(this.key)) == null)) {
46
- await this.#followRelay()
63
+ }
64
+
65
+ for (const id of toFollow) {
66
+ const actor = await this._context.getObject(id)
67
+ if (!await this.#hasFollowActivity(actor)) {
68
+ await this.#followRelay(actor)
47
69
  }
48
70
  }
49
71
  }
@@ -56,40 +78,42 @@ export default class MastodonRelayClientBot extends Bot {
56
78
  return await this.#handleAccept(activity)
57
79
  } else if (activity.type === REJECT) {
58
80
  return await this.#handleReject(activity)
81
+ } else if (activity.type === ANNOUNCE) {
82
+ return await this.#handleAnnounce(activity)
59
83
  } else {
60
84
  return false
61
85
  }
62
86
  }
63
87
 
64
88
  async actorOK (actorId, activity) {
65
- return (actorId === this.#relay && !this.#unsubscribe)
89
+ return this.#relay.includes(actorId)
66
90
  }
67
91
 
68
- async #followRelay () {
92
+ async #followRelay (actor) {
69
93
  this._context.logger.info(
70
- { relay: this.#relay },
94
+ { relay: actor.id },
71
95
  'Following relay'
72
96
  )
73
97
  const activity = await this._context.doActivity({
74
- to: this.#relay,
98
+ to: actor.id,
75
99
  type: 'Follow',
76
100
  object: 'https://www.w3.org/ns/activitystreams#Public'
77
101
  })
78
102
  this._context.logger.info(
79
- { relay: this.#relay, activity: activity.id },
103
+ { relay: actor.id, activity: activity.id },
80
104
  'Saving follow for later'
81
105
  )
82
- this._context.setData(this.key, activity.id)
106
+ await this.#setFollowActivity(actor, activity)
83
107
  }
84
108
 
85
- async #unfollowRelay () {
86
- const activityId = await this._context.getData(this.key)
109
+ async #unfollowRelay (actor) {
110
+ const activityId = await this.#getFollowActivity(actor)
87
111
  this._context.logger.info(
88
- { relay: this.#relay, activity: activityId },
112
+ { relay: actor.id, activity: activityId },
89
113
  'Unfollowing relay'
90
114
  )
91
115
  await this._context.doActivity({
92
- to: this.#relay,
116
+ to: actor.id,
93
117
  type: 'Undo',
94
118
  object: {
95
119
  id: activityId,
@@ -98,19 +122,22 @@ export default class MastodonRelayClientBot extends Bot {
98
122
  }
99
123
  })
100
124
  this._context.logger.info(
101
- { relay: this.#relay },
125
+ { relay: actor.id },
102
126
  'Clearing follow data'
103
127
  )
104
- this._context.deleteData(this.key)
128
+ await this._context.removeFollowingUnsafe(actor)
129
+ await this.#deleteFollowActivity(actor)
105
130
  }
106
131
 
107
132
  async #handleAccept (activity) {
108
- const activityId = await this._context.getData(this.key)
133
+ const actor = activity.actor?.first
134
+ const activityId = await this.#getFollowActivity(actor)
109
135
  if (activity.object?.first?.id === activityId) {
110
136
  this._context.logger.info(
111
137
  { accept: activity.id, follow: activityId },
112
138
  'Follow accepted'
113
139
  )
140
+ await this._context.addFollowingUnsafe(activity.actor.first)
114
141
  return true
115
142
  } else {
116
143
  return false
@@ -118,7 +145,8 @@ export default class MastodonRelayClientBot extends Bot {
118
145
  }
119
146
 
120
147
  async #handleReject (activity) {
121
- const activityId = await this._context.getData(this.key)
148
+ const actor = activity.actor?.first
149
+ const activityId = await this.#getFollowActivity(actor)
122
150
  if (activity.object?.first?.id === activityId) {
123
151
  this._context.logger.info(
124
152
  { accept: activity.id, follow: activityId },
@@ -129,4 +157,32 @@ export default class MastodonRelayClientBot extends Bot {
129
157
  return false
130
158
  }
131
159
  }
160
+
161
+ async #handleAnnounce (activity) {
162
+ if (this.#relay.includes(activity.actor?.first?.id)) {
163
+ await this._context.fanoutPublic(activity)
164
+ return true
165
+ }
166
+ return false
167
+ }
168
+
169
+ async #setFollowActivity (actor, activity) {
170
+ const key = `follow:${actor.id}`
171
+ return await this._context.setData(key, activity.id)
172
+ }
173
+
174
+ async #getFollowActivity (actor) {
175
+ const key = `follow:${actor.id}`
176
+ return await this._context.getData(key)
177
+ }
178
+
179
+ async #deleteFollowActivity (actor) {
180
+ const key = `follow:${actor.id}`
181
+ return await this._context.deleteData(key)
182
+ }
183
+
184
+ async #hasFollowActivity (actor) {
185
+ const key = `follow:${actor.id}`
186
+ return await this._context.hasData(key)
187
+ }
132
188
  }
package/lib/index.js CHANGED
@@ -8,3 +8,4 @@ export { default as MastodonRelayServerBot, default as RelayServerBot } from './
8
8
  export { default as FollowBackBot } from './bots/followback.js'
9
9
  export { default as LitePubRelayClientBot } from './bots/litepubrelayclient.js'
10
10
  export { default as LitePubRelayServerBot } from './bots/litepubrelayserver.js'
11
+ export { default as LoggingBot } from './bots/logging.js'
@@ -37,7 +37,8 @@ router.get('/user/:username', async (req, res, next) => {
37
37
  '@context': [
38
38
  'https://www.w3.org/ns/activitystreams',
39
39
  'https://w3id.org/security/v1',
40
- 'https://purl.archive.org/socialweb/webfinger'
40
+ 'https://purl.archive.org/socialweb/webfinger',
41
+ 'https://purl.archive.org/miscellany'
41
42
  ],
42
43
  id: formatter.format({ username }),
43
44
  type: bot.type,
@@ -67,7 +68,8 @@ router.get('/user/:username', async (req, res, next) => {
67
68
  href: formatter.format({ username, type: 'profile' }),
68
69
  type: 'Link',
69
70
  mediaType: 'text/html'
70
- }
71
+ },
72
+ manuallyApprovesFollowers: false
71
73
  })
72
74
  res.status(200)
73
75
  res.type(as2.mediaType)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evanp/activitypub-bot",
3
- "version": "0.44.3",
3
+ "version": "0.45.2",
4
4
  "description": "server-side ActivityPub bot framework",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",