Browse Source

feat(xmpp): allow setting resource on room jid (#940)

This functionality is being brought back for apps
that use ljm for its prosody logic but not for
video conferencing, ie Jitsi-Meet Spot. The desired
use is for Spot-TVs to be identifiable by their
jid alone and for the resource to be the part
that identifies them.
master
virtuacoplenny 5 years ago
parent
commit
a909e59486
No account linked to committer's email address
3 changed files with 66 additions and 30 deletions
  1. 48
    3
      JitsiConference.js
  2. 6
    2
      authenticateAndUpgradeRole.js
  3. 12
    25
      modules/xmpp/xmpp.js

+ 48
- 3
JitsiConference.js View File

32
 import Statistics from './modules/statistics/statistics';
32
 import Statistics from './modules/statistics/statistics';
33
 import Transcriber from './modules/transcription/transcriber';
33
 import Transcriber from './modules/transcription/transcriber';
34
 import GlobalOnErrorHandler from './modules/util/GlobalOnErrorHandler';
34
 import GlobalOnErrorHandler from './modules/util/GlobalOnErrorHandler';
35
+import RandomUtil from './modules/util/RandomUtil';
35
 import ComponentsVersions from './modules/version/ComponentsVersions';
36
 import ComponentsVersions from './modules/version/ComponentsVersions';
36
 import VideoSIPGW from './modules/videosipgw/VideoSIPGW';
37
 import VideoSIPGW from './modules/videosipgw/VideoSIPGW';
37
 import * as VideoSIPGWConstants from './modules/videosipgw/VideoSIPGWConstants';
38
 import * as VideoSIPGWConstants from './modules/videosipgw/VideoSIPGWConstants';
222
 // FIXME convert JitsiConference to ES6 - ASAP !
223
 // FIXME convert JitsiConference to ES6 - ASAP !
223
 JitsiConference.prototype.constructor = JitsiConference;
224
 JitsiConference.prototype.constructor = JitsiConference;
224
 
225
 
226
+/**
227
+ * Create a resource for the a jid. We use the room nickname (the resource part
228
+ * of the occupant JID, see XEP-0045) as the endpoint ID in colibri. We require
229
+ * endpoint IDs to be 8 hex digits because in some cases they get serialized
230
+ * into a 32bit field.
231
+ *
232
+ * @param {string} jid - The id set onto the XMPP connection.
233
+ * @param {boolean} isAuthenticatedUser - Whether or not the user has connected
234
+ * to the XMPP service with a password.
235
+ * @returns {string}
236
+ * @static
237
+ */
238
+JitsiConference.resourceCreator = function(jid, isAuthenticatedUser) {
239
+    let mucNickname;
240
+
241
+    if (isAuthenticatedUser) {
242
+        // For authenticated users generate a random ID.
243
+        mucNickname = RandomUtil.randomHexString(8).toLowerCase();
244
+    } else {
245
+        // We try to use the first part of the node (which for anonymous users
246
+        // on prosody is a UUID) to match the previous behavior (and maybe make
247
+        // debugging easier).
248
+        mucNickname = Strophe.getNodeFromJid(jid).substr(0, 8)
249
+            .toLowerCase();
250
+
251
+        // But if this doesn't have the required format we just generate a new
252
+        // random nickname.
253
+        const re = /[0-9a-f]{8}/g;
254
+
255
+        if (!re.test(mucNickname)) {
256
+            mucNickname = RandomUtil.randomHexString(8).toLowerCase();
257
+        }
258
+    }
259
+
260
+    return mucNickname;
261
+};
262
+
225
 /**
263
 /**
226
  * Initializes the conference object properties
264
  * Initializes the conference object properties
227
  * @param options {object}
265
  * @param options {object}
240
 
278
 
241
     const { config } = this.options;
279
     const { config } = this.options;
242
 
280
 
243
-    this.room = this.xmpp.createRoom(this.options.name, config);
281
+    this.room = this.xmpp.createRoom(
282
+        this.options.name,
283
+        config,
284
+        JitsiConference.resourceCreator
285
+    );
244
 
286
 
245
     // Connection interrupted/restored listeners
287
     // Connection interrupted/restored listeners
246
     this._onIceConnectionInterrupted
288
     this._onIceConnectionInterrupted
364
  * and (2) has a <tt>cancel</tt> method that allows the caller to interrupt the
406
  * and (2) has a <tt>cancel</tt> method that allows the caller to interrupt the
365
  * process.
407
  * process.
366
  */
408
  */
367
-JitsiConference.prototype.authenticateAndUpgradeRole = function(...args) {
368
-    return authenticateAndUpgradeRole.apply(this, args);
409
+JitsiConference.prototype.authenticateAndUpgradeRole = function(options) {
410
+    return authenticateAndUpgradeRole.call(this, {
411
+        ...options,
412
+        onCreateResource: JitsiConference.resourceCreator
413
+    });
369
 };
414
 };
370
 
415
 
371
 /**
416
 /**

+ 6
- 2
authenticateAndUpgradeRole.js View File

61
     // 1. Log the specified XMPP user in.
61
     // 1. Log the specified XMPP user in.
62
     id,
62
     id,
63
     password,
63
     password,
64
+    onCreateResource,
64
 
65
 
65
     // 2. Let the API client/consumer know as soon as the XMPP user has been
66
     // 2. Let the API client/consumer know as soon as the XMPP user has been
66
     //    successfully logged in.
67
     //    successfully logged in.
96
                 onLoginSuccessful && onLoginSuccessful();
97
                 onLoginSuccessful && onLoginSuccessful();
97
 
98
 
98
                 // Now authenticate with Jicofo and get a new session ID.
99
                 // Now authenticate with Jicofo and get a new session ID.
99
-                const room
100
-                    = xmpp.createRoom(this.options.name, this.options.config);
100
+                const room = xmpp.createRoom(
101
+                    this.options.name,
102
+                    this.options.config,
103
+                    onCreateResource
104
+                );
101
 
105
 
102
                 room.moderator.authenticate()
106
                 room.moderator.authenticate()
103
                     .then(() => {
107
                     .then(() => {

+ 12
- 25
modules/xmpp/xmpp.js View File

408
     }
408
     }
409
 
409
 
410
     /**
410
     /**
411
+     * Joins or creates a muc with the provided jid, created from the passed
412
+     * in room name and muc host and onCreateResource result.
411
      *
413
      *
412
-     * @param roomName
413
-     * @param options
414
+     * @param {string} roomName - The name of the muc to join.
415
+     * @param {Object} options - Configuration for how to join the muc.
416
+     * @param {Function} [onCreateResource] - Callback to invoke when a resource
417
+     * is to be added to the jid.
418
+     * @returns {Promise} Resolves with an instance of a strophe muc.
414
      */
419
      */
415
-    createRoom(roomName, options) {
420
+    createRoom(roomName, options, onCreateResource) {
416
         let roomjid = `${roomName}@${this.options.hosts.muc}/`;
421
         let roomjid = `${roomName}@${this.options.hosts.muc}/`;
417
 
422
 
418
-        let mucNickname;
419
-
420
-        // We use the room nickname (the resourcepart of the occupant JID, see XEP-0045)
421
-        // as the endpoint ID in colibri. We require endpoint IDs to be 8 hex digits because
422
-        // in some cases they get serialized into a 32bit field.
423
-        if (this.authenticatedUser) {
424
-            // For authenticated users generate a random ID.
425
-            mucNickname = RandomUtil.randomHexString(8).toLowerCase();
426
-        } else if (!this.authenticatedUser) {
427
-            // We try to use the first part of the node (which for anonymous users on prosody is a UUID) to match
428
-            // the previous behavior (and maybe make debugging easier).
429
-            mucNickname = Strophe.getNodeFromJid(this.connection.jid).substr(0, 8)
430
-                .toLowerCase();
431
-
432
-            // But if this doesn't have the required format we just generate a new random nickname.
433
-            const re = /[0-9a-f]{8}/g;
434
-
435
-            if (!re.test(mucNickname)) {
436
-                mucNickname = RandomUtil.randomHexString(8).toLowerCase();
437
-            }
438
-        }
423
+        const mucNickname = onCreateResource
424
+            ? onCreateResource(this.connection.jid, this.authenticatedUser)
425
+            : RandomUtil.randomHexString(8).toLowerCase();
439
 
426
 
440
-        logger.info(`JID ${this.connection.jid} using MUC nickname $mucNickname`);
427
+        logger.info(`JID ${this.connection.jid} using MUC nickname ${mucNickname}`);
441
         roomjid += mucNickname;
428
         roomjid += mucNickname;
442
 
429
 
443
         return this.connection.emuc.createRoom(roomjid, null, options);
430
         return this.connection.emuc.createRoom(roomjid, null, options);

Loading…
Cancel
Save