This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
168 lines (152 loc) · 5.64 KB
/
app.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
"use strict";
const express = require("express");
const socketIO = require("socket.io");
const push = require("web-push");
const uuid = require("uuid");
const cookieParser = require("cookie-parser");
let environment = process.env.NODE_ENV || "dev";
require("dotenv").config({ path: `.env.${environment}` });
const sql = require('./sql');
const PORT = process.env.PORT || 3000;
const PUBLIC_URL = process.env.PUBLIC_URL || "http://localhost:" + PORT;
const guid = () => uuid.v4();
const auth = (req, res, next) => {
if (!res.locals.user) {
return res.status(401).end();
}
next();
}
const server = express()
.use(express.static("public"))
.use(express.json())
.set("view engine", "ejs")
.use(cookieParser())
.get("/api/healthz/readiness", (req, res) => res.status(200).json({ status: "ok" }).end())
.use(async (req, res, next) => {
let dbUser = null;
if (!req.cookies.user) {
let userId = guid();
res.cookie("user", userId);
res.locals.userId = userId;
dbUser = { subscription: null, inviteCode: null, partnerId: null, _id: userId };
sql.addUser(dbUser);
} else {
res.locals.userId = req.cookies.user;
}
if (dbUser == null) {
dbUser = await sql.getUser(res.locals.userId);
if (dbUser == null) {
let userId = guid();
res.cookie("user", userId);
res.locals.userId = userId;
dbUser = { subscription: null, inviteCode: null, partnerId: null, _id: userId };
sql.addUser(dbUser);
}
}
res.locals.user = dbUser;
next()
})
.get('/api/user/invite-link', [auth, async (req, res) => {
let inviteCode = '';
if (!res.locals.user.inviteCode) {
inviteCode = Buffer.from(guid()).toString('base64');
let user = res.locals.user;
user.inviteCode = inviteCode;
await sql.updateUser(user);
}
else
inviteCode = res.locals.user.inviteCode;
let inviteLink = PUBLIC_URL + '/user/accept-invite/' + inviteCode;
res.json({ inviteLink: inviteLink, inviteCode: inviteCode }).end();
}])
.post('/api/accept-invite/:invitationCode', [auth, async (req, res) => {
let currentUser = res.locals.user;
console.log('hello,', currentUser)
let currentUserId = res.locals.userId;
let invitationCode = req.params.invitationCode;
if (currentUser.inviteCode == invitationCode) {
res.status(500).json({ status: 'error', message: 'invalid action. You can\'t invite yourself' }).end();
return;
}
let users = await sql.getAllUsers();
let partnerUser = users.find(u => u.inviteCode == invitationCode);
if (partnerUser == null) {
res.status(404).json({ status: 'error', message: 'invitation code does not exist in the database' }).end();
return;
}
let partnerUserId = partnerUser._id;
let user = res.locals.user;
user.partnerId = partnerUserId;
await sql.updateUser(user);
partnerUser.partnerId = currentUserId;
await sql.updateUser(partnerUser);
res.status(201).json({ status: 'ok' }).end();
}])
.get('/api/partner', [auth, (req, res) => {
let partnerId = res.locals.user.partnerId;
if (partnerId != null) {
return res.status(200).json({ status: 'ok', message: 'partner found!' }).end();
} else
return res.status(404).json({ status: 'error', message: 'partner not found' }).end();
}])
.post("/api/user", (req, res) => {
let user = res.locals.user;
user.subscription = req.body;
sql.updateUser(user);
console.log('updated user', user);
res.status(201).end();
})
.get("/", (req, res) => {
res.render("pages/index", {
userId: res.locals.userId,
webpush_key: process.env.VAPID_PUB,
hasPartner: res.locals.user.partnerId != null
});
})
.post('/api/send-love', [auth, async (req, res) => {
let user = res.locals.user;
let users = await sql.getAllUsers();
let partner = users.find(u => u.partnerId == user.partnerId);
if (partner == null) { return res.status(400).json({ status: 'error', message: 'no partner found for this user' }).end(); }
let pushNotifySubscriptionKeys = partner.subscription;
push.sendNotification(pushNotifySubscriptionKeys);
await sql.logEvent('LOVE_SENT', `user ${user._id} sent a love letter to ${partner._id}`);
res.status(201).json({ status: 'ok', description: 'message sent!' }).end();
}])
.get("/receive-love", (req, res) => {
res.render("pages/receive-love", {
userId: res.locals.userId,
webpush_key: process.env.VAPID_PUB
});
})
.get('/user/accept-invite/:invitationCode', async (req, res) => {
let inviteCode = req.params.invitationCode;
let users = await sql.getAllUsers();
let user = users.find(u => u.inviteCode == inviteCode);
if (user == null) {
res.status(404)
.json({ status: 'error', messsage: 'Invitation link already used or does not exist' })
.end();
return;
}
res.render("pages/accept-invite", {
userId: res.locals.userId,
webpush_key: process.env.VAPID_PUB,
inviteCode: inviteCode
});
})
.get("/api/db", async (req, res) => {
if (environment !== 'dev') {
res.status(403).json({ status: 'error', message: 'database is only accessible in dev environment' }).end();
return;
}
res.json(await sql.getAllUsers());
})
.listen(PORT, () => console.log(`Started http://localhost:${PORT}`));
const io = socketIO(server);
io.on("connection", socket => {
console.log("Client connected");
socket.on("disconnect", () => console.log("Client disconnected"));
});
setInterval(() => io.emit("time", new Date().toTimeString()), 1000);