-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextension.js
299 lines (265 loc) · 8.88 KB
/
extension.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"use strict";
const fetch = require('node-fetch')
let crypto;
let WEBHOOK_MODE = true
try {
crypto = require('node:crypto');
} catch (err) {
nodecg.log.error('ERROR: crypto support is disabled! webhook message authenticity cannot be validated. try a newer node version.');
WEBHOOK_MODE = false;
}
module.exports = function (nodecg) {
const app = nodecg.Router();
var donationsRep = nodecg.Replicant("donations");
var allDonationsRep = nodecg.Replicant("alldonations");
var campaignTotalRep = nodecg.Replicant("total");
var pollsRep = nodecg.Replicant("donationpolls");
var scheduleRep = nodecg.Replicant("schedule");
var targetsRep = nodecg.Replicant("targets");
var rewardsRep = nodecg.Replicant("rewards");
var milestonesRep = nodecg.Replicant("milestones");
var donorsRep = nodecg.Replicant("donors");
var TiltifyClient = require("tiltify-api-client");
function isEmpty(string) {
return string === undefined || string === null || string === ""
}
if (isEmpty(nodecg.bundleConfig.tiltify_webhook_secret) || isEmpty(nodecg.bundleConfig.tiltify_webhook_id)) {
WEBHOOK_MODE = false
nodecg.log.info("Running without webhooks!! Please set webhook secret, and webhook id in cfg/nodecg-tiltify.json [See README]");
return;
}
if (isEmpty(nodecg.bundleConfig.tiltify_client_id)) {
nodecg.log.info("Please set tiltify_client_id in cfg/nodecg-tiltify.json");
return;
}
if (isEmpty(nodecg.bundleConfig.tiltify_client_secret)) {
nodecg.log.info("Please set tiltify_client_secret in cfg/nodecg-tiltify.json");
return;
}
if (isEmpty(nodecg.bundleConfig.tiltify_campaign_id)) {
nodecg.log.info(
"Please set tiltify_campaign_id in cfg/nodecg-tiltify.json"
);
return;
}
var client = new TiltifyClient(nodecg.bundleConfig.tiltify_client_id, nodecg.bundleConfig.tiltify_client_secret);
function pushUniqueDonation(donation) {
var found = donationsRep.value.find(function (element) {
return element.id === donation.id;
});
if (found === undefined) {
donation.shown = false;
donation.read = false;
donationsRep.value.push(donation);
}
}
function updateTotal(campaign) {
// Less than check in case webhooks are sent out-of-order. Only update the total if it's higher!
if (campaignTotalRep.value < parseFloat(campaign.amount_raised.value)
) {
campaignTotalRep.value = parseFloat(campaign.amount_raised.value);
}
}
/**
* Verifies that the payload delivered matches the signature provided, using sha256 algorithm and the webhook secret
* Acts as middleware, use in route chain
*/
function validateSignature(req, res, next) {
const signatureIn = req.get('X-Tiltify-Signature')
const timestamp = req.get('X-Tiltify-Timestamp')
const signedPayload = `${timestamp}.${JSON.stringify(req.body)}`
const hmac = crypto.createHmac('sha256', nodecg.bundleConfig.tiltify_webhook_secret);
hmac.update(signedPayload);
const signature = hmac.digest('base64');
if (signatureIn === signature) {
next()
} else {
// Close connection (200 code MUST be sent regardless)
res.sendStatus(200)
};
}
app.post('/nodecg-tiltify/webhook', validateSignature, (req, res) => {
// Verify this webhook is sending out stuff for the campaign we're working on
if (
req.body.meta.event_type === "public:direct:donation_updated" &&
req.body.data.campaign_id === nodecg.bundleConfig.tiltify_campaign_id
) {
// New donation
pushUniqueDonation(req.body.data)
} else if (
req.body.meta.event_type === "public:direct:fact_updated" &&
req.body.data.id === nodecg.bundleConfig.tiltify_campaign_id
) {
// Updated amount raised
updateTotal(req.body.data)
}
// Send ack
res.sendStatus(200)
})
async function askTiltifyForDonations() {
client.Campaigns.getRecentDonations(
nodecg.bundleConfig.tiltify_campaign_id,
function (donations) {
for (let i = 0; i < donations.length; i++) {
pushUniqueDonation(donations[i])
}
}
);
}
async function askTiltifyForAllDonations() {
client.Campaigns.getDonations(
nodecg.bundleConfig.tiltify_campaign_id,
function (alldonations) {
if (
JSON.stringify(allDonationsRep.value) !== JSON.stringify(alldonations)
) {
allDonationsRep.value = alldonations;
}
}
);
}
async function askTiltifyForPolls() {
client.Campaigns.getPolls(
nodecg.bundleConfig.tiltify_campaign_id,
function (polls) {
if (JSON.stringify(pollsRep.value) !== JSON.stringify(polls)) {
pollsRep.value = polls;
}
}
);
}
async function askTiltifyForSchedule() {
client.Campaigns.getSchedule(
nodecg.bundleConfig.tiltify_campaign_id,
function (schedule) {
if (JSON.stringify(scheduleRep.value) !== JSON.stringify(schedule)) {
scheduleRep.value = schedule;
}
}
);
}
async function askTiltifyForTargets() {
client.Campaigns.getTargets(
nodecg.bundleConfig.tiltify_campaign_id,
function (targets) {
if (
JSON.stringify(targetsRep.value) !== JSON.stringify(targets)
) {
targetsRep.value = targets;
}
}
);
}
async function askTiltifyForRewards() {
client.Campaigns.getRewards(
nodecg.bundleConfig.tiltify_campaign_id,
function (rewards) {
if (JSON.stringify(rewardsRep.value) !== JSON.stringify(rewards)) {
rewardsRep.value = rewards;
}
}
);
}
async function askTiltifyForMilestones() {
client.Campaigns.getMilestones(
nodecg.bundleConfig.tiltify_campaign_id,
function (milestones) {
if (JSON.stringify(milestonesRep.value) !== JSON.stringify(milestones)) {
milestonesRep.value = milestones;
}
}
);
}
async function askTiltifyForDonors() {
client.Campaigns.getDonors(
nodecg.bundleConfig.tiltify_campaign_id,
function (donors) {
console.log(donors);
if (JSON.stringify(donorsRep.value) !== JSON.stringify(donors)) {
donorsRep.value = donors;
}
}
);
}
async function askTiltifyForTotal() {
client.Campaigns.get(nodecg.bundleConfig.tiltify_campaign_id, function (
campaign
) {
updateTotal(campaign)
});
}
function askTiltify() {
// Donations and total are handled by websocket normally, only ask if not using websockets
if (!WEBHOOK_MODE) {
askTiltifyForDonations();
askTiltifyForTotal();
}
askTiltifyForPolls();
askTiltifyForTargets();
askTiltifyForSchedule();
askTiltifyForRewards();
askTiltifyForMilestones();
askTiltifyForDonors();
}
client.initialize().then(() => {
if (WEBHOOK_MODE) {
client.Webhook.activate(nodecg.bundleConfig.tiltify_webhook_id, () => {
nodecg.log.info('Webhooks staged!')
})
const events = { "event_types": ["public:direct:fact_updated", "public:direct:donation_updated"] }
client.Webhook.subscribe(nodecg.bundleConfig.tiltify_webhook_id, nodecg.bundleConfig.tiltify_campaign_id, events, () => {
nodecg.log.info('Webhooks activated!')
})
}
askTiltifyForTotal();
askTiltify();
askTiltifyForAllDonations();
setInterval(function () {
askTiltify();
}, WEBHOOK_MODE ? 120000 : 5000);
setInterval(function () {
askTiltifyForAllDonations();
}, 5 * 60000);
})
nodecg.listenFor("clear-donations", (value, ack) => {
for (let i = 0; i < donationsRep.value.length; i++) {
donationsRep.value[i].read = true;
}
if (ack && !ack.handled) {
ack(null, value);
}
});
nodecg.listenFor("mark-donation-as-read", (value, ack) => {
nodecg.log.info("Mark read", value.id)
var isElement = (element) => element.id === value.id;
var elementIndex = donationsRep.value.findIndex(isElement);
if (elementIndex !== -1) {
nodecg.log.info("Found", elementIndex, donationsRep.value[elementIndex])
donationsRep.value[elementIndex].read = true;
if (ack && !ack.handled) {
ack(null, null);
}
} else {
if (ack && !ack.handled) {
nodecg.log.error('Donation not found to mark as read | id:', value.id);
ack(new Error("Donation not found to mark as read"), null);
}
}
});
nodecg.listenFor("mark-donation-as-shown", (value, ack) => {
var isElement = (element) => element.id === value.id;
var elementIndex = donationsRep.value.findIndex(isElement);
if (elementIndex !== -1) {
donationsRep.value[elementIndex].shown = true;
if (ack && !ack.handled) {
ack(null, null);
}
} else {
if (ack && !ack.handled) {
nodecg.log.error('Donation not found to mark as shown | id:', value.id);
ack(new Error("Donation not found to mark as shown"), null);
}
}
});
nodecg.mount(app);
};