MOBILE-3592 user: Implement user services

main
Dani Palou 2020-11-12 09:18:44 +01:00
parent 9258bc0a1e
commit 9ecbdd22b8
4 changed files with 1291 additions and 0 deletions

View File

@ -0,0 +1,95 @@
// (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 { CoreSiteSchema, registerSiteSchema } from '@services/sites';
import { CoreUserBasicData } from './user';
/**
* Database variables for CoreUser service.
*/
export const USERS_TABLE_NAME = 'users';
export const SITE_SCHEMA: CoreSiteSchema = {
name: 'CoreUserProvider',
version: 1,
canBeCleared: [USERS_TABLE_NAME],
tables: [
{
name: USERS_TABLE_NAME,
columns: [
{
name: 'id',
type: 'INTEGER',
primaryKey: true,
},
{
name: 'fullname',
type: 'TEXT',
},
{
name: 'profileimageurl',
type: 'TEXT',
},
],
},
],
};
/**
* Database variables for CoreUserOffline service.
*/
export const PREFERENCES_TABLE_NAME = 'user_preferences';
export const OFFLINE_SITE_SCHEMA: CoreSiteSchema = {
name: 'CoreUserOfflineProvider',
version: 1,
tables: [
{
name: PREFERENCES_TABLE_NAME,
columns: [
{
name: 'name',
type: 'TEXT',
unique: true,
notNull: true,
},
{
name: 'value',
type: 'TEXT',
},
{
name: 'onlinevalue',
type: 'TEXT',
},
],
},
],
};
/**
* Data stored in DB for users.
*/
export type CoreUserDBRecord = CoreUserBasicData;
/**
* Structure of offline user preferences.
*/
export type CoreUserPreferenceDBRecord = {
name: string;
value: string;
onlinevalue: string;
};
export const initCoreUserDB = (): void => {
registerSiteSchema(SITE_SCHEMA);
registerSiteSchema(OFFLINE_SITE_SCHEMA);
};

View File

@ -0,0 +1,67 @@
// (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 { makeSingleton, Translate } from '@singletons/core.singletons';
import { CoreUserRole } from './user';
/**
* Service that provides some features regarding users information.
*/
@Injectable({
providedIn: 'root',
})
export class CoreUserHelperProvider {
/**
* Formats a user address, concatenating address, city and country.
*
* @param address Address.
* @param city City.
* @param country Country.
* @return Formatted address.
*/
formatAddress(address: string, city: string, country: string): string {
const separator = Translate.instance.instant('core.listsep');
let values = [address, city, country];
values = values.filter((value) => value?.length > 0);
return values.join(separator + ' ');
}
/**
* Formats a user role list, translating and concatenating them.
*
* @param roles List of user roles.
* @return The formatted roles.
*/
formatRoleList(roles?: CoreUserRole[]): string {
if (!roles || roles.length <= 0) {
return '';
}
const separator = Translate.instance.instant('core.listsep');
return roles.map((value) => {
const translation = Translate.instance.instant('core.user.' + value.shortname);
return translation.indexOf('core.user.') < 0 ? translation : value.shortname;
}).join(separator + ' ');
}
}
export class CoreUserHelper extends makeSingleton(CoreUserHelperProvider) {}

View File

@ -0,0 +1,83 @@
// (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 { CoreSites } from '@services/sites';
import { makeSingleton } from '@singletons/core.singletons';
import { PREFERENCES_TABLE_NAME, CoreUserPreferenceDBRecord } from './user.db';
/**
* Service to handle offline user preferences.
*/
@Injectable({
providedIn: 'root',
})
export class CoreUserOfflineProvider {
/**
* Get preferences that were changed offline.
*
* @param siteId Site ID. If not defined, current site.
* @return Promise resolved with list of preferences.
*/
async getChangedPreferences(siteId?: string): Promise<CoreUserPreferenceDBRecord[]> {
const site = await CoreSites.instance.getSite(siteId);
return site.getDb().getRecordsSelect(PREFERENCES_TABLE_NAME, 'value != onlineValue');
}
/**
* Get an offline preference.
*
* @param name Name of the preference.
* @param siteId Site ID. If not defined, current site.
* @return Promise resolved with the preference, rejected if not found.
*/
async getPreference(name: string, siteId?: string): Promise<CoreUserPreferenceDBRecord> {
const site = await CoreSites.instance.getSite(siteId);
return site.getDb().getRecord(PREFERENCES_TABLE_NAME, { name });
}
/**
* Set an offline preference.
*
* @param name Name of the preference.
* @param value Value of the preference.
* @param onlineValue Online value of the preference. If undefined, preserve previously stored value.
* @param siteId Site ID. If not defined, current site.
* @return Promise resolved when done.
*/
async setPreference(name: string, value: string, onlineValue?: string, siteId?: string): Promise<void> {
const site = await CoreSites.instance.getSite(siteId);
if (typeof onlineValue == 'undefined') {
const preference = await this.getPreference(name, site.id);
onlineValue = preference.onlinevalue;
}
const record: CoreUserPreferenceDBRecord = {
name,
value,
onlinevalue: onlineValue,
};
await site.getDb().insertRecord(PREFERENCES_TABLE_NAME, record);
}
}
export class CoreUserOffline extends makeSingleton(CoreUserOfflineProvider) {}

File diff suppressed because it is too large Load Diff