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';
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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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 });
}
},
}];

View File

@ -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, {

View File

@ -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<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 { 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<CoreSiteSchema[]>('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<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.
*