MOBILE-2317 user: Delete user from cache when deleted form site

main
Pau Ferrer Ocaña 2018-01-23 15:35:34 +01:00
parent d7c6c25292
commit 09e07c31fe
3 changed files with 59 additions and 10 deletions

View File

@ -17,6 +17,7 @@ import { TranslateService } from '@ngx-translate/core';
import { CoreSitesProvider } from '../../../../providers/sites'; import { CoreSitesProvider } from '../../../../providers/sites';
import { CoreDomUtilsProvider } from '../../../../providers/utils/dom'; import { CoreDomUtilsProvider } from '../../../../providers/utils/dom';
import { CoreTextUtilsProvider } from '../../../../providers/utils/text'; import { CoreTextUtilsProvider } from '../../../../providers/utils/text';
import { CoreUserProvider } from '../../../user/providers/user';
/** /**
* Component to handle activity completion. It shows a checkbox with the current status, and allows manually changing * Component to handle activity completion. It shows a checkbox with the current status, and allows manually changing
@ -39,8 +40,8 @@ export class CoreCourseModuleCompletionComponent implements OnChanges {
completionImage: string; completionImage: string;
completionDescription: string; completionDescription: string;
constructor(private textUtils: CoreTextUtilsProvider, private translate: TranslateService, constructor(private textUtils: CoreTextUtilsProvider, private domUtils: CoreDomUtilsProvider,
private domUtils: CoreDomUtilsProvider, private sitesProvider: CoreSitesProvider) { private translate: TranslateService, private sitesProvider: CoreSitesProvider, private userProvider: CoreUserProvider) {
this.completionChanged = new EventEmitter(); this.completionChanged = new EventEmitter();
} }
@ -130,13 +131,13 @@ export class CoreCourseModuleCompletionComponent implements OnChanges {
if (this.completion.overrideby > 0) { if (this.completion.overrideby > 0) {
langKey += '-override'; langKey += '-override';
// @todo: Get user profile. promise = this.userProvider.getProfile(this.completion.overrideby, this.completion.courseId, true).then(
// promise = $mmUser.getProfile(scope.completion.overrideby, scope.completion.courseId, true).then(function(profile) { (profile) => {
// return { return {
// overrideuser: profile.fullname, overrideuser: profile.fullname,
// modname: modNameFormatted modname: modNameFormatted
// }; };
// }); });
} else { } else {
promise = Promise.resolve(modNameFormatted); promise = Promise.resolve(modNameFormatted);
} }

View File

@ -79,6 +79,32 @@ export class CoreUserProvider {
}); });
} }
/**
* Store user basic information in local DB to be retrieved if the WS call fails.
*
* @param {number} userId User ID.
* @param {string} [siteId] ID of the site the event belongs to. If not defined, use current site.
* @return {Promise<any>} Promise resolve when the user is deleted.
*/
deleteStoredUser(userId: number, siteId?: string): Promise<any> {
if (isNaN(userId)) {
return Promise.reject(null);
}
let promises = [];
siteId = siteId || this.sitesProvider.getCurrentSiteId();
// Invalidate WS calls.
promises.push(this.invalidateUserCache(userId, siteId));
promises.push(this.sitesProvider.getSite(siteId).then((site) => {
return site.getDb().deleteRecords(this.USERS_TABLE, { id: userId });
}));
return Promise.all(promises);
}
/** /**
* Get user profile. The type of profile retrieved depends on the params. * Get user profile. The type of profile retrieved depends on the params.
* *

View File

@ -18,6 +18,8 @@ import { CoreUserProfileFieldDelegate } from './providers/user-profile-field-del
import { CoreUserProvider } from './providers/user'; import { CoreUserProvider } from './providers/user';
import { CoreUserHelperProvider } from './providers/helper'; import { CoreUserHelperProvider } from './providers/helper';
import { CoreUserProfileMailHandler } from './providers/user-handler'; import { CoreUserProfileMailHandler } from './providers/user-handler';
import { CoreEventsProvider } from '../../providers/events';
import { CoreSitesProvider } from '../../providers/sites';
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -33,7 +35,27 @@ import { CoreUserProfileMailHandler } from './providers/user-handler';
] ]
}) })
export class CoreUserModule { export class CoreUserModule {
constructor(userDelegate: CoreUserDelegate, userProfileMailHandler: CoreUserProfileMailHandler) { constructor(userDelegate: CoreUserDelegate, userProfileMailHandler: CoreUserProfileMailHandler,
eventsProvider: CoreEventsProvider, sitesProvider: CoreSitesProvider, userProvider: CoreUserProvider) {
userDelegate.registerHandler(userProfileMailHandler); userDelegate.registerHandler(userProfileMailHandler);
eventsProvider.on(CoreEventsProvider.USER_DELETED, (data) => {
// Search for userid in params.
let params = data.params,
userId = 0;
if (params.userid) {
userId = params.userid;
} else if (params.userids) {
userId = params.userids[0];
} else if (params.field === 'id' && params.values && params.values.length) {
userId = params.values[0];
} else if (params.userlist && params.userlist.length) {
userId = params.userlist[0].userid;
}
if (userId > 0) {
userProvider.deleteStoredUser(userId, data.siteId);
}
}, sitesProvider.getCurrentSiteId());
} }
} }