-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe_notify.js
105 lines (95 loc) · 3.7 KB
/
subscribe_notify.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
import dotenv from 'dotenv';
dotenv.config();
const key = process.env.FCM_KEY;
import { readFileSync } from 'fs';
import { createServer } from 'https';
const privateKey = readFileSync('./privkey.pem', 'utf8');
const certificate = readFileSync('./fullchain.pem', 'utf8');
const options = { key: privateKey, cert: certificate };
import fetch from 'node-fetch';
import express from 'express';
const app = express();
const port = 6969;
app.get('/gwl/delete/:token', async function (req, res) {
const token = req.params.token;
log_events(`Delete request by: ${token}`)
let topics;
try {
// Get all topics the token is subscribed to, in order to then delete them one by one
let sub_topics = [];
let response = await fetch(`https://iid.googleapis.com/iid/info/${token}?details=true`, {
method: 'GET',
headers: { 'Content-Type': 'application/json', 'Authorization': "key=" + key }
});
response = await response.json();
topics = response.rel?.topics || [];
for (let topic in topics) sub_topics.push(topic);
if (response.error) {
log_events("ERROR retrieving subscribed topics: " + response.error.toString());
topics = "error";
};
} catch (e) {
topics = "error";
log_events("ERROR fetching subscribed topics: " + e.toString());
}
if (topics == "error") {
res.send({ success: false });
return;
}
// Delete all topics the token is subscribed to
try {
if (topics) {
for (let topic in topics) {
let responseDelete = await fetch(`https://iid.googleapis.com/iid/v1/${token}/rel/topics/${topic}`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json', 'Authorization': "key=" + key }
});
if (responseDelete.error) log_events("ERROR deleting topics: " + responseDelete.error.toString());
}
}
res.send({ success: true });
return;
} catch (e) {
log_events("ERROR fetch deleting topics: " + e.toString());
res.send({ success: false });
return;
}
});
app.get('/gwl/subscribe/:token/:address', async function (req, res) {
const token = req.params.token;
const address = req.params.address;
log_events(`Subscribe request by: ${token}`)
try {
// Subscribe the token to the topic (address)
let response = await fetch(`https://iid.googleapis.com/iid/v1/${token}/rel/topics/${address}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': "key=" + key }
});
response = await response.json();
if (response.error) {
log_events("ERROR subscribing to topic: " + response.error.toString());
res.send({ success: false });
return;
}
res.send({ success: true });
return;
} catch (e) {
log_events("ERROR fetch subscribing to topic: " + e.toString());
res.send({ success: false });
return;
}
});
function log_events(event) {
const dateObject = new Date();
const day = (`0${dateObject.getDate()}`).slice(-2);
const month = (`0${dateObject.getMonth() + 1}`).slice(-2);
const year = dateObject.getFullYear();
const hours = (`0${dateObject.getHours()}`).slice(-2);
const minutes = (`0${dateObject.getMinutes()}`).slice(-2);
const seconds = (`0${dateObject.getSeconds()}`).slice(-2);
console.log(`${year}-${month}-${day} ${hours}:${minutes}:${seconds} - ${event}`);
}
const httpsServer = createServer(options, app);
httpsServer.listen(port, () => {
log_events("Https server listing on port: " + port)
});