Lint MXC APIs to centralise access

This adds linting rules to ensure that MXC APIs are only accessed via the
`Media` helper so they can be customised easily when desired.

Fixes https://github.com/vector-im/element-web/issues/16933
This commit is contained in:
J. Ryan Stinnett 2021-06-30 14:19:39 +01:00
parent 6b8c38af3a
commit a9f35e8c69
2 changed files with 13 additions and 2 deletions

View file

@ -54,7 +54,11 @@ module.exports = {
"error", "error",
...buildRestrictedPropertiesOptions( ...buildRestrictedPropertiesOptions(
["window.innerHeight", "window.innerWidth", "window.visualViewport"], ["window.innerHeight", "window.innerWidth", "window.visualViewport"],
"Use UIStore to access window dimensions instead", "Use UIStore to access window dimensions instead.",
),
...buildRestrictedPropertiesOptions(
["*.mxcUrlToHttp", "*.getHttpUriForMxc"],
"Use Media helper instead to centralise access for customisation.",
), ),
], ],
}, },
@ -63,7 +67,10 @@ module.exports = {
function buildRestrictedPropertiesOptions(properties, message) { function buildRestrictedPropertiesOptions(properties, message) {
return properties.map(prop => { return properties.map(prop => {
const [object, property] = prop.split("."); let [object, property] = prop.split(".");
if (object === "*") {
object = undefined;
}
return { return {
object, object,
property, property,

View file

@ -75,6 +75,7 @@ export class Media {
* The HTTP URL for the source media. * The HTTP URL for the source media.
*/ */
public get srcHttp(): string { public get srcHttp(): string {
// eslint-disable-next-line no-restricted-properties
return this.client.mxcUrlToHttp(this.srcMxc); return this.client.mxcUrlToHttp(this.srcMxc);
} }
@ -84,6 +85,7 @@ export class Media {
*/ */
public get thumbnailHttp(): string | undefined | null { public get thumbnailHttp(): string | undefined | null {
if (!this.hasThumbnail) return null; if (!this.hasThumbnail) return null;
// eslint-disable-next-line no-restricted-properties
return this.client.mxcUrlToHttp(this.thumbnailMxc); return this.client.mxcUrlToHttp(this.thumbnailMxc);
} }
@ -100,6 +102,7 @@ export class Media {
// scale using the device pixel ratio to keep images clear // scale using the device pixel ratio to keep images clear
width = Math.floor(width * window.devicePixelRatio); width = Math.floor(width * window.devicePixelRatio);
height = Math.floor(height * window.devicePixelRatio); height = Math.floor(height * window.devicePixelRatio);
// eslint-disable-next-line no-restricted-properties
return this.client.mxcUrlToHttp(this.thumbnailMxc, width, height, mode); return this.client.mxcUrlToHttp(this.thumbnailMxc, width, height, mode);
} }
@ -114,6 +117,7 @@ export class Media {
// scale using the device pixel ratio to keep images clear // scale using the device pixel ratio to keep images clear
width = Math.floor(width * window.devicePixelRatio); width = Math.floor(width * window.devicePixelRatio);
height = Math.floor(height * window.devicePixelRatio); height = Math.floor(height * window.devicePixelRatio);
// eslint-disable-next-line no-restricted-properties
return this.client.mxcUrlToHttp(this.srcMxc, width, height, mode); return this.client.mxcUrlToHttp(this.srcMxc, width, height, mode);
} }