MOBILE-3887 notifications: Use new WS get_unread_notification_count
parent
3bc68dd28a
commit
e55bc9ba0a
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -266,31 +266,65 @@ 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<number | string> {
|
||||
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<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
|
||||
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 {
|
||||
count: Math.min(unread.length, AddonNotificationsProvider.LIST_LIMIT),
|
||||
hasMore: unread.length > AddonNotificationsProvider.LIST_LIMIT,
|
||||
};
|
||||
} catch {
|
||||
// Return no notifications if the call fails.
|
||||
return {
|
||||
count: 0,
|
||||
hasMore: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return unread.length;
|
||||
} catch {
|
||||
// Return no messages if the call fails.
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* 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}`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue