From b2b0262c5bc2735eee40ed65074a661fe6264bb2 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 7 Sep 2023 11:13:26 +0200 Subject: [PATCH] MOBIL-3371 sites: Extract visitLink method --- .../recentlyaccesseditems.ts | 6 +--- .../timeline/components/events/events.ts | 6 +--- src/addons/mod/feedback/pages/form/form.ts | 7 +--- src/addons/mod/url/services/url-helper.ts | 10 +++--- .../insights/services/handlers/action-link.ts | 9 +---- src/core/features/mainmenu/pages/more/more.ts | 12 +++---- .../pages/global-search/global-search.ts | 3 +- src/core/services/sites.ts | 34 +++++++++++++++++++ 8 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/addons/block/recentlyaccesseditems/components/recentlyaccesseditems/recentlyaccesseditems.ts b/src/addons/block/recentlyaccesseditems/components/recentlyaccesseditems/recentlyaccesseditems.ts index 3992ed377..1194ca25e 100644 --- a/src/addons/block/recentlyaccesseditems/components/recentlyaccesseditems/recentlyaccesseditems.ts +++ b/src/addons/block/recentlyaccesseditems/components/recentlyaccesseditems/recentlyaccesseditems.ts @@ -21,7 +21,6 @@ import { } from '../../services/recentlyaccesseditems'; import { CoreTextUtils } from '@services/utils/text'; import { CoreDomUtils } from '@services/utils/dom'; -import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; import { CoreUtils } from '@services/utils/utils'; /** @@ -88,10 +87,7 @@ export class AddonBlockRecentlyAccessedItemsComponent extends CoreBlockBaseCompo const modal = await CoreDomUtils.showModalLoading(); try { - const treated = await CoreContentLinksHelper.handleLink(url); - if (!treated) { - return CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(url); - } + await CoreSites.visitLink(url); } finally { modal.dismiss(); } diff --git a/src/addons/block/timeline/components/events/events.ts b/src/addons/block/timeline/components/events/events.ts index 7b5cbbdac..2b855bd02 100644 --- a/src/addons/block/timeline/components/events/events.ts +++ b/src/addons/block/timeline/components/events/events.ts @@ -16,7 +16,6 @@ import { Component, Input, Output, EventEmitter } from '@angular/core'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreTextUtils } from '@services/utils/text'; -import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; import { CoreEnrolledCourseDataWithOptions } from '@features/courses/services/courses-helper'; import { AddonBlockTimelineDayEvents } from '@addons/block/timeline/classes/section'; @@ -54,10 +53,7 @@ export class AddonBlockTimelineEventsComponent { const modal = await CoreDomUtils.showModalLoading(); try { - const treated = await CoreContentLinksHelper.handleLink(url); - if (!treated) { - return CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLogin(url); - } + await CoreSites.visitLink(url); } finally { modal.dismiss(); } diff --git a/src/addons/mod/feedback/pages/form/form.ts b/src/addons/mod/feedback/pages/form/form.ts index 0c45198a1..65f34f18c 100644 --- a/src/addons/mod/feedback/pages/form/form.ts +++ b/src/addons/mod/feedback/pages/form/form.ts @@ -14,7 +14,6 @@ import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { CoreSite } from '@classes/site'; -import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; import { CoreCourse, CoreCourseCommonModWSOptions } from '@features/course/services/course'; import { CoreCourseModuleData } from '@features/course/services/course-helper'; import { CanLeave } from '@guards/can-leave'; @@ -428,11 +427,7 @@ export class AddonModFeedbackFormPage implements OnInit, OnDestroy, CanLeave { const modal = await CoreDomUtils.showModalLoading(); try { - const treated = await CoreContentLinksHelper.handleLink(this.siteAfterSubmit); - - if (!treated) { - await this.currentSite.openInBrowserWithAutoLogin(this.siteAfterSubmit); - } + await CoreSites.visitLink(this.siteAfterSubmit, { siteId: this.currentSite.id }); } finally { modal.dismiss(); } diff --git a/src/addons/mod/url/services/url-helper.ts b/src/addons/mod/url/services/url-helper.ts index c59e77076..da2d65d3b 100644 --- a/src/addons/mod/url/services/url-helper.ts +++ b/src/addons/mod/url/services/url-helper.ts @@ -13,7 +13,6 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { makeSingleton } from '@singletons'; @@ -33,11 +32,10 @@ export class AddonModUrlHelperProvider { const modal = await CoreDomUtils.showModalLoading(); try { - const treated = await CoreContentLinksHelper.handleLink(url, undefined, true, true); - - if (!treated) { - await CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(url); - } + await CoreSites.visitLink(url, { + checkRoot: true, + openBrowserRoot: true, + }); } finally { modal.dismiss(); } diff --git a/src/addons/report/insights/services/handlers/action-link.ts b/src/addons/report/insights/services/handlers/action-link.ts index 1c25b13db..e91ce2386 100644 --- a/src/addons/report/insights/services/handlers/action-link.ts +++ b/src/addons/report/insights/services/handlers/action-link.ts @@ -16,7 +16,6 @@ import { Injectable } from '@angular/core'; import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler'; import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; -import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { makeSingleton, Translate } from '@singletons'; @@ -75,13 +74,7 @@ export class AddonReportInsightsActionLinkHandlerService extends CoreContentLink // Try to open the link in the app. const forwardUrl = decodeURIComponent(params.forwardurl); - const treated = await CoreContentLinksHelper.handleLink(forwardUrl); - if (!treated) { - // Cannot be opened in the app, open in browser. - const site = await CoreSites.getSite(siteId); - - await site.openInBrowserWithAutoLogin(forwardUrl); - } + await CoreSites.visitLink(forwardUrl, { siteId }); } }, }]; diff --git a/src/core/features/mainmenu/pages/more/more.ts b/src/core/features/mainmenu/pages/more/more.ts index dd3eaf5e8..af302e7ad 100644 --- a/src/core/features/mainmenu/pages/more/more.ts +++ b/src/core/features/mainmenu/pages/more/more.ts @@ -22,7 +22,6 @@ import { CoreMainMenu, CoreMainMenuCustomItem } from '../../services/mainmenu'; import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreNavigator } from '@services/navigator'; import { CoreCustomURLSchemes } from '@services/urlschemes'; -import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; import { CoreTextUtils } from '@services/utils/text'; import { Translate } from '@singletons'; import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager'; @@ -161,13 +160,10 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy { CoreCustomURLSchemes.treatHandleCustomURLError(error); }); } else if (/^[^:]{2,}:\/\/[^ ]+$/i.test(text)) { // Check if it's a URL. - // Check if the app can handle the URL. - const treated = await CoreContentLinksHelper.handleLink(text, undefined, true, true); - - if (!treated) { - // Can't handle it, open it in browser. - CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(text); - } + await CoreSites.visitLink(text, { + checkRoot: true, + openBrowserRoot: true, + }); } else { // It's not a URL, open it in a modal so the user can see it and copy it. CoreTextUtils.viewText(Translate.instant('core.qrscanner'), text, { diff --git a/src/core/features/search/pages/global-search/global-search.ts b/src/core/features/search/pages/global-search/global-search.ts index 136847c19..100b8d1bb 100644 --- a/src/core/features/search/pages/global-search/global-search.ts +++ b/src/core/features/search/pages/global-search/global-search.ts @@ -14,7 +14,6 @@ import { Component, OnInit, OnDestroy } from '@angular/core'; import { CoreDomUtils } from '@services/utils/dom'; -import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; import { CoreSearchGlobalSearchResultsSource } from '@features/search/classes/global-search-results-source'; import { CoreSites } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; @@ -136,7 +135,7 @@ export class CoreSearchGlobalSearchPage implements OnInit, OnDestroy { * @param result Result to visit. */ async visitResult(result: CoreSearchGlobalSearchResult): Promise { - await CoreContentLinksHelper.handleLink(result.url); + await CoreSites.visitLink(result.url); } /** diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index 265232db3..29e383242 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -64,6 +64,7 @@ import { CoreNetwork } from '@services/network'; import { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest-support-config'; import { CoreLang, CoreLangFormat } from '@services/lang'; import { CoreNative } from '@features/native/services/native'; +import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; export const CORE_SITE_SCHEMAS = new InjectionToken('CORE_SITE_SCHEMAS'); export const CORE_SITE_CURRENT_SITE_ID_CONFIG = 'current_site_id'; @@ -707,6 +708,39 @@ export class CoreSitesProvider { return CoreConstants.CONFIG.wsservice; } + /** + * Visit a site link. + * + * @param url URL to handle. + * @param options Behaviour options. + * @param options.siteId Site Id. + * @param options.username Username related with the URL. E.g. in 'http://myuser@m.com', url would be 'http://m.com' and + * the username 'myuser'. Don't use it if you don't want to filter by username. + * @param options.checkRoot Whether to check if the URL is the root URL of a site. + * @param options.openBrowserRoot Whether to open in browser if it's root URL and it belongs to current site. + */ + async visitLink( + url: string, + options: { + siteId?: string; + username?: string; + checkRoot?: boolean; + openBrowserRoot?: boolean; + } = {}, + ): Promise { + const treated = await CoreContentLinksHelper.handleLink(url, options.username, options.checkRoot, options.openBrowserRoot); + + if (treated) { + return; + } + + const site = options.siteId + ? await CoreSites.getSite(options.siteId) + : CoreSites.getCurrentSite(); + + await site?.openInBrowserWithAutoLogin(url); + } + /** * Check for the minimum required version. *