MOBIL-3371 sites: Extract visitLink method

main
Noel De Martin 2023-09-07 11:13:26 +02:00
parent e9b4498b7f
commit b2b0262c5b
8 changed files with 47 additions and 40 deletions

View File

@ -21,7 +21,6 @@ import {
} from '../../services/recentlyaccesseditems'; } from '../../services/recentlyaccesseditems';
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
/** /**
@ -88,10 +87,7 @@ export class AddonBlockRecentlyAccessedItemsComponent extends CoreBlockBaseCompo
const modal = await CoreDomUtils.showModalLoading(); const modal = await CoreDomUtils.showModalLoading();
try { try {
const treated = await CoreContentLinksHelper.handleLink(url); await CoreSites.visitLink(url);
if (!treated) {
return CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(url);
}
} finally { } finally {
modal.dismiss(); modal.dismiss();
} }

View File

@ -16,7 +16,6 @@ import { Component, Input, Output, EventEmitter } from '@angular/core';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreEnrolledCourseDataWithOptions } from '@features/courses/services/courses-helper'; import { CoreEnrolledCourseDataWithOptions } from '@features/courses/services/courses-helper';
import { AddonBlockTimelineDayEvents } from '@addons/block/timeline/classes/section'; import { AddonBlockTimelineDayEvents } from '@addons/block/timeline/classes/section';
@ -54,10 +53,7 @@ export class AddonBlockTimelineEventsComponent {
const modal = await CoreDomUtils.showModalLoading(); const modal = await CoreDomUtils.showModalLoading();
try { try {
const treated = await CoreContentLinksHelper.handleLink(url); await CoreSites.visitLink(url);
if (!treated) {
return CoreSites.getRequiredCurrentSite().openInBrowserWithAutoLogin(url);
}
} finally { } finally {
modal.dismiss(); modal.dismiss();
} }

View File

@ -14,7 +14,6 @@
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { CoreSite } from '@classes/site'; import { CoreSite } from '@classes/site';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreCourse, CoreCourseCommonModWSOptions } from '@features/course/services/course'; import { CoreCourse, CoreCourseCommonModWSOptions } from '@features/course/services/course';
import { CoreCourseModuleData } from '@features/course/services/course-helper'; import { CoreCourseModuleData } from '@features/course/services/course-helper';
import { CanLeave } from '@guards/can-leave'; import { CanLeave } from '@guards/can-leave';
@ -428,11 +427,7 @@ export class AddonModFeedbackFormPage implements OnInit, OnDestroy, CanLeave {
const modal = await CoreDomUtils.showModalLoading(); const modal = await CoreDomUtils.showModalLoading();
try { try {
const treated = await CoreContentLinksHelper.handleLink(this.siteAfterSubmit); await CoreSites.visitLink(this.siteAfterSubmit, { siteId: this.currentSite.id });
if (!treated) {
await this.currentSite.openInBrowserWithAutoLogin(this.siteAfterSubmit);
}
} finally { } finally {
modal.dismiss(); modal.dismiss();
} }

View File

@ -13,7 +13,6 @@
// limitations under the License. // limitations under the License.
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { makeSingleton } from '@singletons'; import { makeSingleton } from '@singletons';
@ -33,11 +32,10 @@ export class AddonModUrlHelperProvider {
const modal = await CoreDomUtils.showModalLoading(); const modal = await CoreDomUtils.showModalLoading();
try { try {
const treated = await CoreContentLinksHelper.handleLink(url, undefined, true, true); await CoreSites.visitLink(url, {
checkRoot: true,
if (!treated) { openBrowserRoot: true,
await CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(url); });
}
} finally { } finally {
modal.dismiss(); modal.dismiss();
} }

View File

@ -16,7 +16,6 @@ import { Injectable } from '@angular/core';
import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler'; import { CoreContentLinksHandlerBase } from '@features/contentlinks/classes/base-handler';
import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate'; import { CoreContentLinksAction } from '@features/contentlinks/services/contentlinks-delegate';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { makeSingleton, Translate } from '@singletons'; import { makeSingleton, Translate } from '@singletons';
@ -75,13 +74,7 @@ export class AddonReportInsightsActionLinkHandlerService extends CoreContentLink
// Try to open the link in the app. // Try to open the link in the app.
const forwardUrl = decodeURIComponent(params.forwardurl); const forwardUrl = decodeURIComponent(params.forwardurl);
const treated = await CoreContentLinksHelper.handleLink(forwardUrl); await CoreSites.visitLink(forwardUrl, { siteId });
if (!treated) {
// Cannot be opened in the app, open in browser.
const site = await CoreSites.getSite(siteId);
await site.openInBrowserWithAutoLogin(forwardUrl);
}
} }
}, },
}]; }];

View File

@ -22,7 +22,6 @@ import { CoreMainMenu, CoreMainMenuCustomItem } from '../../services/mainmenu';
import { CoreEventObserver, CoreEvents } from '@singletons/events'; import { CoreEventObserver, CoreEvents } from '@singletons/events';
import { CoreNavigator } from '@services/navigator'; import { CoreNavigator } from '@services/navigator';
import { CoreCustomURLSchemes } from '@services/urlschemes'; import { CoreCustomURLSchemes } from '@services/urlschemes';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager'; import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
@ -161,13 +160,10 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy {
CoreCustomURLSchemes.treatHandleCustomURLError(error); CoreCustomURLSchemes.treatHandleCustomURLError(error);
}); });
} else if (/^[^:]{2,}:\/\/[^ ]+$/i.test(text)) { // Check if it's a URL. } else if (/^[^:]{2,}:\/\/[^ ]+$/i.test(text)) { // Check if it's a URL.
// Check if the app can handle the URL. await CoreSites.visitLink(text, {
const treated = await CoreContentLinksHelper.handleLink(text, undefined, true, true); checkRoot: true,
openBrowserRoot: true,
if (!treated) { });
// Can't handle it, open it in browser.
CoreSites.getCurrentSite()?.openInBrowserWithAutoLogin(text);
}
} else { } else {
// It's not a URL, open it in a modal so the user can see it and copy it. // 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, { CoreTextUtils.viewText(Translate.instant('core.qrscanner'), text, {

View File

@ -14,7 +14,6 @@
import { Component, OnInit, OnDestroy } from '@angular/core'; import { Component, OnInit, OnDestroy } from '@angular/core';
import { CoreDomUtils } from '@services/utils/dom'; 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 { CoreSearchGlobalSearchResultsSource } from '@features/search/classes/global-search-results-source';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
@ -136,7 +135,7 @@ export class CoreSearchGlobalSearchPage implements OnInit, OnDestroy {
* @param result Result to visit. * @param result Result to visit.
*/ */
async visitResult(result: CoreSearchGlobalSearchResult): Promise<void> { async visitResult(result: CoreSearchGlobalSearchResult): Promise<void> {
await CoreContentLinksHelper.handleLink(result.url); await CoreSites.visitLink(result.url);
} }
/** /**

View File

@ -64,6 +64,7 @@ import { CoreNetwork } from '@services/network';
import { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest-support-config'; import { CoreUserGuestSupportConfig } from '@features/user/classes/support/guest-support-config';
import { CoreLang, CoreLangFormat } from '@services/lang'; import { CoreLang, CoreLangFormat } from '@services/lang';
import { CoreNative } from '@features/native/services/native'; import { CoreNative } from '@features/native/services/native';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
export const CORE_SITE_SCHEMAS = new InjectionToken<CoreSiteSchema[]>('CORE_SITE_SCHEMAS'); export const CORE_SITE_SCHEMAS = new InjectionToken<CoreSiteSchema[]>('CORE_SITE_SCHEMAS');
export const CORE_SITE_CURRENT_SITE_ID_CONFIG = 'current_site_id'; export const CORE_SITE_CURRENT_SITE_ID_CONFIG = 'current_site_id';
@ -707,6 +708,39 @@ export class CoreSitesProvider {
return CoreConstants.CONFIG.wsservice; 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<void> {
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. * Check for the minimum required version.
* *