1 | 'use strict'; |
||
2 | |||
3 | var roomModel = require('../database').models.room; |
||
4 | var User = require('../models/user'); |
||
5 | |||
6 | var create = function (data, callback){ |
||
7 | var newRoom = new roomModel(data); |
||
0 ignored issues
–
show
Coding Style
Best Practice
introduced
by
![]() |
|||
8 | newRoom.save(callback); |
||
9 | }; |
||
10 | |||
11 | var find = function (data, callback){ |
||
12 | roomModel.find(data, callback); |
||
13 | } |
||
14 | |||
15 | var findOne = function (data, callback){ |
||
16 | roomModel.findOne(data, callback); |
||
17 | } |
||
18 | |||
19 | var findById = function (id, callback){ |
||
20 | roomModel.findById(id, callback); |
||
21 | } |
||
22 | |||
23 | var findByIdAndUpdate = function(id, data, callback){ |
||
24 | roomModel.findByIdAndUpdate(id, data, { new: true }, callback); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Add a user along with the corresponding socket to the passed room |
||
29 | * |
||
30 | */ |
||
31 | var addUser = function(room, socket, callback){ |
||
32 | |||
33 | // Get current user's id |
||
34 | var userId = socket.request.session.passport.user; |
||
35 | |||
36 | // Push a new connection object(i.e. {userId + socketId}) |
||
37 | var conn = { userId: userId, socketId: socket.id}; |
||
38 | room.connections.push(conn); |
||
39 | room.save(callback); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Get all users in a room |
||
44 | * |
||
45 | */ |
||
46 | var getUsers = function(room, socket, callback){ |
||
47 | |||
48 | var users = [], vis = {}, cunt = 0; |
||
49 | var userId = socket.request.session.passport.user; |
||
50 | |||
51 | // Loop on room's connections, Then: |
||
52 | room.connections.forEach(function(conn){ |
||
53 | |||
54 | // 1. Count the number of connections of the current user(using one or more sockets) to the passed room. |
||
55 | if(conn.userId === userId){ |
||
56 | cunt++; |
||
57 | } |
||
58 | |||
59 | // 2. Create an array(i.e. users) contains unique users' ids |
||
60 | if(!vis[conn.userId]){ |
||
61 | users.push(conn.userId); |
||
62 | } |
||
63 | vis[conn.userId] = true; |
||
64 | }); |
||
65 | |||
66 | // Loop on each user id, Then: |
||
67 | // Get the user object by id, and assign it to users array. |
||
68 | // So, users array will hold users' objects instead of ids. |
||
69 | users.forEach(function(userId, i){ |
||
70 | User.findById(userId, function(err, user){ |
||
71 | if (err) { return callback(err); } |
||
72 | users[i] = user; |
||
73 | if(i + 1 === users.length){ |
||
0 ignored issues
–
show
There is no return statement if
i + 1 === users.length is false . Are you sure this is correct? If so, consider adding return; explicitly.
This check looks for functions where a Consider this little piece of code function isBig(a) {
if (a > 5000) {
return "yes";
}
}
console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined
The function This behaviour may not be what you had intended. In any case, you can add a
![]() |
|||
74 | return callback(null, users, cunt); |
||
75 | } |
||
76 | }); |
||
77 | }); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Remove a user along with the corresponding socket from a room |
||
82 | * |
||
83 | */ |
||
84 | var removeUser = function(socket, callback){ |
||
85 | |||
86 | // Get current user's id |
||
87 | var userId = socket.request.session.passport.user; |
||
88 | |||
89 | find(function(err, rooms){ |
||
90 | if(err) { return callback(err); } |
||
91 | |||
92 | // Loop on each room, Then: |
||
93 | rooms.every(function(room){ |
||
94 | var pass = true, cunt = 0, target = 0; |
||
95 | |||
96 | // For every room, |
||
97 | // 1. Count the number of connections of the current user(using one or more sockets). |
||
98 | room.connections.forEach(function(conn, i){ |
||
99 | if(conn.userId === userId){ |
||
100 | cunt++; |
||
101 | } |
||
102 | if(conn.socketId === socket.id){ |
||
103 | pass = false, target = i; |
||
0 ignored issues
–
show
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.
The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression. This operator is most often used in Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator. This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements. var a,b,c;
a = 1, b = 1, c= 3;
could just as well be written as: var a,b,c;
a = 1;
b = 1;
c = 3;
To learn more about the sequence operator, please refer to the MDN. ![]() |
|||
104 | } |
||
105 | }); |
||
106 | |||
107 | // 2. Check if the current room has the disconnected socket, |
||
108 | // If so, then, remove the current connection object, and terminate the loop. |
||
109 | if(!pass) { |
||
110 | room.connections.id(room.connections[target]._id).remove(); |
||
111 | room.save(function(err){ |
||
112 | callback(err, room, userId, cunt); |
||
113 | }); |
||
114 | } |
||
115 | |||
116 | return pass; |
||
117 | }); |
||
0 ignored issues
–
show
|
|||
118 | }); |
||
119 | } |
||
120 | |||
121 | module.exports = { |
||
122 | create, |
||
123 | find, |
||
124 | findOne, |
||
125 | findById, |
||
126 | addUser, |
||
127 | getUsers, |
||
128 | removeUser |
||
129 | }; |