diff --git a/src/addons/notifications/pages/list/list.ts b/src/addons/notifications/pages/list/list.ts index a6d2406a5..59f83b549 100644 --- a/src/addons/notifications/pages/list/list.ts +++ b/src/addons/notifications/pages/list/list.ts @@ -157,10 +157,9 @@ export class AddonNotificationsListPage implements OnInit, OnDestroy { try { this.loadingMarkAllNotificationsAsRead = true; - let unread = await AddonNotifications.getUnreadNotificationsCount(); + const unreadCountData = await AddonNotifications.getUnreadNotificationsCount(); - unread = typeof unread === 'string' ? parseInt(unread) : unread; - this.canMarkAllNotificationsAsRead = unread > 0; + this.canMarkAllNotificationsAsRead = unreadCountData.count > 0; } finally { this.loadingMarkAllNotificationsAsRead = false; } diff --git a/src/addons/notifications/services/handlers/mainmenu.ts b/src/addons/notifications/services/handlers/mainmenu.ts index e56ef86a5..a19caaae9 100644 --- a/src/addons/notifications/services/handlers/mainmenu.ts +++ b/src/addons/notifications/services/handlers/mainmenu.ts @@ -110,11 +110,12 @@ export class AddonNotificationsMainMenuHandlerService implements CoreMainMenuHan } try { - const unreadCount = await AddonNotifications.getUnreadNotificationsCount(undefined, siteId); + const unreadCountData = await AddonNotifications.getUnreadNotificationsCount(undefined, siteId); - const unreadCountNumber = typeof unreadCount === 'string' ? parseInt(unreadCount) : unreadCount; - this.handlerData.badge = unreadCountNumber > 0 ? String(unreadCount) : ''; - CorePushNotifications.updateAddonCounter('AddonNotifications', unreadCountNumber, siteId); + this.handlerData.badge = unreadCountData.count > 0 ? + unreadCountData.count + (unreadCountData.hasMore ? '+' : '') : + ''; + CorePushNotifications.updateAddonCounter('AddonNotifications', unreadCountData.count, siteId); } catch { this.handlerData.badge = ''; } finally { diff --git a/src/addons/notifications/services/notifications.ts b/src/addons/notifications/services/notifications.ts index 69e414a14..4678f9393 100644 --- a/src/addons/notifications/services/notifications.ts +++ b/src/addons/notifications/services/notifications.ts @@ -266,33 +266,67 @@ export class AddonNotificationsProvider { * @param siteId Site ID. If not defined, use current site. * @return Promise resolved with the message notifications count. */ - async getUnreadNotificationsCount(userId?: number, siteId?: string): Promise { + async getUnreadNotificationsCount(userId?: number, siteId?: string): Promise<{ count: number; hasMore: boolean} > { const site = await CoreSites.getSite(siteId); // @since 4.0 if (site.wsAvailable('core_message_get_unread_notification_count')) { - // @todo + const params: CoreMessageGetUnreadNotificationCountWSParams = { + useridto: userId || site.getUserId(), + }; + + const preSets: CoreSiteWSPreSets = { + cacheKey: this.getUnreadNotificationsCountCacheKey(params.useridto), + getFromCache: false, // Always try to get the latest number. + typeExpected: 'number', + }; + + try { + const count = await site.read('core_message_get_unread_notification_count', params, preSets); + + return { + count, + hasMore: false, + }; + } catch { + // Return no notifications if the call fails. + return { + count: 0, + hasMore: false, + }; + } } // Fallback call try { const unread = await this.getNotificationsWithStatus(AddonNotificationsGetReadType.UNREAD, { - limit: AddonNotificationsProvider.LIST_LIMIT, + limit: AddonNotificationsProvider.LIST_LIMIT + 1, siteId, }); - if (unread.length === AddonNotificationsProvider.LIST_LIMIT) { - // Maybe there are more notifications, include a '+'; - return unread.length + '+'; - } - - return unread.length; + return { + count: Math.min(unread.length, AddonNotificationsProvider.LIST_LIMIT), + hasMore: unread.length > AddonNotificationsProvider.LIST_LIMIT, + }; } catch { - // Return no messages if the call fails. - return 0; + // Return no notifications if the call fails. + return { + count: 0, + hasMore: false, + }; } } + /** + * Get cache key for unread notifications count WS calls. + * + * @param userId User ID. + * @return Cache key. + */ + protected getUnreadNotificationsCountCacheKey(userId: number): string { + return `${ROOT_CACHE_KEY}count:${userId}`; + } + /** * Mark all message notification as read. * @@ -520,6 +554,13 @@ export type CoreMessageMarkNotificationReadWSResponse = { warnings?: CoreWSExternalWarning[]; }; +/** + * Params of core_message_get_unread_notification_count WS. + */ +export type CoreMessageGetUnreadNotificationCountWSParams = { + useridto: number; // User id who received the notification, 0 for any user. +}; + /** * Options to pass to getNotifications. */