جارٍ مزامنة التغييرات التي تم إجراؤها على مكالمات الفيديو في "تقويم Google"

يمكن للمستخدمين تعديل أحداث "تقويم Google" أو حذفها بدون قيود. إذا قام مستخدم تعديل حدث بعد إنشاء مكالمة فيديو له، قد تحتاج الإضافة إلى والاستجابة للتغيير من خلال تحديث بيانات المؤتمر. إذا كان في نظام مكالمات الفيديو التابع لجهة خارجية على تتبع بيانات الحدث، يمكن أن يؤدي الإخفاق في تعديل مكالمة الفيديو بشأن تغيير حدث إلى عرض مكالمة الفيديو. غير قابلة للاستخدام ويؤدي إلى ترك انطباع سيئ لدى المستخدم.

تتم عملية الحفاظ على تحديث بيانات المؤتمر بتغييرات على يُطلق على حدث "تقويم Google" اسم المزامنة. يمكنك مزامنة تغييرات الأحداث من خلال إنشاء برمجة تطبيقات مشغِّل قابل للتثبيت يتم تنشيطه كلما تغيرت الأحداث في تقويم معين. لسوء الحظ، المشغل لا للإبلاغ عن الأحداث التي تغيرت، ولا يمكنك قصرها على الأحداث التي لمؤتمرات الفيديو التي أنشأتها. بدلاً من ذلك، عليك طلب قائمة بجميع التغييرات التي تم إجراؤها على التقويم منذ آخر عملية مزامنة، والفلترة من خلال قائمة الأحداث، وإجراء التعديلات وفقًا لذلك.

في ما يلي إجراء المزامنة العام:

  1. في المرة الأولى التي ينشئ فيها المستخدم مكالمة فيديو، يتم إعداد عملية المزامنة.
  2. كلما أنشأ المستخدم أحد أحداث "تقويم Google" أو يعدِّله أو يحذفه ينفِّذ المشغل دالة تشغيل في مشروع الإضافة.
  3. تفحص دالة المشغّل مجموعة تغييرات الأحداث منذ آخر المزامنة وتحدد ما إذا كان أي منها يتطلب تحديث جهة خارجية مرتبطة مؤتمر.
  4. يتم إجراء أي تعديلات مطلوبة على المؤتمرات من خلال إرسال طلبات واجهة برمجة التطبيقات التابعة لجهة خارجية.
  5. يتم تخزين رمز مميّز جديد للمزامنة كي لا يحتاج تنفيذ العامل المشغِّل التالي سوى فحص أحدث التغييرات في التقويم.

بدء المزامنة

بعد أن تنشئ الإضافة مؤتمرًا على نظام تابع لجهة خارجية بنجاح، من المفترض أن تنشئ عامل تشغيل قابل للتثبيت يستجيب لتغييرات الأحداث في هذا التقويم، إذا لم يكن عامل التشغيل متوفّرًا من قبل.

بعد إنشاء المشغّل، من المفترض أن تنتهي عملية الإعداد عن طريق إنشاء المُشغِّل المزامنة. ويتم ذلك من خلال تنفيذ وظيفة العنصر المشغِّل مباشرةً.

إنشاء عامل تشغيل في "تقويم Google"

للمزامنة، يجب أن ترصد الإضافة التغييرات التي تطرأ على حدث في "تقويم Google" يحتوي على اجتماع فيديو مرفق. يتم تحقيق ذلك من خلال إنشاء EventUpdated مشغِّل قابل للتثبيت. تحتاج الإضافة فقط إلى عامل تشغيل واحد لكل تقويم، ويمكن إنشاؤه آليًا.

من الأوقات المناسبة لإنشاء عامل تشغيل هو عندما ينشئ المستخدم مؤتمره الأول، لأنّه في هذه المرحلة يبدأ المستخدم في استخدام الإضافة. بعد إنشاء مؤتمر والتأكّد من عدم حدوث أي خطأ، من المفترض أن تتحقّق الإضافة ممّا إذا كان هناك عامل تشغيل لهذا المستخدم، وإذا لم يكن الأمر كذلك، أن تنشئ عامل التشغيل.

تنفيذ دالة مشغل المزامنة

يتم تنفيذ دوالّ التفعيل عندما يرصد Apps Script شرطًا يؤدي إلى بدء مفعول عامل التفعيل. EventUpdated مثبّطات التقويم: يتم بدء مثبّطات التقويم عند إنشاء أي حدث أو تعديله أو حذفه في تقويم محدّد.

يجب تنفيذ دالة التفعيل التي تستخدمها الإضافة. دالة التشغيل هذه إجراء ما يلي:

  1. يمكنك إجراء مكالمة عبر خدمة "Calendar.Events.list()" المتقدمة في "تقويم Google" باستخدام syncToken لاسترداد قائمة بالأحداث التي تم تغييرها منذ آخر مرة المزامنة. من خلال استخدام رمز مميز للمزامنة، يمكنك تقليل عدد الأحداث التي تحدث إضافتك. يجب فحصها.

    عند تنفيذ وظيفة المشغِّل بدون رمز مميّز صالح للمزامنة، يتم الرجوع إلى مزامنة كاملة. تحاول المزامنات الكاملة ببساطة استرداد جميع الأحداث خلال فترة زمنية محددة لإنشاء مزامنة جديدة وصالحة الرمز المميز.

  2. يتم فحص كل حدث معدَّل لتحديد ما إذا كان مرتبطًا بمكالمة فيديو تابعة لجهة خارجية.
  3. إذا كان الحدث يتضمّن مؤتمرًا، يتم فحصه لمعرفة ما تم تغييره. استنادًا إلى التغيير، قد يكون تعديل المؤتمر المرتبط ضروريًا. على سبيل المثال، إذا تم حذف حدث، يجب أن تكون الإضافة فربما تحذف المؤتمر أيضًا.
  4. يتم إجراء أي تغييرات مطلوبة على المؤتمر من خلال إجراء طلبات بيانات من واجهة برمجة التطبيقات إلى النظام التابع لجهة خارجية.
  5. بعد إجراء جميع التغييرات اللازمة، خزِّن nextSyncToken التي تم إرجاعها عن طريق طريقة 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.

  }
}