// (C) Copyright 2015 Martin Dougiamas // // 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 { CoreLoggerProvider } from '../../../providers/logger'; import { CoreSitesProvider } from '../../../providers/sites'; /** * Service to handle Offline messages. */ @Injectable() export class AddonMessagesOfflineProvider { protected logger; // Variables for database. protected MESSAGES_TABLE = 'mma_messages_offline_messages'; protected tablesSchema = [ { name: this.MESSAGES_TABLE, columns: [ { name: 'touserid', type: 'INTEGER' }, { name: 'useridfrom', type: 'INTEGER' }, { name: 'smallmessage', type: 'TEXT' }, { name: 'timecreated', type: 'INTEGER' }, { name: 'deviceoffline', // If message was stored because device was offline. type: 'INTEGER' } ], primaryKeys: ['touserid', 'smallmessage', 'timecreated'] } ]; constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider) { this.logger = logger.getInstance('AddonMessagesOfflineProvider'); this.sitesProvider.createTablesFromSchema(this.tablesSchema); } /** * Get all offline messages. * * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved with messages. */ getAllMessages(siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { return site.getDb().getAllRecords(this.MESSAGES_TABLE); }); } }