-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathactivitypub-notifier.js
97 lines (82 loc) · 2.03 KB
/
activitypub-notifier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- mode: js; js-indent-level:2; -*-
SPDX-License-Identifier: MPL-2.0 */
/**
* Activitypub-notifier.js - ActivityPub notifier.
*/
const manifest = require('./manifest.json');
const mastodon = require('mastodon-lite');
const {
Constants,
Database,
Notifier,
Outlet
} = require('gateway-addon');
const config = {
access_token: null,
api: '/api/v1',
hostname: 'mastodon.social',
port: 443,
rejectUnauthorized: false
};
function createTransport() {
return mastodon(config);
}
function message() {
return {
name: 'message',
metadata: {
type: 'string'
}
};
}
function post(message) {
const transport = createTransport();
return new Promise(function(resolve, reject) {
transport.post(message, (err, response) => {
if (err || !response) {
reject(err);
} else {
resolve(response);
}
});
});
}
class ActivityPubOutlet extends Outlet {
constructor(notifier, id) {
super(notifier, id);
this.name = 'ActivityPub Poster';
}
async notify(title, message, level) {
console.log(`Posting message "${message}", and level "${level}"`);
switch (level) {
case Constants.NotificationLevel.LOW:
case Constants.NotificationLevel.NORMAL:
title = `(NOTICE) ${title}`;
break;
case Constants.NotificationLevel.HIGH:
title = `(ALERT) ${title}`;
break;
}
await post(message);
}
}
/**
* ActivityPub Sender Notifier
* Instantiates one activitypub poster outlet
*/
class ActivityPubNotifier extends Notifier {
constructor(addonManager) {
super(addonManager, 'activitypub-sender', manifest.id);
addonManager.addNotifier(this);
const db = new Database(manifest.id);
db.open().then(() => db.loadConfig()).
then((cfg) => {
Object.assign(config, cfg);
if (!this.outlets['activitypub-sender-0']) {
this.handleOutletAdded(new ActivityPubOutlet(this, 'activitypub-sender-0'));
}
}).
catch(console.error);
}
}
module.exports = ActivityPubNotifier;