@evanp/activitypub-bot 0.23.0 → 0.24.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/bots/relayclient.js +46 -4
- package/package.json +1 -1
package/lib/bots/relayclient.js
CHANGED
|
@@ -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
|
-
|
|
22
|
-
|
|
23
|
+
this._context.logger.debug(
|
|
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.debug(
|
|
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.debug(
|
|
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.debug(
|
|
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.debug(
|
|
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
|
}
|