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
pull/28788/head^2
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,
});

View File

@ -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",