|
@@ -1,125 +1,145 @@ |
1 |
|
- |
module.exports = { |
2 |
|
- |
ROLES: { |
3 |
|
- |
OPERATOR: 'OPERATOR', |
4 |
|
- |
CUSTOMER: 'CUSTOMER' |
5 |
|
- |
}, |
6 |
|
- |
|
7 |
|
- |
DEFAULT_ROOMS: { |
8 |
|
- |
OPERATORS: 'OPERATORS' |
9 |
|
- |
}, |
10 |
|
- |
|
11 |
|
- |
EVENTS: { |
12 |
|
- |
CONNECTION: { |
13 |
|
- |
OPEN: 'CONNECTION:OPEN', |
14 |
|
- |
CLOSE: 'CONNECTION:CLOSE' |
15 |
|
- |
}, |
16 |
|
- |
ROOM: { |
17 |
|
- |
JOIN: 'ROOM:JOIN', |
18 |
|
- |
LEAVE: 'ROOM:LEAVE' |
19 |
|
- |
}, |
20 |
|
- |
SEND: { |
21 |
|
- |
MESSAGE: 'SEND:MESSAGE', |
22 |
|
- |
IMAGE: 'SEND:IMAGE', |
23 |
|
- |
ATTACHMENT: 'SEND:FILE', |
24 |
|
- |
LINK: 'SEND:LINK' |
25 |
|
- |
}, |
26 |
|
- |
OPERATORS: { |
27 |
|
- |
LIST: { |
28 |
|
- |
UPDATE: 'OPERATORS:LIST:UPDATE' |
29 |
|
- |
} |
|
1 |
+ |
const CHAT = Symbol.for("My.App.Namespace.chat"); |
|
2 |
+ |
|
|
3 |
+ |
var globalSymbols = Object.getOwnPropertySymbols(global); |
|
4 |
+ |
var hasChat = (globalSymbols.indexOf(CHAT) > -1); |
|
5 |
+ |
|
|
6 |
+ |
if (!hasChat) { |
|
7 |
+ |
global[CHAT] = { |
|
8 |
+ |
ROLES: { |
|
9 |
+ |
OPERATOR: 'OPERATOR', |
|
10 |
+ |
CUSTOMER: 'CUSTOMER' |
30 |
11 |
|
}, |
31 |
|
- |
CUSTOMERS: { |
32 |
|
- |
LIST: { |
33 |
|
- |
UPDATE: 'CUSTOMERS:LIST:UPDATE' |
34 |
|
- |
} |
|
12 |
+ |
|
|
13 |
+ |
DEFAULT_ROOMS: { |
|
14 |
+ |
OPERATORS: 'OPERATORS' |
35 |
15 |
|
}, |
36 |
|
- |
ROOMS: { |
37 |
|
- |
LIST: { |
38 |
|
- |
UPDATE: 'ROOMS:LIST:UPDATE' |
|
16 |
+ |
|
|
17 |
+ |
EVENTS: { |
|
18 |
+ |
CONNECTION: { |
|
19 |
+ |
OPEN: 'CONNECTION:OPEN', |
|
20 |
+ |
CLOSE: 'CONNECTION:CLOSE' |
|
21 |
+ |
}, |
|
22 |
+ |
ROOM: { |
|
23 |
+ |
JOIN: 'ROOM:JOIN', |
|
24 |
+ |
LEAVE: 'ROOM:LEAVE' |
|
25 |
+ |
}, |
|
26 |
+ |
SEND: { |
|
27 |
+ |
MESSAGE: 'SEND:MESSAGE', |
|
28 |
+ |
IMAGE: 'SEND:IMAGE', |
|
29 |
+ |
ATTACHMENT: 'SEND:FILE', |
|
30 |
+ |
LINK: 'SEND:LINK' |
|
31 |
+ |
}, |
|
32 |
+ |
OPERATORS: { |
|
33 |
+ |
LIST: { |
|
34 |
+ |
UPDATE: 'OPERATORS:LIST:UPDATE' |
|
35 |
+ |
} |
|
36 |
+ |
}, |
|
37 |
+ |
CUSTOMERS: { |
|
38 |
+ |
LIST: { |
|
39 |
+ |
UPDATE: 'CUSTOMERS:LIST:UPDATE' |
|
40 |
+ |
} |
|
41 |
+ |
}, |
|
42 |
+ |
ROOMS: { |
|
43 |
+ |
LIST: { |
|
44 |
+ |
UPDATE: 'ROOMS:LIST:UPDATE' |
|
45 |
+ |
} |
39 |
46 |
|
} |
40 |
|
- |
} |
41 |
|
- |
}, |
|
47 |
+ |
}, |
42 |
48 |
|
|
43 |
|
- |
users: [], |
|
49 |
+ |
users: [], |
44 |
50 |
|
|
45 |
|
- |
rooms: { |
46 |
|
- |
CUSTOMER: {}, |
47 |
|
- |
OPERATOR: {} |
48 |
|
- |
}, |
|
51 |
+ |
rooms: { |
|
52 |
+ |
CUSTOMER: {}, |
|
53 |
+ |
OPERATOR: {} |
|
54 |
+ |
}, |
49 |
55 |
|
|
50 |
|
- |
isRoomAllowed(from, to) { |
51 |
|
- |
if (from.role === _this.ROLES.OPERATOR) { |
|
56 |
+ |
isRoomAllowed(from, to) { |
|
57 |
+ |
if (from.role === _this.ROLES.OPERATOR) { |
52 |
58 |
|
|
53 |
|
- |
} |
54 |
|
- |
}, |
|
59 |
+ |
} |
|
60 |
+ |
}, |
55 |
61 |
|
|
56 |
|
- |
usersMap: {}, |
|
62 |
+ |
usersMap: {}, |
57 |
63 |
|
|
58 |
|
- |
getUser(sid) { |
59 |
|
- |
return this.usersMap[sid] |
60 |
|
- |
}, |
61 |
|
- |
|
62 |
|
- |
createUser(id, name, role) { |
63 |
|
- |
const _this = this; |
64 |
|
- |
const user = { |
65 |
|
- |
name: name, |
66 |
|
- |
role: role, |
67 |
|
- |
room: id, |
68 |
|
- |
|
69 |
|
- |
joinOwnerRoom: (owner) => { |
70 |
|
- |
if (this.role === _this.ROLES.OPERATOR) { |
71 |
|
- |
if (!_this.isUserInRoom(owner, this) && !_this.isUserInRoom(this, owner)) { |
72 |
|
- |
_this.rooms[owner.role][owner.room].push(this); |
73 |
|
- |
return _this.rooms[owner.role][owner.room]; |
74 |
|
- |
} else { |
75 |
|
- |
return _this.rooms[owner.role][owner.room] || _this.rooms[this.role][this.room]; |
|
64 |
+ |
getUser(sid) { |
|
65 |
+ |
return this.usersMap[sid] |
|
66 |
+ |
}, |
|
67 |
+ |
|
|
68 |
+ |
createUser(id, name, role) { |
|
69 |
+ |
const _this = this; |
|
70 |
+ |
const user = { |
|
71 |
+ |
name: name, |
|
72 |
+ |
role: role, |
|
73 |
+ |
room: id, |
|
74 |
+ |
|
|
75 |
+ |
joinOwnerRoom: (owner) => { |
|
76 |
+ |
if (this.role === _this.ROLES.OPERATOR) { |
|
77 |
+ |
if (!_this.isUserInRoom(owner, this) && !_this.isUserInRoom(this, owner)) { |
|
78 |
+ |
_this.rooms[owner.role][owner.room].push(this); |
|
79 |
+ |
return _this.rooms[owner.role][owner.room]; |
|
80 |
+ |
} else { |
|
81 |
+ |
return _this.rooms[owner.role][owner.room] || _this.rooms[this.role][this.room]; |
|
82 |
+ |
} |
76 |
83 |
|
} |
77 |
84 |
|
} |
78 |
85 |
|
} |
79 |
|
- |
} |
80 |
86 |
|
|
81 |
|
- |
this.addUser(user); |
82 |
|
- |
}, |
|
87 |
+ |
this.addUser(user); |
|
88 |
+ |
}, |
|
89 |
+ |
|
|
90 |
+ |
addUser(user) { |
|
91 |
+ |
this.usersMap[user.room] = user; |
|
92 |
+ |
this.users.push(user); |
|
93 |
+ |
this.rooms[user.role][user.room] = []; |
|
94 |
+ |
}, |
83 |
95 |
|
|
84 |
|
- |
addUser(user) { |
85 |
|
- |
this.usersMap[user.room] = user; |
86 |
|
- |
this.users.push(user); |
87 |
|
- |
this.rooms[user.role][user.room] = []; |
88 |
|
- |
}, |
89 |
|
- |
|
90 |
|
- |
deleteUser(user) { |
91 |
|
- |
this.users.splice(this.users.indexOf(user), 1); |
92 |
|
- |
delete this.usersMap[user.room]; |
93 |
|
- |
}, |
94 |
|
- |
|
95 |
|
- |
getUsersByRole(role) { |
96 |
|
- |
return this.users.filter((user) => { |
97 |
|
- |
return user.role === role |
98 |
|
- |
}); |
99 |
|
- |
}, |
100 |
|
- |
|
101 |
|
- |
countUsersByRole(role) { |
102 |
|
- |
this.getUsersByRole(role).length |
103 |
|
- |
}, |
104 |
|
- |
|
105 |
|
- |
isUserInRoom(owner, guest) { |
106 |
|
- |
return this.rooms[owner.role][owner.room].indexOf(guest) > - 1; |
107 |
|
- |
}, |
108 |
|
- |
|
109 |
|
- |
event(type, data) { |
110 |
|
- |
return { |
111 |
|
- |
type, |
112 |
|
- |
data, |
113 |
|
- |
ts: Date.now() |
114 |
|
- |
} |
115 |
|
- |
}, |
|
96 |
+ |
deleteUser(user) { |
|
97 |
+ |
this.users.splice(this.users.indexOf(user), 1); |
|
98 |
+ |
delete this.usersMap[user.room]; |
|
99 |
+ |
}, |
|
100 |
+ |
|
|
101 |
+ |
getUsersByRole(role) { |
|
102 |
+ |
return this.users.filter((user) => { |
|
103 |
+ |
return user.role === role |
|
104 |
+ |
}); |
|
105 |
+ |
}, |
|
106 |
+ |
|
|
107 |
+ |
countUsersByRole(role) { |
|
108 |
+ |
this.getUsersByRole(role).length |
|
109 |
+ |
}, |
|
110 |
+ |
|
|
111 |
+ |
isUserInRoom(owner, guest) { |
|
112 |
+ |
return this.rooms[owner.role][owner.room].indexOf(guest) > - 1; |
|
113 |
+ |
}, |
116 |
114 |
|
|
117 |
|
- |
message(data, room) { |
118 |
|
- |
return { |
119 |
|
- |
from: this.user, |
120 |
|
- |
room: room, |
121 |
|
- |
content: data.content, |
122 |
|
- |
contentType: data.contentType |
|
115 |
+ |
event(type, data) { |
|
116 |
+ |
return { |
|
117 |
+ |
type, |
|
118 |
+ |
data, |
|
119 |
+ |
ts: Date.now() |
|
120 |
+ |
} |
|
121 |
+ |
}, |
|
122 |
+ |
|
|
123 |
+ |
message(data, room) { |
|
124 |
+ |
return { |
|
125 |
+ |
from: this.user, |
|
126 |
+ |
room: room, |
|
127 |
+ |
content: data.content, |
|
128 |
+ |
contentType: data.contentType |
|
129 |
+ |
} |
123 |
130 |
|
} |
|
131 |
+ |
}; |
|
132 |
+ |
} |
|
133 |
+ |
|
|
134 |
+ |
var singleton = {}; |
|
135 |
+ |
|
|
136 |
+ |
Object.defineProperty(singleton, "instance", { |
|
137 |
+ |
get: function () { |
|
138 |
+ |
return global[CHAT]; |
124 |
139 |
|
} |
125 |
|
- |
} |
|
140 |
+ |
}); |
|
141 |
+ |
|
|
142 |
+ |
Object.freeze(singleton); |
|
143 |
+ |
|
|
144 |
+ |
|
|
145 |
+ |
module.exports = singleton; |