Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Make use of js-sdk roomNameGenerator to handle i18n for generated roo…
Browse files Browse the repository at this point in the history
…m names (#9209)

* Make use of js-sdk roomNameGenerator to handle i18n for generated room names

* DRY

* Make tsc happier

* Update MatrixClientPeg.ts
  • Loading branch information
t3chguy committed Aug 26, 2022
1 parent 27de00a commit 5aae974
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
79 changes: 70 additions & 9 deletions src/MatrixClientPeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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,
});
Expand Down
9 changes: 8 additions & 1 deletion src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 5aae974

Please sign in to comment.