Skip to content

Commit 20cb3c2

Browse files
committed
Commit
1 parent 9a9d5ac commit 20cb3c2

15 files changed

Lines changed: 295 additions & 193 deletions

File tree

server-src/server.js

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var gvbbaseStorage = require("./storage.js"); //Supabase storage module.
1515
var cons = require("./constants.js");
1616
var bcrypt = require("bcryptjs");
1717
var crypto = require("crypto");
18-
var webpush = require('web-push');
18+
var webpush = require("web-push");
1919
var scratchCloudWss = require("./scratch-cloud.js");
2020
var WebRTCSignaler = require("./rtcsignal/req-handler.js");
2121
var botCheck = require("./bot-check-manager.js");
@@ -48,15 +48,17 @@ var WEBPUSH_ENABLED = true;
4848
var WEBPUSH_PUBLIC_KEY = process.env.publicPushKey;
4949
var WEBPUSH_PRIVATE_KEY = process.env.privatePushKey;
5050
if (!WEBPUSH_PUBLIC_KEY || !WEBPUSH_PRIVATE_KEY) {
51-
console.warn("[WebPush]: No web-push keys or missing one, invite notifications won't work anymore.");
51+
console.warn(
52+
"[WebPush]: No web-push keys or missing one, invite notifications won't work anymore.",
53+
);
5254
WEBPUSH_ENABLED = false;
5355
}
5456

5557
if (WEBPUSH_ENABLED) {
5658
webpush.setVapidDetails(
5759
process.env.website || cons.DEFAULT_WEBSITE,
5860
WEBPUSH_PUBLIC_KEY,
59-
WEBPUSH_PRIVATE_KEY
61+
WEBPUSH_PRIVATE_KEY,
6062
);
6163
}
6264

@@ -2656,11 +2658,13 @@ async function registerPushNotifications(username, newSubscription) {
26562658
// 1. Try to download existing subs
26572659
const buffer = await storage.downloadFile(filename, false);
26582660
subscriptions = JSON.parse(buffer.toString());
2659-
2661+
26602662
// 2. Prevent duplicates (check if endpoint already exists)
2661-
const exists = subscriptions.find(s => s.endpoint === newSubscription.endpoint);
2663+
const exists = subscriptions.find(
2664+
(s) => s.endpoint === newSubscription.endpoint,
2665+
);
26622666
if (!exists) {
2663-
subscriptions.push(newSubscription);
2667+
subscriptions.push(newSubscription);
26642668
}
26652669
subscriptions = subscriptions.slice(-cons.MAX_PUSH_SUBSCRIPTIONS);
26662670
} catch (err) {
@@ -2669,7 +2673,11 @@ async function registerPushNotifications(username, newSubscription) {
26692673
}
26702674

26712675
// 3. Upload the updated list
2672-
await storage.uploadFile(filename, JSON.stringify(subscriptions), "application/json");
2676+
await storage.uploadFile(
2677+
filename,
2678+
JSON.stringify(subscriptions),
2679+
"application/json",
2680+
);
26732681
}
26742682

26752683
async function removeSubscriptionFromFile(username, endpointToRemove) {
@@ -2682,17 +2690,17 @@ async function removeSubscriptionFromFile(username, endpointToRemove) {
26822690

26832691
// 2. Filter out the specific endpoint
26842692
const updatedSubscriptions = subscriptions.filter(
2685-
(sub) => sub.endpoint !== endpointToRemove
2693+
(sub) => sub.endpoint !== endpointToRemove,
26862694
);
26872695

26882696
// 3. If no subscriptions are left, delete the file; otherwise, update it
26892697
if (updatedSubscriptions.length === 0) {
26902698
await storage.deleteFile(filename);
26912699
} else {
26922700
await storage.uploadFile(
2693-
filename,
2694-
JSON.stringify(updatedSubscriptions),
2695-
"application/json"
2701+
filename,
2702+
JSON.stringify(updatedSubscriptions),
2703+
"application/json",
26962704
);
26972705
}
26982706
} catch (err) {
@@ -2701,13 +2709,13 @@ async function removeSubscriptionFromFile(username, endpointToRemove) {
27012709
}
27022710

27032711
async function getSubscriptions(username) {
2704-
try{
2712+
try {
27052713
const filename = `push_sub_${username.toLowerCase()}.json`;
27062714
const buffer = await storage.downloadFile(filename, false);
27072715
let subscriptions = JSON.parse(buffer.toString());
27082716

27092717
return subscriptions;
2710-
}catch(e){
2718+
} catch (e) {
27112719
return;
27122720
}
27132721
}
@@ -2863,7 +2871,6 @@ const server = http.createServer(async function (req, res) {
28632871
}
28642872

28652873
if (urlsplit[2] == "subscribe" && req.method == "POST") {
2866-
28672874
if (!decryptedUserdata) {
28682875
res.statusCode = 401;
28692876
res.end("No user cookie.");
@@ -2876,30 +2883,31 @@ const server = http.createServer(async function (req, res) {
28762883
res.end("User cookie is broken/expired.");
28772884
return;
28782885
}
2879-
try{
2886+
try {
28802887
var jsonText = await waitForBody(req);
2881-
}catch(e){return;}
2882-
try{
2888+
} catch (e) {
2889+
return;
2890+
}
2891+
try {
28832892
var json = JSON.parse(jsonText.toString());
2884-
}catch(e){
2893+
} catch (e) {
28852894
res.statusCode = 401;
28862895
res.end("JSON content is invalid.");
28872896
return;
28882897
}
2889-
2890-
try{
2898+
2899+
try {
28912900
await registerPushNotifications(decryptedUserdata.username, json);
28922901
res.end("");
28932902
return;
2894-
}catch(e){
2903+
} catch (e) {
28952904
res.statusCode = 401;
28962905
res.end("Unable to register.");
28972906
return;
28982907
}
28992908
}
29002909

29012910
if (urlsplit[2] == "unsubscribe" && req.method == "POST") {
2902-
29032911
if (!decryptedUserdata) {
29042912
res.statusCode = 401;
29052913
res.end("No user cookie.");
@@ -2912,12 +2920,14 @@ const server = http.createServer(async function (req, res) {
29122920
res.end("User cookie is broken/expired.");
29132921
return;
29142922
}
2915-
try{
2923+
try {
29162924
var jsonText = await waitForBody(req);
2917-
}catch(e){return;}
2918-
try{
2925+
} catch (e) {
2926+
return;
2927+
}
2928+
try {
29192929
var json = JSON.parse(jsonText.toString());
2920-
}catch(e){
2930+
} catch (e) {
29212931
res.statusCode = 401;
29222932
res.end("JSON content is invalid.");
29232933
return;
@@ -2929,16 +2939,18 @@ const server = http.createServer(async function (req, res) {
29292939
return;
29302940
}
29312941

2932-
try{
2933-
await removeSubscriptionFromFile(decryptedUserdata.username, json.endpoint);
2942+
try {
2943+
await removeSubscriptionFromFile(
2944+
decryptedUserdata.username,
2945+
json.endpoint,
2946+
);
29342947
res.end("");
29352948
return;
2936-
}catch(e){
2949+
} catch (e) {
29372950
res.statusCode = 401;
29382951
res.end("Unable to deregister.");
29392952
return;
29402953
}
2941-
29422954
}
29432955
}
29442956

src/accounthelper/index.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,30 @@ async function getPublicKey() {
154154
}
155155

156156
async function uploadPushSubscription(subscription) {
157-
var response = await fetch(getServerURL() + '/webpush/subscribe', {
158-
method: 'POST',
159-
body: JSON.stringify(subscription),
160-
headers: { 'Content-Type': 'application/json' }
161-
});
162-
if (!response.ok) {
163-
throw new Error("Issue when sending subscription. Error: " + await response.text());
164-
}
157+
var response = await fetch(getServerURL() + "/webpush/subscribe", {
158+
method: "POST",
159+
body: JSON.stringify(subscription),
160+
headers: { "Content-Type": "application/json" },
161+
});
162+
if (!response.ok) {
163+
throw new Error(
164+
"Issue when sending subscription. Error: " + (await response.text()),
165+
);
166+
}
165167
}
166168

167169
async function uploadRemovePushSubscription(subscription) {
168-
var response = await fetch(getServerURL() + '/webpush/unsubscribe', {
169-
method: 'POST',
170-
body: JSON.stringify(subscription),
171-
headers: { 'Content-Type': 'application/json' }
172-
});
173-
if (!response.ok) {
174-
throw new Error("Issue when sending to remove subscription. Error: " + await response.text());
175-
}
170+
var response = await fetch(getServerURL() + "/webpush/unsubscribe", {
171+
method: "POST",
172+
body: JSON.stringify(subscription),
173+
headers: { "Content-Type": "application/json" },
174+
});
175+
if (!response.ok) {
176+
throw new Error(
177+
"Issue when sending to remove subscription. Error: " +
178+
(await response.text()),
179+
);
180+
}
176181
}
177182

178183
pushNotificationHelper.subscribe = async function (retry = false) {
@@ -209,5 +214,5 @@ module.exports = {
209214
getJoinedRooms,
210215
removeJoinedRoom,
211216
getPublicKey,
212-
pushNotificationHelper
217+
pushNotificationHelper,
213218
};

src/accounthelper/pushhelper.js

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,92 @@ var pushNotificationHelper = {};
44
var _registerResolves = [];
55
var _registerRejects = [];
66
(async function () {
7-
pushNotificationHelper.registered = false;
8-
pushNotificationHelper.attempting = false;
9-
pushNotificationHelper.supported = false;
10-
if (!('serviceWorker' in navigator)) return;
11-
pushNotificationHelper.supported = true;
12-
pushNotificationHelper.attempting = true;
13-
try{
14-
var registration = await navigator.serviceWorker.register('/sw.bundle.js');
15-
console.log('ServiceWorker Registered: ', registration);
16-
pushNotificationHelper.registered = true;
17-
}catch(e) {
18-
window.alert("ServiceWorker Couldn't Register: " + e);
7+
pushNotificationHelper.registered = false;
8+
pushNotificationHelper.attempting = false;
9+
pushNotificationHelper.supported = false;
10+
if (!("serviceWorker" in navigator)) return;
11+
pushNotificationHelper.supported = true;
12+
pushNotificationHelper.attempting = true;
13+
try {
14+
var registration = await navigator.serviceWorker.register("/sw.bundle.js");
15+
console.log("ServiceWorker Registered: ", registration);
16+
pushNotificationHelper.registered = true;
17+
} catch (e) {
18+
window.alert("ServiceWorker Couldn't Register: " + e);
19+
}
20+
if (pushNotificationHelper.registered) {
21+
pushNotificationHelper.registration = registration;
22+
for (var resolve of _registerResolves) {
23+
resolve(registration);
1924
}
20-
if (pushNotificationHelper.registered) {
21-
pushNotificationHelper.registration = registration;
22-
for (var resolve of _registerResolves) {
23-
resolve(registration);
24-
}
25-
} else {
26-
for (var rejects of _registerRejects) {
27-
rejects();
28-
}
25+
} else {
26+
for (var rejects of _registerRejects) {
27+
rejects();
2928
}
30-
_registerRejects = [];
31-
_registerResolves = [];
32-
pushNotificationHelper.attempting = false;
29+
}
30+
_registerRejects = [];
31+
_registerResolves = [];
32+
pushNotificationHelper.attempting = false;
3333
})();
3434

3535
pushNotificationHelper.register = function () {
36-
return new Promise((resolve, reject) => {
37-
if (pushNotificationHelper.attempting) {
38-
_registerRejects.push(resolve);
39-
_registerRejects.push(reject);
40-
} else {
41-
if (pushNotificationHelper.registration) {
42-
resolve(pushNotificationHelper.registration);
43-
} else {
44-
reject();
45-
}
46-
}
47-
});
48-
};
36+
return new Promise((resolve, reject) => {
37+
if (pushNotificationHelper.attempting) {
38+
_registerRejects.push(resolve);
39+
_registerRejects.push(reject);
40+
} else {
41+
if (pushNotificationHelper.registration) {
42+
resolve(pushNotificationHelper.registration);
43+
} else {
44+
reject();
45+
}
46+
}
47+
});
48+
};
4949

5050
pushNotificationHelper.getSubscription = async function () {
51-
var registration = pushNotificationHelper.registration;
52-
if (!registration) {
53-
return;
54-
}
55-
var subscription = await registration.pushManager.getSubscription();
56-
return subscription;
51+
var registration = pushNotificationHelper.registration;
52+
if (!registration) {
53+
return;
54+
}
55+
var subscription = await registration.pushManager.getSubscription();
56+
return subscription;
5757
};
5858

5959
pushNotificationHelper.__subscribe = async function (publicKey) {
60-
var registration = pushNotificationHelper.registration;
61-
if (!registration) {
62-
return;
63-
}
64-
var subscription = await registration.pushManager.subscribe({
65-
userVisibleOnly: true,
66-
applicationServerKey: publicKey
67-
});
68-
return subscription;
60+
var registration = pushNotificationHelper.registration;
61+
if (!registration) {
62+
return;
63+
}
64+
var subscription = await registration.pushManager.subscribe({
65+
userVisibleOnly: true,
66+
applicationServerKey: publicKey,
67+
});
68+
return subscription;
6969
};
7070

71-
pushNotificationHelper.subscribe = async () => {window.alert("pushNotificationHelper.subscribe needs to be overriden by accounthelper.");};
71+
pushNotificationHelper.subscribe = async () => {
72+
window.alert(
73+
"pushNotificationHelper.subscribe needs to be overriden by accounthelper.",
74+
);
75+
};
7276

7377
pushNotificationHelper.__unsubscribe = async function () {
74-
var registration = pushNotificationHelper.registration;
75-
if (!registration) {
76-
return;
77-
}
78-
var successful = await subscription.unsubscribe();
79-
if (!successful) {
80-
throw new Error("Unsubscribe was unsuccessful");
81-
}
78+
var registration = pushNotificationHelper.registration;
79+
if (!registration) {
80+
return;
81+
}
82+
var subscription = await pushNotificationHelper.getSubscription();
83+
var successful = await subscription.unsubscribe();
84+
if (!successful) {
85+
throw new Error("Unsubscribe was unsuccessful");
86+
}
8287
};
8388

84-
pushNotificationHelper.unsubscribe = async () => {window.alert("pushNotificationHelper.unsubscribe needs to be overriden by accounthelper.");};
89+
pushNotificationHelper.unsubscribe = async () => {
90+
window.alert(
91+
"pushNotificationHelper.unsubscribe needs to be overriden by accounthelper.",
92+
);
93+
};
8594

86-
module.exports = pushNotificationHelper;
95+
module.exports = pushNotificationHelper;

0 commit comments

Comments
 (0)