Skip to content

Commit 8b2ed81

Browse files
committed
MOBILE-2919 forum: Handle forum discussion notification clicks
1 parent 6e054bf commit 8b2ed81

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

src/addon/mod/forum/components/post/addon-mod-forum-post.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ion-card-header text-wrap no-padding >
1+
<ion-card-header text-wrap no-padding id="addon-mod_forum-post-{{post.id}}">
22
<ion-item text-wrap>
33
<ion-avatar core-user-avatar [user]="post" item-start (click)="openUserProfile(post.userid)"></ion-avatar>
44
<h2><span [class.core-bold]="post.parent == 0"><core-format-text [text]="post.subject"></core-format-text></span></h2>

src/addon/mod/forum/pages/discussion/discussion.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class AddonModForumDiscussionPage implements OnDestroy {
7676
cmId: number;
7777

7878
protected forumId: number;
79+
protected postId: number;
7980
protected onlineObserver: any;
8081
protected syncObserver: any;
8182
protected syncManualObserver: any;
@@ -107,6 +108,7 @@ export class AddonModForumDiscussionPage implements OnDestroy {
107108
this.discussionId = navParams.get('discussionId');
108109
this.trackPosts = navParams.get('trackPosts');
109110
this.locked = navParams.get('locked');
111+
this.postId = navParams.get('postId');
110112

111113
this.isOnline = this.appProvider.isOnline();
112114
this.onlineObserver = network.onchange().subscribe((online) => {
@@ -124,7 +126,14 @@ export class AddonModForumDiscussionPage implements OnDestroy {
124126
* View loaded.
125127
*/
126128
ionViewDidLoad(): void {
127-
this.fetchPosts(true, false, true);
129+
this.fetchPosts(true, false, true).then(() => {
130+
if (this.postId) {
131+
// Scroll to the post.
132+
setTimeout(() => {
133+
this.domUtils.scrollToElementBySelector(this.content, '#addon-mod_forum-post-' + this.postId);
134+
});
135+
}
136+
});
128137
}
129138

130139
/**

src/addon/notifications/notifications.module.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import { CoreSettingsDelegate } from '@core/settings/providers/delegate';
2424
import { CoreCronDelegate } from '@providers/cron';
2525
import { CoreLocalNotificationsProvider } from '@providers/local-notifications';
2626
import { CoreSitesProvider } from '@providers/sites';
27+
import { CoreUrlUtilsProvider } from '@providers/utils/url';
2728
import { CoreUtilsProvider } from '@providers/utils/utils';
2829
import { AddonPushNotificationsDelegate } from '@addon/pushnotifications/providers/delegate';
30+
import { AddonModForumProvider } from '@addon/mod/forum/providers/forum';
2931

3032
// List of providers (without handlers).
3133
export const ADDON_NOTIFICATIONS_PROVIDERS: any[] = [
@@ -50,12 +52,44 @@ export class AddonNotificationsModule {
5052
cronDelegate: CoreCronDelegate, cronHandler: AddonNotificationsCronHandler, zone: NgZone,
5153
appProvider: CoreAppProvider, utils: CoreUtilsProvider, sitesProvider: CoreSitesProvider,
5254
notificationsProvider: AddonNotificationsProvider, localNotifications: CoreLocalNotificationsProvider,
53-
linkHelper: CoreContentLinksHelperProvider, pushNotificationsDelegate: AddonPushNotificationsDelegate) {
55+
linkHelper: CoreContentLinksHelperProvider, pushNotificationsDelegate: AddonPushNotificationsDelegate,
56+
urlUtils: CoreUrlUtilsProvider, forumProvider: AddonModForumProvider) {
57+
5458
mainMenuDelegate.registerHandler(mainMenuHandler);
5559
settingsDelegate.registerHandler(settingsHandler);
5660
cronDelegate.register(cronHandler);
5761

5862
const notificationClicked = (notification: any): void => {
63+
64+
// Temporary fix to make forum notifications work. This will be improved in next release.
65+
if (notification.moodlecomponent == 'mod_forum' && notification.name == 'posts') {
66+
sitesProvider.isFeatureDisabled('CoreCourseModuleDelegate_AddonModForum', notification.site).then((disabled) => {
67+
if (disabled) {
68+
// Forum is disabled, stop.
69+
return;
70+
}
71+
72+
const contextUrlParams = urlUtils.extractUrlParams(notification.contexturl),
73+
pageParams: any = {
74+
courseId: Number(notification.courseid),
75+
discussionId: Number(contextUrlParams.d),
76+
};
77+
78+
if (contextUrlParams.urlHash) {
79+
pageParams.postId = Number(contextUrlParams.urlHash.replace('p', ''));
80+
}
81+
82+
forumProvider.invalidateDiscussionPosts(pageParams.discussionId).catch(() => {
83+
// Ignore errors.
84+
}).then(() => {
85+
linkHelper.goInSite(undefined, 'AddonModForumDiscussionPage', pageParams, notification.site);
86+
});
87+
});
88+
} else {
89+
goToNotifications(notification);
90+
}
91+
};
92+
const goToNotifications = (notification: any): void => {
5993
sitesProvider.isFeatureDisabled('CoreMainMenuDelegate_AddonNotifications', notification.site).then((disabled) => {
6094
if (disabled) {
6195
// Notifications are disabled, stop.

src/providers/utils/url.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,23 @@ export class CoreUrlUtilsProvider {
5252
*/
5353
extractUrlParams(url: string): any {
5454
const regex = /[?&]+([^=&]+)=?([^&]*)?/gi,
55-
params = {};
55+
params: any = {},
56+
urlAndHash = url.split('#');
5657

57-
url.replace(regex, (match: string, key: string, value: string): string => {
58+
urlAndHash[0].replace(regex, (match: string, key: string, value: string): string => {
5859
params[key] = typeof value != 'undefined' ? value : '';
5960

6061
return match;
6162
});
6263

64+
if (urlAndHash.length > 1) {
65+
// Remove the URL from the array.
66+
urlAndHash.shift();
67+
68+
// Add the hash as a param with a special name. Use a join in case there is more than one #.
69+
params.urlHash = urlAndHash.join('#');
70+
}
71+
6372
return params;
6473
}
6574

0 commit comments

Comments
 (0)