| 1 | 
           efrain | 
           1 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           2 | 
           //
  | 
        
        
            | 
            | 
           3 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           4 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           5 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           6 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           7 | 
           //
  | 
        
        
            | 
            | 
           8 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           9 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           10 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           11 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           12 | 
           //
  | 
        
        
            | 
            | 
           13 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           14 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           15 | 
              | 
        
        
            | 
            | 
           16 | 
           /**
  | 
        
        
            | 
            | 
           17 | 
            * JS room updater.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @module      mod_bigbluebuttonbn/roomupdater
  | 
        
        
            | 
            | 
           20 | 
            * @copyright   2021 Blindside Networks Inc
  | 
        
        
            | 
            | 
           21 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           22 | 
            */
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           import Pending from 'core/pending';
  | 
        
        
            | 
            | 
           25 | 
           import Templates from "core/templates";
  | 
        
        
            | 
            | 
           26 | 
           import {exception as displayException} from 'core/notification';
  | 
        
        
            | 
            | 
           27 | 
           import {getMeetingInfo} from './repository';
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           let timerReference = null;
  | 
        
        
            | 
            | 
           30 | 
           let timerRunning = false;
  | 
        
        
            | 
            | 
           31 | 
           let pollInterval = 0;
  | 
        
        
            | 
            | 
           32 | 
           let pollIntervalFactor = 1;
  | 
        
        
            | 
            | 
           33 | 
           const MAX_POLL_INTERVAL_FACTOR = 10;
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
           const resetValues = () => {
  | 
        
        
            | 
            | 
           36 | 
               timerRunning = false;
  | 
        
        
            | 
            | 
           37 | 
               timerReference = null;
  | 
        
        
            | 
            | 
           38 | 
               pollInterval = 0;
  | 
        
        
            | 
            | 
           39 | 
               pollIntervalFactor = 1;
  | 
        
        
            | 
            | 
           40 | 
           };
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
           /**
  | 
        
        
            | 
            | 
           43 | 
            * Start the information poller.
  | 
        
        
            | 
            | 
           44 | 
            * @param {Number} interval interval in miliseconds between each poll action.
  | 
        
        
            | 
            | 
           45 | 
            */
  | 
        
        
            | 
            | 
           46 | 
           export const start = (interval) => {
  | 
        
        
            | 
            | 
           47 | 
               resetValues();
  | 
        
        
            | 
            | 
           48 | 
               timerRunning = true;
  | 
        
        
            | 
            | 
           49 | 
               pollInterval = interval;
  | 
        
        
            | 
            | 
           50 | 
               poll();
  | 
        
        
            | 
            | 
           51 | 
           };
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
           /**
  | 
        
        
            | 
            | 
           54 | 
            * Stop the room updater.
  | 
        
        
            | 
            | 
           55 | 
            */
  | 
        
        
            | 
            | 
           56 | 
           export const stop = () => {
  | 
        
        
            | 
            | 
           57 | 
               if (timerReference) {
  | 
        
        
            | 
            | 
           58 | 
                   clearTimeout(timerReference);
  | 
        
        
            | 
            | 
           59 | 
               }
  | 
        
        
            | 
            | 
           60 | 
               resetValues();
  | 
        
        
            | 
            | 
           61 | 
           };
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
           /**
  | 
        
        
            | 
            | 
           64 | 
            * Start the information poller.
  | 
        
        
            | 
            | 
           65 | 
            */
  | 
        
        
            | 
            | 
           66 | 
           const poll = () => {
  | 
        
        
            | 
            | 
           67 | 
               if (!timerRunning || !pollInterval) {
  | 
        
        
            | 
            | 
           68 | 
                   // The poller has been stopped.
  | 
        
        
            | 
            | 
           69 | 
                   return;
  | 
        
        
            | 
            | 
           70 | 
               }
  | 
        
        
            | 
            | 
           71 | 
               updateRoom()
  | 
        
        
            | 
            | 
           72 | 
                   .then((updateOk) => {
  | 
        
        
            | 
            | 
           73 | 
                       if (!updateOk) {
  | 
        
        
            | 
            | 
           74 | 
                           pollIntervalFactor = (pollIntervalFactor < MAX_POLL_INTERVAL_FACTOR) ?
  | 
        
        
            | 
            | 
           75 | 
                               pollIntervalFactor + 1 : MAX_POLL_INTERVAL_FACTOR;
  | 
        
        
            | 
            | 
           76 | 
                           // We make sure if there is an error that we do not try too often.
  | 
        
        
            | 
            | 
           77 | 
                       }
  | 
        
        
            | 
            | 
           78 | 
                       timerReference = setTimeout(() => poll(), pollInterval * pollIntervalFactor);
  | 
        
        
            | 
            | 
           79 | 
                       return true;
  | 
        
        
            | 
            | 
           80 | 
                   })
  | 
        
        
            | 
            | 
           81 | 
                   .catch();
  | 
        
        
            | 
            | 
           82 | 
           };
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
           /**
  | 
        
        
            | 
            | 
           85 | 
            * Update the room information.
  | 
        
        
            | 
            | 
           86 | 
            *
  | 
        
        
            | 
            | 
           87 | 
            * @param {boolean} [updatecache=false] should we update cache
  | 
        
        
            | 
            | 
           88 | 
            * @returns {Promise}
  | 
        
        
            | 
            | 
           89 | 
            */
  | 
        
        
            | 
            | 
           90 | 
           export const updateRoom = (updatecache = false) => {
  | 
        
        
            | 
            | 
           91 | 
               const bbbRoomViewElement = document.getElementById('bigbluebuttonbn-room-view');
  | 
        
        
            | 
            | 
           92 | 
               if (bbbRoomViewElement === null) {
  | 
        
        
            | 
            | 
           93 | 
                   return Promise.resolve(false);
  | 
        
        
            | 
            | 
           94 | 
               }
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
            | 
            | 
           96 | 
               const bbbId = bbbRoomViewElement.dataset.bbbId;
  | 
        
        
            | 
            | 
           97 | 
               const groupId = bbbRoomViewElement.dataset.groupId;
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
               const pendingPromise = new Pending('mod_bigbluebuttonbn/roomupdater:updateRoom');
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
               return getMeetingInfo(bbbId, groupId, updatecache)
  | 
        
        
            | 
            | 
           102 | 
                   .then(data => {
  | 
        
        
            | 
            | 
           103 | 
                       // Just make sure we have the right information for the template.
  | 
        
        
            | 
            | 
           104 | 
                       data.haspresentations = !!(data.presentations && data.presentations.length);
  | 
        
        
            | 
            | 
           105 | 
                       return Templates.renderForPromise('mod_bigbluebuttonbn/room_view', data);
  | 
        
        
            | 
            | 
           106 | 
                   })
  | 
        
        
            | 
            | 
           107 | 
                   .then(({html, js}) => Templates.replaceNode(bbbRoomViewElement, html, js))
  | 
        
        
            | 
            | 
           108 | 
                   .then(() => pendingPromise.resolve())
  | 
        
        
            | 
            | 
           109 | 
                   .catch(displayException);
  | 
        
        
            | 
            | 
           110 | 
           };
  |