@evanp/activitypub-bot 0.16.3 → 0.16.5

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/app.js CHANGED
@@ -82,10 +82,11 @@ export async function makeApp (databaseUrl, origin, bots, logLevel = 'silent') {
82
82
 
83
83
  // TODO: Make an endpoint for tagged objects
84
84
  const transformer = new Transformer(origin + '/tag/', client)
85
+
85
86
  await Promise.all(
86
- Object.values(bots).map(bot => bot.initialize(
87
+ Object.entries(bots).map(([key, bot]) => bot.initialize(
87
88
  new BotContext(
88
- bot.username,
89
+ key,
89
90
  botDataStorage,
90
91
  objectStorage,
91
92
  actorStorage,
package/lib/botcontext.js CHANGED
@@ -214,27 +214,11 @@ export class BotContext {
214
214
  async unlikeObject (obj) {
215
215
  assert.ok(obj)
216
216
  assert.equal(typeof obj, 'object')
217
- const owners = obj.attributedTo
218
- ? Array.from(obj.attributedTo).map((owner) => owner.id)
219
- : Array.from(obj.actor).map((owner) => owner.id)
220
217
  if (!(await this.#actorStorage.isInCollection(this.#botId, 'liked', obj))) {
221
218
  throw new Error(`not already liked: ${obj.id} by ${this.#botId}`)
222
219
  }
223
- const likeActivity = this.#actorStorage.getLastActivity(
224
- this.#botId,
225
- 'Like',
226
- obj
227
- )
228
- if (!likeActivity) {
229
- throw new Error('no like activity')
230
- }
231
220
  await this.#actorStorage.removeFromCollection(this.#botId, 'liked', obj)
232
- return await this.#doActivity({
233
- type: 'Undo',
234
- object: likeActivity,
235
- to: owners,
236
- cc: 'https://www.w3.org/ns/activitystreams#Public'
237
- })
221
+ return await this.#undoActivity('Like', obj)
238
222
  }
239
223
 
240
224
  async followActor (actor) {
@@ -257,14 +241,6 @@ export class BotContext {
257
241
  async unfollowActor (actor) {
258
242
  assert.ok(actor)
259
243
  assert.equal(typeof actor, 'object')
260
- const followActivity = this.#actorStorage.getLastActivity(
261
- this.#botId,
262
- 'Follow',
263
- actor
264
- )
265
- if (!followActivity) {
266
- throw new Error('no follow activity')
267
- }
268
244
  await this.#actorStorage.removeFromCollection(
269
245
  this.#botId,
270
246
  'pendingFollowing',
@@ -275,11 +251,7 @@ export class BotContext {
275
251
  'following',
276
252
  actor
277
253
  )
278
- return await this.#doActivity({
279
- type: 'Undo',
280
- object: followActivity,
281
- to: actor.id
282
- })
254
+ return await this.#undoActivity('Follow', actor)
283
255
  }
284
256
 
285
257
  async blockActor (actor) {
@@ -305,23 +277,8 @@ export class BotContext {
305
277
  async unblockActor (actor) {
306
278
  assert.ok(actor)
307
279
  assert.equal(typeof actor, 'object')
308
- const blockActivity = this.#actorStorage.getLastActivity(
309
- this.#botId,
310
- 'Block',
311
- actor
312
- )
313
- if (!blockActivity) {
314
- throw new Error('no block activity')
315
- }
316
- await this.#actorStorage.removeFromCollection(
317
- this.#botId,
318
- 'blocked',
319
- actor
320
- )
321
- return await this.#doActivity({
322
- type: 'Undo',
323
- object: blockActivity
324
- }, false)
280
+ await this.#actorStorage.removeFromCollection(this.#botId, 'blocked', actor)
281
+ return await this.#undoActivity('Block', actor, false)
325
282
  }
326
283
 
327
284
  async updateNote (note, content) {
@@ -434,23 +391,7 @@ export class BotContext {
434
391
  async unannounceObject (obj) {
435
392
  assert.ok(obj)
436
393
  assert.equal(typeof obj, 'object')
437
- const announceActivity = await this.#actorStorage.getLastActivity(
438
- this.#botId,
439
- 'Announce',
440
- obj
441
- )
442
- if (!announceActivity) {
443
- throw new Error(`No matching announce activity for ${obj.id}`)
444
- }
445
- const recipients = this.#getRecipients(announceActivity)
446
- return await this.#doActivity({
447
- type: 'Undo',
448
- summary: {
449
- en: `${this.#botId} shared "${await this.#nameOf(obj)}"`
450
- },
451
- object: announceActivity,
452
- ...recipients
453
- })
394
+ return await this.#undoActivity('Announce', obj)
454
395
  }
455
396
 
456
397
  async #findInOutbox (type, obj) {
@@ -469,6 +410,8 @@ export class BotContext {
469
410
  }
470
411
 
471
412
  #getRecipients (obj) {
413
+ assert.ok(obj)
414
+ assert.strictEqual(typeof obj, 'object', 'obj must be an object')
472
415
  const to = obj.to ? Array.from(obj.to).map((to) => to.id) : null
473
416
  const cc = obj.cc ? Array.from(obj.cc).map((cc) => cc.id) : null
474
417
  const bto = obj.bto ? Array.from(obj.bto).map((bto) => bto.id) : null
@@ -518,6 +461,25 @@ export class BotContext {
518
461
  return activity
519
462
  }
520
463
 
464
+ async #undoActivity (type, obj, distribute = true) {
465
+ const originalId = await this.#actorStorage.getLastActivity(
466
+ this.#botId,
467
+ type,
468
+ obj
469
+ )
470
+ if (!originalId) {
471
+ throw new Error(`no ${type} activity for ${obj.id}`)
472
+ }
473
+ const originalActivity = await this.#objectStorage.read(originalId)
474
+ assert.ok(originalActivity)
475
+ const recipients = this.#getRecipients(originalActivity)
476
+ return await this.#doActivity({
477
+ type: 'Undo',
478
+ object: originalId,
479
+ ...recipients
480
+ }, distribute)
481
+ }
482
+
521
483
  async onIdle () {
522
484
  await this.#distributor.onIdle()
523
485
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evanp/activitypub-bot",
3
- "version": "0.16.3",
3
+ "version": "0.16.5",
4
4
  "description": "server-side ActivityPub bot framework",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",