MOBILE-4362 modicon: Properly check if icon should be filtered

main
Pau Ferrer Ocaña 2023-10-25 13:55:17 +02:00
parent 16046c15ec
commit bfaadfbb8c
5 changed files with 76 additions and 43 deletions

View File

@ -22,7 +22,6 @@ import { AddonModLtiHelper } from '../lti-helper';
import { AddonModLtiIndexComponent } from '../../components/index'; import { AddonModLtiIndexComponent } from '../../components/index';
import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler'; import { CoreModuleHandlerBase } from '@features/course/classes/module-base-handler';
import { CoreCourse } from '@features/course/services/course'; import { CoreCourse } from '@features/course/services/course';
import { CoreSites } from '@services/sites';
/** /**
* Handler to support LTI modules. * Handler to support LTI modules.
@ -87,19 +86,6 @@ export class AddonModLtiModuleHandlerService extends CoreModuleHandlerBase imple
return module?.modicon ?? modicon ?? CoreCourse.getModuleIconSrc(this.modName); return module?.modicon ?? modicon ?? CoreCourse.getModuleIconSrc(this.modName);
} }
/**
* @inheritdoc
*/
iconIsShape(module?: CoreCourseModuleData | undefined, modicon?: string | undefined): boolean | undefined {
const iconUrl = module?.modicon ?? modicon;
if (!iconUrl) {
return true;
}
return iconUrl.startsWith(CoreSites.getRequiredCurrentSite().siteUrl);
}
} }
export const AddonModLtiModuleHandler = makeSingleton(AddonModLtiModuleHandlerService); export const AddonModLtiModuleHandler = makeSingleton(AddonModLtiModuleHandlerService);

View File

@ -170,14 +170,5 @@ export class AddonModResourceModuleHandlerService extends CoreModuleHandlerBase
return AddonModResourceIndexComponent; return AddonModResourceIndexComponent;
} }
/**
* @inheritdoc
*/
iconIsShape(module?: CoreCourseModuleData | undefined, modicon?: string | undefined): boolean | undefined {
const iconUrl = module?.modicon ?? modicon;
return !iconUrl?.startsWith('assets/img/files_legacy/') && !iconUrl?.endsWith('.png');
}
} }
export const AddonModResourceModuleHandler = makeSingleton(AddonModResourceModuleHandlerService); export const AddonModResourceModuleHandler = makeSingleton(AddonModResourceModuleHandlerService);

View File

@ -218,15 +218,6 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple
return this.shouldOpenLink(module); return this.shouldOpenLink(module);
} }
/**
* @inheritdoc
*/
iconIsShape(module?: CoreCourseModuleData | undefined, modicon?: string | undefined): boolean | undefined {
const iconUrl = module?.modicon ?? modicon;
return !iconUrl?.startsWith('assets/img/files_legacy/') && !iconUrl?.endsWith('.png');
}
/** /**
* Log module viewed. * Log module viewed.
*/ */

View File

@ -17,6 +17,7 @@ import { Component, ElementRef, Input, OnChanges, OnInit, SimpleChange } from '@
import { CoreCourse } from '@features/course/services/course'; import { CoreCourse } from '@features/course/services/course';
import { CoreCourseModuleDelegate } from '@features/course/services/module-delegate'; import { CoreCourseModuleDelegate } from '@features/course/services/module-delegate';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreUrlUtils } from '@services/utils/url';
const assetsPath = 'assets/img/'; const assetsPath = 'assets/img/';
const fallbackModName = 'external-tool'; const fallbackModName = 'external-tool';
@ -54,12 +55,7 @@ export class CoreModIconComponent implements OnInit, OnChanges {
async ngOnInit(): Promise<void> { async ngOnInit(): Promise<void> {
if (!this.modname && this.modicon) { if (!this.modname && this.modicon) {
// Guess module from the icon url. // Guess module from the icon url.
const matches = this.modicon.match('/theme/image.php/[^/]+/([^/]+)/[-0-9]*/'); this.modname = this.getComponentNameFromIconUrl(this.modicon);
this.modname = (matches && matches[1]) || '';
if (this.modname.startsWith('mod_')) {
this.modname = this.modname.substring(4);
}
} }
this.modNameTranslated = CoreCourse.translateModuleName(this.modname, this.fallbackTranslation); this.modNameTranslated = CoreCourse.translateModuleName(this.modname, this.fallbackTranslation);
@ -105,16 +101,15 @@ export class CoreModIconComponent implements OnInit, OnChanges {
!!this.modname && !!this.modname &&
!!this.componentId && !!this.componentId &&
!this.isLocalUrl && !this.isLocalUrl &&
!this.icon.match('/theme/image.php/[^/]+/' + this.modname + '/[-0-9]*/'); this.getComponentNameFromIconUrl(this.icon) != this.modname;
const iconIsShape = await CoreCourseModuleDelegate.moduleIconIsShape(this.modname, this.icon); this.noFilter = await this.getIconNoFilter();
this.noFilter = iconIsShape === false;
} }
/** /**
* Icon to load on error. * Icon to load on error.
*/ */
loadFallbackIcon(): void { async loadFallbackIcon(): Promise<void> {
this.isLocalUrl = true; this.isLocalUrl = true;
const moduleName = !this.modname || CoreCourse.CORE_MODULES.indexOf(this.modname) < 0 const moduleName = !this.modname || CoreCourse.CORE_MODULES.indexOf(this.modname) < 0
? fallbackModName ? fallbackModName
@ -127,7 +122,75 @@ export class CoreModIconComponent implements OnInit, OnChanges {
} }
this.icon = path + moduleName + '.svg'; this.icon = path + moduleName + '.svg';
this.noFilter = false; this.noFilter = await this.getIconNoFilter();
}
/**
* Returns if the icon does not need to be filtered.
*
* @returns wether the icon does not need to be filtered.
*/
protected async getIconNoFilter(): Promise<boolean> {
// Earlier 4.0, icons were never filtered.
if (this.legacyIcon) {
return true;
}
// No icon or local icon (not legacy), filter it.
if (!this.icon || this.isLocalUrl) {
return false;
}
// If it's an Moodle Theme icon, check if filtericon is set and use it.
if (this.icon && CoreUrlUtils.isThemeImageUrl(this.icon)) {
const iconParams = CoreUrlUtils.extractUrlParams(this.icon);
if (iconParams['filtericon'] === '1') {
return false;
}
// filtericon was introduced in 4.2 and backported to 4.1.3 and 4.0.8.
if (this.modname && !CoreSites.getCurrentSite()?.isVersionGreaterEqualThan(['4.0.8', '4.1.3', '4.2'])) {
// If version is prior to that, check if the url is a module icon and filter it.
if (this.getComponentNameFromIconUrl(this.icon) === this.modname) {
return false;
}
}
}
// External icons, or non monologo, do not filter.
return true;
}
/**
* Guesses the mod name form the url.
*
* @param iconUrl Icon url.
* @returns Guessed modname.
*/
protected getComponentNameFromIconUrl(iconUrl: string): string {
if (!CoreUrlUtils.isThemeImageUrl(this.icon)) {
// Cannot be guessed.
return '';
}
const iconParams = CoreUrlUtils.extractUrlParams(iconUrl);
let component = iconParams['component'];
if (!component) {
const matches = iconUrl.match('/theme/image.php/[^/]+/([^/]+)/[-0-9]*/');
component = (matches && matches[1]) || '';
}
// Some invalid components (others may be added later on).
if (component === 'core' || component === 'theme') {
return '';
}
if (component.startsWith('mod_')) {
component = component.substring(4);
}
return component;
} }
} }

View File

@ -93,6 +93,7 @@ export interface CoreCourseModuleHandler extends CoreDelegateHandler {
* @param module Module to get the icon from. * @param module Module to get the icon from.
* @param modicon The mod icon string. * @param modicon The mod icon string.
* @returns Whether the icon should be treated as a shape. * @returns Whether the icon should be treated as a shape.
* @deprecated since 4.3. Now it uses platform information. This function is not used anymore.
*/ */
iconIsShape?(module?: CoreCourseModuleData, modicon?: string): Promise<boolean | undefined> | boolean | undefined; iconIsShape?(module?: CoreCourseModuleData, modicon?: string): Promise<boolean | undefined> | boolean | undefined;
@ -415,6 +416,7 @@ export class CoreCourseModuleDelegateService extends CoreDelegate<CoreCourseModu
* @param modicon The mod icon string. * @param modicon The mod icon string.
* @param module The module to use. * @param module The module to use.
* @returns Whether the icon should be treated as a shape. * @returns Whether the icon should be treated as a shape.
* @deprecated since 4.3. Now it uses platform information. This function is not used anymore.
*/ */
async moduleIconIsShape(modname: string, modicon?: string, module?: CoreCourseModuleData): Promise<boolean | undefined> { async moduleIconIsShape(modname: string, modicon?: string, module?: CoreCourseModuleData): Promise<boolean | undefined> {
return await this.executeFunctionOnEnabled<Promise<boolean>>(modname, 'iconIsShape', [module, modicon]); return await this.executeFunctionOnEnabled<Promise<boolean>>(modname, 'iconIsShape', [module, modicon]);