-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
306 lines (271 loc) · 8.15 KB
/
start.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const express = require('express');
const mongoose = require('mongoose');
const uuid = require('node-uuid');
const Q = require('q');
const _ = require("underscore");
const dbFunctions = require('./dbStore/dbFunctions');
var config = require('./config');
const compression = require('compression'); // G-ZIP compression
const mail = require('./mail'); //Configure mail.js and un-comment the mail code
const btoa = require('btoa');//Password is btoa hashed
// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });
var moment = require('moment-jalaali');
var app = express();
const shouldCompress = (req, res) => {
if (req.headers['x-no-compression']) {
// don't compress responses if this request header is present
return false;
}
// fallback to standard compression
return compression.filter(req, res);
};
app.use(compression({
// filter decides if the response should be compressed or not,
// based on the `shouldCompress` function above
filter: shouldCompress,
// threshold is the byte threshold for the response body size
// before compression is considered, the default is 1kb
threshold: 0
}));
// Connect to our Database and handle any bad connections
mongoose.set('useCreateIndex', true);
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
mongoose.Promise = global.Promise; // Tell Mongoose to use ES6 promises
mongoose.connection.on('error', (err) => {
console.error(`🙅 🚫 🙅 🚫 🙅 🚫 🙅 🚫 → ${err.message}`);
});
// READY?! Let's go!
require('./models/User');
require('./models/Blog');
require('./models/Admin');
// Start our app!
var app = require('./app');
var server = require('http').Server(app)
var io = require('socket.io')();
var admins = {};
var users = {};
// connect to Redis
dbFunctions.ConnectToRedis(startApp);
//public directory
app.use(express.static(__dirname + '/public'));
// io connection
io.on('connection', function(socket) {
//Login Admin
socket.on('login', function(data) {
if (btoa(data.password) != config.key)
socket.emit('login', {
login: false,
err: "Invalid Login"
})
else {
if (_.find(config.admin_users, function(admin) {
return (admin == data.admin);
})) {
if (admins[data.admin]) {
socket.emit('login', {
login: false,
err: "Already Logged In"
})
} else {
socket.emit('login', {
login: true
})
}
} else {
socket.emit('login', {
login: false,
err: "Invalid Login"
})
}
}
});
//Init admin
socket.on('add admin', function(data) {
this.isAdmin = data.isAdmin;
socket.username = data.admin;
_.each(admins, function(adminSocket) {
adminSocket.emit("admin added", socket.username)
socket.emit("admin added", adminSocket.username)
});
admins[socket.username] = socket;
//If some user is already online on chat
if (Object.keys(users).length > 0) {
_.each(users, function(userSocket) {
dbFunctions.getMessages(userSocket.roomID, 0)
.then(function(history) {
var len = history.length;
var userSocket = users[history[len - 1]];
history.splice(-1, 1);
socket.join(userSocket.roomID);
socket.emit("New Client", {
roomID: userSocket.roomID,
history: history,
details: userSocket.userDetails,
justJoined: true
})
})
});
}
});
//Init user
socket.on('add user', function(data) {
socket.isAdmin = false;
if (data.isNewUser) {
data.roomID = uuid.v4();
dbFunctions.setDetails(data);
socket.emit("roomID", data.roomID);
}
socket.roomID = data.roomID;
//Fetch user details
dbFunctions.getDetails(socket.roomID)
.then(function(details) {
socket.userDetails = details;
})
.catch(function(error) {
console.log("Line 95 : ", error)
})
.done();
socket.join(socket.roomID);
var newUser = false;
if (!users[socket.roomID]) { // Check if different instance of same user. (ie. Multiple tabs)
users[socket.roomID] = socket;
newUser = true;
}
//Fetch message history
dbFunctions.getMessages(socket.roomID, 0)
.then(function(history) {
history.splice(-1, 1)
socket.emit('chat history', {
history: history,
getMore: false
});
if (Object.keys(admins).length == 0) {
//Tell user he will be contacted asap and send admin email
socket.emit('log message', "با عرض پوزش به علت پایان زمان کاری شرکت پشتیبانی برای پاسخگویی شما آنلاین نمیباشد. لطفا منتظر بمانید معمولا کمتر از ۳۰ دقیقه با شما تماس خواهیم گرفت.");
mail.alertMail();
} else {
if (newUser) {
socket.emit('log message', "سلام و وقت بخیر " + socket.userDetails[0] + "، چطوری میتونم کمکتون کنم؟");
//Make all available admins join this users room.
_.each(admins, function(adminSocket) {
adminSocket.join(socket.roomID);
adminSocket.emit("New Client", {
roomID: socket.roomID,
history: history,
details: socket.userDetails,
justJoined: false
})
});
}
}
})
.catch(function(error) {
console.log("Line 132 : ", error)
})
.done();
dbFunctions.getMsgLength(socket.roomID)
.then(function(len) {
socket.MsgHistoryLen = (len * -1) + 10;
socket.TotalMsgLen = (len * -1);
})
.catch(function(error) {
console.log("Line 140 : ", error)
})
.done();
});
socket.on('chat message', function(data) {
if (data.roomID === "null")
data.roomID = socket.roomID;
data.isAdmin = socket.isAdmin;
dbFunctions.pushMessage(data);
socket.broadcast.to(data.roomID).emit('chat message', data);
});
socket.on("typing", function(data) {
socket.broadcast.to(data.roomID).emit("typing", {
isTyping: data.isTyping,
person: data.person,
roomID: data.roomID
});
});
socket.on('disconnect', function() {
if (socket.isAdmin) {
delete admins[socket.username];
_.each(admins, function(adminSocket) {
adminSocket.emit("admin removed", socket.username)
});
} else {
if (io.sockets.adapter.rooms[socket.roomID]) {
var total = io.sockets.adapter.rooms[socket.roomID]["length"];
var totAdmins = Object.keys(admins).length;
var clients = total - totAdmins;
if (clients == 0) {
//check if user reconnects in 4 seconds
setTimeout(function() {
if (io.sockets.adapter.rooms[socket.roomID])
total = io.sockets.adapter.rooms[socket.roomID]["length"];
totAdmins = Object.keys(admins).length;
if (total <= totAdmins) {
mail.sendMail({
roomID: socket.roomID,
MsgLen: socket.TotalMsgLen,
email: socket.userDetails
});
delete users[socket.roomID];
socket.broadcast.to(socket.roomID).emit("User Disconnected", socket.roomID);
_.each(admins, function(adminSocket) {
adminSocket.leave(socket.roomID)
});
}
}, 4000);
}
} else {
if (socket.userDetails)
mail.sendMail({
roomID: socket.roomID,
MsgLen: socket.TotalMsgLen,
email: socket.userDetails
});
delete users[socket.roomID];
}
}
});
socket.on('poke admin', function(targetAdmin) {
admins[targetAdmin].emit("poke admin", {})
});
socket.on('client ack', function() {
for (adminSocket in admins) {
if (!admins.hasOwnProperty(adminSocket)) {
continue;
}
admins[adminSocket].emit("client ack", {})
}
});
socket.on("more messages", function() {
if (socket.MsgHistoryLen < 0) {
dbFunctions.getMessages(socket.roomID, socket.MsgHistoryLen)
.then(function(history) {
history.splice(-1, 1)
socket.emit('more chat history', {
history: history
});
})
socket.MsgHistoryLen += 10;
}
});
});
function startApp(isSuccess) {
if (isSuccess) {
server.listen(config.web_port, function() {
console.log('Server started ' + config.web_port + ' at ' +
(new Date().toLocaleString().substr(0, 24)));
console.log(moment().format('jYYYY/jM/jD HH:mm:ss')); // 1392/6/31 23:59:59
});
io.attach(server, {
'pingInterval': 15000,
'pingTimeout': 15000
});
} else {
console.log("Server failed to start.");
}
}