Nextrek
Diff Revisions
1128
vs
1129
for /socketIO/chat.js
|
@@ -1,7 +1,7 @@ |
1 |
1 |
|
const fs = require('fs'); |
2 |
2 |
|
const sharp = require('sharp'); |
3 |
3 |
|
|
4 |
|
- |
const CHAT = Symbol.for("My.App.Namespace.chat"); |
|
4 |
+ |
const CHAT = Symbol.for("net.nextrek.chattrek"); |
5 |
5 |
|
|
6 |
6 |
|
var globalSymbols = Object.getOwnPropertySymbols(global); |
7 |
7 |
|
var hasChat = (globalSymbols.indexOf(CHAT) > -1); |
|
@@ -51,8 +51,7 @@ |
51 |
51 |
|
}, |
52 |
52 |
|
|
53 |
53 |
|
ERRORS: { |
54 |
|
- |
USER: |
55 |
|
- |
{ |
|
54 |
+ |
USER: { |
56 |
55 |
|
NOT_FOUND: 'ERROR:USER:NOTFOUND' |
57 |
56 |
|
} |
58 |
57 |
|
}, |
|
@@ -77,7 +76,7 @@ |
77 |
76 |
|
getUser(id) { |
78 |
77 |
|
const user = this.usersMap[id]; |
79 |
78 |
|
return { |
80 |
|
- |
id: user.id, |
|
79 |
+ |
room: user.room, |
81 |
80 |
|
name: user.name |
82 |
81 |
|
} |
83 |
82 |
|
}, |
|
@@ -103,10 +102,10 @@ |
103 |
102 |
|
}, |
104 |
103 |
|
|
105 |
104 |
|
deleteUser(user) { |
106 |
|
- |
this.leaveRoom(user, user); |
107 |
105 |
|
this.users.splice(this.users.indexOf(user), 1); |
108 |
106 |
|
delete this.usersMap[user.room]; |
109 |
107 |
|
delete this.socketsMap[user.room]; |
|
108 |
+ |
return this.leaveRoom(user, user); |
110 |
109 |
|
}, |
111 |
110 |
|
|
112 |
111 |
|
getUsersByRole(role) { |
|
@@ -124,7 +123,7 @@ |
124 |
123 |
|
}, |
125 |
124 |
|
|
126 |
125 |
|
joinRoom(host, guest) { |
127 |
|
- |
if (guest && guest.role === this.ROLES.OPERATOR) { |
|
126 |
+ |
if (guest && guest.role === this.ROLES.OPERATOR && this.rooms[host.role][host.room]) { |
128 |
127 |
|
if (!this.isUserInRoom(host, guest) && !this.isUserInRoom(guest, host)) { |
129 |
128 |
|
this.rooms[host.role][host.room].push(guest); |
130 |
129 |
|
return host.room; |
|
@@ -135,12 +134,20 @@ |
135 |
134 |
|
}, |
136 |
135 |
|
|
137 |
136 |
|
leaveRoom(host, guest) { |
|
137 |
+ |
/** |
|
138 |
+ |
* tbd: check leaving permissions |
|
139 |
+ |
*/ |
|
140 |
+ |
const promises = []; |
138 |
141 |
|
if (host.room === guest.room) { |
139 |
142 |
|
// host leaving his own room means throw out guest(s) |
140 |
143 |
|
this.rooms[host.role][host.room].forEach(user => { |
141 |
|
- |
this.socketsMap[user.room].leave(host.room); |
|
144 |
+ |
if (this.socketsMap[user.room]) { |
|
145 |
+ |
promises.push(new Promise(resolve => { |
|
146 |
+ |
resolve(this.socketsMap[user.room]); |
|
147 |
+ |
})); |
|
148 |
+ |
} |
142 |
149 |
|
}); |
143 |
|
- |
this.rooms[host.role][host.room] = []; |
|
150 |
+ |
delete this.rooms[host.role][host.room]; |
144 |
151 |
|
} else { |
145 |
152 |
|
// operators are only allowed to leave other operators' rooms |
146 |
153 |
|
if (host.role === this.ROLES.OPERATOR) { |
|
@@ -148,6 +155,7 @@ |
148 |
155 |
|
this.rooms[host.role][host.room].splice(ix, 1); |
149 |
156 |
|
} |
150 |
157 |
|
} |
|
158 |
+ |
return promises; |
151 |
159 |
|
}, |
152 |
160 |
|
|
153 |
161 |
|
storeImage(data, user) { |