-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
44 lines (35 loc) · 898 Bytes
/
users.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
const users = {};
function isEmptyUserData(username) {
return !username || !username.trim() ;
}
function isValid(username) {
let isValid = true;
isValid = !!username && username.trim();
isValid = isValid && username.match(/^[A-Za-z0-9_]+$/);
return isValid;
}
function addUserSession(sid,username){
if(!users[username]){
users[username] = [];
}
users[username].push(sid);
}
function removeSessionFromUser(sid,username){
if(!users[username]||!users[username].includes(sid)){
return;
}
users[username] = users[username].filter((session)=> session !== sid);
}
function getOnlineUsers(){
const usersWithSessions = Object.entries(users).filter(
([_,sessions]) => sessions.length>0
);
return usersWithSessions.map(([username])=> username);
}
module.exports = {
isValid,
isEmptyUserData,
addUserSession,
removeSessionFromUser,
getOnlineUsers,
};