Eliminate the use of MatrixClientPeg in utils (#10910)

This commit is contained in:
Michael Telatynski
2023-05-23 16:24:12 +01:00
committed by GitHub
parent a0c2676c38
commit 30429df948
108 changed files with 409 additions and 325 deletions

View File

@@ -20,8 +20,8 @@ import { Room } from "matrix-js-sdk/src/models/room";
import { logger } from "matrix-js-sdk/src/logger";
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
import { EventType } from "matrix-js-sdk/src/@types/event";
import { MatrixClient } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from "../../MatrixClientPeg";
import MatrixToPermalinkConstructor, {
baseUrl as matrixtoBaseUrl,
baseUrlPattern as matrixToBaseUrlPattern,
@@ -284,7 +284,7 @@ export function makeUserPermalink(userId: string): string {
return getPermalinkConstructor().forUser(userId);
}
export function makeRoomPermalink(roomId: string): string {
export function makeRoomPermalink(matrixClient: MatrixClient, roomId: string): string {
if (!roomId) {
throw new Error("can't permalink a falsy roomId");
}
@@ -293,8 +293,7 @@ export function makeRoomPermalink(roomId: string): string {
// Aliases are already routable, and don't need extra information.
if (roomId[0] !== "!") return getPermalinkConstructor().forRoom(roomId, []);
const client = MatrixClientPeg.get();
const room = client.getRoom(roomId);
const room = matrixClient.getRoom(roomId);
if (!room) {
return getPermalinkConstructor().forRoom(roomId, []);
}
@@ -317,11 +316,11 @@ export function isPermalinkHost(host: string): boolean {
* @param {string} entity The entity to transform.
* @returns {string|null} The transformed permalink or null if unable.
*/
export function tryTransformEntityToPermalink(entity: string): string | null {
export function tryTransformEntityToPermalink(matrixClient: MatrixClient, entity: string): string | null {
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] === "#" || entity[0] === "!") return makeRoomPermalink(matrixClient, entity);
if (entity[0] === "@") return makeUserPermalink(entity);
if (entity.slice(0, 7) === "matrix:") {