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
This commit is contained in:
Michael Telatynski 2022-08-26 11:23:56 +01:00 committed by GitHub
parent 27de00a859
commit 5aae974e93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 10 deletions

View file

@ -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,
});