Browse Source

fix: Checks presence editing and make sure we send only on change.

tags/v0.0.2
damencho 4 years ago
parent
commit
e83fb93d2d
4 changed files with 29 additions and 26 deletions
  1. 5
    9
      JitsiConference.js
  2. 2
    2
      modules/xmpp/Caps.js
  3. 19
    10
      modules/xmpp/ChatRoom.js
  4. 3
    5
      modules/xmpp/Lobby.js

+ 5
- 9
JitsiConference.js View File

@@ -881,8 +881,7 @@ JitsiConference.prototype.sendPrivateTextMessage = function(
881 881
  **/
882 882
 JitsiConference.prototype.sendCommand = function(name, values) {
883 883
     if (this.room) {
884
-        this.room.addToPresence(name, values);
885
-        this.room.sendPresence();
884
+        this.room.addOrReplaceInPresence(name, values) && this.room.sendPresence();
886 885
     } else {
887 886
         logger.warn('Not sending a command, room not initialized.');
888 887
     }
@@ -915,11 +914,10 @@ JitsiConference.prototype.removeCommand = function(name) {
915 914
  */
916 915
 JitsiConference.prototype.setDisplayName = function(name) {
917 916
     if (this.room) {
918
-        this.room.addToPresence('nick', {
917
+        this.room.addOrReplaceInPresence('nick', {
919 918
             attributes: { xmlns: 'http://jabber.org/protocol/nick' },
920 919
             value: name
921
-        });
922
-        this.room.sendPresence();
920
+        }) && this.room.sendPresence();
923 921
     }
924 922
 };
925 923
 
@@ -1172,7 +1170,6 @@ JitsiConference.prototype._setupNewTrack = function(newTrack) {
1172 1170
         }
1173 1171
     }
1174 1172
     if (newTrack.isVideoTrack()) {
1175
-        this.removeCommand('videoType');
1176 1173
         this.sendCommand('videoType', {
1177 1174
             value: newTrack.videoType,
1178 1175
             attributes: {
@@ -2370,14 +2367,13 @@ JitsiConference.prototype.setStartMutedPolicy = function(policy) {
2370 2367
         return;
2371 2368
     }
2372 2369
     this.startMutedPolicy = policy;
2373
-    this.room.addToPresence('startmuted', {
2370
+    this.room.addOrReplaceInPresence('startmuted', {
2374 2371
         attributes: {
2375 2372
             audio: policy.audio,
2376 2373
             video: policy.video,
2377 2374
             xmlns: 'http://jitsi.org/jitmeet/start-muted'
2378 2375
         }
2379
-    });
2380
-    this.room.sendPresence();
2376
+    }) && this.room.sendPresence();
2381 2377
 };
2382 2378
 
2383 2379
 /**

+ 2
- 2
modules/xmpp/Caps.js View File

@@ -162,7 +162,7 @@ export default class Caps extends Listenable {
162 162
                 });
163 163
             });
164 164
 
165
-            room.addToPresence('features', { children });
165
+            room.addOrReplaceInPresence('features', { children });
166 166
         }
167 167
     }
168 168
 
@@ -235,7 +235,7 @@ export default class Caps extends Listenable {
235 235
      * @param {ChatRoom} room the room.
236 236
      */
237 237
     _fixChatRoomPresenceMap(room) {
238
-        room.addToPresence('c', {
238
+        room.addOrReplaceInPresence('c', {
239 239
             attributes: {
240 240
                 xmlns: Strophe.NS.CAPS,
241 241
                 hash: HASH,

+ 19
- 10
modules/xmpp/ChatRoom.js View File

@@ -1382,14 +1382,26 @@ export default class ChatRoom extends Listenable {
1382 1382
 
1383 1383
     /**
1384 1384
      * Adds the key to the presence map, overriding any previous value.
1385
-     * @param key
1386
-     * @param values
1385
+     * @param key The key to add or replace.
1386
+     * @param values The new values.
1387
+     * @returns {boolean|null} <tt>true</tt> if the operation succeeded or <tt>false</tt> when no add or replce was
1388
+     * performed as the value was already there.
1387 1389
      */
1388
-    addToPresence(key, values) {
1390
+    addOrReplaceInPresence(key, values) {
1389 1391
         values.tagName = key;
1392
+
1393
+        const matchingNodes = this.presMap.nodes.filter(node => key === node.tagName);
1394
+
1395
+        // if we have found just one, let's check is it the same
1396
+        if (matchingNodes.length === 1 && isEqual(matchingNodes[0], values)) {
1397
+            return false;
1398
+        }
1399
+
1390 1400
         this.removeFromPresence(key);
1391 1401
         this.presMap.nodes.push(values);
1392 1402
         this.presenceUpdateTime = Date.now();
1403
+
1404
+        return true;
1393 1405
     }
1394 1406
 
1395 1407
     /**
@@ -1515,7 +1527,7 @@ export default class ChatRoom extends Listenable {
1515 1527
      * @param mute
1516 1528
      */
1517 1529
     addAudioInfoToPresence(mute) {
1518
-        this.addToPresence(
1530
+        return this.addOrReplaceInPresence(
1519 1531
             'audiomuted',
1520 1532
             {
1521 1533
                 attributes: { 'xmlns': 'http://jitsi.org/jitmeet/audio' },
@@ -1529,10 +1541,8 @@ export default class ChatRoom extends Listenable {
1529 1541
      * @param callback
1530 1542
      */
1531 1543
     sendAudioInfoPresence(mute, callback) {
1532
-        this.addAudioInfoToPresence(mute);
1533
-
1534 1544
         // FIXME resend presence on CONNECTED
1535
-        this.sendPresence();
1545
+        this.addAudioInfoToPresence(mute) && this.sendPresence();
1536 1546
         if (callback) {
1537 1547
             callback();
1538 1548
         }
@@ -1543,7 +1553,7 @@ export default class ChatRoom extends Listenable {
1543 1553
      * @param mute
1544 1554
      */
1545 1555
     addVideoInfoToPresence(mute) {
1546
-        this.addToPresence(
1556
+        return this.addOrReplaceInPresence(
1547 1557
             'videomuted',
1548 1558
             {
1549 1559
                 attributes: { 'xmlns': 'http://jitsi.org/jitmeet/video' },
@@ -1556,8 +1566,7 @@ export default class ChatRoom extends Listenable {
1556 1566
      * @param mute
1557 1567
      */
1558 1568
     sendVideoInfoPresence(mute) {
1559
-        this.addVideoInfoToPresence(mute);
1560
-        this.sendPresence();
1569
+        this.addVideoInfoToPresence(mute) && this.sendPresence();
1561 1570
     }
1562 1571
 
1563 1572
     /**

+ 3
- 5
modules/xmpp/Lobby.js View File

@@ -153,8 +153,7 @@ export default class Lobby {
153 153
 
154 154
         if (displayName) {
155 155
             // remove previously set nickname
156
-            this.lobbyRoom.removeFromPresence('nick');
157
-            this.lobbyRoom.addToPresence('nick', {
156
+            this.lobbyRoom.addOrReplaceInPresence('nick', {
158 157
                 attributes: { xmlns: 'http://jabber.org/protocol/nick' },
159 158
                 value: displayName
160 159
             });
@@ -261,9 +260,8 @@ export default class Lobby {
261 260
 
262 261
                 // send our email, as we do not handle this on initial presence we need a second one
263 262
                 if (email && !isModerator) {
264
-                    this.lobbyRoom.removeFromPresence(EMAIL_COMMAND);
265
-                    this.lobbyRoom.addToPresence(EMAIL_COMMAND, { value: email });
266
-                    this.lobbyRoom.sendPresence();
263
+                    this.lobbyRoom.addOrReplaceInPresence(EMAIL_COMMAND, { value: email })
264
+                        && this.lobbyRoom.sendPresence();
267 265
                 }
268 266
             });
269 267
             this.lobbyRoom.addEventListener(XMPPEvents.ROOM_JOIN_ERROR, reject);

Loading…
Cancel
Save