Bläddra i källkod

Splits a long function into two shorter functions.

dev1
Lyubomir Marinov 9 år sedan
förälder
incheckning
cad01c5c3d
1 ändrade filer med 116 tillägg och 86 borttagningar
  1. 116
    86
      modules/xmpp/moderator.js

+ 116
- 86
modules/xmpp/moderator.js Visa fil

@@ -246,103 +246,133 @@ Moderator.prototype.parseConfigOptions =  function (resultIq) {
246 246
     logger.info("Sip gateway enabled:  " + this.sipGatewayEnabled);
247 247
 };
248 248
 
249
-// FIXME =  we need to show the fact that we're waiting for the focus
250
-// to the user(or that focus is not available)
249
+// FIXME We need to show the fact that we're waiting for the focus to the user
250
+// (or that the focus is not available)
251
+/**
252
+ * Allocates the conference focus.
253
+ *
254
+ * @param {Function} callback - the function to be called back upon the
255
+ * successful allocation of the conference focus
256
+ */
251 257
 Moderator.prototype.allocateConferenceFocus =  function (callback) {
252 258
     // Try to use focus user JID from the config
253 259
     this.setFocusUserJid(this.xmppService.options.focusUserJid);
254 260
     // Send create conference IQ
255
-    var iq = this.createConferenceIq();
256 261
     var self = this;
257 262
     this.connection.sendIQ(
258
-        iq,
263
+        this.createConferenceIq(),
259 264
         function (result) {
260
-
261
-            // Setup config options
262
-            self.parseConfigOptions(result);
263
-
264
-            if ('true' === $(result).find('conference').attr('ready')) {
265
-                // Reset both timers
266
-                self.getNextTimeout(true);
267
-                self.getNextErrorTimeout(true);
268
-                // Exec callback
269
-                callback();
270
-            } else {
271
-                var waitMs = self.getNextTimeout();
272
-                logger.info("Waiting for the focus... " + waitMs);
273
-                // Reset error timeout
274
-                self.getNextErrorTimeout(true);
275
-                window.setTimeout(
276
-                    function () {
277
-                        self.allocateConferenceFocus(callback);
278
-                    }, waitMs);
279
-            }
265
+            self._allocateConferenceFocusSuccess(result, callback);
280 266
         },
281 267
         function (error) {
282
-            // Invalid session ? remove and try again
283
-            // without session ID to get a new one
284
-            var invalidSession
285
-                = $(error).find('>error>session-invalid').length;
286
-            if (invalidSession) {
287
-                logger.info("Session expired! - removing");
288
-                self.settings.clearSessionId();
289
-            }
290
-            if ($(error).find('>error>graceful-shutdown').length) {
291
-                self.eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
292
-                return;
293
-            }
294
-            // Check for error returned by the reservation system
295
-            var reservationErr = $(error).find('>error>reservation-error');
296
-            if (reservationErr.length) {
297
-                // Trigger error event
298
-                var errorCode = reservationErr.attr('error-code');
299
-                var errorMsg;
300
-                if ($(error).find('>error>text')) {
301
-                    errorMsg = $(error).find('>error>text').text();
302
-                }
303
-                self.eventEmitter.emit(
304
-                    XMPPEvents.RESERVATION_ERROR, errorCode, errorMsg);
305
-                return;
306
-            }
307
-            // Not authorized to create new room
308
-            if ($(error).find('>error>not-authorized').length) {
309
-                logger.warn("Unauthorized to start the conference", error);
310
-                var toDomain
311
-                    = Strophe.getDomainFromJid(error.getAttribute('to'));
312
-                if (toDomain !==
313
-                    self.xmppService.options.hosts.anonymousdomain) {
314
-                    //FIXME:  "is external" should come either from
315
-                    // the focus or config.js
316
-                    self.externalAuthEnabled = true;
317
-                }
318
-                self.eventEmitter.emit(
319
-                    XMPPEvents.AUTHENTICATION_REQUIRED,
320
-                    function () {
321
-                        self.allocateConferenceFocus(
322
-                            callback);
323
-                    });
324
-                return;
325
-            }
326
-            var waitMs = self.getNextErrorTimeout();
327
-            logger.error("Focus error, retry after " + waitMs, error);
328
-            // Show message
329
-            var focusComponent = self.getFocusComponent();
330
-            var retrySec = waitMs / 1000;
331
-            //FIXME:  message is duplicated ?
332
-            // Do not show in case of session invalid
333
-            // which means just a retry
334
-            if (!invalidSession) {
335
-                self.eventEmitter.emit(XMPPEvents.FOCUS_DISCONNECTED,
336
-                    focusComponent, retrySec);
337
-            }
338
-            // Reset response timeout
339
-            self.getNextTimeout(true);
340
-            window.setTimeout(
268
+            self._allocateConferenceFocusError(error);
269
+        });
270
+};
271
+
272
+/**
273
+ * Invoked by {@link #allocateConferenceFocus} upon its request receiving an
274
+ * error result.
275
+ *
276
+ * @param error - the error result of the request that
277
+ * {@link #allocateConferenceFocus} sent
278
+ * @param {Function} callback - the function to be called back upon the
279
+ * successful allocation of the conference focus
280
+ */
281
+Moderator.prototype._allocateConferenceFocusError = function (error, callback) {
282
+    var self = this;
283
+
284
+    // If the session is invalid, remove and try again without session ID to get
285
+    // a new one
286
+    var invalidSession = $(error).find('>error>session-invalid').length;
287
+    if (invalidSession) {
288
+        logger.info("Session expired! - removing");
289
+        self.settings.clearSessionId();
290
+    }
291
+    if ($(error).find('>error>graceful-shutdown').length) {
292
+        self.eventEmitter.emit(XMPPEvents.GRACEFUL_SHUTDOWN);
293
+        return;
294
+    }
295
+    // Check for error returned by the reservation system
296
+    var reservationErr = $(error).find('>error>reservation-error');
297
+    if (reservationErr.length) {
298
+        // Trigger error event
299
+        var errorCode = reservationErr.attr('error-code');
300
+        var errorTextNode = $(error).find('>error>text');
301
+        var errorMsg;
302
+        if (errorTextNode) {
303
+            errorMsg = errorTextNode.text();
304
+        }
305
+        self.eventEmitter.emit(
306
+                XMPPEvents.RESERVATION_ERROR, errorCode, errorMsg);
307
+        return;
308
+    }
309
+    // Not authorized to create new room
310
+    if ($(error).find('>error>not-authorized').length) {
311
+        logger.warn("Unauthorized to start the conference", error);
312
+        var toDomain = Strophe.getDomainFromJid(error.getAttribute('to'));
313
+        if (toDomain !== self.xmppService.options.hosts.anonymousdomain) {
314
+            //FIXME "is external" should come either from the focus or config.js
315
+            self.externalAuthEnabled = true;
316
+        }
317
+        self.eventEmitter.emit(
318
+                XMPPEvents.AUTHENTICATION_REQUIRED,
341 319
                 function () {
342 320
                     self.allocateConferenceFocus(callback);
343
-                }, waitMs);
344
-        }
345
-    );
321
+                });
322
+        return;
323
+    }
324
+    var waitMs = self.getNextErrorTimeout();
325
+    logger.error("Focus error, retry after " + waitMs, error);
326
+    // Show message
327
+    var focusComponent = self.getFocusComponent();
328
+    var retrySec = waitMs / 1000;
329
+    //FIXME: message is duplicated ? Do not show in case of session invalid
330
+    // which means just a retry
331
+    if (!invalidSession) {
332
+        self.eventEmitter.emit(
333
+                XMPPEvents.FOCUS_DISCONNECTED, focusComponent, retrySec);
334
+    }
335
+    // Reset response timeout
336
+    self.getNextTimeout(true);
337
+    window.setTimeout(
338
+            function () {
339
+                self.allocateConferenceFocus(callback);
340
+            },
341
+            waitMs);
342
+};
343
+
344
+/**
345
+ * Invoked by {@link #allocateConferenceFocus} upon its request receiving a
346
+ * success (i.e. non-error) result.
347
+ *
348
+ * @param result - the success (i.e. non-error) result of the request that
349
+ * {@link #allocateConferenceFocus} sent
350
+ * @param {Function} callback - the function to be called back upon the
351
+ * successful allocation of the conference focus
352
+ */
353
+Moderator.prototype._allocateConferenceFocusSuccess = function (
354
+        result,
355
+        callback) {
356
+    // Setup config options
357
+    this.parseConfigOptions(result);
358
+
359
+    // Reset the error timeout (because we haven't failed here).
360
+    this.getNextErrorTimeout(true);
361
+    if ('true' === $(result).find('conference').attr('ready')) {
362
+        // Reset the non-error timeout (because we've succeeded here).
363
+        this.getNextTimeout(true);
364
+        // Exec callback
365
+        callback();
366
+    } else {
367
+        var waitMs = this.getNextTimeout();
368
+        logger.info("Waiting for the focus... " + waitMs);
369
+        var self = this;
370
+        window.setTimeout(
371
+                function () {
372
+                    self.allocateConferenceFocus(callback);
373
+                },
374
+                waitMs);
375
+    }
346 376
 };
347 377
 
348 378
 Moderator.prototype.authenticate = function () {

Laddar…
Avbryt
Spara