סנכרון השינויים בשיחת הוועידה ביומן

המשתמשים יכולים לעדכן או למחוק את האירועים שלהם ביומן Google. אם משתמשים מעדכנים אירוע אחרי שיוצרים עבורו שיחת ועידה, יכול להיות שהתוסף צריך לעדכן את הנתונים של שיחת הוועידה כדי להגיב לשינוי. אם מערכת שיחות ועידה של צד שלישי תלויה במעקב אחר נתוני האירועים, אם לא תעדכנו את שיחת הוועידה במקרה של שינוי באירוע, ייתכן ששיחת הוועידה תטופל לא ניתנים לשימוש ועלולים לפגוע בחוויית המשתמש.

איך מעדכנים את הנתונים של שיחת הוועידה בשינויים? האירוע ביומן Google נקרא סנכרון. אפשר לסנכרן את השינויים באירועים באמצעות יצירת Apps Script טריגר שניתן להתקנה שמפעיל בכל פעם שאירועים משתנים ביומן נתון. לצערנו, הטריגר לא לדווח אילו אירועים השתנו, ולא ניתן להגביל את האירוע רק לאירועים עם שיחות הוועידה שיצרתם. במקום זאת, צריך לבקש רשימה של את כל השינויים שבוצעו ביומן מאז הסנכרון האחרון, רשימת האירועים ולעדכן אותה בהתאם.

תהליך הסנכרון הכללי הוא ככה:

  1. בפעם הראשונה שמשתמש יוצר שיחת ועידה, תהליך הסנכרון מתחיל לפעול.
  2. בכל פעם שהמשתמש יוצר, מעדכן או מוחק אחד מהאירועים שלו ביומן הטריגר מפעיל פונקציית טריגר בפרויקט התוסף.
  3. פונקציית הטריגר בודקת את קבוצת השינויים באירוע מאז הפעם האחרונה מסונכרן וקובע אם יש צורך לעדכן צד שלישי משויך לשיחת ועידה.
  4. צד שלישי מבצע את כל העדכונים הנדרשים לשיחות ועידה בקשות API.
  5. אסימון סנכרון חדש מאוחסן, כך שביצוע הטריגר הבא צריך רק לבדוק את השינויים האחרונים ביומן.

הפעלת הסנכרון

אחרי שהתוסף יצר שיחת ועידה במערכת של צד שלישי, היא צריכה ליצור טריגר שניתן להתקנה שמגיב שינויים באירועים ביומן הזה, אם הטריגר עדיין לא קיים.

אחרי יצירת הטריגר, האתחול אמור להסתיים על ידי יצירת לאסימון לסנכרון. כדי לעשות זאת, צריך להפעיל את פונקציית הטריגר באופן ישיר.

יצירת טריגר ליומן

כדי לסנכרן, התוסף צריך לזהות אירוע ביומן שמצורף אליו שיחת הוועידה תשתנה. ניתן לעשות זאת על ידי יצירת EventUpdated טריגר שניתן להתקנה. התוסף שלך נדרש רק טריגר אחד לכל יומן, ויכול ליצור אותם באופן פרוגרמטי.

זמן טוב ליצור טריגר הוא כשהמשתמש יוצר את שיחת הוועידה הראשונה שלו, כי בשלב הזה המשתמש מתחיל להשתמש בתוסף. אחרי יצירת שיחת ועידה ואימות שאין שגיאה, התוסף צריך לבדוק אם קיים עבור משתמש זה ואם לא ליצור אותו.

הטמעת פונקציה של טריגר לסנכרון

פונקציות הטריגר מבוצעות כש-Apps Script מזהה תנאי שגורם טריגר להפעלה. EventUpdated טריגרים ליומן מופעלת כשמשתמש יוצר, משנה או מוחק אירוע כלשהו באירוע מסוים יומן.

אתם צריכים להטמיע את פונקציית הטריגר שהתוסף שלכם משתמש בה. פונקציית הטריגר הזו צריך לבצע את הפעולות הבאות:

  1. ביצוע שיחת Calendar.Events.list() לשירות מתקדם של יומן Google באמצעות syncToken כדי לאחזר רשימת אירועים שהשתנו מאז הפעם האחרונה לסנכרן שימוש באסימון סנכרון כדי לצמצם את מספר האירועים שהתוסף חייבים לבחון אותם.

    כשפונקציית הטריגר פועלת ללא אסימון סנכרון תקף, היא מחזירה למצב של סנכרון מלא. סנכרונים מלאים פשוט מנסים לאחזר את כל האירועים בתוך חלון זמן מוגדר כדי ליצור סנכרון תקין וחדש ב-Assistant.

  2. כל אירוע ששונה נבדק כדי לקבוע אם יש לו שיחת ועידה עם צד שלישי.
  3. אם לאירוע יש שיחת ועידה, הוא נבדק כדי לראות מה השתנה. בהתאם לשינוי, יכול להיות ששינוי בשיחת הוועידה המשויכת במקרה הצורך. לדוגמה, אם אירוע נמחק, התוסף אמור וסביר להניח שגם תמחקו את שיחת הוועידה.
  4. אם צריך לבצע שינויים בשיחת הוועידה, צריך לבצע קריאות ל-API במערכת של צד שלישי.
  5. לאחר ביצוע כל השינויים הנחוצים, שומרים את ה-nextSyncToken שהוחזר על ידי באמצעות ה-method Calendar.Events.list(). אסימון הסנכרון הזה נמצא דף התוצאות שהוחזר על ידי הקריאה Calendar.Events.list().

עדכון האירוע ביומן Google

במקרים מסוימים, ייתכן שתרצו לעדכן את האירוע ביומן Google במהלך ביצוע סנכרון. אם בחרת לעשות זאת, צריך לעדכן את האירוע עם שירות מתקדם של יומן Google בקשה. חשוב להשתמש עדכון מותנה עם הכותרת If-Match. ההגדרה הזו מונעת שינויים בטעות ביטול שינויים בו-זמנית שביצע המשתמש בלקוח אחר.

דוגמה

בדוגמה הבאה אפשר לראות איך מגדירים סנכרון של אירועים ביומן ואת הכנסים שמשויכים אליהם.

/**
 *  Initializes syncing of conference data by creating a sync trigger and
 *  sync token if either does not exist yet.
 *
 *  @param {String} calendarId The ID of the Google Calendar.
 */
function initializeSyncing(calendarId) {
  // Create a syncing trigger if it doesn't exist yet.
  createSyncTrigger(calendarId);

  // Perform an event sync to create the initial sync token.
  syncEvents({'calendarId': calendarId});
}

/**
 *  Creates a sync trigger if it does not exist yet.
 *
 *  @param {String} calendarId The ID of the Google Calendar.
 */
function createSyncTrigger(calendarId) {
  // Check to see if the trigger already exists; if does, return.
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
    var trigger = allTriggers[i];
    if (trigger.getTriggerSourceId() == calendarId) {
      return;
    }
  }

  // Trigger does not exist, so create it. The trigger calls the
  // 'syncEvents()' trigger function when it fires.
  var trigger = ScriptApp.newTrigger('syncEvents')
      .forUserCalendar(calendarId)
      .onEventUpdated()
      .create();
}

/**
 *  Sync events for the given calendar; this is the syncing trigger
 *  function. If a sync token already exists, this retrieves all events
 *  that have been modified since the last sync, then checks each to see
 *  if an associated conference needs to be updated and makes any required
 *  changes. If the sync token does not exist or is invalid, this
 *  retrieves future events modified in the last 24 hours instead. In
 *  either case, a new sync token is created and stored.
 *
 *  @param {Object} e If called by a event updated trigger, this object
 *      contains the Google Calendar ID, authorization mode, and
 *      calling trigger ID. Only the calendar ID is actually used here,
 *      however.
 */
function syncEvents(e) {
  var calendarId = e.calendarId;
  var properties = PropertiesService.getUserProperties();
  var syncToken = properties.getProperty('syncToken');

  var options;
  if (syncToken) {
    // There's an existing sync token, so configure the following event
    // retrieval request to only get events that have been modified
    // since the last sync.
    options = {
      syncToken: syncToken
    };
  } else {
    // No sync token, so configure to do a 'full' sync instead. In this
    // example only recently updated events are retrieved in a full sync.
    // A larger time window can be examined during a full sync, but this
    // slows down the script execution. Consider the trade-offs while
    // designing your add-on.
    var now = new Date();
    var yesterday = new Date();
    yesterday.setDate(now.getDate() - 1);
    options = {
      timeMin: now.toISOString(),          // Events that start after now...
      updatedMin: yesterday.toISOString(), // ...and were modified recently
      maxResults: 50,   // Max. number of results per page of responses
      orderBy: 'updated'
    }
  }

  // Examine the list of updated events since last sync (or all events
  // modified after yesterday if the sync token is missing or invalid), and
  // update any associated conferences as required.
  var events;
  var pageToken;
  do {
    try {
      options.pageToken = pageToken;
      events = Calendar.Events.list(calendarId, options);
    } catch (err) {
      // Check to see if the sync token was invalidated by the server;
      // if so, perform a full sync instead.
      if (err.message ===
            "Sync token is no longer valid, a full sync is required.") {
        properties.deleteProperty('syncToken');
        syncEvents(e);
        return;
      } else {
        throw new Error(err.message);
      }
    }

    // Read through the list of returned events looking for conferences
    // to update.
    if (events.items && events.items.length > 0) {
      for (var i = 0; i < events.items.length; i++) {
         var calEvent = events.items[i];
         // Check to see if there is a record of this event has a
         // conference that needs updating.
         if (eventHasConference(calEvent)) {
           updateConference(calEvent, calEvent.conferenceData.conferenceId);
         }
      }
    }

    pageToken = events.nextPageToken;
  } while (pageToken);

  // Record the new sync token.
  if (events.nextSyncToken) {
    properties.setProperty('syncToken', events.nextSyncToken);
  }
}

/**
 *  Returns true if the specified event has an associated conference
 *  of the type managed by this add-on; retuns false otherwise.
 *
 *  @param {Object} calEvent The Google Calendar event object, as defined by
 *      the Calendar API.
 *  @return {boolean}
 */
function eventHasConference(calEvent) {
  var name = calEvent.conferenceData.conferenceSolution.name || null;

  // This version checks if the conference data solution name matches the
  // one of the solution names used by the add-on. Alternatively you could
  // check the solution's entry point URIs or other solution-specific
  // information.
  if (name) {
    if (name === "My Web Conference" ||
        name === "My Recorded Web Conference") {
      return true;
    }
  }
  return false;
}

/**
 *  Update a conference based on new Google Calendar event information.
 *  The exact implementation of this function is highly dependant on the
 *  details of the third-party conferencing system, so only a rough outline
 *  is shown here.
 *
 *  @param {Object} calEvent The Google Calendar event object, as defined by
 *      the Calendar API.
 *  @param {String} conferenceId The ID used to identify the conference on
 *      the third-party conferencing system.
 */
function updateConference(calEvent, conferenceId) {
  // Check edge case: the event was cancelled
  if (calEvent.status === 'cancelled' || eventHasConference(calEvent)) {
    // Use the third-party API to delete the conference too.


  } else {
    // Extract any necessary information from the event object, then
    // make the appropriate third-party API requests to update the
    // conference with that information.

  }
}