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
|
module.exports = {
ROLES: {
OPERATOR: 'OPERATOR',
CUSTOMER: 'CUSTOMER'
},
DEFAULT_ROOMS: {
OPERATORS: 'OPERATORS'
},
EVENTS: {
CONNECTION: {
OPEN: 'CONNECTION:OPEN',
CLOSE: 'CONNECTION:CLOSE'
},
ROOM: {
JOIN: 'ROOM:JOIN',
LEAVE: 'ROOM:LEAVE'
},
SEND: {
MESSAGE: 'SEND:MESSAGE',
IMAGE: 'SEND:IMAGE',
ATTACHMENT: 'SEND:FILE',
LINK: 'SEND:LINK'
},
OPERATORS: {
LIST: {
UPDATE: 'OPERATORS:LIST:UPDATE'
}
},
CUSTOMERS: {
LIST: {
UPDATE: 'CUSTOMERS:LIST:UPDATE'
}
},
ROOMS: {
LIST: {
UPDATE: 'ROOMS:LIST:UPDATE'
}
}
},
users: [],
rooms: {
CUSTOMER: {},
OPERATOR: {}
},
isRoomAllowed(from, to) {
if (from.role === _this.ROLES.OPERATOR) {
}
},
usersMap: {},
getUser(sid) {
return this.usersMap[sid]
},
createUser(id, name, role) {
const _this = this;
const user = {
name: name,
role: role,
room: id,
joinOwnerRoom: (owner) => {
if (this.role === _this.ROLES.OPERATOR) {
if (!_this.isUserInRoom(owner, this) && !_this.isUserInRoom(this, owner)) {
_this.rooms[owner.role][owner.room].push(this);
return _this.rooms[owner.role][owner.room];
} else {
return _this.rooms[owner.role][owner.room] || _this.rooms[this.role][this.room];
}
}
}
}
this.addUser(user);
},
addUser(user) {
this.usersMap[user.room] = user;
this.users.push(user);
this.rooms[user.role][user.room] = [];
},
deleteUser(user) {
this.users.splice(this.users.indexOf(user), 1);
delete this.usersMap[user.room];
},
getUsersByRole(role) {
return this.users.filter((user) => {
return user.role === role
});
},
countUsersByRole(role) {
this.getUsersByRole(role).length
},
isUserInRoom(owner, guest) {
return this.rooms[owner.role][owner.room].indexOf(guest) > - 1;
},
event(type, data) {
return {
type,
data,
ts: Date.now()
}
},
message(data, room) {
return {
from: this.user,
room: room,
content: data.content,
contentType: data.contentType
}
}
}
|