2019-09-26 12:43:29 +02:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
2018-01-24 15:06:29 +01:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-03-27 10:02:54 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
|
|
|
import { CoreSyncProvider } from '@providers/sync';
|
|
|
|
import { CoreLoggerProvider } from '@providers/logger';
|
|
|
|
import { CoreAppProvider } from '@providers/app';
|
2018-03-13 09:12:30 +01:00
|
|
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
2018-12-13 15:35:47 +01:00
|
|
|
import { CoreTimeUtilsProvider } from '@providers/utils/time';
|
2018-01-24 15:06:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class to create sync providers. It provides some common functions.
|
|
|
|
*/
|
|
|
|
export class CoreSyncBaseProvider {
|
2018-02-14 17:19:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Logger instance get from CoreLoggerProvider.
|
|
|
|
*/
|
|
|
|
protected logger;
|
|
|
|
|
2018-01-24 15:06:29 +01:00
|
|
|
/**
|
|
|
|
* Component of the sync provider.
|
|
|
|
*/
|
|
|
|
component = 'core';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync provider's interval.
|
|
|
|
*/
|
|
|
|
syncInterval = 300000;
|
|
|
|
|
|
|
|
// Store sync promises.
|
2018-01-29 10:05:20 +01:00
|
|
|
protected syncPromises: { [siteId: string]: { [uniqueId: string]: Promise<any> } } = {};
|
2018-01-24 15:06:29 +01:00
|
|
|
|
2018-03-27 10:02:54 +02:00
|
|
|
constructor(component: string, loggerProvider: CoreLoggerProvider, protected sitesProvider: CoreSitesProvider,
|
2018-03-13 09:12:30 +01:00
|
|
|
protected appProvider: CoreAppProvider, protected syncProvider: CoreSyncProvider,
|
2018-12-13 15:35:47 +01:00
|
|
|
protected textUtils: CoreTextUtilsProvider, protected translate: TranslateService,
|
|
|
|
protected timeUtils: CoreTimeUtilsProvider) {
|
2018-03-27 10:02:54 +02:00
|
|
|
|
|
|
|
this.logger = loggerProvider.getInstance(component);
|
2018-02-14 17:19:09 +01:00
|
|
|
this.component = component;
|
|
|
|
}
|
2018-01-24 15:06:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an ongoing sync to the syncPromises list. On finish the promise will be removed.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param promise The promise of the sync to add.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return The sync promise.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
addOngoingSync(id: string | number, promise: Promise<any>, siteId?: string): Promise<any> {
|
2018-01-24 15:06:29 +01:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncId(id);
|
|
|
|
if (!this.syncPromises[siteId]) {
|
|
|
|
this.syncPromises[siteId] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.syncPromises[siteId][uniqueId] = promise;
|
|
|
|
|
|
|
|
// Promise will be deleted when finish.
|
|
|
|
return promise.finally(() => {
|
|
|
|
delete this.syncPromises[siteId][uniqueId];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there's an ongoing sync for a certain identifier return it.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise of the current sync or undefined if there isn't any.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
getOngoingSync(id: string | number, siteId?: string): Promise<any> {
|
2018-01-24 15:06:29 +01:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
if (this.isSyncing(id, siteId)) {
|
|
|
|
// There's already a sync ongoing for this discussion, return the promise.
|
|
|
|
const uniqueId = this.getUniqueSyncId(id);
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2018-01-24 15:06:29 +01:00
|
|
|
return this.syncPromises[siteId][uniqueId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 10:02:54 +02:00
|
|
|
/**
|
|
|
|
* Get the synchronization time in a human readable format.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved with the readable time.
|
2018-03-27 10:02:54 +02:00
|
|
|
*/
|
|
|
|
getReadableSyncTime(id: string | number, siteId?: string): Promise<string> {
|
|
|
|
return this.getSyncTime(id, siteId).then((time) => {
|
|
|
|
return this.getReadableTimeFromTimestamp(time);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a timestamp return it in a human readable format.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param timestamp Timestamp
|
|
|
|
* @return Human readable time.
|
2018-03-27 10:02:54 +02:00
|
|
|
*/
|
|
|
|
getReadableTimeFromTimestamp(timestamp: number): string {
|
|
|
|
if (!timestamp) {
|
|
|
|
return this.translate.instant('core.never');
|
|
|
|
} else {
|
2018-12-13 15:35:47 +01:00
|
|
|
return this.timeUtils.userDate(timestamp);
|
2018-03-27 10:02:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 15:06:29 +01:00
|
|
|
/**
|
|
|
|
* Get the synchronization time. Returns 0 if no time stored.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved with the time.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
getSyncTime(id: string | number, siteId?: string): Promise<number> {
|
|
|
|
return this.syncProvider.getSyncRecord(this.component, id, siteId).then((entry) => {
|
|
|
|
return entry.time;
|
|
|
|
}).catch(() => {
|
|
|
|
return 0;
|
2018-01-24 15:06:29 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the synchronization warnings of an instance.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved with the warnings.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
getSyncWarnings(id: string | number, siteId?: string): Promise<string[]> {
|
|
|
|
return this.syncProvider.getSyncRecord(this.component, id, siteId).then((entry) => {
|
2018-03-13 09:12:30 +01:00
|
|
|
return this.textUtils.parseJSON(entry.warnings, []);
|
2018-03-08 13:25:20 +01:00
|
|
|
}).catch(() => {
|
|
|
|
return [];
|
2018-01-24 15:06:29 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a unique identifier from component and id.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @return Unique identifier from component and id.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
protected getUniqueSyncId(id: string | number): string {
|
2018-01-24 15:06:29 +01:00
|
|
|
return this.component + '#' + id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a there's an ongoing syncronization for the given id.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Whether it's synchronizing.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
isSyncing(id: string | number, siteId?: string): boolean {
|
2018-01-24 15:06:29 +01:00
|
|
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncId(id);
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2018-01-24 15:06:29 +01:00
|
|
|
return !!(this.syncPromises[siteId] && this.syncPromises[siteId][uniqueId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a sync is needed: if a certain time has passed since the last time.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved with boolean: whether sync is needed.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-20 11:47:59 +01:00
|
|
|
isSyncNeeded(id: string | number, siteId?: string): Promise<boolean> {
|
2018-01-24 15:06:29 +01:00
|
|
|
return this.getSyncTime(id, siteId).then((time) => {
|
|
|
|
return Date.now() - this.syncInterval >= time;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the synchronization time.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @param time Time to set. If not defined, current time.
|
|
|
|
* @return Promise resolved when the time is set.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
setSyncTime(id: string | number, siteId?: string, time?: number): Promise<any> {
|
|
|
|
time = typeof time != 'undefined' ? time : Date.now();
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2018-03-08 13:25:20 +01:00
|
|
|
return this.syncProvider.insertOrUpdateSyncRecord(this.component, id, { time: time }, siteId);
|
2018-01-24 15:06:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the synchronization warnings.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param warnings Warnings to set.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved when done.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-20 11:47:59 +01:00
|
|
|
setSyncWarnings(id: string | number, warnings: string[], siteId?: string): Promise<any> {
|
2018-03-08 13:25:20 +01:00
|
|
|
const warningsText = JSON.stringify(warnings || []);
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2018-03-08 13:25:20 +01:00
|
|
|
return this.syncProvider.insertOrUpdateSyncRecord(this.component, id, { warnings: warningsText }, siteId);
|
2018-01-24 15:06:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 17:19:09 +01:00
|
|
|
/**
|
|
|
|
* Execute a sync function on selected sites.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param syncFunctionLog Log message to explain the sync function purpose.
|
|
|
|
* @param syncFunction Sync function to execute.
|
|
|
|
* @param params Array that defines the params that admit the funcion.
|
|
|
|
* @param siteId Site ID to sync. If not defined, sync all sites.
|
|
|
|
* @return Resolved with siteIds selected. Rejected if offline.
|
2018-02-14 17:19:09 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
syncOnSites(syncFunctionLog: string, syncFunction: Function, params?: any[], siteId?: string): Promise<any> {
|
2018-02-14 17:19:09 +01:00
|
|
|
if (!this.appProvider.isOnline()) {
|
|
|
|
this.logger.debug(`Cannot sync '${syncFunctionLog}' because device is offline.`);
|
|
|
|
|
|
|
|
return Promise.reject(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
let promise;
|
|
|
|
if (!siteId) {
|
|
|
|
// No site ID defined, sync all sites.
|
|
|
|
this.logger.debug(`Try to sync '${syncFunctionLog}' in all sites.`);
|
2019-02-25 17:12:47 +01:00
|
|
|
promise = this.sitesProvider.getLoggedInSitesIds();
|
2018-02-14 17:19:09 +01:00
|
|
|
} else {
|
|
|
|
this.logger.debug(`Try to sync '${syncFunctionLog}' in site '${siteId}'.`);
|
|
|
|
promise = Promise.resolve([siteId]);
|
|
|
|
}
|
|
|
|
|
2018-03-08 13:25:20 +01:00
|
|
|
params = params || [];
|
|
|
|
|
2018-02-14 17:19:09 +01:00
|
|
|
return promise.then((siteIds) => {
|
|
|
|
const sitePromises = [];
|
|
|
|
siteIds.forEach((siteId) => {
|
|
|
|
// Execute function for every site selected.
|
2018-03-08 13:25:20 +01:00
|
|
|
sitePromises.push(syncFunction.apply(syncFunction, [siteId].concat(params)));
|
2018-02-14 17:19:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(sitePromises);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-24 15:06:29 +01:00
|
|
|
/**
|
|
|
|
* If there's an ongoing sync for a certain identifier, wait for it to end.
|
|
|
|
* If there's no sync ongoing the promise will be resolved right away.
|
|
|
|
*
|
2019-09-10 16:48:56 +02:00
|
|
|
* @param id Unique sync identifier per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved when there's no sync going on for the identifier.
|
2018-01-24 15:06:29 +01:00
|
|
|
*/
|
2018-03-08 13:25:20 +01:00
|
|
|
waitForSync(id: string | number, siteId?: string): Promise<any> {
|
2018-01-24 15:06:29 +01:00
|
|
|
const promise = this.getOngoingSync(id, siteId);
|
|
|
|
if (promise) {
|
2018-01-29 10:05:20 +01:00
|
|
|
return promise.catch(() => {
|
|
|
|
// Ignore errors.
|
|
|
|
});
|
2018-01-24 15:06:29 +01:00
|
|
|
}
|
2018-01-29 10:05:20 +01:00
|
|
|
|
2018-01-24 15:06:29 +01:00
|
|
|
return Promise.resolve();
|
2018-03-20 11:47:59 +01:00
|
|
|
}
|
2018-01-24 15:06:29 +01:00
|
|
|
}
|