Avoid using deprecated exports, fields, and duplicate code (#12555)

This commit is contained in:
Michael Telatynski
2024-05-28 08:41:20 +01:00
committed by GitHub
parent 1973197eb6
commit 148a360598
33 changed files with 258 additions and 108 deletions

View File

@@ -78,15 +78,6 @@ export interface IMatrixClientPeg {
*/
opts: IStartClientOpts;
/**
* Return the server name of the user's homeserver
* Throws an error if unable to deduce the homeserver name
* (e.g. if the user is not logged in)
*
* @returns {string} The homeserver name, if present.
*/
getHomeserverName(): string;
/**
* Get the current MatrixClient, if any
*/
@@ -384,14 +375,6 @@ class MatrixClientPegClass implements IMatrixClientPeg {
logger.log(`MatrixClientPeg: MatrixClient started`);
}
public getHomeserverName(): string {
const matches = /^@[^:]+:(.+)$/.exec(this.safeGet().getSafeUserId());
if (matches === null || matches.length < 1) {
throw new Error("Failed to derive homeserver name from user ID!");
}
return matches[1];
}
private namesToRoomName(names: string[], count: number): string | undefined {
const countWithoutMe = count - 1;
if (!names.length) {

View File

@@ -14,13 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import {
DeviceVerificationStatus,
ICryptoCallbacks,
MatrixClient,
encodeBase64,
SecretStorage,
} from "matrix-js-sdk/src/matrix";
import { Crypto, ICryptoCallbacks, MatrixClient, encodeBase64, SecretStorage } from "matrix-js-sdk/src/matrix";
import { deriveKey } from "matrix-js-sdk/src/crypto/key_passphrase";
import { decodeRecoveryKey } from "matrix-js-sdk/src/crypto/recoverykey";
import { logger } from "matrix-js-sdk/src/logger";
@@ -249,7 +243,7 @@ async function onSecretRequested(
deviceId: string,
requestId: string,
name: string,
deviceTrust: DeviceVerificationStatus,
deviceTrust: Crypto.DeviceVerificationStatus,
): Promise<string | undefined> {
logger.log("onSecretRequested", userId, deviceId, requestId, name, deviceTrust);
const client = MatrixClientPeg.safeGet();

View File

@@ -436,7 +436,7 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {
</summary>
<LabelledToggleSwitch
label={_t("create_room|unfederated", {
serverName: MatrixClientPeg.getHomeserverName(),
serverName: MatrixClientPeg.safeGet().getDomain(),
})}
onChange={this.onNoFederateChange}
value={this.state.noFederate}

View File

@@ -95,7 +95,7 @@ export default class ServerOfflineDialog extends React.PureComponent<IProps> {
timeline = [<div key={1}>{_t("server_offline|empty_timeline")}</div>];
}
const serverName = MatrixClientPeg.getHomeserverName();
const serverName = MatrixClientPeg.safeGet().getDomain();
return (
<BaseDialog
title={_t("server_offline|title")}

View File

@@ -38,7 +38,7 @@ export async function getServerVersionFromFederationApi(client: MatrixClient): P
let baseUrl = client.getHomeserverUrl();
try {
const hsName = MatrixClientPeg.getHomeserverName();
const hsName = MatrixClientPeg.safeGet().getDomain();
// We don't use the js-sdk Autodiscovery module here as it only support client well-known, not server ones.
const response = await fetch(`https://${hsName}/.well-known/matrix/server`);
const json = await response.json();

View File

@@ -118,7 +118,7 @@ function useServers(): ServerList {
SettingLevel.ACCOUNT,
);
const homeServer = MatrixClientPeg.getHomeserverName();
const homeServer = MatrixClientPeg.safeGet().getDomain()!;
const configServers = new Set<string>(SdkConfig.getObject("room_directory")?.get("servers") ?? []);
removeAll(configServers, homeServer);
// configured servers take preference over user-defined ones, if one occurs in both ignore the latter one.

View File

@@ -43,7 +43,6 @@ import DMRoomMap from "../../../utils/DMRoomMap";
import AccessibleButton, { ButtonEvent } from "../elements/AccessibleButton";
import SdkConfig from "../../../SdkConfig";
import MultiInviter from "../../../utils/MultiInviter";
import { MatrixClientPeg } from "../../../MatrixClientPeg";
import E2EIcon from "../rooms/E2EIcon";
import { useTypedEventEmitter } from "../../../hooks/useEventEmitter";
import { textualPowerLevel } from "../../../Roles";
@@ -1413,8 +1412,7 @@ const BasicUserInfo: React.FC<{
// We don't need a perfect check here, just something to pass as "probably not our homeserver". If
// someone does figure out how to bypass this check the worst that happens is an error.
// FIXME this should be using cli instead of MatrixClientPeg.matrixClient
if (isSynapseAdmin && member.userId.endsWith(`:${MatrixClientPeg.getHomeserverName()}`)) {
if (isSynapseAdmin && member.userId.endsWith(`:${cli.getDomain()}`)) {
synapseDeactivateButton = (
<AccessibleButton
kind="link"

View File

@@ -90,7 +90,7 @@ export const usePublicRoomDirectory = (): {
async ({ limit = 20, query, roomTypes }: IPublicRoomsOpts): Promise<boolean> => {
const opts: IRoomDirectoryOptions = { limit };
if (config?.roomServer != MatrixClientPeg.getHomeserverName()) {
if (config?.roomServer != MatrixClientPeg.safeGet().getDomain()) {
opts.server = config?.roomServer;
}
@@ -139,7 +139,7 @@ export const usePublicRoomDirectory = (): {
return;
}
const myHomeserver = MatrixClientPeg.getHomeserverName();
const myHomeserver = MatrixClientPeg.safeGet().getDomain()!;
const lsRoomServer = localStorage.getItem(LAST_SERVER_KEY);
const lsInstanceId: string | undefined = localStorage.getItem(LAST_INSTANCE_KEY) ?? undefined;

View File

@@ -17,7 +17,7 @@ limitations under the License.
*/
import { logger } from "matrix-js-sdk/src/logger";
import { Method, MatrixClient, CryptoApi } from "matrix-js-sdk/src/matrix";
import { Method, MatrixClient, Crypto } from "matrix-js-sdk/src/matrix";
import type * as Pako from "pako";
import { MatrixClientPeg } from "../MatrixClientPeg";
@@ -177,7 +177,7 @@ async function collectSynapseSpecific(client: MatrixClient, body: FormData): Pro
/**
* Collects crypto related information.
*/
async function collectCryptoInfo(cryptoApi: CryptoApi, body: FormData): Promise<void> {
async function collectCryptoInfo(cryptoApi: Crypto.CryptoApi, body: FormData): Promise<void> {
body.append("crypto_version", cryptoApi.getVersion());
const ownDeviceKeys = await cryptoApi.getOwnDeviceKeys();
@@ -206,7 +206,7 @@ async function collectCryptoInfo(cryptoApi: CryptoApi, body: FormData): Promise<
/**
* Collects information about secret storage and backup.
*/
async function collectRecoveryInfo(client: MatrixClient, cryptoApi: CryptoApi, body: FormData): Promise<void> {
async function collectRecoveryInfo(client: MatrixClient, cryptoApi: Crypto.CryptoApi, body: FormData): Promise<void> {
const secretStorage = client.secretStorage;
body.append("secret_storage_ready", String(await cryptoApi.isSecretStorageReady()));
body.append("secret_storage_key_in_account", String(await secretStorage.hasKey()));

View File

@@ -68,7 +68,7 @@ const mapAutoDiscoveryErrorTranslation = (err: AutoDiscoveryError): TranslationK
return _td("auth|autodiscovery_no_well_known");
case AutoDiscoveryError.InvalidJson:
return _td("auth|autodiscovery_invalid_json");
case AutoDiscoveryError.HomeserverTooOld:
case AutoDiscoveryError.UnsupportedHomeserverSpecVersion:
return _td("auth|autodiscovery_hs_incompatible");
}
};

View File

@@ -20,7 +20,6 @@ import * as zxcvbnEnPackage from "@zxcvbn-ts/language-en";
import { MatrixClient } from "matrix-js-sdk/src/matrix";
import { _t } from "../languageHandler";
import { MatrixClientPeg } from "../MatrixClientPeg";
import SdkConfig from "../SdkConfig";
zxcvbnOptions.setOptions({
@@ -96,13 +95,13 @@ export function scorePassword(
if (matrixClient) {
inputs.push(matrixClient.getUserIdLocalpart()!);
}
try {
const domain = MatrixClientPeg.getHomeserverName();
inputs.push(domain);
} catch {
// This is fine
try {
const domain = matrixClient.getDomain()!;
inputs.push(domain);
} catch {
// This is fine
}
}
zxcvbnOptions.setTranslations(getTranslations());

View File

@@ -15,7 +15,7 @@ limitations under the License.
*/
import { logger } from "matrix-js-sdk/src/logger";
import { CryptoApi } from "matrix-js-sdk/src/matrix";
import { Crypto } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from "../../MatrixClientPeg";
@@ -29,7 +29,7 @@ import { MatrixClientPeg } from "../../MatrixClientPeg";
*
* Dehydration can currently only be enabled by setting a flag in the .well-known file.
*/
async function deviceDehydrationEnabled(crypto: CryptoApi | undefined): Promise<boolean> {
async function deviceDehydrationEnabled(crypto: Crypto.CryptoApi | undefined): Promise<boolean> {
if (!crypto) {
return false;
}

View File

@@ -46,7 +46,6 @@ export async function createDmLocalRoom(client: MatrixClient, targets: Member[])
room_version: KNOWN_SAFE_ROOM_VERSION,
},
state_key: "",
user_id: userId,
sender: userId,
room_id: localRoom.roomId,
origin_server_ts: Date.now(),
@@ -62,7 +61,6 @@ export async function createDmLocalRoom(client: MatrixClient, targets: Member[])
content: {
algorithm: MEGOLM_ALGORITHM,
},
user_id: userId,
sender: userId,
state_key: "",
room_id: localRoom.roomId,
@@ -80,7 +78,6 @@ export async function createDmLocalRoom(client: MatrixClient, targets: Member[])
membership: KnownMembership.Join,
},
state_key: userId,
user_id: userId,
sender: userId,
room_id: localRoom.roomId,
}),