From dbec10b9d6413bcf2932a3b563b40335d67398fd Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 1 Dec 2020 15:23:40 +0100 Subject: [PATCH] MOBILE-3592 user: Implement profile link and mail handlers --- .../user/pages/profile/profile.page.ts | 2 +- .../user/services/handlers/profile-link.ts | 76 +++++++++++++++++++ .../user/services/handlers/profile-mail.ts | 75 ++++++++++++++++++ .../features/user/services/user-delegate.ts | 4 +- src/core/features/user/user.module.ts | 29 +++++-- 5 files changed, 174 insertions(+), 12 deletions(-) create mode 100644 src/core/features/user/services/handlers/profile-link.ts create mode 100644 src/core/features/user/services/handlers/profile-mail.ts diff --git a/src/core/features/user/pages/profile/profile.page.ts b/src/core/features/user/pages/profile/profile.page.ts index 537ce68c8..86f53ecfe 100644 --- a/src/core/features/user/pages/profile/profile.page.ts +++ b/src/core/features/user/pages/profile/profile.page.ts @@ -275,7 +275,7 @@ export class CoreUserProfilePage implements OnInit, OnDestroy { */ handlerClicked(event: Event, handler: CoreUserProfileHandlerData): void { // @todo: Pass the right navCtrl if this page is in the right pane of split view. - handler.action(event, this.navCtrl, this.user!, this.courseId); + handler.action(event, this.user!, this.courseId); } /** diff --git a/src/core/features/user/services/handlers/profile-link.ts b/src/core/features/user/services/handlers/profile-link.ts new file mode 100644 index 000000000..4cd8b434b --- /dev/null +++ b/src/core/features/user/services/handlers/profile-link.ts @@ -0,0 +1,76 @@ +// (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 { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler'; +import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; +import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; + +/** + * Handler to treat links to user profiles. + */ +@Injectable({ providedIn: 'root' }) +export class CoreUserProfileLinkHandler extends CoreContentLinksHandlerBase { + + name = 'CoreUserProfileLinkHandler'; + // Match user/view.php and user/profile.php but NOT grade/report/user/. + pattern = /((\/user\/view\.php)|(\/user\/profile\.php)).*([?&]id=\d+)/; + + /** + * Get the list of actions for a link (url). + * + * @param siteIds List of sites the URL belongs to. + * @param url The URL to treat. + * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param courseId Course ID related to the URL. Optional but recommended. + * @param data Extra data to handle the URL. + * @return List of (or promise resolved with list of) actions. + */ + getActions( + siteIds: string[], + url: string, + params: Params, + courseId?: number, // eslint-disable-line @typescript-eslint/no-unused-vars + data?: unknown, // eslint-disable-line @typescript-eslint/no-unused-vars + ): CoreContentLinksAction[] | Promise { + return [{ + action: (siteId): void => { + const pageParams = { + courseId: params.course, + userId: parseInt(params.id, 10), + }; + + CoreContentLinksHelper.instance.goInSite('/user', pageParams, siteId); + }, + }]; + } + + /** + * Check if the handler is enabled for a certain site (site + user) and a URL. + * If not defined, defaults to true. + * + * @param siteId The site ID. + * @param url The URL to treat. + * @param params The params of the URL. E.g. 'mysite.com?id=1' -> {id: 1} + * @param courseId Course ID related to the URL. Optional but recommended. + * @return Whether the handler is enabled for the URL and site. + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + isEnabled(siteId: string, url: string, params: Params, courseId?: number): boolean | Promise { + return url.indexOf('/grade/report/') == -1; + } + +} diff --git a/src/core/features/user/services/handlers/profile-mail.ts b/src/core/features/user/services/handlers/profile-mail.ts new file mode 100644 index 000000000..0b61adce8 --- /dev/null +++ b/src/core/features/user/services/handlers/profile-mail.ts @@ -0,0 +1,75 @@ +// (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 { CoreUserDelegate, CoreUserProfileHandler, CoreUserProfileHandlerData } from '../user-delegate'; +import { CoreSites } from '@services/sites'; +import { CoreUtils } from '@services/utils/utils'; +import { CoreUserProfile } from '../user'; + +/** + * Handler to send a email to a user. + */ +@Injectable({ providedIn: 'root' }) +export class CoreUserProfileMailHandler implements CoreUserProfileHandler { + + name = 'CoreUserProfileMail'; + priority = 700; + type = CoreUserDelegate.TYPE_COMMUNICATION; + + /** + * Check if handler is enabled. + * + * @return Always enabled. + */ + async isEnabled(): Promise { + return true; + } + + /** + * Check if handler is enabled for this user in this context. + * + * @param user User to check. + * @param courseId Course ID. + * @param navOptions Course navigation options for current user. See CoreCoursesProvider.getUserNavigationOptions. + * @param admOptions Course admin options for current user. See CoreCoursesProvider.getUserAdministrationOptions. + * @return Promise resolved with true if enabled, resolved with false otherwise. + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + async isEnabledForUser(user: CoreUserProfile, courseId: number, navOptions?: unknown, admOptions?: unknown): Promise { + return user.id != CoreSites.instance.getCurrentSiteUserId() && !!user.email; + } + + /** + * Returns the data needed to render the handler. + * + * @return Data needed to render the handler. + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + getDisplayData(user: CoreUserProfile, courseId: number): CoreUserProfileHandlerData { + return { + icon: 'mail', + title: 'core.user.sendemail', + class: 'core-user-profile-mail', + action: (event: Event, user: CoreUserProfile): void => { + event.preventDefault(); + event.stopPropagation(); + + CoreUtils.instance.openInBrowser('mailto:' + user.email); + }, + }; + } + +} diff --git a/src/core/features/user/services/user-delegate.ts b/src/core/features/user/services/user-delegate.ts index 411a375f3..0c11f480c 100644 --- a/src/core/features/user/services/user-delegate.ts +++ b/src/core/features/user/services/user-delegate.ts @@ -13,7 +13,6 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { NavController } from '@ionic/angular'; import { Subject, BehaviorSubject } from 'rxjs'; import { CoreDelegate, CoreDelegateHandler } from '@classes/delegate'; @@ -93,11 +92,10 @@ export interface CoreUserProfileHandlerData { * Action to do when clicked. * * @param event Click event. - * @param Nav controller to use to navigate. * @param user User object. * @param courseId Course ID being viewed. If not defined, site context. */ - action(event: Event, navCtrl: NavController, user: CoreUserProfile, courseId?: number): void; + action(event: Event, user: CoreUserProfile, courseId?: number): void; } /** diff --git a/src/core/features/user/user.module.ts b/src/core/features/user/user.module.ts index 1a7190632..591ed616c 100644 --- a/src/core/features/user/user.module.ts +++ b/src/core/features/user/user.module.ts @@ -12,13 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { NgModule } from '@angular/core'; +import { APP_INITIALIZER, NgModule } from '@angular/core'; import { Routes } from '@angular/router'; import { CoreMainMenuMoreRoutingModule } from '@features/mainmenu/pages/more/more-routing.module'; import { CORE_SITE_SCHEMAS } from '@services/sites'; import { SITE_SCHEMA, OFFLINE_SITE_SCHEMA } from './services/db/user'; import { CoreUserComponentsModule } from './components/components.module'; +import { CoreUserDelegate } from './services/user-delegate'; +import { CoreUserProfileMailHandler } from './services/handlers/profile-mail'; +import { CoreContentLinksDelegate } from '@features/contentlinks/services/contentlinks-delegate'; +import { CoreUserProfileLinkHandler } from './services/handlers/profile-link'; const routes: Routes = [ { @@ -41,13 +45,22 @@ const routes: Routes = [ ], multi: true, }, - // { @todo: Uncomment when the init process has been fixed. - // provide: APP_INITIALIZER, - // multi: true, - // deps: [CoreCronDelegate, CoreUserSyncCronHandler], - // useFactory: (cronDelegate: CoreCronDelegate, syncHandler: CoreUserSyncCronHandler) => - // () => cronDelegate.register(syncHandler), - // }, + { + provide: APP_INITIALIZER, + multi: true, + deps: [CoreUserDelegate, CoreUserProfileMailHandler, CoreContentLinksDelegate, CoreUserProfileLinkHandler], + useFactory: ( + userDelegate: CoreUserDelegate, + mailHandler: CoreUserProfileMailHandler, + linksDelegate: CoreContentLinksDelegate, + profileLinkHandler: CoreUserProfileLinkHandler, + + ) => () => { + // @todo: Register sync handler when init process has been fixed. + userDelegate.registerHandler(mailHandler); + linksDelegate.registerHandler(profileLinkHandler); + }, + }, ], }) export class CoreUserModule {}