From dc65d4a00deb65547c89e3ec176dfa93e7a84234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Mon, 1 Jul 2019 15:56:24 +0200 Subject: [PATCH] MOBILE-2877 comments: Invalidate comments count --- src/addon/blog/components/entries/entries.ts | 4 ++++ src/addon/mod/data/pages/entry/entry.ts | 4 ++++ .../comments/components/comments/comments.ts | 4 +--- src/core/comments/pages/viewer/viewer.ts | 2 +- src/core/comments/providers/comments.ts | 22 ++++++++++--------- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/addon/blog/components/entries/entries.ts b/src/addon/blog/components/entries/entries.ts index b66db02a3..d737c8e1b 100644 --- a/src/addon/blog/components/entries/entries.ts +++ b/src/addon/blog/components/entries/entries.ts @@ -169,6 +169,10 @@ export class AddonBlogEntriesComponent implements OnInit { * @param {any} refresher Refresher instance. */ refresh(refresher?: any): void { + this.entries.forEach((entry) => { + this.commentsProvider.invalidateCommentsData('user', entry.userid, this.component, entry.id, 'format_blog'); + }); + this.blogProvider.invalidateEntries(this.filter).finally(() => { this.fetchEntries(true).finally(() => { if (refresher) { diff --git a/src/addon/mod/data/pages/entry/entry.ts b/src/addon/mod/data/pages/entry/entry.ts index 51e95ce9c..2a938cbb1 100644 --- a/src/addon/mod/data/pages/entry/entry.ts +++ b/src/addon/mod/data/pages/entry/entry.ts @@ -218,6 +218,10 @@ export class AddonModDataEntryPage implements OnDestroy { promises.push(this.dataProvider.invalidateDatabaseData(this.courseId)); if (this.data) { + if (this.data.comments && this.entry && this.entry.id > 0 && this.commentsEnabled) { + promises.push(this.commentsProvider.invalidateCommentsData('module', this.data.coursemodule, 'mod_data', + this.entry.id, 'database_entry')); + } promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId)); promises.push(this.groupsProvider.invalidateActivityGroupInfo(this.data.coursemodule)); promises.push(this.dataProvider.invalidateEntriesData(this.data.id)); diff --git a/src/core/comments/components/comments/comments.ts b/src/core/comments/components/comments/comments.ts index fed935d1d..a0146c85a 100644 --- a/src/core/comments/components/comments/comments.ts +++ b/src/core/comments/components/comments/comments.ts @@ -31,7 +31,6 @@ export class CoreCommentsCommentsComponent implements OnChanges, OnDestroy { @Input() component: string; @Input() itemId: number; @Input() area = ''; - @Input() page = 0; @Input() title?: string; @Input() displaySpinner = true; // Whether to display the loading spinner. @Output() onLoading: EventEmitter; // Eevent that indicates whether the component is loading data. @@ -72,7 +71,7 @@ export class CoreCommentsCommentsComponent implements OnChanges, OnDestroy { */ ngOnChanges(changes: { [name: string]: SimpleChange }): void { // If something change, update the fields. - if (changes) { + if (changes && this.commentsLoaded) { this.fetchData(); } } @@ -108,7 +107,6 @@ export class CoreCommentsCommentsComponent implements OnChanges, OnDestroy { component: this.component, itemId: this.itemId, area: this.area, - page: this.page, title: this.title, }); } diff --git a/src/core/comments/pages/viewer/viewer.ts b/src/core/comments/pages/viewer/viewer.ts index e9533a7d2..74f31663a 100644 --- a/src/core/comments/pages/viewer/viewer.ts +++ b/src/core/comments/pages/viewer/viewer.ts @@ -51,8 +51,8 @@ export class CoreCommentsViewerPage { this.component = navParams.get('component'); this.itemId = navParams.get('itemId'); this.area = navParams.get('area') || ''; - this.page = navParams.get('page') || 0; this.title = navParams.get('title') || this.translate.instant('core.comments.comments'); + this.page = 0; } /** diff --git a/src/core/comments/providers/comments.ts b/src/core/comments/providers/comments.ts index cd671d84d..0279c711e 100644 --- a/src/core/comments/providers/comments.ts +++ b/src/core/comments/providers/comments.ts @@ -76,12 +76,11 @@ export class CoreCommentsProvider { * @param {string} component Component name. * @param {number} itemId Associated id. * @param {string} [area=''] String comment area. Default empty. - * @param {number} [page=0] Page number (0 based). Default 0. * @return {string} Cache key. */ - protected getCommentsCacheKey(contextLevel: string, instanceId: number, component: string, - itemId: number, area: string = '', page: number = 0): string { - return this.getCommentsPrefixCacheKey(contextLevel, instanceId) + ':' + component + ':' + itemId + ':' + area + ':' + page; + protected getCommentsCacheKey(contextLevel: string, instanceId: number, component: string, itemId: number, + area: string = ''): string { + return this.getCommentsPrefixCacheKey(contextLevel, instanceId) + ':' + component + ':' + itemId + ':' + area; } /** @@ -107,8 +106,8 @@ export class CoreCommentsProvider { * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved with the comments. */ - getComments(contextLevel: string, instanceId: number, component: string, itemId: number, - area: string = '', page: number = 0, siteId?: string): Promise { + getComments(contextLevel: string, instanceId: number, component: string, itemId: number, area: string = '', page: number = 0, + siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { const params: any = { contextlevel: contextLevel, @@ -120,7 +119,7 @@ export class CoreCommentsProvider { }; const preSets = { - cacheKey: this.getCommentsCacheKey(contextLevel, instanceId, component, itemId, area, page), + cacheKey: this.getCommentsCacheKey(contextLevel, instanceId, component, itemId, area), updateFrequency: CoreSite.FREQUENCY_SOMETIMES }; @@ -142,14 +141,17 @@ export class CoreCommentsProvider { * @param {string} component Component name. * @param {number} itemId Associated id. * @param {string} [area=''] String comment area. Default empty. - * @param {number} [page=0] Page number (0 based). Default 0. * @param {string} [siteId] Site ID. If not defined, current site. * @return {Promise} Promise resolved when the data is invalidated. */ invalidateCommentsData(contextLevel: string, instanceId: number, component: string, itemId: number, - area: string = '', page: number = 0, siteId?: string): Promise { + area: string = '', siteId?: string): Promise { return this.sitesProvider.getSite(siteId).then((site) => { - return site.invalidateWsCacheForKey(this.getCommentsCacheKey(contextLevel, instanceId, component, itemId, area, page)); + // This is done with starting with to avoid conflicts with previous keys that were including page. + site.invalidateWsCacheForKeyStartingWith(this.getCommentsCacheKey(contextLevel, instanceId, component, itemId, + area) + ':'); + + return site.invalidateWsCacheForKey(this.getCommentsCacheKey(contextLevel, instanceId, component, itemId, area)); }); }