-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
126 lines (105 loc) · 2.9 KB
/
index.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
const express = require("express");
const cors = require("cors");
const { decodeJWT } = require("./middlewares/decodeJWT.js");
const { FRONT_END, NODE_ENV } = require("./configs/environments.js");
const morgan = require("morgan");
const socketIO = require("socket.io");
const { auth, firestore, serverTimestamp } = require("./configs/firebase");
/**
* Express setup
*/
const app = express();
// middlewares
app.use(
cors({
origin: FRONT_END,
})
);
// setting rawBody for webhook handling
app.use(
express.json({
limit: "10mb",
verify: (req, _res, buf) => {
req.rawBody = buf;
},
})
);
app.use(decodeJWT);
// enable trust proxy for nginx reverse proxy
app.enable("trust proxy");
const skipLoggingEndpoints = ["/api/ping"];
app.use(
morgan(NODE_ENV === "production" ? "combined" : "dev", {
skip: (req, res) => {
return skipLoggingEndpoints.includes(req.url);
},
})
);
// endpoints
app.get("/", (_req, res) => {
res.redirect(FRONT_END);
});
app.get("/api/ping", (_req, res) => {
res.send("okay here you go... ponggggg!!!!");
});
app.use("/api/suggestions", require("./routes/suggestions.js"));
app.use("/api/friends", require("./routes/friends.js"));
app.use("/api/search", require("./routes/search.js"));
app.use("/api/call", require("./routes/call.js"));
app.use("/api/user", require("./routes/user.js"));
app.use("/api/payments", require("./routes/payments.js"));
// listner
const port = process.env.PORT || 8000;
const server = app.listen(port, () => {
console.log(`Hello from port ${port} @ ${new Date().toLocaleString()}`);
});
// socket.io setup to detect presence for a user
const io = socketIO(server, {
cors: {
origin: FRONT_END,
},
});
// socket.io events
io.on("connection", async (socket) => {
try {
const token = socket.handshake.auth.token;
if (!token) {
socket.disconnect();
return;
}
const currentUser = await auth.verifyIdToken(token);
// set status online in firestore
const { uid } = currentUser;
const userRef = firestore.collection("users").doc(uid);
const userSnapshot = await userRef.get();
if (!userSnapshot.exists) {
socket.disconnect();
return;
}
const statusRef = firestore.collection("status").doc(uid);
// set status online in status ref
await statusRef.set({
status: "online",
timestamp: serverTimestamp(),
});
console.log(`${currentUser.email} is online`);
socket.on("disconnect", async () => {
try {
// set status offline in firestore
if ((await statusRef.get()).exists) {
await statusRef.set({
status: "offline",
timestamp: serverTimestamp(),
});
}
console.log(`${currentUser.email} is offline`);
} catch (error) {
console.log(error);
socket.disconnect();
}
});
} catch (error) {
console.log(error);
socket.disconnect();
}
});