Browse Source

store userId and callStatsUserName globally (was per-conference)

move sessionId reading/storing into Settings.js
master
isymchych 9 years ago
parent
commit
e2154a2a5f
4 changed files with 97 additions and 59 deletions
  1. 0
    1
      .jshintignore
  2. 1
    1
      JitsiConference.js
  3. 86
    47
      modules/settings/Settings.js
  4. 10
    10
      modules/xmpp/moderator.js

+ 0
- 1
.jshintignore View File

@@ -18,7 +18,6 @@ modules/xmpp/ChatRoom.js
18 18
 modules/xmpp/ChatRoom.js
19 19
 modules/statistics/RTPStatsCollector.js
20 20
 modules/statistics/LocalStatsCollector.js
21
-modules/settings/Settings.js
22 21
 modules/connectionquality/connectionquality.js
23 22
 modules/RTC/adapter.screenshare.js
24 23
 modules/statistics/statistics.js

+ 1
- 1
JitsiConference.js View File

@@ -34,7 +34,7 @@ function JitsiConference(options) {
34 34
     this.xmpp = this.connection.xmpp;
35 35
     this.eventEmitter = new EventEmitter();
36 36
     var confID = this.options.name  + '@' + this.xmpp.options.hosts.muc;
37
-    this.settings = new Settings(confID);
37
+    this.settings = new Settings();
38 38
     this.room = this.xmpp.createRoom(this.options.name, this.options.config,
39 39
         this.settings);
40 40
     this.room.updateDeviceAvailability(RTC.getDeviceAvailability());

+ 86
- 47
modules/settings/Settings.js View File

@@ -2,11 +2,14 @@ var logger = require("jitsi-meet-logger").getLogger(__filename);
2 2
 
3 3
 var UsernameGenerator = require('../util/UsernameGenerator');
4 4
 
5
+/**
6
+ * Check if browser supports localStorage.
7
+ * @returns {boolean} true if supports, false otherwise
8
+ */
5 9
 function supportsLocalStorage() {
6 10
     try {
7 11
         return 'localStorage' in window && window.localStorage !== null;
8 12
     } catch (e) {
9
-        logger.log("localstorage is not supported");
10 13
         return false;
11 14
     }
12 15
 }
@@ -19,62 +22,68 @@ function generateUniqueId() {
19 22
     return _p8() + _p8() + _p8() + _p8();
20 23
 }
21 24
 
22
-function Settings(conferenceID) {
23
-    this.displayName = '';
25
+/**
26
+ * Generate unique id.
27
+ * @returns {string} random unique id
28
+ */
29
+function generateJitsiMeetId() {
30
+    var jitsiMeetId = generateUniqueId();
31
+    logger.log("generated id", jitsiMeetId);
32
+
33
+    return jitsiMeetId;
34
+}
35
+
36
+/**
37
+ * Generate fake username for callstats.
38
+ * @returns {string} fake random username
39
+ */
40
+function generateCallStatsUsername() {
41
+    var username = UsernameGenerator.generateUsername();
42
+    logger.log('generated callstats uid', username);
43
+
44
+    return username;
45
+}
46
+
47
+function Settings() {
24 48
     this.userId;
25
-    this.confSettings = null;
26
-    this.conferenceID = conferenceID;
27 49
     this.callStatsUserName;
50
+
28 51
     if (supportsLocalStorage()) {
29
-        if(!window.localStorage.getItem(conferenceID))
30
-            this.confSettings = {};
31
-        else
32
-            this.confSettings = JSON.parse(window.localStorage.getItem(conferenceID));
33
-        if(!this.confSettings.jitsiMeetId) {
34
-            this.confSettings.jitsiMeetId = generateUniqueId();
35
-            logger.log("generated id", this.confSettings.jitsiMeetId);
36
-            this.save();
37
-        }
38
-        if (!this.confSettings.callStatsUserName) {
39
-            this.confSettings.callStatsUserName
40
-                = UsernameGenerator.generateUsername();
41
-            logger.log('generated callstats uid',
42
-                this.confSettings.callStatsUserName);
43
-            this.save();
44
-        }
45
-
46
-        this.userId = this.confSettings.jitsiMeetId || '';
47
-        this.displayName = this.confSettings.displayname || '';
48
-        this.callStatsUserName = this.confSettings.callStatsUserName || '';
52
+        this.userId = window.localStorage.getItem('jitsiMeetId')
53
+            || generateJitsiMeetId();
54
+
55
+
56
+        this.callStatsUserName = window.localStorage.getItem(
57
+            'callStatsUserName'
58
+        ) || generateCallStatsUsername();
59
+
60
+        this.save();
49 61
     } else {
50
-        logger.log("local storage is not supported");
51
-        this.userId = generateUniqueId();
52
-        this.callStatsUserName = UsernameGenerator.generateUsername();
62
+        logger.log("localStorage is not supported");
63
+        this.userId = generateJitsiMeetId();
64
+        this.callStatsUserName = generateCallStatsUsername();
53 65
     }
54 66
 }
55 67
 
68
+/**
69
+ * Save settings to localStorage if browser supports that.
70
+ */
56 71
 Settings.prototype.save = function () {
57
-    if (supportsLocalStorage()) {
58
-        window.localStorage.setItem(
59
-            this.conferenceID, JSON.stringify(this.confSettings)
60
-        );
72
+    if (!supportsLocalStorage()) {
73
+        return;
61 74
     }
62
-};
63 75
 
64
-Settings.prototype.setDisplayName = function (newDisplayName) {
65
-    this.displayName = newDisplayName;
66
-    if(this.confSettings != null)
67
-        this.confSettings.displayname = displayName;
68
-    this.save();
69
-    return this.displayName;
70
-}
76
+    window.localStorage.setItem('jitsiMeetId', this.userId);
77
+    window.localStorage.setItem('callStatsUserName', this.callStatsUserName);
78
+};
71 79
 
72
-Settings.prototype.getSettings = function () {
73
-    return {
74
-        displayName: this.displayName,
75
-        uid: this.userId
76
-    };
77
-}
80
+/**
81
+ * Returns current user id.
82
+ * @returns {string} user id
83
+ */
84
+Settings.prototype.getUserId = function () {
85
+    return this.userId;
86
+};
78 87
 
79 88
 /**
80 89
  * Returns fake username for callstats
@@ -82,6 +91,36 @@ Settings.prototype.getSettings = function () {
82 91
  */
83 92
 Settings.prototype.getCallStatsUserName = function () {
84 93
     return this.callStatsUserName;
85
-}
94
+};
95
+
96
+/**
97
+ * Save current session id.
98
+ * @param {string} sessionId session id
99
+ */
100
+Settings.prototype.setSessionId = function (sessionId) {
101
+    if (sessionId) {
102
+        window.localStorage.setItem('sessionId', sessionId);
103
+    } else {
104
+        window.localStorage.removeItem('sessionId');
105
+    }
106
+};
107
+
108
+/**
109
+ * Clear current session id.
110
+ */
111
+Settings.prototype.clearSessionId = function () {
112
+    this.setSessionId(undefined);
113
+};
114
+
115
+/**
116
+ * Returns current session id.
117
+ * @returns {string} current session id
118
+ */
119
+Settings.prototype.getSessionId = function () {
120
+    // we can update session id in localStorage from
121
+    // another JitsiConference instance
122
+    // thats why we should always re-read it
123
+    return window.localStorage.getItem('sessionId');
124
+};
86 125
 
87 126
 module.exports = Settings;

+ 10
- 10
modules/xmpp/moderator.js View File

@@ -48,7 +48,7 @@ function Moderator(roomName, xmpp, emitter, settings) {
48 48
                     event.origin);
49 49
                 return;
50 50
             }
51
-            localStorage.setItem('sessionId', event.data.sessionId);
51
+            settings.setSessionId(event.data.sessionId);
52 52
             // After popup is closed we will authenticate
53 53
         }
54 54
     }
@@ -107,8 +107,8 @@ Moderator.prototype.createConferenceIq =  function () {
107 107
     var elem = $iq({to: this.getFocusComponent(), type: 'set'});
108 108
 
109 109
     // Session Id used for authentication
110
-    var sessionId = localStorage.getItem('sessionId');
111
-    var machineUID = this.settings.getSettings().uid;
110
+    var sessionId = this.settings.getSessionId();
111
+    var machineUID = this.settings.getUserId();
112 112
 
113 113
     logger.info(
114 114
             "Session ID: " + sessionId + " machine UID: " + machineUID);
@@ -202,7 +202,7 @@ Moderator.prototype.parseSessionId =  function (resultIq) {
202 202
     var sessionId = $(resultIq).find('conference').attr('session-id');
203 203
     if (sessionId) {
204 204
         logger.info('Received sessionId:  ' + sessionId);
205
-        localStorage.setItem('sessionId', sessionId);
205
+        this.settings.setSessionId(sessionId);
206 206
     }
207 207
 };
208 208
 
@@ -285,7 +285,7 @@ Moderator.prototype.allocateConferenceFocus =  function (callback) {
285 285
                 = $(error).find('>error>session-invalid').length;
286 286
             if (invalidSession) {
287 287
                 logger.info("Session expired! - removing");
288
-                localStorage.removeItem("sessionId");
288
+                self.settings.clearSessionId();
289 289
             }
290 290
             if ($(error).find('>error>graceful-shutdown').length) {
291 291
                 self.eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
@@ -366,7 +366,7 @@ Moderator.prototype.getLoginUrl =  function (urlCallback, failureCallback) {
366 366
     iq.c('login-url', {
367 367
         xmlns: 'http://jitsi.org/protocol/focus',
368 368
         room: this.roomName,
369
-        'machine-uid': this.settings.getSettings().uid
369
+        'machine-uid': this.settings.getUserId()
370 370
     });
371 371
     this.connection.sendIQ(
372 372
         iq,
@@ -394,7 +394,7 @@ Moderator.prototype.getPopupLoginUrl = function (urlCallback, failureCallback) {
394 394
     iq.c('login-url', {
395 395
         xmlns: 'http://jitsi.org/protocol/focus',
396 396
         room: this.roomName,
397
-        'machine-uid': this.settings.getSettings().uid,
397
+        'machine-uid': this.settings.getUserId(),
398 398
         popup: true
399 399
     });
400 400
     this.connection.sendIQ(
@@ -420,7 +420,7 @@ Moderator.prototype.getPopupLoginUrl = function (urlCallback, failureCallback) {
420 420
 
421 421
 Moderator.prototype.logout =  function (callback) {
422 422
     var iq = $iq({to: this.getFocusComponent(), type: 'set'});
423
-    var sessionId = localStorage.getItem('sessionId');
423
+    var sessionId = this.settings.getSessionId();
424 424
     if (!sessionId) {
425 425
         callback();
426 426
         return;
@@ -437,9 +437,9 @@ Moderator.prototype.logout =  function (callback) {
437 437
                 logoutUrl = decodeURIComponent(logoutUrl);
438 438
             }
439 439
             logger.info("Log out OK, url: " + logoutUrl, result);
440
-            localStorage.removeItem('sessionId');
440
+            this.settings.clearSessionId();
441 441
             callback(logoutUrl);
442
-        },
442
+        }.bind(this),
443 443
         function (error) {
444 444
             logger.error("Logout error", error);
445 445
         }

Loading…
Cancel
Save