@evanp/activitypub-bot 0.23.0 → 0.24.1

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.
@@ -102,7 +102,9 @@ export class ActivityPubClient {
102
102
  assert.equal(typeof obj, 'object')
103
103
  assert.ok(username)
104
104
  assert.equal(typeof username, 'string')
105
- const body = await obj.write()
105
+ const json = await obj.export()
106
+ this.#fixupJson(json)
107
+ const body = JSON.stringify(json)
106
108
  const headers = {
107
109
  date: new Date().toUTCString(),
108
110
  'user-agent': ActivityPubClient.#userAgent,
@@ -174,4 +176,30 @@ export class ActivityPubClient {
174
176
  }
175
177
  }
176
178
  }
179
+
180
+ #fixupJson (json) {
181
+ this.#fixupRelayFollow(json)
182
+ this.#fixupRelayUndoFollow(json)
183
+ }
184
+
185
+ #fixupRelayFollow (json) {
186
+ if (typeof json.type === 'string' &&
187
+ json.type === 'Follow' &&
188
+ typeof json.object === 'string' &&
189
+ json.object === 'as:Public') {
190
+ json.object = 'https://www.w3.org/ns/activitystreams#Public'
191
+ }
192
+ }
193
+
194
+ #fixupRelayUndoFollow (json) {
195
+ if (typeof json.type === 'string' &&
196
+ json.type === 'Undo' &&
197
+ typeof json.object === 'object' &&
198
+ typeof json.object.type === 'string' &&
199
+ json.object.type === 'Follow' &&
200
+ typeof json.object.object === 'string' &&
201
+ json.object.object === 'as:Public') {
202
+ json.object.object = 'https://www.w3.org/ns/activitystreams#Public'
203
+ }
204
+ }
177
205
  }
@@ -2,10 +2,12 @@ import Bot from '../bot.js'
2
2
 
3
3
  export default class RelayClientBot extends Bot {
4
4
  #relay
5
+ #unsubscribe
5
6
 
6
- constructor (username, relay) {
7
+ constructor (username, relay, unsubscribe = null) {
7
8
  super(username)
8
9
  this.#relay = relay
10
+ this.#unsubscribe = !!unsubscribe
9
11
  }
10
12
 
11
13
  get fullname () {
@@ -18,21 +20,61 @@ export default class RelayClientBot extends Bot {
18
20
 
19
21
  async initialize (context) {
20
22
  super.initialize(context)
21
- if (!(await this._context.hasData(`follow:${this.#relay}`))) {
22
- await this.#followRelay()
23
+ this._context.logger.info(
24
+ { relay: this.#relay, unsubscribe: this.#unsubscribe },
25
+ 'Initialising relay client'
26
+ )
27
+ if (this.#unsubscribe) {
28
+ if (await this._context.hasData(`follow:${this.#relay}`)) {
29
+ await this.#unfollowRelay()
30
+ }
31
+ } else {
32
+ if (!(await this._context.hasData(`follow:${this.#relay}`))) {
33
+ await this.#followRelay()
34
+ }
23
35
  }
24
36
  }
25
37
 
26
38
  async #followRelay () {
39
+ this._context.logger.info(
40
+ { relay: this.#relay },
41
+ 'Following relay'
42
+ )
27
43
  const activity = await this._context.doActivity({
28
44
  to: this.#relay,
29
45
  type: 'Follow',
30
46
  object: 'https://www.w3.org/ns/activitystreams#Public'
31
47
  })
48
+ this._context.logger.info(
49
+ { relay: this.#relay, activity: activity.id },
50
+ 'Saving follow for later'
51
+ )
32
52
  this._context.setData(`follow:${this.#relay}`, activity.id)
33
53
  }
34
54
 
55
+ async #unfollowRelay () {
56
+ const activityId = await this._context.getData(`follow:${this.#relay}`)
57
+ this._context.logger.info(
58
+ { relay: this.#relay, activity: activityId },
59
+ 'Unfollowing relay'
60
+ )
61
+ await this._context.doActivity({
62
+ to: this.#relay,
63
+ type: 'Undo',
64
+ object: {
65
+ id: activityId,
66
+ type: 'Follow',
67
+ object: 'https://www.w3.org/ns/activitystreams#Public'
68
+ }
69
+ })
70
+ this._context.logger.info(
71
+ { relay: this.#relay },
72
+ 'Clearing follow data'
73
+ )
74
+ this._context.setData(`follow:${this.#relay}`, null)
75
+ }
76
+
35
77
  async actorOK (actorId, activity) {
36
- return (actorId === this.#relay)
78
+ return (actorId === this.#relay && !this.#unsubscribe)
37
79
  }
38
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evanp/activitypub-bot",
3
- "version": "0.23.0",
3
+ "version": "0.24.1",
4
4
  "description": "server-side ActivityPub bot framework",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",