-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
150 lines (112 loc) · 3.62 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
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
require('dotenv').config();
if (!process.env.FCM_API_KEY || !process.env.VAPID_EMAIL || !process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
return console.error('❗ Failed to load in the environment variables. Are they missing from the `.env` file?');
}
const subscriptions = {};
function messageValidator() {
const TITLE_MAX_LENGTH = 31;
for (messageKey in messages) {
const message = messages[messageKey];
if (message.title.length > 31) {
console.log(`[${messageKey}] Title is too big (${message.title.length}), more than ${TITLE_MAX_LENGTH}.`)
}
}
}
const passages = require('./twison.json').passages;
const webPush = require('web-push');
const express = require('express');
const bodyParser = require('body-parser');
webPush.setGCMAPIKey(process.env.FCM_API_KEY);
webPush.setVapidDetails(
`mailto:${process.env.VAPID_EMAIL}`,
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
const app = express();
const jsonParser = bodyParser.json();
// Static files
app.use(express.static('.'));
app.post('/subscription', jsonParser, (req, res) => {
const { subscription } = req.body;
// check if already subscribed
const userSubscription = subscriptions[subscription.endpoint];
if (!userSubscription) {
// initialise the first messages
subscription.currentMessage = '1';
// add new subscription
subscriptions[subscription.endpoint] = subscription;
// send the first message
sendMessage(subscription);
}
res.sendStatus(201);
});
app.delete('/subscription', jsonParser, (req, res) => {
const { subscription } = req.body;
if (!subscriptions[subscription.endpoint]) {
return res.sendStatus(404);
}
// remove subscription from array
delete subscriptions[subscription.endpoint];
// A real world application would store the subscription info.
// we'd stick this data into subscriptions
res.sendStatus(201);
});
app.post('/action', jsonParser, (req, res) => {
const { action, endpoint } = req.body;
const subscription = subscriptions[endpoint];
if (!subscription) {
return res.sendStatus(404);
}
// change currentMessage
subscriptions[endpoint].currentMessage = action;
sendMessage(subscription);
res.sendStatus(201);
});
function sendMessage(subscription) {
const passage = passages.find(passage => passage.pid === subscription.currentMessage);
if (!passage) {
return;
}
let actions = '';
let bodActions = '';
if (passage.links && passage.links.length > 0) {
actions = passage.links.map((link, i) => {
if (link.name === 'Start again') {
return { action: link.pid, title: 'Start again' };
}
if (link.name === link.link) {
return { action: link.pid, title: 'Continue' };
}
return { action: link.pid, title: `Choose: ${i+1}` };
});
bodyActions = passage.links.map((link, i) => {
if (link.name === 'Start again' || link.name === link.link) {
return '';
}
return `${i+1}: ${link.name}`;
});
}
const bodyText = passage.text.replace(/\[\[.+\]\]/g, '').trim(); // remove twine links
const body = `${bodyText}\n\n${bodyActions.join('\n')}`.trim();
let icon = '';
if (passage.tags && passage.tags.length > 0) {
icon = `images/${passage.tags[0]}.png`;
}
const data = {
tag: subscription.endpoint,
title: '',
body,
actions,
icon,
vibrate: [0],
// badge: ''
};
webPush.sendNotification(subscription, JSON.stringify(data));
}
const port = process.env.PORT || 3000;
app.listen(port, (err) => {
if (err) {
return console.error(err);
}
console.info(`🌐 Listening at http://localhost:${port}/`);
});