From 5aae974e93aa05a1a65d73be1e4fe80416f502bd Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 26 Aug 2022 11:23:56 +0100 Subject: [PATCH] Make use of js-sdk roomNameGenerator to handle i18n for generated room names (#9209) * Make use of js-sdk roomNameGenerator to handle i18n for generated room names * DRY * Make tsc happier * Update MatrixClientPeg.ts --- src/MatrixClientPeg.ts | 79 ++++++++++++++++++++++++++++++++----- src/i18n/strings/en_EN.json | 9 ++++- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/MatrixClientPeg.ts b/src/MatrixClientPeg.ts index 8881e7ef49d..1fba136942c 100644 --- a/src/MatrixClientPeg.ts +++ b/src/MatrixClientPeg.ts @@ -17,7 +17,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { ICreateClientOpts, PendingEventOrdering } from 'matrix-js-sdk/src/matrix'; +import { ICreateClientOpts, PendingEventOrdering, RoomNameState, RoomNameType } from 'matrix-js-sdk/src/matrix'; import { IStartClientOpts, MatrixClient } from 'matrix-js-sdk/src/client'; import { MemoryStore } from 'matrix-js-sdk/src/store/memory'; import * as utils from 'matrix-js-sdk/src/utils'; @@ -37,6 +37,7 @@ import IdentityAuthClient from './IdentityAuthClient'; import { crossSigningCallbacks, tryToUnlockSecretStorageWithDehydrationKey } from './SecurityManager'; import SecurityCustomisations from "./customisations/Security"; import CryptoStoreTooNewDialog from "./components/views/dialogs/CryptoStoreTooNewDialog"; +import { _t } from "./languageHandler"; export interface IMatrixClientCreds { homeserverUrl: string; @@ -278,6 +279,48 @@ class MatrixClientPegClass implements IMatrixClientPeg { return matches[1]; } + private namesToRoomName(names: string[], count: number): string | undefined { + const countWithoutMe = count - 1; + if (!names.length) { + return _t("Empty room"); + } + if (names.length === 1 && countWithoutMe <= 1) { + return names[0]; + } + } + + private memberNamesToRoomName(names: string[], count: number): string { + const name = this.namesToRoomName(names, count); + if (name) return name; + + if (names.length === 2 && count === 2) { + return _t("%(user1)s and %(user2)s", { + user1: names[0], + user2: names[1], + }); + } + return _t("%(user)s and %(count)s others", { + user: names[0], + count: count - 1, + }); + } + + private inviteeNamesToRoomName(names: string[], count: number): string { + const name = this.namesToRoomName(names, count); + if (name) return name; + + if (names.length === 2 && count === 2) { + return _t("Inviting %(user1)s and %(user2)s", { + user1: names[0], + user2: names[1], + }); + } + return _t("Inviting %(user)s and %(count)s others", { + user: names[0], + count: count - 1, + }); + } + private createClient(creds: IMatrixClientCreds): void { const opts: ICreateClientOpts = { baseUrl: creds.homeserverUrl, @@ -299,16 +342,34 @@ class MatrixClientPegClass implements IMatrixClientPeg { verificationMethods.RECIPROCATE_QR_CODE, ], identityServer: new IdentityAuthClient(), - cryptoCallbacks: {}, + // These are always installed regardless of the labs flag so that cross-signing features + // can toggle on without reloading and also be accessed immediately after login. + cryptoCallbacks: { ...crossSigningCallbacks }, + roomNameGenerator: (_: string, state: RoomNameState) => { + switch (state.type) { + case RoomNameType.Generated: + switch (state.subtype) { + case "Inviting": + return this.inviteeNamesToRoomName(state.names, state.count); + default: + return this.memberNamesToRoomName(state.names, state.count); + } + case RoomNameType.EmptyRoom: + if (state.oldName) { + return _t("Empty room (was %(oldName)s)", { + oldName: state.oldName, + }); + } else { + return _t("Empty room"); + } + default: + return null; + } + }, }; - // These are always installed regardless of the labs flag so that - // cross-signing features can toggle on without reloading and also be - // accessed immediately after login. - Object.assign(opts.cryptoCallbacks, crossSigningCallbacks); if (SecurityCustomisations.getDehydrationKey) { - opts.cryptoCallbacks.getDehydrationKey = - SecurityCustomisations.getDehydrationKey; + opts.cryptoCallbacks!.getDehydrationKey = SecurityCustomisations.getDehydrationKey; } this.matrixClient = createMatrixClient(opts); @@ -319,7 +380,7 @@ class MatrixClientPegClass implements IMatrixClientPeg { this.matrixClient.setGuest(Boolean(creds.guest)); - const notifTimelineSet = new EventTimelineSet(null, { + const notifTimelineSet = new EventTimelineSet(undefined, { timelineSupport: true, pendingEvents: false, }); diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 3134a04b08a..5135e51da88 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -97,6 +97,14 @@ "Try again": "Try again", "Your homeserver was unreachable and was not able to log you in. Please try again. If this continues, please contact your homeserver administrator.": "Your homeserver was unreachable and was not able to log you in. Please try again. If this continues, please contact your homeserver administrator.", "Your homeserver rejected your log in attempt. This could be due to things just taking too long. Please try again. If this continues, please contact your homeserver administrator.": "Your homeserver rejected your log in attempt. This could be due to things just taking too long. Please try again. If this continues, please contact your homeserver administrator.", + "Empty room": "Empty room", + "%(user1)s and %(user2)s": "%(user1)s and %(user2)s", + "%(user)s and %(count)s others|other": "%(user)s and %(count)s others", + "%(user)s and %(count)s others|one": "%(user)s and 1 other", + "Inviting %(user1)s and %(user2)s": "Inviting %(user1)s and %(user2)s", + "Inviting %(user)s and %(count)s others|other": "Inviting %(user)s and %(count)s others", + "Inviting %(user)s and %(count)s others|one": "Inviting %(user)s and 1 other", + "Empty room (was %(oldName)s)": "Empty room (was %(oldName)s)", "%(name)s is requesting verification": "%(name)s is requesting verification", "%(brand)s does not have permission to send you notifications - please check your browser settings": "%(brand)s does not have permission to send you notifications - please check your browser settings", "%(brand)s was not given permission to send notifications - please try again": "%(brand)s was not given permission to send notifications - please try again", @@ -1882,7 +1890,6 @@ "System Alerts": "System Alerts", "Historical": "Historical", "Suggested Rooms": "Suggested Rooms", - "Empty room": "Empty room", "Add space": "Add space", "You do not have permissions to add spaces to this space": "You do not have permissions to add spaces to this space", "Join public room": "Join public room",