@evanp/activitypub-bot 0.24.0 → 0.24.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.
@@ -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
  }
@@ -1,5 +1,9 @@
1
1
  import Bot from '../bot.js'
2
2
 
3
+ const NS = 'https://www.w3.org/ns/activitystreams#'
4
+ const ACCEPT = `${NS}Accept`
5
+ const REJECT = `${NS}Reject`
6
+
3
7
  export default class RelayClientBot extends Bot {
4
8
  #relay
5
9
  #unsubscribe
@@ -20,7 +24,7 @@ export default class RelayClientBot extends Bot {
20
24
 
21
25
  async initialize (context) {
22
26
  super.initialize(context)
23
- this._context.logger.debug(
27
+ this._context.logger.info(
24
28
  { relay: this.#relay, unsubscribe: this.#unsubscribe },
25
29
  'Initialising relay client'
26
30
  )
@@ -35,8 +39,25 @@ export default class RelayClientBot extends Bot {
35
39
  }
36
40
  }
37
41
 
38
- async #followRelay () {
42
+ async handleActivity (activity) {
39
43
  this._context.logger.debug(
44
+ 'handling activity'
45
+ )
46
+ if (activity.type === ACCEPT) {
47
+ return await this.#handleAccept(activity)
48
+ } else if (activity.type === REJECT) {
49
+ return await this.#handleReject(activity)
50
+ } else {
51
+ return false
52
+ }
53
+ }
54
+
55
+ async actorOK (actorId, activity) {
56
+ return (actorId === this.#relay && !this.#unsubscribe)
57
+ }
58
+
59
+ async #followRelay () {
60
+ this._context.logger.info(
40
61
  { relay: this.#relay },
41
62
  'Following relay'
42
63
  )
@@ -45,7 +66,7 @@ export default class RelayClientBot extends Bot {
45
66
  type: 'Follow',
46
67
  object: 'https://www.w3.org/ns/activitystreams#Public'
47
68
  })
48
- this._context.logger.debug(
69
+ this._context.logger.info(
49
70
  { relay: this.#relay, activity: activity.id },
50
71
  'Saving follow for later'
51
72
  )
@@ -54,7 +75,7 @@ export default class RelayClientBot extends Bot {
54
75
 
55
76
  async #unfollowRelay () {
56
77
  const activityId = await this._context.getData(`follow:${this.#relay}`)
57
- this._context.logger.debug(
78
+ this._context.logger.info(
58
79
  { relay: this.#relay, activity: activityId },
59
80
  'Unfollowing relay'
60
81
  )
@@ -67,14 +88,36 @@ export default class RelayClientBot extends Bot {
67
88
  object: 'https://www.w3.org/ns/activitystreams#Public'
68
89
  }
69
90
  })
70
- this._context.logger.debug(
91
+ this._context.logger.info(
71
92
  { relay: this.#relay },
72
93
  'Clearing follow data'
73
94
  )
74
95
  this._context.setData(`follow:${this.#relay}`, null)
75
96
  }
76
97
 
77
- async actorOK (actorId, activity) {
78
- return (actorId === this.#relay && !this.#unsubscribe)
98
+ async #handleAccept (activity) {
99
+ const activityId = await this._context.getData(`follow:${this.#relay}`)
100
+ if (activity.object?.first?.id === activityId) {
101
+ this._context.logger.info(
102
+ { accept: activity.id, follow: activityId },
103
+ 'Follow accepted'
104
+ )
105
+ return true
106
+ } else {
107
+ return false
108
+ }
109
+ }
110
+
111
+ async #handleReject (activity) {
112
+ const activityId = await this._context.getData(`follow:${this.#relay}`)
113
+ if (activity.object?.first?.id === activityId) {
114
+ this._context.logger.info(
115
+ { accept: activity.id, follow: activityId },
116
+ 'Follow rejected'
117
+ )
118
+ return true
119
+ } else {
120
+ return false
121
+ }
79
122
  }
80
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evanp/activitypub-bot",
3
- "version": "0.24.0",
3
+ "version": "0.24.2",
4
4
  "description": "server-side ActivityPub bot framework",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",