Merge pull request #1839 from dpalou/MOBILE-2927

Mobile 2927
main
Juan Leyva 2019-04-25 17:50:33 +02:00 committed by GitHub
commit 1be7a614da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 303 additions and 92 deletions

View File

@ -121,8 +121,8 @@ export class AddonMessagesModule {
// Check if we have enough information to open the conversation.
if (notification.convid && enabled) {
pageParams.conversationId = Number(notification.convid);
} else if (notification.userfromid) {
pageParams.discussionUserId = Number(notification.userfromid);
} else if (notification.userfromid || notification.useridfrom) {
pageParams.discussionUserId = Number(notification.userfromid || notification.useridfrom);
}
linkHelper.goInSite(undefined, pageName, pageParams, notification.site);

View File

@ -229,8 +229,59 @@ export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCr
* @return {Promise<any>} Promise resolved with the notifications.
*/
protected fetchMessages(siteId?: string): Promise<any> {
return this.messagesProvider.getUnreadReceivedMessages(true, false, true, siteId).then((response) => {
return response.messages;
return this.sitesProvider.getSite(siteId).then((site) => {
if (site.isVersionGreaterEqualThan('3.7')) {
// Use get conversations WS to be able to get group conversations messages.
return this.messagesProvider.getConversations(undefined, undefined, 0, site.id, undefined, false, true)
.then((result) => {
// Find the first unmuted conversation.
const conv = result.conversations.find((conversation) => {
return !conversation.ismuted;
});
if (conv.isread) {
// The conversation is read, no unread messages.
return [];
}
const currentUserId = site.getUserId(),
message = conv.messages[0]; // Treat only the last message, is the one we're interested.
if (!message || message.useridfrom == currentUserId) {
// No last message or not from current user. Return empty list.
return [];
}
// Add some calculated data.
message.contexturl = '';
message.contexturlname = '';
message.convid = conv.id;
message.fullmessage = message.text;
message.fullmessageformat = 0;
message.fullmessagehtml = '';
message.notification = 0;
message.read = 0;
message.smallmessage = message.smallmessage || message.text;
message.subject = conv.name;
message.timecreated = message.timecreated * 1000;
message.timeread = 0;
message.useridto = currentUserId;
message.usertofullname = site.getInfo().fullname;
const userFrom = conv.members.find((member) => {
return member.id == message.useridfrom;
});
message.userfromfullname = userFrom && userFrom.fullname;
return [message];
});
} else {
return this.messagesProvider.getUnreadReceivedMessages(true, false, true, siteId).then((response) => {
return response.messages;
});
}
});
}
@ -242,7 +293,7 @@ export class AddonMessagesMainMenuHandler implements CoreMainMenuHandler, CoreCr
*/
protected getTitleAndText(message: any): Promise<any> {
const data = {
title: message.userfromfullname,
title: message.name || message.userfromfullname,
};
return this.textUtils.formatText(message.text, true, true).catch(() => {

View File

@ -882,14 +882,20 @@ export class AddonMessagesProvider {
result.messages = result.messages.slice(0, limitTo);
}
let lastReceived;
result.messages.forEach((message) => {
// Convert time to milliseconds.
message.timecreated = message.timecreated ? message.timecreated * 1000 : 0;
if (!lastReceived && message.useridfrom != userId) {
lastReceived = message;
}
});
if (this.appProvider.isDesktop() && params.useridto == userId && limitFrom === 0) {
if (this.appProvider.isDesktop() && limitFrom === 0 && lastReceived) {
// Store the last received message (we cannot know if it's unread or not). Don't block the user for this.
this.storeLastReceivedMessageIfNeeded(conversationId, result.messages[0], site.getId());
this.storeLastReceivedMessageIfNeeded(conversationId, lastReceived, site.getId());
}
if (excludePending) {
@ -923,11 +929,13 @@ export class AddonMessagesProvider {
* @param {number} [limitFrom=0] The offset to start at.
* @param {string} [siteId] Site ID. If not defined, use current site.
* @param {number} [userId] User ID. If not defined, current user in the site.
* @param {boolean} [forceCache] True if it should return cached data. Has priority over ignoreCache.
* @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down).
* @return {Promise<any>} Promise resolved with the conversations.
* @since 3.6
*/
getConversations(type?: number, favourites?: boolean, limitFrom: number = 0, siteId?: string, userId?: number)
: Promise<{conversations: any[], canLoadMore: boolean}> {
getConversations(type?: number, favourites?: boolean, limitFrom: number = 0, siteId?: string, userId?: number,
forceCache?: boolean, ignoreCache?: boolean): Promise<{conversations: any[], canLoadMore: boolean}> {
return this.sitesProvider.getSite(siteId).then((site) => {
userId = userId || site.getUserId();
@ -941,6 +949,13 @@ export class AddonMessagesProvider {
limitnum: this.LIMIT_MESSAGES + 1,
};
if (forceCache) {
preSets['omitExpires'] = true;
} else if (ignoreCache) {
preSets['getFromCache'] = false;
preSets['emergencyCache'] = false;
}
if (typeof type != 'undefined' && type != null) {
params.type = type;
}
@ -951,8 +966,15 @@ export class AddonMessagesProvider {
return site.read('core_message_get_conversations', params, preSets).then((response) => {
// Format the conversations, adding some calculated fields.
const conversations = response.conversations.slice(0, this.LIMIT_MESSAGES).map((conversation) => {
return this.formatConversation(conversation, userId);
});
return this.formatConversation(conversation, userId);
}),
conv = conversations[0],
lastMessage = conv && conv.messages[0];
if (this.appProvider.isDesktop() && limitFrom === 0 && lastMessage && !conv.sentfromcurrentuser) {
// Store the last received message (we cannot know if it's unread or not). Don't block the user for this.
this.storeLastReceivedMessageIfNeeded(conv.id, lastMessage, site.getId());
}
return {
conversations: conversations,

View File

@ -14,23 +14,23 @@
import { NgModule, NgZone } from '@angular/core';
import { AddonNotificationsProvider } from './providers/notifications';
import { AddonNotificationsHelperProvider } from './providers/helper';
import { AddonNotificationsMainMenuHandler } from './providers/mainmenu-handler';
import { AddonNotificationsSettingsHandler } from './providers/settings-handler';
import { AddonNotificationsCronHandler } from './providers/cron-handler';
import { AddonNotificationsPushClickHandler } from './providers/push-click-handler';
import { CoreAppProvider } from '@providers/app';
import { CoreContentLinksHelperProvider } from '@core/contentlinks/providers/helper';
import { CoreInitDelegate } from '@providers/init';
import { CoreMainMenuDelegate } from '@core/mainmenu/providers/delegate';
import { CoreSettingsDelegate } from '@core/settings/providers/delegate';
import { CoreCronDelegate } from '@providers/cron';
import { CoreLocalNotificationsProvider } from '@providers/local-notifications';
import { CoreSitesProvider } from '@providers/sites';
import { CoreUtilsProvider } from '@providers/utils/utils';
import { CorePushNotificationsDelegate } from '@core/pushnotifications/providers/delegate';
// List of providers (without handlers).
export const ADDON_NOTIFICATIONS_PROVIDERS: any[] = [
AddonNotificationsProvider
AddonNotificationsProvider,
AddonNotificationsHelperProvider
];
@NgModule({
@ -40,6 +40,7 @@ export const ADDON_NOTIFICATIONS_PROVIDERS: any[] = [
],
providers: [
AddonNotificationsProvider,
AddonNotificationsHelperProvider,
AddonNotificationsMainMenuHandler,
AddonNotificationsSettingsHandler,
AddonNotificationsCronHandler,
@ -50,9 +51,8 @@ export class AddonNotificationsModule {
constructor(mainMenuDelegate: CoreMainMenuDelegate, mainMenuHandler: AddonNotificationsMainMenuHandler,
settingsDelegate: CoreSettingsDelegate, settingsHandler: AddonNotificationsSettingsHandler,
cronDelegate: CoreCronDelegate, cronHandler: AddonNotificationsCronHandler, zone: NgZone,
appProvider: CoreAppProvider, utils: CoreUtilsProvider, sitesProvider: CoreSitesProvider,
notificationsProvider: AddonNotificationsProvider, localNotifications: CoreLocalNotificationsProvider,
linkHelper: CoreContentLinksHelperProvider, pushNotificationsDelegate: CorePushNotificationsDelegate,
appProvider: CoreAppProvider, localNotifications: CoreLocalNotificationsProvider,
initDelegate: CoreInitDelegate, pushNotificationsDelegate: CorePushNotificationsDelegate,
pushClickHandler: AddonNotificationsPushClickHandler) {
mainMenuDelegate.registerHandler(mainMenuHandler);
@ -60,22 +60,13 @@ export class AddonNotificationsModule {
cronDelegate.register(cronHandler);
pushNotificationsDelegate.registerClickHandler(pushClickHandler);
const notificationClicked = (notification: any): void => {
sitesProvider.isFeatureDisabled('CoreMainMenuDelegate_AddonNotifications', notification.site).then((disabled) => {
if (disabled) {
// Notifications are disabled, stop.
return;
}
notificationsProvider.invalidateNotificationsList().finally(() => {
linkHelper.goInSite(undefined, 'AddonNotificationsListPage', undefined, notification.site);
});
});
};
if (appProvider.isDesktop()) {
// Listen for clicks in simulated push notifications.
localNotifications.registerClick(AddonNotificationsProvider.PUSH_SIMULATION_COMPONENT, notificationClicked);
localNotifications.registerClick(AddonNotificationsProvider.PUSH_SIMULATION_COMPONENT, (notification) => {
initDelegate.ready().then(() => {
pushNotificationsDelegate.clicked(notification);
});
});
}
}
}

View File

@ -21,6 +21,7 @@ import { CoreEventsProvider, CoreEventObserver } from '@providers/events';
import { CoreSitesProvider } from '@providers/sites';
import { CoreUtilsProvider } from '@providers/utils/utils';
import { AddonNotificationsProvider } from '../../providers/notifications';
import { AddonNotificationsHelperProvider } from '../../providers/helper';
import { CorePushNotificationsDelegate } from '@core/pushnotifications/providers/delegate';
/**
@ -40,15 +41,14 @@ export class AddonNotificationsListPage {
canMarkAllNotificationsAsRead = false;
loadingMarkAllNotificationsAsRead = false;
protected readCount = 0;
protected unreadCount = 0;
protected cronObserver: CoreEventObserver;
protected pushObserver: Subscription;
constructor(navParams: NavParams, private domUtils: CoreDomUtilsProvider, private eventsProvider: CoreEventsProvider,
private sitesProvider: CoreSitesProvider, private textUtils: CoreTextUtilsProvider,
private utils: CoreUtilsProvider, private notificationsProvider: AddonNotificationsProvider,
private pushNotificationsDelegate: CorePushNotificationsDelegate) {
private pushNotificationsDelegate: CorePushNotificationsDelegate,
private notificationsHelper: AddonNotificationsHelperProvider) {
}
/**
@ -79,53 +79,17 @@ export class AddonNotificationsListPage {
protected fetchNotifications(refresh?: boolean): Promise<any> {
this.loadMoreError = false;
if (refresh) {
this.readCount = 0;
this.unreadCount = 0;
}
return this.notificationsHelper.getNotifications(refresh ? [] : this.notifications).then((result) => {
result.notifications.forEach(this.formatText.bind(this));
const limit = AddonNotificationsProvider.LIST_LIMIT;
return this.notificationsProvider.getUnreadNotifications(this.unreadCount, limit).then((unread) => {
const promises = [];
unread.forEach(this.formatText.bind(this));
/* Don't add the unread notifications to this.notifications yet. If there are no unread notifications
that causes that the "There are no notifications" message is shown in pull to refresh. */
this.unreadCount += unread.length;
if (unread.length < limit) {
// Limit not reached. Get read notifications until reach the limit.
const readLimit = limit - unread.length;
promises.push(this.notificationsProvider.getReadNotifications(this.readCount, readLimit).then((read) => {
read.forEach(this.formatText.bind(this));
this.readCount += read.length;
if (refresh) {
this.notifications = unread.concat(read);
} else {
this.notifications = this.notifications.concat(unread, read);
}
this.canLoadMore = read.length >= readLimit;
}).catch((error) => {
if (unread.length == 0) {
this.domUtils.showErrorModalDefault(error, 'addon.notifications.errorgetnotifications', true);
this.loadMoreError = true; // Set to prevent infinite calls with infinite-loading.
}
}));
if (refresh) {
this.notifications = result.notifications;
} else {
if (refresh) {
this.notifications = unread;
} else {
this.notifications = this.notifications.concat(unread);
}
this.canLoadMore = true;
this.notifications = this.notifications.concat(result.notifications);
}
this.canLoadMore = result.canLoadMore;
return Promise.all(promises).then(() => {
// Mark retrieved notifications as read if they are not.
this.markNotificationsAsRead(unread);
});
this.markNotificationsAsRead(result.notifications);
}).catch((error) => {
this.domUtils.showErrorModalDefault(error, 'addon.notifications.errorgetnotifications', true);
this.loadMoreError = true; // Set to prevent infinite calls with infinite-loading.
@ -162,6 +126,11 @@ export class AddonNotificationsListPage {
if (notifications.length > 0) {
const promises = notifications.map((notification) => {
if (notification.read) {
// Already read, don't mark it.
return Promise.resolve();
}
return this.notificationsProvider.markNotificationRead(notification.id);
});

View File

@ -21,6 +21,7 @@ import { CoreSitesProvider } from '@providers/sites';
import { CoreTextUtilsProvider } from '@providers/utils/text';
import { CoreEmulatorHelperProvider } from '@core/emulator/providers/helper';
import { AddonNotificationsProvider } from './notifications';
import { AddonNotificationsHelperProvider } from './helper';
/**
* Notifications cron handler.
@ -32,7 +33,7 @@ export class AddonNotificationsCronHandler implements CoreCronHandler {
constructor(private appProvider: CoreAppProvider, private eventsProvider: CoreEventsProvider,
private sitesProvider: CoreSitesProvider, private localNotifications: CoreLocalNotificationsProvider,
private notificationsProvider: AddonNotificationsProvider, private textUtils: CoreTextUtilsProvider,
private emulatorHelper: CoreEmulatorHelperProvider) {}
private emulatorHelper: CoreEmulatorHelperProvider, private notificationsHelper: AddonNotificationsHelperProvider) {}
/**
* Get the time between consecutive executions.
@ -91,7 +92,9 @@ export class AddonNotificationsCronHandler implements CoreCronHandler {
* @return {Promise<any[]>} Promise resolved with the notifications.
*/
protected fetchNotifications(siteId: string): Promise<any[]> {
return this.notificationsProvider.getUnreadNotifications(0, undefined, true, false, true, siteId);
return this.notificationsHelper.getNotifications([], undefined, true, false, true, siteId).then((result) => {
return result.notifications;
});
}
/**
@ -102,7 +105,7 @@ export class AddonNotificationsCronHandler implements CoreCronHandler {
*/
protected getTitleAndText(notification: any): Promise<any> {
const data = {
title: notification.userfromfullname,
title: notification.subject || notification.userfromfullname,
text: notification.mobiletext.replace(/-{4,}/ig, '')
};
data.text = this.textUtils.replaceNewLines(data.text, '<br>');

View File

@ -0,0 +1,93 @@
// (C) Copyright 2015 Martin Dougiamas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injectable } from '@angular/core';
import { CoreSitesProvider } from '@providers/sites';
import { AddonNotificationsProvider } from './notifications';
/**
* Service that provides some helper functions for notifications.
*/
@Injectable()
export class AddonNotificationsHelperProvider {
constructor(private notificationsProvider: AddonNotificationsProvider, private sitesProvider: CoreSitesProvider) {
}
/**
* Get some notifications. It will try to use the new WS if available.
*
* @param {any[]} notifications Current list of loaded notifications. It's used to calculate the offset.
* @param {number} [limit] Number of notifications to get. Defaults to LIST_LIMIT.
* @param {boolean} [toDisplay=true] True if notifications will be displayed to the user, either in view or in a notification.
* @param {boolean} [forceCache] True if it should return cached data. Has priority over ignoreCache.
* @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down).
* @param {string} [siteId] Site ID. If not defined, use current site.
* @return {Promise<{notifications: any[], canLoadMore: boolean}>} Promise resolved with notifications and if can load more.
*/
getNotifications(notifications: any[], limit?: number, toDisplay: boolean = true, forceCache?: boolean, ignoreCache?: boolean,
siteId?: string): Promise<{notifications: any[], canLoadMore: boolean}> {
notifications = notifications || [];
limit = limit || AddonNotificationsProvider.LIST_LIMIT;
siteId = siteId || this.sitesProvider.getCurrentSiteId();
return this.notificationsProvider.isPopupAvailable(siteId).then((available) => {
if (available) {
return this.notificationsProvider.getPopupNotifications(notifications.length, limit, toDisplay, forceCache,
ignoreCache, siteId);
} else {
// Fallback to get_messages. We need 2 calls, one for read and the other one for unread.
const unreadFrom = notifications.reduce((total, current) => {
return total + (current.read ? 0 : 1);
}, 0);
return this.notificationsProvider.getUnreadNotifications(unreadFrom, limit, toDisplay, forceCache, ignoreCache,
siteId).then((unread) => {
let promise;
if (unread.length < limit) {
// Limit not reached. Get read notifications until reach the limit.
const readLimit = limit - unread.length,
readFrom = notifications.length - unreadFrom;
promise = this.notificationsProvider.getReadNotifications(readFrom, readLimit, toDisplay, forceCache,
ignoreCache, siteId).then((read) => {
return unread.concat(read);
}).catch((error): any => {
if (unread.length > 0) {
// We were able to get some unread, return only the unread ones.
return unread;
}
return Promise.reject(error);
});
} else {
promise = Promise.resolve(unread);
}
return promise.then((notifications) => {
return {
notifications: notifications,
canLoadMore: notifications.length >= limit
};
});
});
}
});
}
}

View File

@ -45,9 +45,10 @@ export class AddonNotificationsProvider {
* Function to format notification data.
*
* @param {any[]} notifications List of notifications.
* @param {boolean} [read] Whether the notifications are read or unread.
* @return {Promise<any[]>} Promise resolved with notifications.
*/
protected formatNotificationsData(notifications: any[]): Promise<any> {
protected formatNotificationsData(notifications: any[], read?: boolean): Promise<any> {
const promises = notifications.map((notification) => {
// Set message to show.
if (notification.contexturl && notification.contexturl.indexOf('/mod/forum/') >= 0) {
@ -55,6 +56,7 @@ export class AddonNotificationsProvider {
} else {
notification.mobiletext = notification.fullmessage;
}
// Try to set courseid the notification belongs to.
const cid = notification.fullmessagehtml.match(/course\/view\.php\?id=([^"]*)/);
if (cid && cid[1]) {
@ -71,6 +73,13 @@ export class AddonNotificationsProvider {
});
}
notification.moodlecomponent = notification.component;
notification.notification = 1;
notification.notif = 1;
if (typeof read != 'undefined') {
notification.read = read;
}
return Promise.resolve(notification);
});
@ -154,7 +163,7 @@ export class AddonNotificationsProvider {
if (response.messages) {
const notifications = response.messages;
return this.formatNotificationsData(notifications).then(() => {
return this.formatNotificationsData(notifications, read).then(() => {
if (this.appProvider.isDesktop() && toDisplay && !read && limitFrom === 0) {
// Store the last received notification. Don't block the user for this.
this.emulatorHelper.storeLastReceivedNotification(
@ -170,6 +179,67 @@ export class AddonNotificationsProvider {
});
}
/**
* Get notifications from site using the new WebService.
*
* @param {number} offset Position of the first notification to get.
* @param {number} [limit] Number of notifications to get. Defaults to LIST_LIMIT.
* @param {boolean} [toDisplay=true] True if notifications will be displayed to the user, either in view or in a notification.
* @param {boolean} [forceCache] True if it should return cached data. Has priority over ignoreCache.
* @param {boolean} [ignoreCache] True if it should ignore cached data (it will always fail in offline or server down).
* @param {string} [siteId] Site ID. If not defined, use current site.
* @return {Promise<{notifications: any[], canLoadMore: boolean}>} Promise resolved with notifications and if can load more.
* @since 3.2
*/
getPopupNotifications(offset: number, limit?: number, toDisplay: boolean = true, forceCache?: boolean, ignoreCache?: boolean,
siteId?: string): Promise<{notifications: any[], canLoadMore: boolean}> {
limit = limit || AddonNotificationsProvider.LIST_LIMIT;
this.logger.debug('Get popup notifications from ' + offset + '. Limit: ' + limit);
return this.sitesProvider.getSite(siteId).then((site) => {
const data = {
useridto: site.getUserId(),
newestfirst: 1,
offset: offset,
limit: limit + 1 // Get one more to calculate canLoadMore.
},
preSets = {
cacheKey: this.getNotificationsCacheKey(),
omitExpires: forceCache,
getFromCache: forceCache || !ignoreCache,
emergencyCache: forceCache || !ignoreCache,
};
// Get notifications.
return site.read('message_popup_get_popup_notifications', data, preSets).then((response) => {
if (response.notifications) {
const result: any = {
canLoadMore: response.notifications.length > limit
},
notifications = response.notifications.slice(0, limit);
result.notifications = notifications;
return this.formatNotificationsData(notifications).then(() => {
const first = notifications[0];
if (this.appProvider.isDesktop() && toDisplay && offset === 0 && first && !first.read) {
// Store the last received notification. Don't block the user for this.
this.emulatorHelper.storeLastReceivedNotification(
AddonNotificationsProvider.PUSH_SIMULATION_COMPONENT, first, siteId);
}
return result;
});
} else {
return Promise.reject(null);
}
});
});
}
/**
* Get read notifications from site.
*
@ -244,6 +314,18 @@ export class AddonNotificationsProvider {
});
}
/**
* Returns whether or not popup WS is available for a certain site.
*
* @param {string} [siteId] Site ID. If not defined, current site.
* @return {Promise<boolean>} Promise resolved with true if available, resolved with false or rejected otherwise.
*/
isPopupAvailable(siteId?: string): Promise<boolean> {
return this.sitesProvider.getSite(siteId).then((site) => {
return site.wsAvailable('message_popup_get_popup_notifications');
});
}
/**
* Mark all message notification as read.
*

View File

@ -40,7 +40,9 @@ export class AddonNotificationsPushClickHandler implements CorePushNotifications
handles(notification: any): boolean | Promise<boolean> {
if (this.utils.isTrueOrOne(notification.notif)) {
// Notification clicked, mark as read. Don't block for this.
this.notificationsProvider.markNotificationRead(notification.savedmessageid, notification.site).then(() => {
const notifId = notification.savedmessageid || notification.id;
this.notificationsProvider.markNotificationRead(notifId, notification.site).then(() => {
this.eventsProvider.trigger(AddonNotificationsProvider.READ_CHANGED_EVENT, null, notification.site);
}).catch(() => {
// Ignore errors.

View File

@ -161,17 +161,15 @@ export class CoreEmulatorHelperProvider implements CoreInitHandler {
// There is a new notification, show it.
return getDataFn(notification).then((titleAndText) => {
// Set some calculated data.
notification.site = siteId;
notification.name = notification.name || notification.eventtype;
const localNotif: ILocalNotification = {
id: 1,
trigger: {
at: new Date()
},
title: titleAndText.title,
text: titleAndText.text,
data: {
notif: notification,
site: siteId
}
data: notification
};
return this.localNotifProvider.schedule(localNotif, component, siteId);

View File

@ -630,7 +630,7 @@ export class LocalNotificationsMock extends LocalNotifications {
* @return {number} Trigger time.
*/
protected getNotificationTriggerAt(notification: ILocalNotification): number {
const triggerAt = (notification.trigger && notification.trigger.at) || 0;
const triggerAt = (notification.trigger && notification.trigger.at) || new Date();
if (typeof triggerAt != 'number') {
return triggerAt.getTime();