-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
105 lines (85 loc) · 2.38 KB
/
main.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
98
99
100
101
102
103
104
105
const fetch = require('node-fetch');
const { computeSignature } = require('./lib/hmac.js');
// TODO: how to get this from Peertube?
const VideoState = {
PUBLISHED: 1,
TO_TRANSCODE: 2,
TO_IMPORT: 3,
WAITING_FOR_LIVE: 4,
LIVE_ENDED: 5
};
let WH_SECRET;
let WH_ENDPOINT;
async function register ({
registerHook,
registerSetting,
settingsManager,
}) {
registerSetting({
name: 'webhook-endpoint',
label: 'Waasabi webhook URL',
type: 'input',
// Optional
descriptionHTML: 'The URL of the Waasabi server webhook endpoint where the '
+'plugin needs to report the state changes.',
default: 'https://',
private: true
});
registerSetting({
name: 'webhook-secret',
label: 'Waasabi webhook secret',
type: 'input-password',
// Optional
descriptionHTML: 'This field stores the shared secret that is used to encode '
+'and verify the webhook calls sent to the Waasabi instance',
default: '',
private: true
});
// Need the shared webhook secret to initialize
const settings = await settingsManager.getSettings(['webhook-endpoint', 'webhook-secret']);
WH_ENDPOINT = settings['webhook-endpoint'];
WH_SECRET = settings['webhook-secret'];
// Listen to live video status changes
registerHook({
target: 'action:api.live-video.statechange',
handler: statechangeHandler,
});
}
async function unregister () {
}
async function statechangeHandler({ video }) {
const { id, uuid, name, description, url, state } = video.dataValues;
let event = '';
if (state == VideoState.WAITING_FOR_LIVE) {
event = 'live-now';
}
// TODO: permalives never switch to LIVE_ENDED
if (state == VideoState.LIVE_ENDED) {
event = 'ended';
}
const data = {
"type": "livestream",
"event": event,
"sender": "peertube",
video: {
id, uuid, name, description, url, state,
},
};
const payload = JSON.stringify({ event: 'livestream.'+event, data });
const payloadHmac = computeSignature(payload, WH_SECRET);
const res = await fetch(WH_ENDPOINT, {
method: 'post',
headers: {
'content-type': 'application/json',
'hmac': payloadHmac,
},
body: payload,
});
if (!res.ok) {
return console.error('[peertube-plugin-waasabi] Request to backend for `streamStarted` failed: HTTP/'+res.status);
}
}
module.exports = {
register,
unregister
}