diff --git a/src/addons/messages/guards/index.ts b/src/addons/messages/guards/index.ts new file mode 100644 index 000000000..8d09ab80b --- /dev/null +++ b/src/addons/messages/guards/index.ts @@ -0,0 +1,44 @@ +// (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 { CanActivate, UrlTree } from '@angular/router'; +import { Router } from '@singletons'; +import { AddonMessagesMainMenuHandlerService } from '../services/handlers/mainmenu'; +import { AddonMessages } from '../services/messages'; + +/** + * Guard to redirect to the right page based on the current Moodle site version. + */ +@Injectable({ providedIn: 'root' }) +export class AddonMessagesIndexGuard implements CanActivate { + + /** + * @inheritdoc + */ + canActivate(): UrlTree { + return this.guard(); + } + + /** + * Check if there is a pending redirect and trigger it. + */ + private guard(): UrlTree { + const enabled = AddonMessages.isGroupMessagingEnabled(); + const path = `/main/${AddonMessagesMainMenuHandlerService.PAGE_NAME}/` + ( enabled ? 'group-conversations' : 'index'); + + return Router.parseUrl(path); + } + +} diff --git a/src/addons/messages/messages-lazy.module.ts b/src/addons/messages/messages-lazy.module.ts index c606a6ae0..9d3af46bf 100644 --- a/src/addons/messages/messages-lazy.module.ts +++ b/src/addons/messages/messages-lazy.module.ts @@ -16,6 +16,7 @@ import { Injector, NgModule } from '@angular/core'; import { Route, RouterModule, ROUTES, Routes } from '@angular/router'; import { buildTabMainRoutes } from '@features/mainmenu/mainmenu-tab-routing.module'; +import { AddonMessagesIndexGuard } from './guards'; export const AddonMessagesDiscussionRoute: Route = { path: 'discussion', @@ -51,8 +52,7 @@ function buildRoutes(injector: Injector): Routes { .then(m => m.AddonMessagesContactsPageModule), }, ...buildTabMainRoutes(injector, { - redirectTo: 'index', - pathMatch: 'full', + canActivate: [AddonMessagesIndexGuard], }), ]; } diff --git a/src/addons/messages/services/handlers/index-link.ts b/src/addons/messages/services/handlers/index-link.ts index e528bce36..d552a6886 100644 --- a/src/addons/messages/services/handlers/index-link.ts +++ b/src/addons/messages/services/handlers/index-link.ts @@ -18,6 +18,7 @@ import { CoreContentLinksAction } from '@features/contentlinks/services/contentl import { CoreNavigator } from '@services/navigator'; import { makeSingleton } from '@singletons'; import { AddonMessages } from '../messages'; +import { AddonMessagesMainMenuHandlerService } from './mainmenu'; /** * Content links handler for messaging index. @@ -37,9 +38,7 @@ export class AddonMessagesIndexLinkHandlerService extends CoreContentLinksHandle getActions(): CoreContentLinksAction[] | Promise { return [{ action: async (siteId): Promise => { - const pageName = await AddonMessages.getMainMessagesPagePathInSite(siteId); - - CoreNavigator.navigateToSitePath(pageName, { + CoreNavigator.navigateToSitePath(AddonMessagesMainMenuHandlerService.PAGE_NAME, { siteId, preferCurrentTab: false, }); diff --git a/src/addons/messages/services/handlers/mainmenu.ts b/src/addons/messages/services/handlers/mainmenu.ts index 4461ee4f9..686071072 100644 --- a/src/addons/messages/services/handlers/mainmenu.ts +++ b/src/addons/messages/services/handlers/mainmenu.ts @@ -43,7 +43,7 @@ export class AddonMessagesMainMenuHandlerService implements CoreMainMenuHandler, protected handler: CoreMainMenuHandlerToDisplay = { icon: 'fas-comments', title: 'addon.messages.messages', - page: AddonMessages.getMainMessagesPagePath(), + page: AddonMessagesMainMenuHandlerService.PAGE_NAME, class: 'addon-messages-handler', showBadge: true, // Do not check isMessageCountEnabled because we'll use fallback it not enabled. badge: '', @@ -107,8 +107,6 @@ export class AddonMessagesMainMenuHandlerService implements CoreMainMenuHandler, * @return Data needed to render the handler. */ getDisplayData(): CoreMainMenuHandlerToDisplay { - this.handler.page = AddonMessages.getMainMessagesPagePath(); - if (this.handler.loading) { this.refreshBadge(); } diff --git a/src/addons/messages/services/handlers/push-click.ts b/src/addons/messages/services/handlers/push-click.ts index 4363fa90c..f90495ab4 100644 --- a/src/addons/messages/services/handlers/push-click.ts +++ b/src/addons/messages/services/handlers/push-click.ts @@ -20,6 +20,7 @@ import { CoreNavigator } from '@services/navigator'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton } from '@singletons'; import { AddonMessages } from '../messages'; +import { AddonMessagesMainMenuHandlerService } from './mainmenu'; /** * Handler for messaging push notifications clicks. @@ -61,7 +62,6 @@ export class AddonMessagesPushClickHandlerService implements CorePushNotificatio // Check if group messaging is enabled, to determine which page should be loaded. const enabled = await AddonMessages.isGroupMessagingEnabledInSite(notification.site); - const pageName = await AddonMessages.getMainMessagesPagePathInSite(notification.site); let nextPageParams: Params | undefined; @@ -76,7 +76,7 @@ export class AddonMessagesPushClickHandlerService implements CorePushNotificatio }; } - await CoreNavigator.navigateToSitePath(pageName, { + await CoreNavigator.navigateToSitePath(AddonMessagesMainMenuHandlerService.PAGE_NAME, { siteId: notification.site, preferCurrentTab: false, nextNavigation: nextPageParams ? diff --git a/src/addons/messages/services/messages.ts b/src/addons/messages/services/messages.ts index e987af5e7..d845f73ad 100644 --- a/src/addons/messages/services/messages.ts +++ b/src/addons/messages/services/messages.ts @@ -30,7 +30,6 @@ import { CoreSite, CoreSiteWSPreSets } from '@classes/site'; import { CoreWSExternalWarning } from '@services/ws'; import { makeSingleton } from '@singletons'; import { CoreError } from '@classes/errors/error'; -import { AddonMessagesMainMenuHandlerService } from './handlers/mainmenu'; import { AddonMessagesSyncEvents, AddonMessagesSyncProvider } from './messages-sync'; const ROOT_CACHE_KEY = 'mmaMessages:'; @@ -1411,29 +1410,6 @@ export class AddonMessagesProvider { throw new CoreError('Error getting message preferences'); } - /** - * Gets the site main messages page path for a site. - * - * @param siteId Site ID. If not defined, use current site. - * @return Main messages page path of the site. - */ - async getMainMessagesPagePathInSite(siteId?: string): Promise { - const enabled = await this.isGroupMessagingEnabledInSite(siteId); - - return AddonMessagesMainMenuHandlerService.PAGE_NAME + ( enabled ? '/group-conversations' : ''); - } - - /** - * Gets the site main messages page path. - * - * @return Main messages page path of the site. - */ - getMainMessagesPagePath(): string { - const enabled = this.isGroupMessagingEnabled(); - - return AddonMessagesMainMenuHandlerService.PAGE_NAME + ( enabled ? '/group-conversations' : ''); - } - /** * Get messages according to the params. *