@evanp/activitypub-bot 0.25.0 → 0.26.0

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/lib/botcontext.js CHANGED
@@ -378,12 +378,18 @@ export class BotContext {
378
378
  return `${username}@${actorUrl.hostname}`
379
379
  }
380
380
 
381
- async announceObject (obj) {
381
+ async announceObject (obj, actors = null) {
382
382
  assert.ok(obj)
383
383
  assert.equal(typeof obj, 'object')
384
- const owners = obj.attributedTo
385
- ? Array.from(obj.attributedTo).map((owner) => owner.id)
386
- : Array.from(obj.actor).map((owner) => owner.id)
384
+
385
+ const owners = (actors)
386
+ ? Array.from(actors).map(actor => actor.id)
387
+ : (obj.attributedTo)
388
+ ? Array.from(obj.attributedTo).map((owner) => owner.id)
389
+ : (obj.actor)
390
+ ? Array.from(obj.actor).map((owner) => owner.id)
391
+ : []
392
+
387
393
  const summary = `${this.#botId} shared "${await this.#nameOf(obj)}"`
388
394
  const activity = await this.#doActivity({
389
395
  type: 'Announce',
@@ -447,9 +453,9 @@ export class BotContext {
447
453
 
448
454
  #nameOf (obj) {
449
455
  if (obj.name) {
450
- return obj.name.valueOf()
456
+ return obj.name.get()
451
457
  } else if (obj.summary) {
452
- return obj.summary.valueOf()
458
+ return obj.summary.get()
453
459
  } else if (obj.type) {
454
460
  return `a(n) ${obj.type.first}`
455
461
  } else {
package/lib/botfactory.js CHANGED
@@ -21,4 +21,8 @@ export default class BotFactory {
21
21
  async onPublic (activity) {
22
22
  ; // no-op
23
23
  }
24
+
25
+ async actorOK (actorId, activity) {
26
+ return false
27
+ }
24
28
  }
@@ -22,6 +22,10 @@ export default class RelayClientBot extends Bot {
22
22
  return 'A bot for subscribing to relays'
23
23
  }
24
24
 
25
+ get key () {
26
+ return `follow:${this.#relay}`
27
+ }
28
+
25
29
  async initialize (context) {
26
30
  super.initialize(context)
27
31
  this._context.logger.info(
@@ -29,11 +33,12 @@ export default class RelayClientBot extends Bot {
29
33
  'Initialising relay client'
30
34
  )
31
35
  if (this.#unsubscribe) {
32
- if (await this._context.hasData(`follow:${this.#relay}`)) {
36
+ if (await this._context.hasData(this.key)) {
33
37
  await this.#unfollowRelay()
34
38
  }
35
39
  } else {
36
- if (!(await this._context.hasData(`follow:${this.#relay}`))) {
40
+ if (!(await this._context.hasData(this.key)) ||
41
+ ((await this._context.getData(this.key)) == null)) {
37
42
  await this.#followRelay()
38
43
  }
39
44
  }
@@ -70,11 +75,11 @@ export default class RelayClientBot extends Bot {
70
75
  { relay: this.#relay, activity: activity.id },
71
76
  'Saving follow for later'
72
77
  )
73
- this._context.setData(`follow:${this.#relay}`, activity.id)
78
+ this._context.setData(this.key, activity.id)
74
79
  }
75
80
 
76
81
  async #unfollowRelay () {
77
- const activityId = await this._context.getData(`follow:${this.#relay}`)
82
+ const activityId = await this._context.getData(this.key)
78
83
  this._context.logger.info(
79
84
  { relay: this.#relay, activity: activityId },
80
85
  'Unfollowing relay'
@@ -92,11 +97,11 @@ export default class RelayClientBot extends Bot {
92
97
  { relay: this.#relay },
93
98
  'Clearing follow data'
94
99
  )
95
- this._context.setData(`follow:${this.#relay}`, null)
100
+ this._context.deleteData(this.key)
96
101
  }
97
102
 
98
103
  async #handleAccept (activity) {
99
- const activityId = await this._context.getData(`follow:${this.#relay}`)
104
+ const activityId = await this._context.getData(this.key)
100
105
  if (activity.object?.first?.id === activityId) {
101
106
  this._context.logger.info(
102
107
  { accept: activity.id, follow: activityId },
@@ -109,7 +114,7 @@ export default class RelayClientBot extends Bot {
109
114
  }
110
115
 
111
116
  async #handleReject (activity) {
112
- const activityId = await this._context.getData(`follow:${this.#relay}`)
117
+ const activityId = await this._context.getData(this.key)
113
118
  if (activity.object?.first?.id === activityId) {
114
119
  this._context.logger.info(
115
120
  { accept: activity.id, follow: activityId },
@@ -15,7 +15,10 @@ async function asyncSome (array, asyncPredicate) {
15
15
  }
16
16
 
17
17
  async function actorOK (subject, activity, bots) {
18
- await asyncSome(Object.values(bots), bot => bot.actorOK(subject, activity))
18
+ return await asyncSome(
19
+ Object.values(bots),
20
+ bot => bot.actorOK(subject, activity)
21
+ )
19
22
  }
20
23
 
21
24
  router.post('/shared/inbox', async (req, res, next) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evanp/activitypub-bot",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "description": "server-side ActivityPub bot framework",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",