From 205fb9e7cbffa7a626f3cfa5b3661b2f384b6485 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 25 Feb 2021 13:32:34 +0100 Subject: [PATCH] MOBILE-3643 forum: Migrate push-click handler --- src/addons/mod/forum/forum.module.ts | 3 + .../mod/forum/services/handlers/push-click.ts | 88 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/addons/mod/forum/services/handlers/push-click.ts diff --git a/src/addons/mod/forum/forum.module.ts b/src/addons/mod/forum/forum.module.ts index cc6cc3b7c..283057df3 100644 --- a/src/addons/mod/forum/forum.module.ts +++ b/src/addons/mod/forum/forum.module.ts @@ -36,6 +36,8 @@ import { AddonModForumListLinkHandler } from './services/handlers/list-link'; import { AddonModForumPostLinkHandler } from './services/handlers/post-link'; import { CoreTagAreaDelegate } from '@features/tag/services/tag-area-delegate'; import { AddonModForumTagAreaHandler } from './services/handlers/tag-area'; +import { CorePushNotificationsDelegate } from '@features/pushnotifications/services/push-delegate'; +import { AddonModForumPushClickHandler } from './services/handlers/push-click'; const mainMenuRoutes: Routes = [ { @@ -97,6 +99,7 @@ const courseContentsRoutes: Routes = conditionalRoutes( CoreContentLinksDelegate.instance.registerHandler(AddonModForumListLinkHandler.instance); CoreContentLinksDelegate.instance.registerHandler(AddonModForumPostLinkHandler.instance); CoreTagAreaDelegate.instance.registerHandler(AddonModForumTagAreaHandler.instance); + CorePushNotificationsDelegate.instance.registerClickHandler(AddonModForumPushClickHandler.instance); }, }, ], diff --git a/src/addons/mod/forum/services/handlers/push-click.ts b/src/addons/mod/forum/services/handlers/push-click.ts new file mode 100644 index 000000000..dc898d75b --- /dev/null +++ b/src/addons/mod/forum/services/handlers/push-click.ts @@ -0,0 +1,88 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// 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 { Params } from '@angular/router'; + +import { AddonModForum } from '@addons/mod/forum/services/forum.service'; +import { CoreNavigator } from '@services/navigator'; +import { CorePushNotificationsClickHandler } from '@features/pushnotifications/services/push-delegate'; +import { CorePushNotificationsNotificationBasicData } from '@features/pushnotifications/services/pushnotifications'; +import { CoreUrlUtils } from '@services/utils/url'; +import { CoreUtils } from '@services/utils/utils'; +import { makeSingleton } from '@singletons'; + +import { AddonModForumModuleHandlerService } from './module'; + +/** + * Handler for forum push notifications clicks. + */ +@Injectable({ providedIn: 'root' }) +export class AddonModForumPushClickHandlerService implements CorePushNotificationsClickHandler { + + name = 'AddonModForumPushClickHandler'; + priority = 200; + featureName = 'CoreCourseModuleDelegate_AddonModForum'; + + /** + * Check if a notification click is handled by this handler. + * + * @param notification The notification to check. + * @return Whether the notification click is handled by this handler + */ + async handles(notification: NotificationData): Promise { + return CoreUtils.instance.isTrueOrOne(notification.notif) + && notification.moodlecomponent == 'mod_forum' + && notification.name == 'posts'; + } + + /** + * Handle the notification click. + * + * @param notification The notification to check. + * @return Promise resolved when done. + */ + async handleClick(notification: NotificationData): Promise { + const contextUrlParams = CoreUrlUtils.instance.extractUrlParams(notification.contexturl); + const data = notification.customdata || {}; + const courseId = Number(notification.courseid); + const discussionId = Number(contextUrlParams.d || data.discussionid); + const cmId = Number(data.cmid); + const pageParams: Params = { + forumId: Number(data.instance), + }; + + if (data.postid || contextUrlParams.urlHash) { + pageParams.postId = Number(data.postid || contextUrlParams.urlHash.replace('p', '')); + } + + await CoreUtils.instance.ignoreErrors( + AddonModForum.instance.invalidateDiscussionPosts(pageParams.discussionId, undefined, notification.site), + ); + + await CoreNavigator.instance.navigateToSitePath( + `${AddonModForumModuleHandlerService.PAGE_NAME}/${courseId}/${cmId}/${discussionId}`, + { siteId: notification.site, params: pageParams }, + ); + } + +} + +export class AddonModForumPushClickHandler extends makeSingleton(AddonModForumPushClickHandlerService) {} + +type NotificationData = CorePushNotificationsNotificationBasicData & { + courseid: number; + discussionid: number; + contexturl: string; +};