MOBILE-3887 notifications: Use new WS get_unread_notification_count

main
Dani Palou 2021-10-19 09:02:14 +02:00
parent 3bc68dd28a
commit e55bc9ba0a
3 changed files with 59 additions and 18 deletions

View File

@ -157,10 +157,9 @@ export class AddonNotificationsListPage implements OnInit, OnDestroy {
try { try {
this.loadingMarkAllNotificationsAsRead = true; this.loadingMarkAllNotificationsAsRead = true;
let unread = await AddonNotifications.getUnreadNotificationsCount(); const unreadCountData = await AddonNotifications.getUnreadNotificationsCount();
unread = typeof unread === 'string' ? parseInt(unread) : unread; this.canMarkAllNotificationsAsRead = unreadCountData.count > 0;
this.canMarkAllNotificationsAsRead = unread > 0;
} finally { } finally {
this.loadingMarkAllNotificationsAsRead = false; this.loadingMarkAllNotificationsAsRead = false;
} }

View File

@ -110,11 +110,12 @@ export class AddonNotificationsMainMenuHandlerService implements CoreMainMenuHan
} }
try { 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 = unreadCountData.count > 0 ?
this.handlerData.badge = unreadCountNumber > 0 ? String(unreadCount) : ''; unreadCountData.count + (unreadCountData.hasMore ? '+' : '') :
CorePushNotifications.updateAddonCounter('AddonNotifications', unreadCountNumber, siteId); '';
CorePushNotifications.updateAddonCounter('AddonNotifications', unreadCountData.count, siteId);
} catch { } catch {
this.handlerData.badge = ''; this.handlerData.badge = '';
} finally { } finally {

View File

@ -266,31 +266,65 @@ export class AddonNotificationsProvider {
* @param siteId Site ID. If not defined, use current site. * @param siteId Site ID. If not defined, use current site.
* @return Promise resolved with the message notifications count. * @return Promise resolved with the message notifications count.
*/ */
async getUnreadNotificationsCount(userId?: number, siteId?: string): Promise<number | string> { async getUnreadNotificationsCount(userId?: number, siteId?: string): Promise<{ count: number; hasMore: boolean} > {
const site = await CoreSites.getSite(siteId); const site = await CoreSites.getSite(siteId);
// @since 4.0 // @since 4.0
if (site.wsAvailable('core_message_get_unread_notification_count')) { 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<number>('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 // Fallback call
try { try {
const unread = await this.getNotificationsWithStatus(AddonNotificationsGetReadType.UNREAD, { const unread = await this.getNotificationsWithStatus(AddonNotificationsGetReadType.UNREAD, {
limit: AddonNotificationsProvider.LIST_LIMIT, limit: AddonNotificationsProvider.LIST_LIMIT + 1,
siteId, siteId,
}); });
if (unread.length === AddonNotificationsProvider.LIST_LIMIT) { return {
// Maybe there are more notifications, include a '+'; count: Math.min(unread.length, AddonNotificationsProvider.LIST_LIMIT),
return unread.length + '+'; hasMore: unread.length > AddonNotificationsProvider.LIST_LIMIT,
};
} catch {
// Return no notifications if the call fails.
return {
count: 0,
hasMore: false,
};
}
} }
return unread.length; /**
} catch { * Get cache key for unread notifications count WS calls.
// Return no messages if the call fails. *
return 0; * @param userId User ID.
} * @return Cache key.
*/
protected getUnreadNotificationsCountCacheKey(userId: number): string {
return `${ROOT_CACHE_KEY}count:${userId}`;
} }
/** /**
@ -520,6 +554,13 @@ export type CoreMessageMarkNotificationReadWSResponse = {
warnings?: CoreWSExternalWarning[]; 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. * Options to pass to getNotifications.
*/ */