Apply prettier formatting

This commit is contained in:
Michael Weimann 2022-12-12 12:24:14 +01:00
parent 1cac306093
commit 526645c791
No known key found for this signature in database
GPG key ID: 53F535A266BB9584
1576 changed files with 65385 additions and 62478 deletions

View file

@ -44,9 +44,9 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
}
forEntity(entityId: string): string {
if (entityId[0] === '!' || entityId[0] === '#') {
if (entityId[0] === "!" || entityId[0] === "#") {
return this.forRoom(entityId);
} else if (entityId[0] === '@') {
} else if (entityId[0] === "@") {
return this.forUser(entityId);
} else throw new Error("Unrecognized entity");
}
@ -57,8 +57,8 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
}
encodeServerCandidates(candidates?: string[]) {
if (!candidates || candidates.length === 0) return '';
return `?via=${candidates.map(c => encodeURIComponent(c)).join("&via=")}`;
if (!candidates || candidates.length === 0) return "";
return `?via=${candidates.map((c) => encodeURIComponent(c)).join("&via=")}`;
}
// Heavily inspired by/borrowed from the matrix-bot-sdk (with permission):
@ -82,7 +82,8 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
static parseAppRoute(route: string): PermalinkParts {
const parts = route.split("/");
if (parts.length < 2) { // we're expecting an entity and an ID of some kind at least
if (parts.length < 2) {
// we're expecting an entity and an ID of some kind at least
throw new Error("URL is missing parts");
}
@ -93,13 +94,13 @@ export default class ElementPermalinkConstructor extends PermalinkConstructor {
const entityType = parts[0];
const entity = parts[1];
if (entityType === 'user') {
if (entityType === "user") {
// Probably a user, no further parsing needed.
return PermalinkParts.forUser(entity);
} else if (entityType === 'room') {
} else if (entityType === "room") {
// Rejoin the rest because v3 events can have slashes (annoyingly)
const eventId = parts.length > 2 ? parts.slice(2).join('/') : "";
const via = query.split(/&?via=/).filter(p => !!p);
const eventId = parts.length > 2 ? parts.slice(2).join("/") : "";
const via = query.split(/&?via=/).filter((p) => !!p);
return PermalinkParts.forEvent(entity, eventId, via);
} else {
throw new Error("Unknown entity type in permalink");

View file

@ -39,8 +39,10 @@ export default class MatrixSchemePermalinkConstructor extends PermalinkConstruct
}
forEvent(roomId: string, eventId: string, serverCandidates: string[]): string {
return `matrix:${this.encodeEntity(roomId)}` +
`/${this.encodeEntity(eventId)}${this.encodeServerCandidates(serverCandidates)}`;
return (
`matrix:${this.encodeEntity(roomId)}` +
`/${this.encodeEntity(eventId)}${this.encodeServerCandidates(serverCandidates)}`
);
}
forRoom(roomIdOrAlias: string, serverCandidates: string[]): string {
@ -61,8 +63,8 @@ export default class MatrixSchemePermalinkConstructor extends PermalinkConstruct
}
encodeServerCandidates(candidates: string[]) {
if (!candidates || candidates.length === 0) return '';
return `?via=${candidates.map(c => encodeURIComponent(c)).join("&via=")}`;
if (!candidates || candidates.length === 0) return "";
return `?via=${candidates.map((c) => encodeURIComponent(c)).join("&via=")}`;
}
parsePermalink(fullUrl: string): PermalinkParts {
@ -70,26 +72,28 @@ export default class MatrixSchemePermalinkConstructor extends PermalinkConstruct
throw new Error("Does not appear to be a permalink");
}
const parts = fullUrl.substring("matrix:".length).split('/');
const parts = fullUrl.substring("matrix:".length).split("/");
const identifier = parts[0];
const entityNoSigil = parts[1];
if (identifier === 'u') {
if (identifier === "u") {
// Probably a user, no further parsing needed.
return PermalinkParts.forUser(`@${entityNoSigil}`);
} else if (identifier === 'r' || identifier === 'roomid') {
const sigil = identifier === 'r' ? '#' : '!';
} else if (identifier === "r" || identifier === "roomid") {
const sigil = identifier === "r" ? "#" : "!";
if (parts.length === 2) { // room without event permalink
if (parts.length === 2) {
// room without event permalink
const [roomId, query = ""] = entityNoSigil.split("?");
const via = query.split(/&?via=/g).filter(p => !!p);
const via = query.split(/&?via=/g).filter((p) => !!p);
return PermalinkParts.forRoom(`${sigil}${roomId}`, via);
}
if (parts[2] === 'e') { // event permalink
const eventIdAndQuery = parts.length > 3 ? parts.slice(3).join('/') : "";
if (parts[2] === "e") {
// event permalink
const eventIdAndQuery = parts.length > 3 ? parts.slice(3).join("/") : "";
const [eventId, query = ""] = eventIdAndQuery.split("?");
const via = query.split(/&?via=/g).filter(p => !!p);
const via = query.split(/&?via=/g).filter((p) => !!p);
return PermalinkParts.forEvent(`${sigil}${entityNoSigil}`, `$${eventId}`, via);
}

View file

@ -48,8 +48,8 @@ export default class MatrixToPermalinkConstructor extends PermalinkConstructor {
}
encodeServerCandidates(candidates: string[]) {
if (!candidates || candidates.length === 0) return '';
return `?via=${candidates.map(c => encodeURIComponent(c)).join("&via=")}`;
if (!candidates || candidates.length === 0) return "";
return `?via=${candidates.map((c) => encodeURIComponent(c)).join("&via=")}`;
}
// Heavily inspired by/borrowed from the matrix-bot-sdk (with permission):
@ -62,20 +62,21 @@ export default class MatrixToPermalinkConstructor extends PermalinkConstructor {
const parts = fullUrl.substring(`${baseUrl}/#/`.length).split("/");
const entity = parts[0];
if (entity[0] === '@') {
if (entity[0] === "@") {
// Probably a user, no further parsing needed.
return PermalinkParts.forUser(entity);
} else if (entity[0] === '#' || entity[0] === '!') {
if (parts.length === 1) { // room without event permalink
const [roomId, query=""] = entity.split("?");
const via = query.split(/&?via=/g).filter(p => !!p);
} else if (entity[0] === "#" || entity[0] === "!") {
if (parts.length === 1) {
// room without event permalink
const [roomId, query = ""] = entity.split("?");
const via = query.split(/&?via=/g).filter((p) => !!p);
return PermalinkParts.forRoom(roomId, via);
}
// rejoin the rest because v3 events can have slashes (annoyingly)
const eventIdAndQuery = parts.length > 1 ? parts.slice(1).join('/') : "";
const [eventId, query=""] = eventIdAndQuery.split("?");
const via = query.split(/&?via=/g).filter(p => !!p);
const eventIdAndQuery = parts.length > 1 ? parts.slice(1).join("/") : "";
const [eventId, query = ""] = eventIdAndQuery.split("?");
const via = query.split(/&?via=/g).filter((p) => !!p);
return PermalinkParts.forEvent(entity, eventId, via);
} else {

View file

@ -191,13 +191,18 @@ export class RoomPermalinkCreator {
const serverName = getServerName(userId);
const domain = getHostnameFromMatrixServerName(serverName) ?? serverName;
return !isHostnameIpAddress(domain) &&
return (
!isHostnameIpAddress(domain) &&
!isHostInRegex(domain, this.bannedHostsRegexps) &&
isHostInRegex(domain, this.allowedHostsRegexps);
isHostInRegex(domain, this.allowedHostsRegexps)
);
});
const maxEntry = allowedEntries.reduce((max, entry) => {
return (entry[1] > max[1]) ? entry : max;
}, [null, 0]);
const maxEntry = allowedEntries.reduce(
(max, entry) => {
return entry[1] > max[1] ? entry : max;
},
[null, 0],
);
const [userId, powerLevel] = maxEntry;
// object wasn't empty, and max entry wasn't a demotion from the default
if (userId !== null && powerLevel >= 50) {
@ -219,11 +224,11 @@ export class RoomPermalinkCreator {
const getRegex = (hostname) => new RegExp("^" + utils.globToRegexp(hostname, false) + "$");
const denied = aclEvent.getContent().deny || [];
denied.forEach(h => bannedHostsRegexps.push(getRegex(h)));
denied.forEach((h) => bannedHostsRegexps.push(getRegex(h)));
const allowed = aclEvent.getContent().allow || [];
allowedHostsRegexps = []; // we don't want to use the default rule here
allowed.forEach(h => allowedHostsRegexps.push(getRegex(h)));
allowed.forEach((h) => allowedHostsRegexps.push(getRegex(h)));
}
}
this.bannedHostsRegexps = bannedHostsRegexps;
@ -248,8 +253,9 @@ export class RoomPermalinkCreator {
candidates.add(getServerName(this.highestPlUserId));
}
const serversByPopulation = Object.keys(this.populationMap)
.sort((a, b) => this.populationMap[b] - this.populationMap[a]);
const serversByPopulation = Object.keys(this.populationMap).sort(
(a, b) => this.populationMap[b] - this.populationMap[a],
);
for (let i = 0; i < serversByPopulation.length && candidates.size < MAX_SERVER_CANDIDATES; i++) {
const serverName = serversByPopulation[i];
@ -283,7 +289,7 @@ export function makeRoomPermalink(roomId: string): string {
// If the roomId isn't actually a room ID, don't try to list the servers.
// Aliases are already routable, and don't need extra information.
if (roomId[0] !== '!') return getPermalinkConstructor().forRoom(roomId, []);
if (roomId[0] !== "!") return getPermalinkConstructor().forRoom(roomId, []);
const client = MatrixClientPeg.get();
const room = client.getRoom(roomId);
@ -313,15 +319,15 @@ export function tryTransformEntityToPermalink(entity: string): string {
if (!entity) return null;
// Check to see if it is a bare entity for starters
if (entity[0] === '#' || entity[0] === '!') return makeRoomPermalink(entity);
if (entity[0] === '@') return makeUserPermalink(entity);
if (entity[0] === "#" || entity[0] === "!") return makeRoomPermalink(entity);
if (entity[0] === "@") return makeUserPermalink(entity);
if (entity.slice(0, 7) === "matrix:") {
try {
const permalinkParts = parsePermalink(entity);
if (permalinkParts) {
if (permalinkParts.roomIdOrAlias) {
const eventIdPart = permalinkParts.eventId ? `/${permalinkParts.eventId}` : '';
const eventIdPart = permalinkParts.eventId ? `/${permalinkParts.eventId}` : "";
let pl = matrixtoBaseUrl + `/#/${permalinkParts.roomIdOrAlias}${eventIdPart}`;
if (permalinkParts.viaServers.length > 0) {
pl += new MatrixToPermalinkConstructor().encodeServerCandidates(permalinkParts.viaServers);
@ -344,7 +350,8 @@ export function tryTransformEntityToPermalink(entity: string): string {
* @returns {string} The transformed permalink or original URL if unable.
*/
export function tryTransformPermalinkToLocalHref(permalink: string): string {
if (!permalink.startsWith("http:") &&
if (
!permalink.startsWith("http:") &&
!permalink.startsWith("https:") &&
!permalink.startsWith("matrix:") &&
!permalink.startsWith("vector:") // Element Desktop
@ -367,7 +374,7 @@ export function tryTransformPermalinkToLocalHref(permalink: string): string {
const permalinkParts = parsePermalink(permalink);
if (permalinkParts) {
if (permalinkParts.roomIdOrAlias) {
const eventIdPart = permalinkParts.eventId ? `/${permalinkParts.eventId}` : '';
const eventIdPart = permalinkParts.eventId ? `/${permalinkParts.eventId}` : "";
permalink = `#/room/${permalinkParts.roomIdOrAlias}${eventIdPart}`;
if (permalinkParts.viaServers.length > 0) {
permalink += new MatrixToPermalinkConstructor().encodeServerCandidates(permalinkParts.viaServers);
@ -393,7 +400,7 @@ export function getPrimaryPermalinkEntity(permalink: string): string {
if (m) {
// A bit of a hack, but it gets the job done
const handler = new ElementPermalinkConstructor("http://localhost");
const entityInfo = m[1].split('#').slice(1).join('#');
const entityInfo = m[1].split("#").slice(1).join("#");
permalinkParts = handler.parsePermalink(`http://localhost/#${entityInfo}`);
}
}
@ -452,7 +459,7 @@ function isHostInRegex(hostname: string, regexps: RegExp[]): boolean {
if (!hostname) return true; // assumed
if (regexps.length > 0 && !regexps[0].test) throw new Error(regexps[0].toString());
return regexps.some(h => h.test(hostname));
return regexps.some((h) => h.test(hostname));
}
function isHostnameIpAddress(hostname: string): boolean {

View file

@ -23,7 +23,8 @@ import { tryTransformPermalinkToLocalHref } from "./Permalinks";
*/
export function navigateToPermalink(uri: string): void {
const localUri = tryTransformPermalinkToLocalHref(uri);
if (!localUri || localUri === uri) { // parse failure can lead to an unmodified URL
if (!localUri || localUri === uri) {
// parse failure can lead to an unmodified URL
throw new Error("Failed to transform URI");
}
window.location.hash = localUri; // it'll just be a fragment