Subversion Repository Public Repository

Nextrek

Diff Revisions 1127 vs 1128 for /socketIO/chat.js

Diff revisions: vs.
  @@ -72,6 +72,8 @@
72 72
73 73 usersMap: {},
74 74
75 + socketsMap: {},
76 +
75 77 getUser(id) {
76 78 const user = this.usersMap[id];
77 79 return {
  @@ -80,7 +82,7 @@
80 82 }
81 83 },
82 84
83 - createUser(name, role, id) {
85 + createUser(name, role, id, socket) {
84 86 const _this = this;
85 87 const user = {
86 88 name: name,
  @@ -88,20 +90,23 @@
88 90 room: id,
89 91 }
90 92
91 - this.addUser(user);
93 + this.addUser(user, socket);
92 94
93 95 return user;
94 96 },
95 97
96 - addUser(user) {
98 + addUser(user, socket) {
97 99 this.usersMap[user.room] = user;
100 + this.socketsMap[user.room] = socket;
98 101 this.users.push(user);
99 102 this.rooms[user.role][user.room] = [];
100 103 },
101 104
102 105 deleteUser(user) {
106 + this.leaveRoom(user, user);
103 107 this.users.splice(this.users.indexOf(user), 1);
104 108 delete this.usersMap[user.room];
109 + delete this.socketsMap[user.room];
105 110 },
106 111
107 112 getUsersByRole(role) {
  @@ -114,17 +119,33 @@
114 119 this.getUsersByRole(role).length
115 120 },
116 121
117 - isUserInRoom(owner, guest) {
118 - return this.rooms[owner.role][owner.room].indexOf(guest) > - 1;
122 + isUserInRoom(host, guest) {
123 + return this.rooms[host.role][host.room].indexOf(guest) > - 1;
119 124 },
120 125
121 - joinOwnerRoom(owner, user) {
122 - if (user && user.role === this.ROLES.OPERATOR) {
123 - if (!this.isUserInRoom(owner, user) && !this.isUserInRoom(user, owner)) {
124 - this.rooms[owner.role][owner.room].push(user);
125 - return owner.room;
126 + joinRoom(host, guest) {
127 + if (guest && guest.role === this.ROLES.OPERATOR) {
128 + if (!this.isUserInRoom(host, guest) && !this.isUserInRoom(guest, host)) {
129 + this.rooms[host.role][host.room].push(guest);
130 + return host.room;
126 131 } else {
127 - return this.rooms[owner.role][owner.room] ? owner.room : user.room;
132 + return this.rooms[host.role][host.room] ? host.room : guest.room;
133 + }
134 + }
135 + },
136 +
137 + leaveRoom(host, guest) {
138 + if (host.room === guest.room) {
139 + // host leaving his own room means throw out guest(s)
140 + this.rooms[host.role][host.room].forEach(user => {
141 + this.socketsMap[user.room].leave(host.room);
142 + });
143 + this.rooms[host.role][host.room] = [];
144 + } else {
145 + // operators are only allowed to leave other operators' rooms
146 + if (host.role === this.ROLES.OPERATOR) {
147 + const ix = this.rooms[host.role][host.room].indexOf(guest);
148 + this.rooms[host.role][host.room].splice(ix, 1);
128 149 }
129 150 }
130 151 },