@evanp/activitypub-bot 0.13.12 → 0.13.13

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.
@@ -129,18 +129,12 @@ export class ActivityHandler {
129
129
  )
130
130
  const recipients = this.#getRecipients(original)
131
131
  this.#addRecipient(recipients, actor, 'to')
132
- await this.#doActivity(bot, await as2.import({
132
+ await this.#doActivity(bot, {
133
133
  type: 'Add',
134
- id: this.#formatter.format({
135
- username: bot.username,
136
- type: 'add',
137
- nanoid: nanoid()
138
- }),
139
- actor: await this.#botActor(bot),
140
134
  object,
141
135
  target: original.replies,
142
136
  ...recipients
143
- }))
137
+ })
144
138
  }
145
139
  }
146
140
  }
@@ -189,18 +183,12 @@ export class ActivityHandler {
189
183
  )
190
184
  const recipients = this.#getRecipients(root)
191
185
  this.#addRecipient(recipients, actor, 'to')
192
- await this.#doActivity(bot, await as2.import({
186
+ await this.#doActivity(bot, {
193
187
  type: 'Add',
194
- id: this.#formatter.format({
195
- username: bot.username,
196
- type: 'add',
197
- nanoid: nanoid()
198
- }),
199
- actor: this.#botActor(bot),
200
188
  object,
201
189
  target: root.thread,
202
190
  ...recipients
203
- }))
191
+ })
204
192
  }
205
193
  }
206
194
  }
@@ -291,33 +279,20 @@ export class ActivityHandler {
291
279
  'Sending accept',
292
280
  { actor: actor.id }
293
281
  )
294
- const addActivityId = this.#formatter.format({
295
- username: bot.username,
296
- type: 'add',
297
- nanoid: nanoid()
298
- })
299
- await this.#doActivity(bot, await as2.import({
300
- id: addActivityId,
282
+ await this.#doActivity(bot, {
301
283
  type: 'Add',
302
- actor: await this.#botActor(bot),
303
284
  object: actor,
304
285
  target: this.#formatter.format({
305
286
  username: bot.username,
306
287
  collection: 'followers'
307
288
  }),
308
289
  to: ['as:Public', actor.id]
309
- }))
310
- await this.#doActivity(bot, await as2.import({
311
- id: this.#formatter.format({
312
- username: bot.username,
313
- type: 'accept',
314
- nanoid: nanoid()
315
- }),
290
+ })
291
+ await this.#doActivity(bot, {
316
292
  type: 'Accept',
317
- actor: this.#formatter.format({ username: bot.username }),
318
293
  object: activity,
319
294
  to: actor
320
- }))
295
+ })
321
296
  this.#logger.debug({
322
297
  msg: 'Notifying bot of new follow',
323
298
  actor: actor.id
@@ -379,21 +354,15 @@ export class ActivityHandler {
379
354
  'pendingFollowing',
380
355
  followActivity
381
356
  )
382
- await this.#doActivity(bot, await as2.import({
383
- id: this.#formatter.format({
384
- username: bot.username,
385
- type: 'add',
386
- nanoid: nanoid()
387
- }),
357
+ await this.#doActivity(bot, {
388
358
  type: 'Add',
389
- actor: await this.#botActor(bot),
390
359
  object: actor,
391
360
  target: this.#formatter.format({
392
361
  username: bot.username,
393
362
  collection: 'following'
394
363
  }),
395
364
  to: ['as:Public', actor.id]
396
- }))
365
+ })
397
366
  }
398
367
 
399
368
  async #handleReject (bot, activity) {
@@ -510,21 +479,15 @@ export class ActivityHandler {
510
479
  await this.#objectStorage.addToCollection(object.id, 'likers', actor)
511
480
  const recipients = this.#getRecipients(object)
512
481
  this.#addRecipient(recipients, actor, 'to')
513
- await this.#doActivity(bot, await as2.import({
482
+ await this.#doActivity(bot, {
514
483
  type: 'Add',
515
- id: this.#formatter.format({
516
- username: bot.username,
517
- type: 'add',
518
- nanoid: nanoid()
519
- }),
520
- actor: await this.#botActor(bot),
521
484
  object: activity,
522
485
  target: this.#formatter.format({
523
486
  username: bot.username,
524
487
  collection: 'likes'
525
488
  }),
526
489
  ...recipients
527
- }))
490
+ })
528
491
  await bot.onLike(object, activity)
529
492
  }
530
493
 
@@ -587,21 +550,15 @@ export class ActivityHandler {
587
550
  await this.#objectStorage.addToCollection(object.id, 'sharers', actor)
588
551
  const recipients = this.#getRecipients(object)
589
552
  this.#addRecipient(recipients, actor, 'to')
590
- await this.#doActivity(bot, await as2.import({
553
+ await this.#doActivity(bot, {
591
554
  type: 'Add',
592
- id: this.#formatter.format({
593
- username: bot.username,
594
- type: 'add',
595
- nanoid: nanoid()
596
- }),
597
- actor: await this.#botActor(bot),
598
555
  object: activity,
599
556
  target: this.#formatter.format({
600
557
  username: bot.username,
601
558
  collection: 'shares'
602
559
  }),
603
560
  ...recipients
604
- }))
561
+ })
605
562
  try {
606
563
  await bot.onAnnounce(object, activity)
607
564
  } catch (err) {
@@ -878,13 +835,38 @@ export class ActivityHandler {
878
835
  }
879
836
  }
880
837
 
881
- async #doActivity (bot, activity) {
838
+ async #doActivity (bot, activityData) {
839
+ const now = new Date().toISOString()
840
+ const activity = await as2.import({
841
+ ...activityData,
842
+ id: this.#formatActivityId(bot, activityData.type),
843
+ actor: await this.#botActor(bot),
844
+ published: now,
845
+ updated: now
846
+ })
882
847
  await this.#objectStorage.create(activity)
883
848
  await this.#actorStorage.addToCollection(bot.username, 'outbox', activity)
884
849
  await this.#actorStorage.addToCollection(bot.username, 'inbox', activity)
885
850
  await this.#distributor.distribute(activity, bot.username)
886
851
  }
887
852
 
853
+ #formatActivityId (bot, type) {
854
+ const typeName = this.#activityTypeName(type)
855
+ return this.#formatter.format({
856
+ username: bot.username,
857
+ type: typeName,
858
+ nanoid: nanoid()
859
+ })
860
+ }
861
+
862
+ #activityTypeName (type) {
863
+ if (!type || typeof type !== 'string') {
864
+ return 'activity'
865
+ }
866
+ const parts = type.split(/[\/#]/)
867
+ return parts[parts.length - 1].toLowerCase()
868
+ }
869
+
888
870
  #getActor (activity) {
889
871
  return activity.actor?.first
890
872
  }
package/lib/authorizer.js CHANGED
@@ -1,3 +1,5 @@
1
+ import as2 from './activitystreams.js'
2
+
1
3
  export class Authorizer {
2
4
  #PUBLIC = 'https://www.w3.org/ns/activitystreams#Public'
3
5
  #actorStorage = null
@@ -96,6 +98,11 @@ export class Authorizer {
96
98
  return object.actor.first
97
99
  } else if (object.owner) {
98
100
  return object.owner.first
101
+ } else if (this.#formatter.isLocal(object.id)) {
102
+ const parts = this.#formatter.unformat(object.id)
103
+ return as2.import({
104
+ id: this.#formatter.format({username: parts.username})
105
+ })
99
106
  } else {
100
107
  return null
101
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evanp/activitypub-bot",
3
- "version": "0.13.12",
3
+ "version": "0.13.13",
4
4
  "description": "server-side ActivityPub bot framework",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",