Skip to content

Commit

Permalink
Merge pull request #3967 from crazyserver/MOBILE-4540
Browse files Browse the repository at this point in the history
MOBILE-4540 url: Reduce WS calls on module handler
  • Loading branch information
dpalou authored Mar 12, 2024
2 parents 04e6f6d + dd194b1 commit 7934e78
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 100 deletions.
5 changes: 3 additions & 2 deletions src/addons/mod/url/components/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import { CoreCourseContentsPage } from '@features/course/pages/contents/contents
import { CoreCourse } from '@features/course/services/course';
import { CoreMimetypeUtils } from '@services/utils/mimetype';
import { CoreTextUtils } from '@services/utils/text';
import { AddonModUrl, AddonModUrlDisplayOptions, AddonModUrlProvider, AddonModUrlUrl } from '../../services/url';
import { AddonModUrl, AddonModUrlDisplayOptions, AddonModUrlUrl } from '../../services/url';
import { AddonModUrlHelper } from '../../services/url-helper';
import { ADDON_MOD_URL_COMPONENT } from '../../constants';

/**
* Component that displays a url.
Expand All @@ -33,7 +34,7 @@ import { AddonModUrlHelper } from '../../services/url-helper';
})
export class AddonModUrlIndexComponent extends CoreCourseModuleMainResourceComponent implements OnInit {

component = AddonModUrlProvider.COMPONENT;
component = ADDON_MOD_URL_COMPONENT;
pluginName = 'url';

url?: string;
Expand Down
22 changes: 22 additions & 0 deletions src/addons/mod/url/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// (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.

export const ADDON_MOD_URL_COMPONENT = 'mmaModUrl';

// Routing.
export const ADDON_MOD_URL_PAGE_NAME = 'mod_url';

// Handlers.
export const ADDON_MOD_URL_ADDON_NAME = 'AddonModUrl';
export const ADDON_MOD_URL_MODNAME = 'url';
3 changes: 2 additions & 1 deletion src/addons/mod/url/services/handlers/index-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { Injectable } from '@angular/core';
import { CoreContentLinksModuleIndexHandler } from '@features/contentlinks/classes/module-index-handler';
import { makeSingleton } from '@singletons';
import { ADDON_MOD_URL_ADDON_NAME, ADDON_MOD_URL_MODNAME } from '../../constants';

/**
* Handler to treat links to url.
Expand All @@ -26,7 +27,7 @@ export class AddonModUrlIndexLinkHandlerService extends CoreContentLinksModuleIn
useModNameToGetModule = true;

constructor() {
super('AddonModUrl', 'url', 'u');
super(ADDON_MOD_URL_ADDON_NAME, ADDON_MOD_URL_MODNAME, 'u');
}

}
Expand Down
3 changes: 2 additions & 1 deletion src/addons/mod/url/services/handlers/list-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { Injectable } from '@angular/core';
import { CoreContentLinksModuleListHandler } from '@features/contentlinks/classes/module-list-handler';
import { makeSingleton } from '@singletons';
import { ADDON_MOD_URL_ADDON_NAME, ADDON_MOD_URL_MODNAME } from '../../constants';

/**
* Handler to treat links to URL list page.
Expand All @@ -25,7 +26,7 @@ export class AddonModUrlListLinkHandlerService extends CoreContentLinksModuleLis
name = 'AddonModUrlListLinkHandler';

constructor() {
super('AddonModUrl', 'url');
super(ADDON_MOD_URL_ADDON_NAME, ADDON_MOD_URL_MODNAME);
}

}
Expand Down
128 changes: 71 additions & 57 deletions src/addons/mod/url/services/handlers/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { CoreConstants, ModPurpose } from '@/core/constants';
import { Injectable, Type } from '@angular/core';
import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper';
import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler';
import { CoreCourse } from '@features/course/services/course';
import { CoreCourse, CoreCourseModuleContentFile } from '@features/course/services/course';
import { CoreCourseModuleData } from '@features/course/services/course-helper';
import { CoreCourseModuleHandler, CoreCourseModuleHandlerData } from '@features/course/services/module-delegate';
import { CoreNavigationOptions } from '@services/navigator';
Expand All @@ -27,18 +27,19 @@ import { AddonModUrlIndexComponent } from '../../components/index/index';
import { AddonModUrl } from '../url';
import { AddonModUrlHelper } from '../url-helper';
import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics';
import { CoreUrlUtils } from '@services/utils/url';
import { CoreMimetypeUtils } from '@services/utils/mimetype';
import { ADDON_MOD_URL_ADDON_NAME, ADDON_MOD_URL_MODNAME, ADDON_MOD_URL_PAGE_NAME } from '../../constants';

/**
* Handler to support url modules.
*/
@Injectable({ providedIn: 'root' })
export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase implements CoreCourseModuleHandler {

static readonly PAGE_NAME = 'mod_url';

name = 'AddonModUrl';
modName = 'url';
protected pageName = AddonModUrlModuleHandlerService.PAGE_NAME;
name = ADDON_MOD_URL_ADDON_NAME;
modName = ADDON_MOD_URL_MODNAME;
protected pageName = ADDON_MOD_URL_PAGE_NAME;

supportedFeatures = {
[CoreConstants.FEATURE_MOD_ARCHETYPE]: CoreConstants.MOD_ARCHETYPE_RESOURCE,
Expand All @@ -57,7 +58,6 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple
* @inheritdoc
*/
async getData(module: CoreCourseModuleData): Promise<CoreCourseModuleHandlerData> {

/**
* Open the URL.
*
Expand All @@ -69,8 +69,12 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple

CoreCourse.storeModuleViewed(courseId, module.id);

const contents = await CoreCourse.getModuleContents(module);
AddonModUrlHelper.open(contents[0].fileurl);
const mainFile = await this.getModuleMainFile(module);
if (!mainFile) {
return;
}

AddonModUrlHelper.open(mainFile.fileurl);
};

const handlerData: CoreCourseModuleHandlerData = {
Expand All @@ -94,7 +98,6 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple
}
},
button: {
hidden: true, // Hide it until we calculate if it should be displayed or not.
icon: 'fas-link',
label: 'core.openmodinbrowser',
action: (event: Event, module: CoreCourseModuleData, courseId: number): void => {
Expand All @@ -103,14 +106,8 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple
},
};

const hideButton = await CoreUtils.ignoreErrors(this.hideLinkButton(module));

if (handlerData.button && hideButton !== undefined) {
handlerData.button.hidden = hideButton;
}

try {
handlerData.icon = await this.getIconSrc(module);
handlerData.icon = await this.getIconSrc(module, handlerData.icon as string);
} catch {
// Ignore errors.
}
Expand All @@ -121,57 +118,72 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple
/**
* @inheritdoc
*/
async getIconSrc(module?: CoreCourseModuleData): Promise<string | undefined> {
async getIconSrc(module?: CoreCourseModuleData, modIcon?: string): Promise<string | undefined> {
if (!module) {
return;
return modIcon;
}

let mainFile = module.contents?.[0];

if (!mainFile) {
try {
// Try to get module contents, it's needed to get the URL with parameters.
const contents = await CoreCourse.getModuleContents(
module,
undefined,
undefined,
true,
false,
undefined,
'url',
);

mainFile = contents[0];
} catch {
// Fallback in case is not prefetched.
const mod = await CoreCourse.getModule(module.id, module.course, undefined, true, false, undefined, 'url');

mainFile = mod.contents?.[0];
}
const component = CoreUrlUtils.getThemeImageUrlParam(module.modicon, 'component');
if (component === this.modName) {
return modIcon;
}

const icon = mainFile? AddonModUrl.guessIcon(mainFile.fileurl) : undefined;
let icon: string | undefined;

let image = CoreUrlUtils.getThemeImageUrlParam(module.modicon, 'image');
if (image.startsWith('f/')) {
// Remove prefix, and hyphen + numbered suffix.
image = image.substring(2).replace(/-[0-9]+$/, '');

// In case we get an extension, try to get the type.
image = CoreMimetypeUtils.getExtensionType(image) ?? image;

icon = CoreMimetypeUtils.getFileIconForType(image);
} else {
const mainFile = await this.getModuleMainFile(module);

icon = mainFile? AddonModUrl.guessIcon(mainFile.fileurl) : undefined;
}

// Calculate the icon to use.
return CoreCourse.getModuleIconSrc(module.modname, module.modicon, icon);
}

/**
* Returns if contents are loaded to show link button.
* Get the module main file if not set.
*
* @param module The module object.
* @returns Resolved when done.
* @param module Module.
* @returns Module contents.
*/
protected async hideLinkButton(module: CoreCourseModuleData): Promise<boolean> {
try {
const contents =
await CoreCourse.getModuleContents(module, undefined, undefined, false, false, undefined, this.modName);
protected async getModuleMainFile(module?: CoreCourseModuleData): Promise<CoreCourseModuleContentFile | undefined> {
if (!module) {
return;
}

if (module.contents?.[0]) {
return module.contents[0];
}

return !(contents[0] && contents[0].fileurl);
try {
// Try to get module contents, it's needed to get the URL with parameters.
const contents = await CoreCourse.getModuleContents(
module,
undefined,
undefined,
true,
false,
undefined,
'url',
);

module.contents = contents;
} catch {
// Module contents could not be loaded, most probably device is offline.
return true;
// Fallback in case is not prefetched.
const mod = await CoreCourse.getModule(module.id, module.course, undefined, true, false, undefined, 'url');
module.contents = mod.contents;
}

return module.contents?.[0];
}

/**
Expand All @@ -189,11 +201,13 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple
*/
protected async shouldOpenLink(module: CoreCourseModuleData): Promise<boolean> {
try {
const contents =
await CoreCourse.getModuleContents(module, undefined, undefined, false, false, undefined, this.modName);
const mainFile = await this.getModuleMainFile(module);
if (!mainFile) {
return false;
}

// Check if the URL can be handled by the app. If so, always open it directly.
const canHandle = await CoreContentLinksHelper.canHandleLink(contents[0].fileurl, module.course, undefined, true);
const canHandle = await CoreContentLinksHelper.canHandleLink(mainFile.fileurl, module.course, undefined, true);

if (canHandle) {
// URL handled by the app, open it directly.
Expand All @@ -203,8 +217,8 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple
const url = await CoreUtils.ignoreErrors(AddonModUrl.getUrl(module.course, module.id));
const displayType = AddonModUrl.getFinalDisplayType(url);

return displayType == CoreConstants.RESOURCELIB_DISPLAY_OPEN ||
displayType == CoreConstants.RESOURCELIB_DISPLAY_POPUP;
return displayType === CoreConstants.RESOURCELIB_DISPLAY_OPEN ||
displayType === CoreConstants.RESOURCELIB_DISPLAY_POPUP;
}
} catch {
return false;
Expand Down
26 changes: 8 additions & 18 deletions src/addons/mod/url/services/handlers/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,21 @@ import { Injectable } from '@angular/core';
import { CoreCourseResourcePrefetchHandlerBase } from '@features/course/classes/resource-prefetch-handler';
import { CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/course';
import { makeSingleton } from '@singletons';
import { AddonModUrlProvider } from '../url';
import {
ADDON_MOD_URL_COMPONENT,
ADDON_MOD_URL_MODNAME,
ADDON_MOD_URL_ADDON_NAME,
} from '../../constants';

/**
* Handler to prefetch URLs. URLs cannot be prefetched, but the handler will be used to invalidate some data on course PTR.
*/
@Injectable({ providedIn: 'root' })
export class AddonModUrlPrefetchHandlerService extends CoreCourseResourcePrefetchHandlerBase {

name = 'AddonModUrl';
modName = 'url';
component = AddonModUrlProvider.COMPONENT;

/**
* @inheritdoc
*/
async download(): Promise<void> {
return;
}
name = ADDON_MOD_URL_ADDON_NAME;
modName = ADDON_MOD_URL_MODNAME;
component = ADDON_MOD_URL_COMPONENT;

/**
* @inheritdoc
Expand All @@ -49,12 +46,5 @@ export class AddonModUrlPrefetchHandlerService extends CoreCourseResourcePrefetc
return false; // URLs aren't downloadable.
}

/**
* @inheritdoc
*/
async prefetch(): Promise<void> {
return;
}

}
export const AddonModUrlPrefetchHandler = makeSingleton(AddonModUrlPrefetchHandlerService);
12 changes: 6 additions & 6 deletions src/addons/mod/url/services/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import { CoreUtils } from '@services/utils/utils';
import { CoreCourseLogHelper } from '@features/course/services/log-helper';
import { CoreError } from '@classes/errors/error';
import { CoreSiteWSPreSets } from '@classes/sites/authenticated-site';

const ROOT_CACHE_KEY = 'mmaModUrl:';
import { ADDON_MOD_URL_COMPONENT } from '../constants';

/**
* Service that provides some features for urls.
*/
@Injectable({ providedIn: 'root' })
export class AddonModUrlProvider {

static readonly COMPONENT = 'mmaModUrl';
protected static readonly ROOT_CACHE_KEY = 'mmaModUrl:';
static readonly COMPONENT = ADDON_MOD_URL_COMPONENT;

/**
* Get the final display type for a certain URL. Based on Moodle's url_get_final_display_type.
Expand Down Expand Up @@ -94,7 +94,7 @@ export class AddonModUrlProvider {
* @returns Cache key.
*/
protected getUrlCacheKey(courseId: number): string {
return ROOT_CACHE_KEY + 'url:' + courseId;
return AddonModUrlProvider.ROOT_CACHE_KEY + 'url:' + courseId;
}

/**
Expand All @@ -121,7 +121,7 @@ export class AddonModUrlProvider {
const preSets: CoreSiteWSPreSets = {
cacheKey: this.getUrlCacheKey(courseId),
updateFrequency: CoreSite.FREQUENCY_RARELY,
component: AddonModUrlProvider.COMPONENT,
component: ADDON_MOD_URL_COMPONENT,
...CoreSites.getReadingStrategyPreSets(options.readingStrategy),
};

Expand Down Expand Up @@ -222,7 +222,7 @@ export class AddonModUrlProvider {
return CoreCourseLogHelper.log(
'mod_url_view_url',
params,
AddonModUrlProvider.COMPONENT,
ADDON_MOD_URL_COMPONENT,
id,
siteId,
);
Expand Down
Loading

0 comments on commit 7934e78

Please sign in to comment.