-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
172 lines (146 loc) · 5.41 KB
/
server.js
File metadata and controls
172 lines (146 loc) · 5.41 KB
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
168
169
170
171
const express = require("express");
const { MongoClient } = require("mongodb");
const compression = require("compression");
const helmet = require("helmet");
const logger = require("morgan");
const socketIO = require("socket.io");
const http = require("http");
const dotenv = require("dotenv");
let userSockets = {};
dotenv.config();
const app = express();
let db, notifications, users, boards, history;
MongoClient.connect(process.env.MONGODB_URL).then(client => {
db = client.db(process.env.MONGODB_NAME);
history = db.collection("history");
boards = db.collection("boards");
notifications = db.collection("notifications");
users = db.collection("users");
app.get("/isalive", (req, res, next) => {
res.send("alive");
})
app.use(helmet());
app.use(logger("tiny"));
app.use(compression());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
let port = process.env.PORT || "8200";
/* eslint-disable no-console */
const server = http.createServer(app);
server.listen(port, () => console.log(`Server listening on port ${port}`));
const io = socketIO(server);
io.on("connection", socket => {
socket.emit("connected");
socket.on("userDetails", ({ user }) => {
const { _id: userID } = user;
if (!(userID in userSockets))
userSockets[userID] = [];
const doesSocketExists = userSockets[userID].filter(sock => sock.id === socket.id).length;
if (doesSocketExists === 0)
userSockets[userID].push(socket);
})
socket.on("disconnect", () => {
let keys = Object.keys(userSockets);
for (let i = 0; i < keys.length; i++) {
const original_length = userSockets[keys[i]].length;
userSockets[keys[i]] = userSockets[keys[i]].filter(sock => sock.id !== socket.id);
if (original_length !== userSockets[keys[i]].length)
break;
}
})
})
let historyCursor = history.watch();
historyCursor.on("change", ({ fullDocument }) => {
if (fullDocument) {
let { userId, action, payload, boardId, socketId, date } = fullDocument;
sendChangeAndHistoryMessage(userId, action, payload, boardId, socketId, date);
createNotifications(userId, action, boardId);
}
})
let notifCursor = notifications.watch();
notifCursor.on("change", ({ fullDocument }) => {
if (fullDocument) {
let { notifTo } = fullDocument;
if (notifTo) {
sendNotification(notifTo, fullDocument)
}
}
})
});
function sendHistoryMessage(users, action, boardId, userId, date) {
users.forEach(user => {
if (userSockets[user] && userSockets[user].length > 0) {
userSockets[user].forEach(sock => {
sock.emit("historyItem", { action, boardId, userId, date });
})
}
})
}
function sendChangeAndHistoryMessage(userId, action, payload, boardId, socketId, date) {
boardId ? boards.findOne({ _id: boardId }).then(board => {
if (board) {
let { users } = board;
let userSet = new Set(users.map(user => user.id));
users = [...userSet];
sendHistoryMessage(users, action, boardId, userId, date);
users.forEach(user => {
if (userSockets[user] && userSockets[user].length > 0) {
userSockets[user].forEach(sock => {
if (sock.id !== socketId)
sock.emit("change", { action, payload });
})
}
})
}
}).catch(err => console.error(err)): null;
}
function sendNotification(user, notification) {
if (userSockets[user] && userSockets[user].length > 0) {
userSockets[user].forEach(sock => {
sock.emit("notification", notification);
})
}
}
async function createNotifications(userId, action, boardId) {
const board = await boards.findOne({ _id: boardId });
let { name } = await users.findOne({ _id: userId });
const { firstName, lastName } = name;
if (firstName || lastName) {
name = `${firstName} ${lastName}`;
}
const { users: boardUsers, title } = board;
boardUsers.forEach(user => {
shouldSendNotification(user.id, action, user.watch, userId) ? createNotification({ action, boardId, title, from: name, wasSeen: false, notifTo: user.id }) : null;
})
}
async function createNotification(notification) {
await notifications.insertOne(notification);
}
function shouldSendNotification(userId, action, watchMode, skipUserId) {
if (watchMode === "Ignoring" || userId === skipUserId)
return false;
const notWatchingFunctions = [
"UPDATE_ASSIGNED_USER",
"ADD_USER",
"REMOVE_USER",
"CHANGE_USER_ROLE"
];
const watchingFunctions = [
"TOGGLE_SOCKET_CONNECTION",
"ENTER_AS_GUEST",
"UPDATE_FILTER",
"CHANGE_CARD_FILTER",
"SET_CURRENT_CARD",
"PUT_BOARD_ID_IN_REDUX",
"ADD_BOARD",
"LOAD_BOARD_USERS_DATA",
"CHANGE_USER_WATCH"
];
if (watchMode === "Watching" && !watchingFunctions.includes(action)) {
return true;
}
if (watchMode === "Not watching" && notWatchingFunctions.includes(action)) {
return true;
}
return false;
}