From 8b2ed818fa0851d8df6476176f6c25f646e51f9a Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 14 Mar 2019 09:25:29 +0100 Subject: [PATCH] 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; }