Advertisement
Guest User

Untitled

a guest
Aug 17th, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 7.00 KB | Source Code | 0 0
  1. var BASE_DOMAIN = "my.base.domain.com";
  2. var INSTRUCTIONS_LINK = "https://my.guide.page.domain.com/jitsi_guide";
  3. //var MORE_NUMBERS_LINK = "https://" + BASE_DOMAIN + "/static/dialInInfo.html?room=";
  4. var PHONE_NUMBERS_LIST_LINK = "https://api.jitsi.net/phoneNumberList?calendar=true";
  5. var CONF_MAPPER_LINK = "https://api.jitsi.net/conferenceMapper?conference=";
  6. var MUC_HOST = "@conference." + BASE_DOMAIN;
  7.  
  8. /**
  9.  *  Creates a conference, then builds and returns a ConferenceData object
  10.  *  with the corresponding conference information. This method is called
  11.  *  when a user selects a conference solution defined by the add-on that
  12.  *  uses this function as its 'onCreateFunction' in the add-on manifest.
  13.  *
  14.  *  @param {Object} arg The default argument passed to a 'onCreateFunction';
  15.  *      it carries information about the Google Calendar event.
  16.  *  @return {ConferenceData}
  17.  */
  18. function createConference(arg) {
  19.   const eventData = arg.eventData;
  20.   const calendarId = eventData.calendarId;
  21.   const eventId = eventData.eventId;
  22.  
  23.   // Retrieve the Calendar event information using the Calendar
  24.   // Advanced service.
  25.   var calendarEvent;
  26.   try {
  27.     calendarEvent = Calendar.Events.get(calendarId, eventId);
  28.   } catch (err) {
  29.     // The calendar event does not exist just yet; just proceed with the
  30.     // given event ID and allow the event details to sync later.
  31.     console.log(err);
  32.     calendarEvent = {
  33.       id: eventId,
  34.     };
  35.   }
  36.  
  37.   // Create a conference on the third-party service and return the
  38.   // conference data or errors in a custom JSON object.
  39.   var conferenceInfo = create3rdPartyConference(calendarId);
  40.  
  41.   // Build and return a ConferenceData object, either with conference or
  42.   // error information.
  43.   var dataBuilder = ConferenceDataService.newConferenceDataBuilder();
  44.  
  45.   if (!conferenceInfo.error) {
  46.     // No error, so build the ConferenceData object from the
  47.     // returned conference info.
  48.     var conferenceTypeParameter = ConferenceDataService.newConferenceParameter()
  49.         .setKey('conferenceSolutionType')
  50.         .setValue('jitsi');
  51.  
  52.     dataBuilder.setConferenceId(conferenceInfo.id)
  53.         .addConferenceParameter(conferenceTypeParameter)
  54.    
  55.     var moreEntryPoint = ConferenceDataService.newEntryPoint()
  56.         .setEntryPointType(ConferenceDataService.EntryPointType.MORE)
  57.         .setUri(conferenceInfo.moreLink);
  58.       dataBuilder.addEntryPoint(moreEntryPoint);
  59.  
  60.     if (conferenceInfo.videoUri) {
  61.       var videoEntryPoint = ConferenceDataService.newEntryPoint()
  62.           .setEntryPointType(ConferenceDataService.EntryPointType.VIDEO)
  63.           .setUri(conferenceInfo.videoUri);
  64.       dataBuilder.addEntryPoint(videoEntryPoint);
  65.     }
  66.   } else {
  67.     // Other error type;
  68.     var error = ConferenceDataService.newConferenceError()
  69.         .setConferenceErrorType(
  70.             ConferenceDataService.ConferenceErrorType.TEMPORARY);
  71.     dataBuilder.setError(error);
  72.   }
  73.  
  74.   // Don't forget to build the ConferenceData object.
  75.   return dataBuilder.build();
  76. }
  77.  
  78. /**
  79.  *  Contact the third-party conferencing system to create a conference there,
  80.  *  using the provided calendar event information. Collects and returns the
  81.  *  conference data returned by the third-party system in a custom JSON object
  82.  *  with the following fields:
  83.  *
  84.  *    data.adminEmail - the conference administrator's email
  85.  *    data.conferenceLegalNotice - the conference legal notice text
  86.  *    data.error - Only present if there was an error during
  87.  *         conference creation. Equal to 'AUTH' if the add-on user needs to
  88.  *         authorize on the third-party system.
  89.  *    data.id - the conference ID
  90.  *    data.phoneNumber - the conference phone entry point phone number
  91.  *    data.phonePin - the conference phone entry point PIN
  92.  *    data.videoPasscode - the conference video entry point passcode
  93.  *    data.videoUri - the conference video entry point URI
  94.  *
  95.  *  The above fields are specific to this example; which conference information
  96.  *  you add-on needs is dependent on the third-party conferencing system
  97.  *  requirements.
  98.  *
  99.  * @param {Object} calendarId ID of A Calendar resource object returned by
  100.  *     the Google Calendar API.
  101.  * @return {Object}
  102.  */
  103. function create3rdPartyConference(calendarId) {
  104.   var data = {};
  105.  
  106.   var response = UrlFetchApp.fetch(PHONE_NUMBERS_LIST_LINK);
  107.   var jsonobj = JSON.parse(response.getContentText());
  108.  
  109.   //The calendarID actually is the user's email address in a normal Google Calendar
  110.   var roomName = calendarId + "_" + generateRoomWithoutSeparator(jsonobj.roomNameDictionary);
  111.   data.id = BASE_DOMAIN +"/" + roomName;
  112.   data.videoUri = "https://" + BASE_DOMAIN +"/" + roomName;
  113.   data.moreLink = INSTRUCTIONS_LINK; //MORE_NUMBERS_LINK + roomName;
  114.  
  115.   var responseMapper = UrlFetchApp.fetch(CONF_MAPPER_LINK + roomName + MUC_HOST);
  116.   var jsonobjMapper = JSON.parse(responseMapper.getContentText());
  117.   data.phonePin = jsonobjMapper.id;
  118.  
  119.   data.numbers = [];
  120.  
  121.   var keys = [];
  122.  
  123.   for (var key in jsonobj.numbers) {
  124.     if (jsonobj.numbers.hasOwnProperty(key)) {
  125.       data.numbers.push.apply(data.numbers, jsonobj.numbers[key]);
  126.     }
  127.   }
  128.  
  129.   return data;
  130. }
  131.  
  132. /*
  133.  * Returns true if the string 's' contains one of the
  134.  * template strings.
  135.  */
  136. function hasTemplate(s, categories) {
  137.     for (var template in categories){
  138.         if (s.indexOf(template) >= 0){
  139.             return true;
  140.         }
  141.     }
  142. }
  143.  
  144. /**
  145.  * Generates random int within the range [min, max]
  146.  * @param min the minimum value for the generated number
  147.  * @param max the maximum value for the generated number
  148.  * @returns random int number
  149.  */
  150. function randomInt(min, max) {
  151.     return Math.floor(Math.random() * (max - min + 1)) + min;
  152. }
  153.  
  154. /**
  155.  * Get random element from array or string.
  156.  * @param {Array|string} arr source
  157.  * @returns array element or string character
  158.  */
  159. function randomElement(arr) {
  160.     return arr[randomInt(0, arr.length -1)];
  161. }
  162.  
  163. var PATTERNS = [
  164.     "_ADJECTIVE__PLURALNOUN__VERB__ADVERB_"
  165. ];
  166.  
  167. /**
  168.  * Generates new room name.
  169.  * @param customDictionary a dictionary containing keys pluralNouns, verbs,
  170.  * adverbs and adjectives, values are array of strings.
  171.  */
  172. function generateRoomWithoutSeparator(customDictionary) {
  173.     // Note that if more than one pattern is available, the choice of
  174.     // 'name' won't have a uniform distribution amongst all patterns (names
  175.     // from patterns with fewer options will have higher probability of
  176.     // being chosen that names from patterns with more options).
  177.     var name = randomElement(PATTERNS);
  178.     var word;
  179.     var categories = {
  180.             "_PLURALNOUN_": customDictionary.pluralNouns,
  181.             "_VERB_": customDictionary.verbs,
  182.             "_ADVERB_": customDictionary.adverbs,
  183.             "_ADJECTIVE_": customDictionary.adjectives
  184.         };
  185.     while (hasTemplate(name, categories)) {
  186.         for (var template in categories) {
  187.             word = randomElement(categories[template]);
  188.             name = name.replace(template, word);
  189.         }
  190.     }
  191.  
  192.     return name;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement