2020-12-14 14:50:37 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
2021-02-10 11:36:54 +01:00
|
|
|
import { CoreSyncBaseProvider, CoreSyncBlockedError } from '@classes/base-sync';
|
2020-12-14 14:50:37 +01:00
|
|
|
import { CoreApp } from '@services/app';
|
|
|
|
import { CoreEvents } from '@singletons/events';
|
|
|
|
import { CoreSites } from '@services/sites';
|
|
|
|
import { CoreUtils } from '@services/utils/utils';
|
|
|
|
import {
|
|
|
|
AddonCalendar,
|
|
|
|
AddonCalendarEvent,
|
|
|
|
AddonCalendarProvider,
|
|
|
|
AddonCalendarSubmitCreateUpdateFormDataWSParams,
|
|
|
|
} from './calendar';
|
|
|
|
import { AddonCalendarOffline } from './calendar-offline';
|
|
|
|
import { AddonCalendarHelper } from './calendar-helper';
|
|
|
|
import { makeSingleton, Translate } from '@singletons';
|
|
|
|
import { CoreSync } from '@services/sync';
|
2021-02-10 11:36:54 +01:00
|
|
|
import { CoreNetworkError } from '@classes/errors/network-error';
|
2021-12-16 12:29:13 +01:00
|
|
|
import moment from 'moment';
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Service to sync calendar.
|
|
|
|
*/
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
export class AddonCalendarSyncProvider extends CoreSyncBaseProvider<AddonCalendarSyncEvents> {
|
|
|
|
|
|
|
|
static readonly AUTO_SYNCED = 'addon_calendar_autom_synced';
|
|
|
|
static readonly MANUAL_SYNCED = 'addon_calendar_manual_synced';
|
|
|
|
static readonly SYNC_ID = 'calendar';
|
|
|
|
|
2021-03-18 11:19:06 +01:00
|
|
|
protected componentTranslatableString = 'addon.calendar.calendarevent';
|
|
|
|
|
2020-12-14 14:50:37 +01:00
|
|
|
constructor() {
|
|
|
|
super('AddonCalendarSync');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to synchronize all events in a certain site or in all sites.
|
|
|
|
*
|
|
|
|
* @param siteId Site ID to sync. If not defined, sync all sites.
|
|
|
|
* @param force Wether to force sync not depending on last execution.
|
|
|
|
* @return Promise resolved if sync is successful, rejected if sync fails.
|
|
|
|
*/
|
2021-02-10 11:36:54 +01:00
|
|
|
async syncAllEvents(siteId?: string, force = false): Promise<void> {
|
2021-02-09 13:11:29 +01:00
|
|
|
await this.syncOnSites('all calendar events', this.syncAllEventsFunc.bind(this, force), siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync all events on a site.
|
|
|
|
*
|
|
|
|
* @param force Wether to force sync not depending on last execution.
|
2021-02-10 11:36:54 +01:00
|
|
|
* @param siteId Site ID to sync.
|
2020-12-14 14:50:37 +01:00
|
|
|
* @return Promise resolved if sync is successful, rejected if sync fails.
|
|
|
|
*/
|
2021-02-10 11:36:54 +01:00
|
|
|
protected async syncAllEventsFunc(force = false, siteId?: string): Promise<void> {
|
|
|
|
const result = force
|
|
|
|
? await this.syncEvents(siteId)
|
|
|
|
: await this.syncEventsIfNeeded(siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
|
2021-02-10 11:36:54 +01:00
|
|
|
if (result?.updated) {
|
2020-12-14 14:50:37 +01:00
|
|
|
// Sync successful, send event.
|
2021-03-10 12:30:18 +01:00
|
|
|
CoreEvents.trigger(AddonCalendarSyncProvider.AUTO_SYNCED, result, siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync a site events only if a certain time has passed since the last time.
|
|
|
|
*
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved when the events are synced or if it doesn't need to be synced.
|
|
|
|
*/
|
2021-02-10 11:36:54 +01:00
|
|
|
async syncEventsIfNeeded(siteId?: string): Promise<AddonCalendarSyncEvents | undefined> {
|
2021-03-02 11:41:04 +01:00
|
|
|
siteId = siteId || CoreSites.getCurrentSiteId();
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
const needed = await this.isSyncNeeded(AddonCalendarSyncProvider.SYNC_ID, siteId);
|
|
|
|
|
|
|
|
if (needed) {
|
2021-02-10 11:36:54 +01:00
|
|
|
return this.syncEvents(siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronize all offline events of a certain site.
|
|
|
|
*
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved if sync is successful, rejected otherwise.
|
|
|
|
*/
|
|
|
|
async syncEvents(siteId?: string): Promise<AddonCalendarSyncEvents> {
|
2021-03-02 11:41:04 +01:00
|
|
|
siteId = siteId || CoreSites.getCurrentSiteId();
|
2020-12-14 14:50:37 +01:00
|
|
|
|
2021-11-11 10:02:53 +01:00
|
|
|
const currentSyncPromise = this.getOngoingSync(AddonCalendarSyncProvider.SYNC_ID, siteId);
|
|
|
|
if (currentSyncPromise) {
|
2020-12-14 14:50:37 +01:00
|
|
|
// There's already a sync ongoing for this site, return the promise.
|
2021-11-11 10:02:53 +01:00
|
|
|
return currentSyncPromise;
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.debug('Try to sync calendar events for site ' + siteId);
|
|
|
|
|
|
|
|
// Get offline events.
|
|
|
|
const syncPromise = this.performSyncEvents(siteId);
|
|
|
|
|
|
|
|
return this.addOngoingSync(AddonCalendarSyncProvider.SYNC_ID, syncPromise, siteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync user preferences of a site.
|
|
|
|
*
|
|
|
|
* @param siteId Site ID to sync.
|
|
|
|
* @param Promise resolved if sync is successful, rejected if sync fails.
|
|
|
|
*/
|
|
|
|
protected async performSyncEvents(siteId: string): Promise<AddonCalendarSyncEvents> {
|
|
|
|
const result: AddonCalendarSyncEvents = {
|
|
|
|
warnings: [],
|
|
|
|
events: [],
|
2021-11-15 09:03:58 +01:00
|
|
|
offlineIdMap: {},
|
2020-12-14 14:50:37 +01:00
|
|
|
deleted: [],
|
|
|
|
toinvalidate: [],
|
|
|
|
updated: false,
|
|
|
|
};
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
const eventIds: number[] = await CoreUtils.ignoreErrors(AddonCalendarOffline.getAllEventsIds(siteId), []);
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
if (eventIds.length > 0) {
|
2021-03-02 11:41:04 +01:00
|
|
|
if (!CoreApp.isOnline()) {
|
2020-12-14 14:50:37 +01:00
|
|
|
// Cannot sync in offline.
|
2021-02-10 11:36:54 +01:00
|
|
|
throw new CoreNetworkError();
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const promises = eventIds.map((eventId) => this.syncOfflineEvent(eventId, result, siteId));
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreUtils.allPromises(promises);
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
if (result.updated) {
|
|
|
|
|
|
|
|
// Data has been sent to server. Now invalidate the WS calls.
|
|
|
|
const promises = [
|
2021-03-02 11:41:04 +01:00
|
|
|
AddonCalendar.invalidateEventsList(siteId),
|
|
|
|
AddonCalendarHelper.refreshAfterChangeEvents(result.toinvalidate, siteId),
|
2020-12-14 14:50:37 +01:00
|
|
|
];
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreUtils.ignoreErrors(Promise.all(promises));
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync finished, set sync time.
|
2021-03-02 11:41:04 +01:00
|
|
|
await CoreUtils.ignoreErrors(this.setSyncTime(AddonCalendarSyncProvider.SYNC_ID, siteId));
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
// All done, return the result.
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronize an offline event.
|
|
|
|
*
|
|
|
|
* @param eventId The event ID to sync.
|
|
|
|
* @param result Object where to store the result of the sync.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved if sync is successful, rejected otherwise.
|
|
|
|
*/
|
|
|
|
protected async syncOfflineEvent(eventId: number, result: AddonCalendarSyncEvents, siteId?: string): Promise<void> {
|
|
|
|
|
|
|
|
// Verify that event isn't blocked.
|
2021-03-02 11:41:04 +01:00
|
|
|
if (CoreSync.isBlocked(AddonCalendarProvider.COMPONENT, eventId, siteId)) {
|
2020-12-14 14:50:37 +01:00
|
|
|
this.logger.debug('Cannot sync event ' + eventId + ' because it is blocked.');
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
throw new CoreSyncBlockedError(Translate.instant(
|
2020-12-14 14:50:37 +01:00
|
|
|
'core.errorsyncblocked',
|
2021-03-02 11:41:04 +01:00
|
|
|
{ $a: Translate.instant('addon.calendar.calendarevent') },
|
2021-02-10 11:36:54 +01:00
|
|
|
));
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// First of all, check if the event has been deleted.
|
|
|
|
try {
|
2021-03-02 11:41:04 +01:00
|
|
|
const data = await AddonCalendarOffline.getDeletedEvent(eventId, siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
// Delete the event.
|
|
|
|
try {
|
2021-03-02 11:41:04 +01:00
|
|
|
await AddonCalendar.deleteEventOnline(data.id, !!data.repeat, siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
result.updated = true;
|
|
|
|
result.deleted.push(eventId);
|
|
|
|
|
|
|
|
// Event sent, delete the offline data.
|
|
|
|
const promises: Promise<void>[] = [];
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
promises.push(AddonCalendarOffline.unmarkDeleted(eventId, siteId));
|
|
|
|
promises.push(AddonCalendarOffline.deleteEvent(eventId, siteId).catch(() => {
|
2020-12-14 14:50:37 +01:00
|
|
|
// Ignore errors, maybe there was no edit data.
|
|
|
|
}));
|
|
|
|
|
|
|
|
// We need the event data to invalidate it. Get it from local DB.
|
2021-03-02 11:41:04 +01:00
|
|
|
promises.push(AddonCalendar.getEventFromLocalDb(eventId, siteId).then((event) => {
|
2020-12-14 14:50:37 +01:00
|
|
|
result.toinvalidate.push({
|
|
|
|
id: event.id,
|
|
|
|
repeatid: event.repeatid,
|
|
|
|
timestart: event.timestart,
|
2021-05-13 14:12:42 +02:00
|
|
|
repeated: data?.repeat ? (event as AddonCalendarEvent).eventcount || 1 : 1,
|
2020-12-14 14:50:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}).catch(() => {
|
|
|
|
// Ignore errors.
|
|
|
|
}));
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
} catch (error) {
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
if (!CoreUtils.isWebServiceError(error)) {
|
2020-12-14 14:50:37 +01:00
|
|
|
// Local error, reject.
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The WebService has thrown an error, this means that the event cannot be created. Delete it.
|
|
|
|
result.updated = true;
|
|
|
|
|
|
|
|
const promises: Promise<void>[] = [];
|
|
|
|
|
2021-03-02 11:41:04 +01:00
|
|
|
promises.push(AddonCalendarOffline.unmarkDeleted(eventId, siteId));
|
|
|
|
promises.push(AddonCalendarOffline.deleteEvent(eventId, siteId).catch(() => {
|
2020-12-14 14:50:37 +01:00
|
|
|
// Ignore errors, maybe there was no edit data.
|
|
|
|
}));
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
2021-03-18 11:19:06 +01:00
|
|
|
|
2020-12-14 14:50:37 +01:00
|
|
|
// Event deleted, add a warning.
|
2021-03-18 11:19:06 +01:00
|
|
|
this.addOfflineDataDeletedWarning(result.warnings, data.name, error);
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch {
|
|
|
|
// Not deleted.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not deleted. Now get the event data.
|
2021-03-02 11:41:04 +01:00
|
|
|
const event = await AddonCalendarOffline.getEvent(eventId, siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
// Try to send the data.
|
|
|
|
const data: AddonCalendarSubmitCreateUpdateFormDataWSParams = Object.assign(
|
2021-03-02 11:41:04 +01:00
|
|
|
CoreUtils.clone(event),
|
2020-12-14 14:50:37 +01:00
|
|
|
{
|
|
|
|
description: {
|
|
|
|
text: event.description || '',
|
|
|
|
format: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
); // Clone the object because it will be modified in the submit function.
|
|
|
|
|
|
|
|
try {
|
2021-11-15 09:03:58 +01:00
|
|
|
const newEvent = await AddonCalendar.submitEventOnline(eventId, data, siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
result.updated = true;
|
|
|
|
result.events.push(newEvent);
|
2021-11-15 09:03:58 +01:00
|
|
|
if (eventId < 0) {
|
|
|
|
result.offlineIdMap[eventId] = newEvent.id;
|
|
|
|
}
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
// Add data to invalidate.
|
|
|
|
const numberOfRepetitions = data.repeat ? data.repeats :
|
|
|
|
(data.repeateditall && newEvent.repeatid ? newEvent.eventcount : 1);
|
|
|
|
|
|
|
|
result.toinvalidate.push({
|
|
|
|
id: newEvent.id,
|
|
|
|
repeatid: newEvent.repeatid,
|
|
|
|
timestart: newEvent.timestart,
|
|
|
|
repeated: numberOfRepetitions || 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Event sent, delete the offline data.
|
2021-11-11 10:02:53 +01:00
|
|
|
return AddonCalendarOffline.deleteEvent(event.id, siteId);
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
} catch (error) {
|
2021-03-02 11:41:04 +01:00
|
|
|
if (!CoreUtils.isWebServiceError(error)) {
|
2020-12-14 14:50:37 +01:00
|
|
|
// Local error, reject.
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The WebService has thrown an error, this means that the event cannot be created. Delete it.
|
|
|
|
result.updated = true;
|
|
|
|
|
2021-11-11 10:02:53 +01:00
|
|
|
await AddonCalendarOffline.deleteEvent(event.id, siteId);
|
2021-03-18 11:19:06 +01:00
|
|
|
|
2020-12-14 14:50:37 +01:00
|
|
|
// Event deleted, add a warning.
|
2021-03-18 11:19:06 +01:00
|
|
|
this.addOfflineDataDeletedWarning(result.warnings, event.name, error);
|
2020-12-14 14:50:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-15 14:31:00 +01:00
|
|
|
export const AddonCalendarSync = makeSingleton(AddonCalendarSyncProvider);
|
2020-12-14 14:50:37 +01:00
|
|
|
|
|
|
|
export type AddonCalendarSyncEvents = {
|
|
|
|
warnings: string[];
|
|
|
|
events: AddonCalendarEvent[];
|
2021-11-15 09:03:58 +01:00
|
|
|
offlineIdMap: Record<number, number>; // Map offline ID with online ID for created events.
|
2020-12-14 14:50:37 +01:00
|
|
|
deleted: number[];
|
|
|
|
toinvalidate: AddonCalendarSyncInvalidateEvent[];
|
|
|
|
updated: boolean;
|
|
|
|
source?: string; // Added on pages.
|
2021-12-16 12:29:13 +01:00
|
|
|
moment?: moment.Moment; // Added on day page.
|
2020-12-14 14:50:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export type AddonCalendarSyncInvalidateEvent = {
|
|
|
|
id: number;
|
|
|
|
repeatid?: number;
|
|
|
|
timestart: number;
|
|
|
|
repeated: number;
|
|
|
|
};
|