// (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'; import { CoreEvents, CoreEventsProvider } from '@services/events'; import { CoreSites, CoreSiteSchema } from '@services/sites'; import { makeSingleton } from '@singletons/core.singletons'; const SYNC_TABLE = 'sync'; /* * Service that provides some features regarding synchronization. */ @Injectable() export class CoreSyncProvider { // Variables for the database. protected siteSchema: CoreSiteSchema = { name: 'CoreSyncProvider', version: 1, tables: [ { name: SYNC_TABLE, columns: [ { name: 'component', type: 'TEXT', notNull: true, }, { name: 'id', type: 'TEXT', notNull: true, }, { name: 'time', type: 'INTEGER', }, { name: 'warnings', type: 'TEXT', }, ], primaryKeys: ['component', 'id'], }, ], }; // Store blocked sync objects. protected blockedItems: { [siteId: string]: { [blockId: string]: { [operation: string]: boolean } } } = {}; constructor() { CoreSites.instance.registerSiteSchema(this.siteSchema); // Unblock all blocks on logout. CoreEvents.instance.on(CoreEventsProvider.LOGOUT, (data: {siteId: string}) => { 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. * * @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. */ getSyncRecord(component: string, id: string | number, siteId?: string): Promise { return CoreSites.instance.getSiteDb(siteId).then((db) => db.getRecord(SYNC_TABLE, { component: component, id: id })); } /** * Inserts or Updates info of a sync record. * * @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. */ async insertOrUpdateSyncRecord(component: string, id: string, data: CoreSyncRecord, siteId?: string): Promise { const db = await CoreSites.instance.getSiteDb(siteId); data.component = component; data.id = id; await db.insertRecord(SYNC_TABLE, data); } /** * 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]; } } } export class CoreSync extends makeSingleton(CoreSyncProvider) {} export type CoreSyncRecord = { component: string; id: string; time: number; warnings: string; };