From fdf8d8e59c4e151d46be65e75529b648cff632bc Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 24 May 2024 11:23:06 +0100 Subject: [PATCH 1/4] Remove backwards compatibility exports Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SecurityManager.ts | 10 ++-------- src/rageshake/submit-rageshake.ts | 6 +++--- src/utils/AutoDiscoveryUtils.tsx | 2 +- src/utils/device/dehydration.ts | 4 ++-- test/DeviceListener-test.ts | 12 ++---------- test/PosthogAnalytics-test.ts | 3 ++- .../security/CreateSecretStorageDialog-test.tsx | 4 ++-- .../dialogs/security/ExportE2eKeysDialog-test.tsx | 4 ++-- .../dialogs/security/ImportE2eKeysDialog-test.tsx | 4 ++-- .../components/views/right_panel/UserInfo-test.tsx | 14 +++----------- test/components/views/rooms/EventTile-test.tsx | 3 +-- .../settings/tabs/user/SessionManagerTab-test.tsx | 4 +--- test/createRoom-test.ts | 3 ++- test/stores/SetupEncryptionStore-test.ts | 3 ++- test/toasts/UnverifiedSessionToast-test.tsx | 3 ++- test/utils/AutoDiscoveryUtils-test.tsx | 2 +- 16 files changed, 30 insertions(+), 51 deletions(-) diff --git a/src/SecurityManager.ts b/src/SecurityManager.ts index 6d43d83f617..c2254d3dfe0 100644 --- a/src/SecurityManager.ts +++ b/src/SecurityManager.ts @@ -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 { logger.log("onSecretRequested", userId, deviceId, requestId, name, deviceTrust); const client = MatrixClientPeg.safeGet(); diff --git a/src/rageshake/submit-rageshake.ts b/src/rageshake/submit-rageshake.ts index 5f2a3990ad2..92c2aa3e339 100644 --- a/src/rageshake/submit-rageshake.ts +++ b/src/rageshake/submit-rageshake.ts @@ -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 { +async function collectCryptoInfo(cryptoApi: Crypto.CryptoApi, body: FormData): Promise { 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 { +async function collectRecoveryInfo(client: MatrixClient, cryptoApi: Crypto.CryptoApi, body: FormData): Promise { const secretStorage = client.secretStorage; body.append("secret_storage_ready", String(await cryptoApi.isSecretStorageReady())); body.append("secret_storage_key_in_account", String(await secretStorage.hasKey())); diff --git a/src/utils/AutoDiscoveryUtils.tsx b/src/utils/AutoDiscoveryUtils.tsx index c1f2a7a7d08..fb25870c38f 100644 --- a/src/utils/AutoDiscoveryUtils.tsx +++ b/src/utils/AutoDiscoveryUtils.tsx @@ -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"); } }; diff --git a/src/utils/device/dehydration.ts b/src/utils/device/dehydration.ts index 83297f42a4e..38b7019fe98 100644 --- a/src/utils/device/dehydration.ts +++ b/src/utils/device/dehydration.ts @@ -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 { +async function deviceDehydrationEnabled(crypto: Crypto.CryptoApi | undefined): Promise { if (!crypto) { return false; } diff --git a/test/DeviceListener-test.ts b/test/DeviceListener-test.ts index 7f447d36822..6c1c059cdd6 100644 --- a/test/DeviceListener-test.ts +++ b/test/DeviceListener-test.ts @@ -15,20 +15,12 @@ limitations under the License. */ import { Mocked, mocked } from "jest-mock"; -import { - MatrixEvent, - Room, - MatrixClient, - DeviceVerificationStatus, - CryptoApi, - Device, - ClientStoppedError, -} from "matrix-js-sdk/src/matrix"; +import { MatrixEvent, Room, MatrixClient, Device, ClientStoppedError } from "matrix-js-sdk/src/matrix"; import { logger } from "matrix-js-sdk/src/logger"; import { CryptoEvent } from "matrix-js-sdk/src/crypto"; import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup"; import { CryptoSessionStateChange } from "@matrix-org/analytics-events/types/typescript/CryptoSessionStateChange"; -import { CrossSigningStatus, KeyBackupInfo } from "matrix-js-sdk/src/crypto-api"; +import { CrossSigningStatus, CryptoApi, DeviceVerificationStatus, KeyBackupInfo } from "matrix-js-sdk/src/crypto-api"; import DeviceListener from "../src/DeviceListener"; import { MatrixClientPeg } from "../src/MatrixClientPeg"; diff --git a/test/PosthogAnalytics-test.ts b/test/PosthogAnalytics-test.ts index c131e536d1f..31822e1c0d1 100644 --- a/test/PosthogAnalytics-test.ts +++ b/test/PosthogAnalytics-test.ts @@ -16,7 +16,8 @@ limitations under the License. import { mocked } from "jest-mock"; import { PostHog } from "posthog-js"; -import { CryptoApi, MatrixClient } from "matrix-js-sdk/src/matrix"; +import { MatrixClient } from "matrix-js-sdk/src/matrix"; +import { CryptoApi } from "matrix-js-sdk/src/crypto-api"; import { Anonymity, getRedactedCurrentLocation, IPosthogEvent, PosthogAnalytics } from "../src/PosthogAnalytics"; import SdkConfig from "../src/SdkConfig"; diff --git a/test/components/views/dialogs/security/CreateSecretStorageDialog-test.tsx b/test/components/views/dialogs/security/CreateSecretStorageDialog-test.tsx index 1412074ed9d..06b13f1df7a 100644 --- a/test/components/views/dialogs/security/CreateSecretStorageDialog-test.tsx +++ b/test/components/views/dialogs/security/CreateSecretStorageDialog-test.tsx @@ -18,7 +18,7 @@ import { render, RenderResult, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import React from "react"; import { mocked, MockedObject } from "jest-mock"; -import { CryptoApi, MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix"; +import { Crypto, MatrixClient, MatrixError } from "matrix-js-sdk/src/matrix"; import { defer, IDeferred, sleep } from "matrix-js-sdk/src/utils"; import { BackupTrustInfo, KeyBackupInfo } from "matrix-js-sdk/src/crypto-api"; @@ -35,7 +35,7 @@ import RestoreKeyBackupDialog from "../../../../../src/components/views/dialogs/ describe("CreateSecretStorageDialog", () => { let mockClient: MockedObject; - let mockCrypto: MockedObject; + let mockCrypto: MockedObject; beforeEach(() => { mockClient = getMockClientWithEventEmitter({ diff --git a/test/components/views/dialogs/security/ExportE2eKeysDialog-test.tsx b/test/components/views/dialogs/security/ExportE2eKeysDialog-test.tsx index 0436fb2bf25..c4a5ef1ee1b 100644 --- a/test/components/views/dialogs/security/ExportE2eKeysDialog-test.tsx +++ b/test/components/views/dialogs/security/ExportE2eKeysDialog-test.tsx @@ -17,7 +17,7 @@ limitations under the License. import React from "react"; import { screen, fireEvent, render, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { CryptoApi, IMegolmSessionData } from "matrix-js-sdk/src/matrix"; +import { Crypto, IMegolmSessionData } from "matrix-js-sdk/src/matrix"; import * as MegolmExportEncryption from "../../../../../src/utils/MegolmExportEncryption"; import ExportE2eKeysDialog from "../../../../../src/async-components/views/dialogs/security/ExportE2eKeysDialog"; @@ -70,7 +70,7 @@ describe("ExportE2eKeysDialog", () => { cli.getCrypto = () => { return { exportRoomKeysAsJson, - } as unknown as CryptoApi; + } as unknown as Crypto.CryptoApi; }; // Mock the result of encrypting the sessions. If we don't do this, the diff --git a/test/components/views/dialogs/security/ImportE2eKeysDialog-test.tsx b/test/components/views/dialogs/security/ImportE2eKeysDialog-test.tsx index af7b85b0c2d..f1199660301 100644 --- a/test/components/views/dialogs/security/ImportE2eKeysDialog-test.tsx +++ b/test/components/views/dialogs/security/ImportE2eKeysDialog-test.tsx @@ -17,7 +17,7 @@ limitations under the License. import React from "react"; import { fireEvent, render, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { CryptoApi } from "matrix-js-sdk/src/matrix"; +import { Crypto } from "matrix-js-sdk/src/matrix"; import ImportE2eKeysDialog from "../../../../../src/async-components/views/dialogs/security/ImportE2eKeysDialog"; import * as MegolmExportEncryption from "../../../../../src/utils/MegolmExportEncryption"; @@ -75,7 +75,7 @@ describe("ImportE2eKeysDialog", () => { cli.getCrypto = () => { return { importRoomKeysAsJson, - } as unknown as CryptoApi; + } as unknown as Crypto.CryptoApi; }; // Mock the result of decrypting the sessions, to avoid needing to diff --git a/test/components/views/right_panel/UserInfo-test.tsx b/test/components/views/right_panel/UserInfo-test.tsx index 1c9e375e04b..b7670d34733 100644 --- a/test/components/views/right_panel/UserInfo-test.tsx +++ b/test/components/views/right_panel/UserInfo-test.tsx @@ -18,17 +18,7 @@ import React from "react"; import { fireEvent, render, screen, waitFor, cleanup, act, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { Mocked, mocked } from "jest-mock"; -import { - Room, - User, - MatrixClient, - RoomMember, - MatrixEvent, - EventType, - CryptoApi, - DeviceVerificationStatus, - Device, -} from "matrix-js-sdk/src/matrix"; +import { Room, User, MatrixClient, RoomMember, MatrixEvent, EventType, Device } from "matrix-js-sdk/src/matrix"; import { KnownMembership } from "matrix-js-sdk/src/types"; import { defer } from "matrix-js-sdk/src/utils"; import { EventEmitter } from "events"; @@ -37,6 +27,8 @@ import { VerificationRequest, VerificationPhase as Phase, VerificationRequestEvent, + CryptoApi, + DeviceVerificationStatus, } from "matrix-js-sdk/src/crypto-api"; import UserInfo, { diff --git a/test/components/views/rooms/EventTile-test.tsx b/test/components/views/rooms/EventTile-test.tsx index cb4cbc56f0f..9697e29671d 100644 --- a/test/components/views/rooms/EventTile-test.tsx +++ b/test/components/views/rooms/EventTile-test.tsx @@ -18,7 +18,6 @@ import * as React from "react"; import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"; import { mocked } from "jest-mock"; import { - CryptoApi, EventType, IEventDecryptionResult, MatrixClient, @@ -28,7 +27,7 @@ import { Room, TweakName, } from "matrix-js-sdk/src/matrix"; -import { EventEncryptionInfo, EventShieldColour, EventShieldReason } from "matrix-js-sdk/src/crypto-api"; +import { CryptoApi, EventEncryptionInfo, EventShieldColour, EventShieldReason } from "matrix-js-sdk/src/crypto-api"; import { mkEncryptedMatrixEvent } from "matrix-js-sdk/src/testing"; import EventTile, { EventTileProps } from "../../../../src/components/views/rooms/EventTile"; diff --git a/test/components/views/settings/tabs/user/SessionManagerTab-test.tsx b/test/components/views/settings/tabs/user/SessionManagerTab-test.tsx index c34b1159455..b31e5cb0d8a 100644 --- a/test/components/views/settings/tabs/user/SessionManagerTab-test.tsx +++ b/test/components/views/settings/tabs/user/SessionManagerTab-test.tsx @@ -18,7 +18,7 @@ import React from "react"; import { act, fireEvent, render, RenderResult, screen } from "@testing-library/react"; import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo"; import { logger } from "matrix-js-sdk/src/logger"; -import { VerificationRequest } from "matrix-js-sdk/src/crypto-api"; +import { CryptoApi, DeviceVerificationStatus, VerificationRequest } from "matrix-js-sdk/src/crypto-api"; import { defer, sleep } from "matrix-js-sdk/src/utils"; import { ClientEvent, @@ -30,8 +30,6 @@ import { PUSHER_ENABLED, IAuthData, GET_LOGIN_TOKEN_CAPABILITY, - CryptoApi, - DeviceVerificationStatus, MatrixError, MatrixClient, } from "matrix-js-sdk/src/matrix"; diff --git a/test/createRoom-test.ts b/test/createRoom-test.ts index eeca9532698..8fcf1df586a 100644 --- a/test/createRoom-test.ts +++ b/test/createRoom-test.ts @@ -15,7 +15,8 @@ limitations under the License. */ import { mocked, Mocked } from "jest-mock"; -import { CryptoApi, MatrixClient, Device, Preset, RoomType } from "matrix-js-sdk/src/matrix"; +import { MatrixClient, Device, Preset, RoomType } from "matrix-js-sdk/src/matrix"; +import { CryptoApi } from "matrix-js-sdk/src/crypto-api"; // eslint-disable-next-line no-restricted-imports import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession"; diff --git a/test/stores/SetupEncryptionStore-test.ts b/test/stores/SetupEncryptionStore-test.ts index d220d7db9fa..8186e5e2649 100644 --- a/test/stores/SetupEncryptionStore-test.ts +++ b/test/stores/SetupEncryptionStore-test.ts @@ -16,9 +16,10 @@ limitations under the License. import { mocked, Mocked } from "jest-mock"; import { IBootstrapCrossSigningOpts } from "matrix-js-sdk/src/crypto"; -import { CryptoApi, DeviceVerificationStatus, MatrixClient, Device } from "matrix-js-sdk/src/matrix"; +import { MatrixClient, Device } from "matrix-js-sdk/src/matrix"; import { SecretStorageKeyDescriptionAesV1, ServerSideSecretStorage } from "matrix-js-sdk/src/secret-storage"; import { IDehydratedDevice } from "matrix-js-sdk/src/crypto/dehydration"; +import { CryptoApi, DeviceVerificationStatus } from "matrix-js-sdk/src/crypto-api"; import { SdkContextClass } from "../../src/contexts/SDKContext"; import { accessSecretStorage } from "../../src/SecurityManager"; diff --git a/test/toasts/UnverifiedSessionToast-test.tsx b/test/toasts/UnverifiedSessionToast-test.tsx index 2799f0f7916..1c282f9693b 100644 --- a/test/toasts/UnverifiedSessionToast-test.tsx +++ b/test/toasts/UnverifiedSessionToast-test.tsx @@ -18,8 +18,9 @@ import React from "react"; import { render, RenderResult, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { mocked, Mocked } from "jest-mock"; -import { CryptoApi, DeviceVerificationStatus, IMyDevice, MatrixClient } from "matrix-js-sdk/src/matrix"; +import { IMyDevice, MatrixClient } from "matrix-js-sdk/src/matrix"; import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo"; +import { CryptoApi, DeviceVerificationStatus } from "matrix-js-sdk/src/crypto-api"; import dis from "../../src/dispatcher/dispatcher"; import { showToast } from "../../src/toasts/UnverifiedSessionToast"; diff --git a/test/utils/AutoDiscoveryUtils-test.tsx b/test/utils/AutoDiscoveryUtils-test.tsx index 452287225be..f78c166d9a1 100644 --- a/test/utils/AutoDiscoveryUtils-test.tsx +++ b/test/utils/AutoDiscoveryUtils-test.tsx @@ -218,7 +218,7 @@ describe("AutoDiscoveryUtils", () => { ...validIsConfig, "m.homeserver": { state: AutoDiscoveryAction.FAIL_ERROR, - error: AutoDiscovery.ERROR_HOMESERVER_TOO_OLD, + error: AutoDiscovery.ERROR_UNSUPPORTED_HOMESERVER_SPEC_VERSION, base_url: "https://matrix.org", }, }; From 5b1f1fecd34dc7cdb87745854db1b41dc3805403 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 24 May 2024 11:27:45 +0100 Subject: [PATCH 2/4] Remove long deprecated event fields `age` `user_id` `prev_content` Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/utils/dm/createDmLocalRoom.ts | 3 -- test/TextForEvent-test.ts | 32 ++++++++++++-------- test/components/structures/RoomView-test.tsx | 1 - test/test-utils/test-utils.ts | 6 ++-- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/utils/dm/createDmLocalRoom.ts b/src/utils/dm/createDmLocalRoom.ts index 5ffa491bcfc..ffc76550c7b 100644 --- a/src/utils/dm/createDmLocalRoom.ts +++ b/src/utils/dm/createDmLocalRoom.ts @@ -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, }), diff --git a/test/TextForEvent-test.ts b/test/TextForEvent-test.ts index ff1e0a06bc9..ce5c4a97f69 100644 --- a/test/TextForEvent-test.ts +++ b/test/TextForEvent-test.ts @@ -49,8 +49,10 @@ function mockPinnedEvent(pinnedMessageIds?: string[], prevPinnedMessageIds?: str content: { pinned: pinnedMessageIds, }, - prev_content: { - pinned: prevPinnedMessageIds, + unsigned: { + prev_content: { + pinned: prevPinnedMessageIds, + }, }, }); } @@ -183,9 +185,11 @@ describe("TextForEvent", () => { users_default: usersDefault, users, }, - prev_content: { - users: prevUsers, - users_default: prevDefault, + unsigned: { + prev_content: { + users: prevUsers, + users_default: prevDefault, + }, }, }); mxEvent.sender = { name: userA.name } as RoomMember; @@ -315,9 +319,11 @@ describe("TextForEvent", () => { alias, alt_aliases: altAliases, }, - prev_content: { - alias: prevAlias, - alt_aliases: prevAltAliases, + unsigned: { + prev_content: { + alias: prevAlias, + alt_aliases: prevAltAliases, + }, }, }); @@ -512,10 +518,12 @@ describe("TextForEvent", () => { avatar_url: "b", displayname: "Bob", }, - prev_content: { - membership: KnownMembership.Join, - avatar_url: "a", - displayname: "Andy", + unsigned: { + prev_content: { + membership: KnownMembership.Join, + avatar_url: "a", + displayname: "Andy", + }, }, state_key: "@a:foo", }), diff --git a/test/components/structures/RoomView-test.tsx b/test/components/structures/RoomView-test.tsx index 31f5c896aec..8624e562468 100644 --- a/test/components/structures/RoomView-test.tsx +++ b/test/components/structures/RoomView-test.tsx @@ -353,7 +353,6 @@ describe("RoomView", () => { content: { algorithm: MEGOLM_ALGORITHM, }, - user_id: cli.getUserId()!, sender: cli.getUserId()!, state_key: "", room_id: localRoom.roomId, diff --git a/test/test-utils/test-utils.ts b/test/test-utils/test-utils.ts index 0759f2e739d..a9e61e3f365 100644 --- a/test/test-utils/test-utils.ts +++ b/test/test-utils/test-utils.ts @@ -364,10 +364,12 @@ export function mkEvent(opts: MakeEventProps): MatrixEvent { room_id: opts.room, sender: opts.user, content: opts.content, - prev_content: opts.prev_content, event_id: opts.id ?? "$" + Math.random() + "-" + Math.random(), origin_server_ts: opts.ts ?? 0, - unsigned: opts.unsigned, + unsigned: { + ...opts.unsigned, + prev_content: opts.prev_content, + }, redacts: opts.redacts, }; if (opts.skey !== undefined) { From 1351780d70bdce6b80cd032a0a49b7db90d9db61 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 24 May 2024 11:28:25 +0100 Subject: [PATCH 3/4] Use `MatrixClient::getDomain` over `getHomeserverName` Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/MatrixClientPeg.ts | 17 ----------------- .../views/dialogs/CreateRoomDialog.tsx | 2 +- .../views/dialogs/ServerOfflineDialog.tsx | 2 +- .../views/dialogs/devtools/ServerInfo.tsx | 2 +- .../views/directory/NetworkDropdown.tsx | 2 +- src/components/views/right_panel/UserInfo.tsx | 3 +-- src/hooks/usePublicRoomDirectory.ts | 4 ++-- src/utils/PasswordScorer.ts | 13 ++++++------- .../views/dialogs/SpotlightDialog-test.tsx | 2 +- test/hooks/usePublicRoomDirectory-test.tsx | 2 +- test/hooks/useUserDirectory-test.tsx | 2 +- test/submit-rageshake-test.ts | 4 +--- 12 files changed, 17 insertions(+), 38 deletions(-) diff --git a/src/MatrixClientPeg.ts b/src/MatrixClientPeg.ts index d14003dbfac..a1c277fc28b 100644 --- a/src/MatrixClientPeg.ts +++ b/src/MatrixClientPeg.ts @@ -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) { diff --git a/src/components/views/dialogs/CreateRoomDialog.tsx b/src/components/views/dialogs/CreateRoomDialog.tsx index a1fdc13f299..fa0eef5086c 100644 --- a/src/components/views/dialogs/CreateRoomDialog.tsx +++ b/src/components/views/dialogs/CreateRoomDialog.tsx @@ -436,7 +436,7 @@ export default class CreateRoomDialog extends React.Component { { timeline = [
{_t("server_offline|empty_timeline")}
]; } - const serverName = MatrixClientPeg.getHomeserverName(); + const serverName = MatrixClientPeg.safeGet().getDomain(); return ( (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. diff --git a/src/components/views/right_panel/UserInfo.tsx b/src/components/views/right_panel/UserInfo.tsx index dbc6acb29be..50b43a3237b 100644 --- a/src/components/views/right_panel/UserInfo.tsx +++ b/src/components/views/right_panel/UserInfo.tsx @@ -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"; @@ -1414,7 +1413,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 = ( => { 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; diff --git a/src/utils/PasswordScorer.ts b/src/utils/PasswordScorer.ts index 19506d84771..ed69be527fe 100644 --- a/src/utils/PasswordScorer.ts +++ b/src/utils/PasswordScorer.ts @@ -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()); diff --git a/test/components/views/dialogs/SpotlightDialog-test.tsx b/test/components/views/dialogs/SpotlightDialog-test.tsx index 5bf1029bc9e..f8fe3c00a7d 100644 --- a/test/components/views/dialogs/SpotlightDialog-test.tsx +++ b/test/components/views/dialogs/SpotlightDialog-test.tsx @@ -82,8 +82,8 @@ function mockClient({ }: MockClientOptions = {}): MatrixClient { stubClient(); const cli = MatrixClientPeg.safeGet(); - MatrixClientPeg.getHomeserverName = jest.fn(() => homeserver); cli.getUserId = jest.fn(() => userId); + cli.getDomain = jest.fn(() => homeserver); cli.getHomeserverUrl = jest.fn(() => homeserver); cli.getThirdpartyProtocols = jest.fn(() => Promise.resolve(thirdPartyProtocols)); cli.publicRooms = jest.fn((options) => { diff --git a/test/hooks/usePublicRoomDirectory-test.tsx b/test/hooks/usePublicRoomDirectory-test.tsx index c313e54d9b9..fe814162085 100644 --- a/test/hooks/usePublicRoomDirectory-test.tsx +++ b/test/hooks/usePublicRoomDirectory-test.tsx @@ -33,7 +33,7 @@ describe("usePublicRoomDirectory", () => { stubClient(); cli = MatrixClientPeg.safeGet(); - MatrixClientPeg.getHomeserverName = () => "matrix.org"; + cli.getDomain = () => "matrix.org"; cli.getThirdpartyProtocols = () => Promise.resolve({}); cli.publicRooms = ({ filter }: IRoomDirectoryOptions) => { const chunk = filter?.generic_search_term diff --git a/test/hooks/useUserDirectory-test.tsx b/test/hooks/useUserDirectory-test.tsx index 8d2906f349b..7bda5cfe0b3 100644 --- a/test/hooks/useUserDirectory-test.tsx +++ b/test/hooks/useUserDirectory-test.tsx @@ -33,7 +33,7 @@ describe("useUserDirectory", () => { stubClient(); cli = MatrixClientPeg.safeGet(); - MatrixClientPeg.getHomeserverName = () => "matrix.org"; + cli.getDomain = () => "matrix.org"; cli.getThirdpartyProtocols = () => Promise.resolve({}); cli.searchUserDirectory = ({ term: query }) => Promise.resolve({ diff --git a/test/submit-rageshake-test.ts b/test/submit-rageshake-test.ts index b2496c1d30c..3a6e15aaedb 100644 --- a/test/submit-rageshake-test.ts +++ b/test/submit-rageshake-test.ts @@ -27,7 +27,6 @@ import fetchMock from "fetch-mock-jest"; import { getMockClientWithEventEmitter, mockClientMethodsCrypto, mockPlatformPeg } from "./test-utils"; import { collectBugReport } from "../src/rageshake/submit-rageshake"; -import { MatrixClientPeg } from "../src/MatrixClientPeg"; import SettingsStore from "../src/settings/SettingsStore"; import { ConsoleLogger } from "../src/rageshake/rageshake"; @@ -45,13 +44,12 @@ describe("Rageshakes", () => { ); beforeEach(() => { - jest.spyOn(MatrixClientPeg, "getHomeserverName").mockReturnValue("alice-server.com"); - mockClient = getMockClientWithEventEmitter({ credentials: { userId: "@test:example.com" }, deviceId: "AAAAAAAAAA", baseUrl: "https://alice-server.com", getHomeserverUrl: jest.fn().mockReturnValue("https://alice-server.com"), + getDomain: jest.fn().mockReturnValue("alice-server.com"), ...mockClientMethodsCrypto(), http: mockHttpAPI, }); From 70de427686456a2b62469d42bf638876768fa588 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 24 May 2024 11:54:36 +0100 Subject: [PATCH 4/4] Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/components/views/right_panel/UserInfo.tsx | 1 - .../views/right_panel/UserInfo-test.tsx | 14 ++ .../__snapshots__/UserInfo-test.tsx.snap | 173 ++++++++++++++++++ 3 files changed, 187 insertions(+), 1 deletion(-) diff --git a/src/components/views/right_panel/UserInfo.tsx b/src/components/views/right_panel/UserInfo.tsx index 50b43a3237b..d9839252f90 100644 --- a/src/components/views/right_panel/UserInfo.tsx +++ b/src/components/views/right_panel/UserInfo.tsx @@ -1412,7 +1412,6 @@ 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(`:${cli.getDomain()}`)) { synapseDeactivateButton = ( { isCryptoEnabled: jest.fn(), getUserId: jest.fn(), getSafeUserId: jest.fn(), + getDomain: jest.fn(), on: jest.fn(), off: jest.fn(), isSynapseAdministrator: jest.fn().mockResolvedValue(false), @@ -576,6 +577,19 @@ describe("", () => { expect(within(device2Button).getByText("dehydrated device 2")).toBeInTheDocument(); }); }); + + it("should render a deactivate button for users of the same server if we are a server admin", async () => { + mockClient.isSynapseAdministrator.mockResolvedValue(true); + mockClient.getDomain.mockReturnValue("example.com"); + + const { container } = renderComponent({ + phase: RightPanelPhases.RoomMemberInfo, + room: mockRoom, + }); + + await waitFor(() => expect(screen.getByRole("button", { name: "Deactivate user" })).toBeInTheDocument()); + expect(container).toMatchSnapshot(); + }); }); describe("with an encrypted room", () => { diff --git a/test/components/views/right_panel/__snapshots__/UserInfo-test.tsx.snap b/test/components/views/right_panel/__snapshots__/UserInfo-test.tsx.snap index 95f75eadc88..8989e0c6be6 100644 --- a/test/components/views/right_panel/__snapshots__/UserInfo-test.tsx.snap +++ b/test/components/views/right_panel/__snapshots__/UserInfo-test.tsx.snap @@ -223,3 +223,176 @@ exports[` with crypto enabled renders 1`] = ` `; + +exports[` with crypto enabled should render a deactivate button for users of the same server if we are a server admin 1`] = ` +
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+

+ + @user:example.com + +

+
+
+ customUserIdentifier +
+
+
+ Unknown +
+
+
+
+
+

+ Security +

+

+ Messages in this room are not end-to-end encrypted. +

+
+ +
+
+
+
+ +
+
+
+

+ Options +

+
+ + + +
+
+
+

+ Admin Tools +

+
+ +
+
+
+
+
+`;