2020-10-07 08:53:19 +00: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';
|
2020-10-22 10:48:23 +00:00
|
|
|
import { CoreEvents } from '@singletons/events';
|
2020-11-12 10:20:56 +00:00
|
|
|
import { CoreSites } from '@services/sites';
|
2020-11-24 08:31:11 +00:00
|
|
|
import { makeSingleton } from '@singletons';
|
2020-12-01 17:37:24 +00:00
|
|
|
import { SYNC_TABLE_NAME, CoreSyncRecord } from '@services/database/sync';
|
2020-10-14 06:31:47 +00:00
|
|
|
|
2020-10-07 08:53:19 +00:00
|
|
|
/*
|
|
|
|
* Service that provides some features regarding synchronization.
|
|
|
|
*/
|
2020-11-19 15:35:17 +00:00
|
|
|
@Injectable({ providedIn: 'root' })
|
2020-10-07 08:53:19 +00:00
|
|
|
export class CoreSyncProvider {
|
|
|
|
|
|
|
|
// Store blocked sync objects.
|
|
|
|
protected blockedItems: { [siteId: string]: { [blockId: string]: { [operation: string]: boolean } } } = {};
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
// Unblock all blocks on logout.
|
2020-10-22 10:48:23 +00:00
|
|
|
CoreEvents.on(CoreEvents.LOGOUT, (data: {siteId: string}) => {
|
2020-10-07 08:53:19 +00:00
|
|
|
this.clearAllBlocks(data.siteId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Block a component and ID so it cannot be synchronized.
|
|
|
|
*
|
|
|
|
* @param component Component name.
|
|
|
|
* @param id Unique ID per component.
|
|
|
|
* @param operation Operation name. If not defined, a default text is used.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
*/
|
|
|
|
blockOperation(component: string, id: string | number, operation?: string, siteId?: string): void {
|
|
|
|
siteId = siteId || CoreSites.instance.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncBlockId(component, id);
|
|
|
|
|
|
|
|
if (!this.blockedItems[siteId]) {
|
|
|
|
this.blockedItems[siteId] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.blockedItems[siteId][uniqueId]) {
|
|
|
|
this.blockedItems[siteId][uniqueId] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
operation = operation || '-';
|
|
|
|
|
|
|
|
this.blockedItems[siteId][uniqueId][operation] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all blocks for a site or all sites.
|
|
|
|
*
|
|
|
|
* @param siteId If set, clear the blocked objects only for this site. Otherwise clear them for all sites.
|
|
|
|
*/
|
|
|
|
clearAllBlocks(siteId?: string): void {
|
|
|
|
if (siteId) {
|
|
|
|
delete this.blockedItems[siteId];
|
|
|
|
} else {
|
|
|
|
this.blockedItems = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all blocks for a certain component.
|
|
|
|
*
|
|
|
|
* @param component Component name.
|
|
|
|
* @param id Unique ID per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
*/
|
|
|
|
clearBlocks(component: string, id: string | number, siteId?: string): void {
|
|
|
|
siteId = siteId || CoreSites.instance.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncBlockId(component, id);
|
|
|
|
if (this.blockedItems[siteId]) {
|
|
|
|
delete this.blockedItems[siteId][uniqueId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a sync record.
|
2020-10-14 06:31:47 +00:00
|
|
|
*
|
2020-10-07 08:53:19 +00:00
|
|
|
* @param component Component name.
|
|
|
|
* @param id Unique ID per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Record if found or reject.
|
|
|
|
*/
|
2020-11-12 10:20:56 +00:00
|
|
|
async getSyncRecord(component: string, id: string | number, siteId?: string): Promise<CoreSyncRecord> {
|
|
|
|
const db = await CoreSites.instance.getSiteDb(siteId);
|
|
|
|
|
|
|
|
return await db.getRecord(SYNC_TABLE_NAME, { component: component, id: id });
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts or Updates info of a sync record.
|
2020-10-14 06:31:47 +00:00
|
|
|
*
|
2020-10-07 08:53:19 +00:00
|
|
|
* @param component Component name.
|
|
|
|
* @param id Unique ID per component.
|
|
|
|
* @param data Data that updates the record.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Promise resolved with done.
|
|
|
|
*/
|
2020-12-01 11:50:10 +00:00
|
|
|
async insertOrUpdateSyncRecord(component: string, id: string, data: Partial<CoreSyncRecord>, siteId?: string): Promise<void> {
|
2020-10-14 06:31:47 +00:00
|
|
|
const db = await CoreSites.instance.getSiteDb(siteId);
|
2020-10-07 08:53:19 +00:00
|
|
|
|
2020-10-14 06:31:47 +00:00
|
|
|
data.component = component;
|
|
|
|
data.id = id;
|
|
|
|
|
2020-10-28 13:25:18 +00:00
|
|
|
await db.insertRecord(SYNC_TABLE_NAME, data);
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to create unique identifiers for a component and id.
|
|
|
|
*
|
|
|
|
* @param component Component name.
|
|
|
|
* @param id Unique ID per component.
|
|
|
|
* @return Unique sync id.
|
|
|
|
*/
|
|
|
|
protected getUniqueSyncBlockId(component: string, id: string | number): string {
|
|
|
|
return component + '#' + id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a component is blocked.
|
|
|
|
* One block can have different operations. Here we check how many operations are being blocking the object.
|
|
|
|
*
|
|
|
|
* @param component Component name.
|
|
|
|
* @param id Unique ID per component.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
* @return Whether it's blocked.
|
|
|
|
*/
|
|
|
|
isBlocked(component: string, id: string | number, siteId?: string): boolean {
|
|
|
|
siteId = siteId || CoreSites.instance.getCurrentSiteId();
|
|
|
|
|
|
|
|
if (!this.blockedItems[siteId]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncBlockId(component, id);
|
|
|
|
if (!this.blockedItems[siteId][uniqueId]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(this.blockedItems[siteId][uniqueId]).length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblock an operation on a component and ID.
|
|
|
|
*
|
|
|
|
* @param component Component name.
|
|
|
|
* @param id Unique ID per component.
|
|
|
|
* @param operation Operation name. If not defined, a default text is used.
|
|
|
|
* @param siteId Site ID. If not defined, current site.
|
|
|
|
*/
|
|
|
|
unblockOperation(component: string, id: string | number, operation?: string, siteId?: string): void {
|
|
|
|
operation = operation || '-';
|
|
|
|
siteId = siteId || CoreSites.instance.getCurrentSiteId();
|
|
|
|
|
|
|
|
const uniqueId = this.getUniqueSyncBlockId(component, id);
|
|
|
|
|
|
|
|
if (this.blockedItems[siteId] && this.blockedItems[siteId][uniqueId]) {
|
|
|
|
delete this.blockedItems[siteId][uniqueId][operation];
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 06:31:47 +00:00
|
|
|
|
2020-10-07 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CoreSync extends makeSingleton(CoreSyncProvider) {}
|