From 6e054bf6cdc938d468418273b76afd7616c2dfa4 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 13 Mar 2019 11:08:15 +0100 Subject: [PATCH 1/2] MOBILE-2919 message: Open conversation when push clicked --- src/addon/messages/messages.module.ts | 11 ++++++++++- .../pages/group-conversations/group-conversations.ts | 7 +++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/addon/messages/messages.module.ts b/src/addon/messages/messages.module.ts index 43b3609c6..9ea151d83 100644 --- a/src/addon/messages/messages.module.ts +++ b/src/addon/messages/messages.module.ts @@ -109,11 +109,20 @@ export class AddonMessagesModule { messagesProvider.invalidateDiscussionsCache(notification.site).finally(() => { // Check if group messaging is enabled, to determine which page should be loaded. messagesProvider.isGroupMessagingEnabledInSite(notification.site).then((enabled) => { + const pageParams: any = {}; let pageName = 'AddonMessagesIndexPage'; if (enabled) { pageName = 'AddonMessagesGroupConversationsPage'; } - linkHelper.goInSite(undefined, pageName, undefined, notification.site); + + // 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); + } + + linkHelper.goInSite(undefined, pageName, pageParams, notification.site); }); }); }); diff --git a/src/addon/messages/pages/group-conversations/group-conversations.ts b/src/addon/messages/pages/group-conversations/group-conversations.ts index 39be42305..6dd5f0d28 100644 --- a/src/addon/messages/pages/group-conversations/group-conversations.ts +++ b/src/addon/messages/pages/group-conversations/group-conversations.ts @@ -70,6 +70,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { protected siteId: string; protected currentUserId: number; protected conversationId: number; + protected discussionUserId: number; protected newMessagesObserver: any; protected pushObserver: any; protected appResumeSubscription: any; @@ -89,7 +90,9 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { this.loadingString = translate.instant('core.loading'); this.siteId = sitesProvider.getCurrentSiteId(); this.currentUserId = sitesProvider.getCurrentSiteUserId(); + // Conversation to load. this.conversationId = navParams.get('conversationId') || false; + this.discussionUserId = !this.conversationId && (navParams.get('discussionUserId') || false); // Update conversations when new message is received. this.newMessagesObserver = eventsProvider.on(AddonMessagesProvider.NEW_MESSAGE_EVENT, (data) => { @@ -213,9 +216,9 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy { * Component loaded. */ ngOnInit(): void { - if (this.conversationId) { + if (this.conversationId || this.discussionUserId) { // There is a discussion to load, open the discussion in a new state. - this.gotoConversation(this.conversationId); + this.gotoConversation(this.conversationId, this.discussionUserId); } this.fetchData().then(() => { From 8b2ed818fa0851d8df6476176f6c25f646e51f9a Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 14 Mar 2019 09:25:29 +0100 Subject: [PATCH 2/2] MOBILE-2919 forum: Handle forum discussion notification clicks --- .../components/post/addon-mod-forum-post.html | 2 +- .../mod/forum/pages/discussion/discussion.ts | 11 +++++- .../notifications/notifications.module.ts | 36 ++++++++++++++++++- src/providers/utils/url.ts | 13 +++++-- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/addon/mod/forum/components/post/addon-mod-forum-post.html b/src/addon/mod/forum/components/post/addon-mod-forum-post.html index 19a55682d..6592dabb2 100644 --- a/src/addon/mod/forum/components/post/addon-mod-forum-post.html +++ b/src/addon/mod/forum/components/post/addon-mod-forum-post.html @@ -1,4 +1,4 @@ - +

diff --git a/src/addon/mod/forum/pages/discussion/discussion.ts b/src/addon/mod/forum/pages/discussion/discussion.ts index d25e6e4d1..7b5f6f6f1 100644 --- a/src/addon/mod/forum/pages/discussion/discussion.ts +++ b/src/addon/mod/forum/pages/discussion/discussion.ts @@ -76,6 +76,7 @@ export class AddonModForumDiscussionPage implements OnDestroy { cmId: number; protected forumId: number; + protected postId: number; protected onlineObserver: any; protected syncObserver: any; protected syncManualObserver: any; @@ -107,6 +108,7 @@ export class AddonModForumDiscussionPage implements OnDestroy { this.discussionId = navParams.get('discussionId'); this.trackPosts = navParams.get('trackPosts'); this.locked = navParams.get('locked'); + this.postId = navParams.get('postId'); this.isOnline = this.appProvider.isOnline(); this.onlineObserver = network.onchange().subscribe((online) => { @@ -124,7 +126,14 @@ export class AddonModForumDiscussionPage implements OnDestroy { * View loaded. */ ionViewDidLoad(): void { - this.fetchPosts(true, false, true); + this.fetchPosts(true, false, true).then(() => { + if (this.postId) { + // Scroll to the post. + setTimeout(() => { + this.domUtils.scrollToElementBySelector(this.content, '#addon-mod_forum-post-' + this.postId); + }); + } + }); } /** diff --git a/src/addon/notifications/notifications.module.ts b/src/addon/notifications/notifications.module.ts index 384901f44..3dbd875f4 100644 --- a/src/addon/notifications/notifications.module.ts +++ b/src/addon/notifications/notifications.module.ts @@ -24,8 +24,10 @@ import { CoreSettingsDelegate } from '@core/settings/providers/delegate'; import { CoreCronDelegate } from '@providers/cron'; import { CoreLocalNotificationsProvider } from '@providers/local-notifications'; import { CoreSitesProvider } from '@providers/sites'; +import { CoreUrlUtilsProvider } from '@providers/utils/url'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { AddonPushNotificationsDelegate } from '@addon/pushnotifications/providers/delegate'; +import { AddonModForumProvider } from '@addon/mod/forum/providers/forum'; // List of providers (without handlers). export const ADDON_NOTIFICATIONS_PROVIDERS: any[] = [ @@ -50,12 +52,44 @@ export class AddonNotificationsModule { cronDelegate: CoreCronDelegate, cronHandler: AddonNotificationsCronHandler, zone: NgZone, appProvider: CoreAppProvider, utils: CoreUtilsProvider, sitesProvider: CoreSitesProvider, notificationsProvider: AddonNotificationsProvider, localNotifications: CoreLocalNotificationsProvider, - linkHelper: CoreContentLinksHelperProvider, pushNotificationsDelegate: AddonPushNotificationsDelegate) { + linkHelper: CoreContentLinksHelperProvider, pushNotificationsDelegate: AddonPushNotificationsDelegate, + urlUtils: CoreUrlUtilsProvider, forumProvider: AddonModForumProvider) { + mainMenuDelegate.registerHandler(mainMenuHandler); settingsDelegate.registerHandler(settingsHandler); cronDelegate.register(cronHandler); const notificationClicked = (notification: any): void => { + + // Temporary fix to make forum notifications work. This will be improved in next release. + if (notification.moodlecomponent == 'mod_forum' && notification.name == 'posts') { + sitesProvider.isFeatureDisabled('CoreCourseModuleDelegate_AddonModForum', notification.site).then((disabled) => { + if (disabled) { + // Forum is disabled, stop. + return; + } + + const contextUrlParams = urlUtils.extractUrlParams(notification.contexturl), + pageParams: any = { + courseId: Number(notification.courseid), + discussionId: Number(contextUrlParams.d), + }; + + if (contextUrlParams.urlHash) { + pageParams.postId = Number(contextUrlParams.urlHash.replace('p', '')); + } + + forumProvider.invalidateDiscussionPosts(pageParams.discussionId).catch(() => { + // Ignore errors. + }).then(() => { + linkHelper.goInSite(undefined, 'AddonModForumDiscussionPage', pageParams, notification.site); + }); + }); + } else { + goToNotifications(notification); + } + }; + const goToNotifications = (notification: any): void => { sitesProvider.isFeatureDisabled('CoreMainMenuDelegate_AddonNotifications', notification.site).then((disabled) => { if (disabled) { // Notifications are disabled, stop. diff --git a/src/providers/utils/url.ts b/src/providers/utils/url.ts index 4f463276e..35a1655ba 100644 --- a/src/providers/utils/url.ts +++ b/src/providers/utils/url.ts @@ -52,14 +52,23 @@ export class CoreUrlUtilsProvider { */ extractUrlParams(url: string): any { const regex = /[?&]+([^=&]+)=?([^&]*)?/gi, - params = {}; + params: any = {}, + urlAndHash = url.split('#'); - url.replace(regex, (match: string, key: string, value: string): string => { + urlAndHash[0].replace(regex, (match: string, key: string, value: string): string => { params[key] = typeof value != 'undefined' ? value : ''; return match; }); + if (urlAndHash.length > 1) { + // Remove the URL from the array. + urlAndHash.shift(); + + // Add the hash as a param with a special name. Use a join in case there is more than one #. + params.urlHash = urlAndHash.join('#'); + } + return params; }