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

Make slash command errors translatable but also work in rageshakes #7377

Merged
merged 22 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 65 additions & 33 deletions src/SlashCommands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { logger } from "matrix-js-sdk/src/logger";

import { MatrixClientPeg } from './MatrixClientPeg';
import dis from './dispatcher/dispatcher';
import { _t, _td } from './languageHandler';
import { _t, _td, newTranslatableError } from './languageHandler';
import Modal from './Modal';
import MultiInviter from './utils/MultiInviter';
import { linkifyAndSanitizeHtml } from './HtmlUtils';
Expand Down Expand Up @@ -141,13 +141,25 @@ export class Command {

run(roomId: string, threadId: string, args: string) {
// if it has no runFn then its an ignored/nop command (autocomplete only) e.g `/me`
if (!this.runFn) return reject(_t("Command error"));
if (!this.runFn) {
return reject(
newTranslatableError(
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
"Command error: This command should have been handled by another handler and we" +
" can't try to run something without a command function.",
),
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
);
}

const renderingType = threadId
? TimelineRenderingType.Thread
: TimelineRenderingType.Room;
if (this.renderingTypes && !this.renderingTypes?.includes(renderingType)) {
return reject(_t("Command error"));
return reject(
newTranslatableError(
"Command error: Unable to find rendering type (%(renderingType)s)",
{ renderingType },
),
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
);
}

return this.runFn.bind(this)(roomId, args);
Expand Down Expand Up @@ -270,7 +282,9 @@ export const Commands = [
const cli = MatrixClientPeg.get();
const room = cli.getRoom(roomId);
if (!room.currentState.mayClientSendStateEvent("m.room.tombstone", cli)) {
return reject(_t("You do not have the required permissions to use this command."));
return reject(
newTranslatableError("You do not have the required permissions to use this command."),
);
}

const { finished } = Modal.createTrackedDialog('Slash Commands', 'upgrade room confirmation',
Expand All @@ -297,15 +311,10 @@ export const Commands = [
return success((async () => {
const unixTimestamp = Date.parse(args);
if (!unixTimestamp) {
throw new Error(
// FIXME: Use newTranslatableError here instead
// otherwise the rageshake error messages will be
// translated too
_t(
// eslint-disable-next-line max-len
'We were unable to understand the given date (%(inputDate)s). Try using the format YYYY-MM-DD.',
{ inputDate: args },
),
throw newTranslatableError(
'We were unable to understand the given date (%(inputDate)s). ' +
'Try using the format YYYY-MM-DD.',
{ inputDate: args },
);
}

Expand Down Expand Up @@ -437,7 +446,7 @@ export const Commands = [
return success(cli.setRoomTopic(roomId, args));
}
const room = cli.getRoom(roomId);
if (!room) return reject(_t("Failed to set topic"));
if (!room) return reject(newTranslatableError("Failed to set topic"));

const topicEvents = room.currentState.getStateEvents('m.room.topic', '');
const topic = topicEvents && topicEvents.getContent().topic;
Expand Down Expand Up @@ -509,10 +518,16 @@ export const Commands = [
useDefaultIdentityServer();
return;
}
throw new Error(_t("Use an identity server to invite by email. Manage in Settings."));
throw newTranslatableError(
"Use an identity server to invite by email. Manage in Settings.",
);
});
} else {
return reject(_t("Use an identity server to invite by email. Manage in Settings."));
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
return reject(
newTranslatableError(
"Use an identity server to invite by email. Manage in Settings.",
),
);
}
}
const inviter = new MultiInviter(roomId);
Expand Down Expand Up @@ -680,7 +695,14 @@ export const Commands = [
}
if (targetRoomId) break;
}
if (!targetRoomId) return reject(_t('Unrecognised room address:') + ' ' + roomAlias);
if (!targetRoomId) {
return reject(
newTranslatableError(
'Unrecognised room address: %(roomAlias)s',
{ roomAlias },
),
);
}
}
}

Expand Down Expand Up @@ -819,10 +841,10 @@ export const Commands = [
if (!isNaN(powerLevel)) {
const cli = MatrixClientPeg.get();
const room = cli.getRoom(roomId);
if (!room) return reject(_t("Command failed"));
if (!room) return reject(newTranslatableError("Command failed"));
const member = room.getMember(userId);
if (!member || getEffectiveMembership(member.membership) === EffectiveMembership.Leave) {
return reject(_t("Could not find user in room"));
return reject(newTranslatableError("Could not find user in room"));
}
const powerLevelEvent = room.currentState.getStateEvents('m.room.power_levels', '');
return success(cli.setPowerLevel(roomId, userId, powerLevel, powerLevelEvent));
Expand All @@ -849,10 +871,16 @@ export const Commands = [
if (matches) {
const cli = MatrixClientPeg.get();
const room = cli.getRoom(roomId);
if (!room) return reject(_t("Command failed"));
if (!room) {
return reject(
newTranslatableError("Command failed: Unable to find room (%(roomId)s", { roomId }),
);
}

const powerLevelEvent = room.currentState.getStateEvents('m.room.power_levels', '');
if (!powerLevelEvent.getContent().users[args]) return reject(_t("Could not find user in room"));
if (!powerLevelEvent.getContent().users[args]) {
return reject(newTranslatableError("Could not find user in room"));
}
return success(cli.setPowerLevel(roomId, args, undefined, powerLevelEvent));
}
}
Expand All @@ -877,7 +905,7 @@ export const Commands = [
isEnabled: () => SettingsStore.getValue(UIFeature.Widgets),
runFn: function(roomId, widgetUrl) {
if (!widgetUrl) {
return reject(_t("Please supply a widget URL or embed code"));
return reject(newTranslatableError("Please supply a widget URL or embed code"));
}

// Try and parse out a widget URL from iframes
Expand All @@ -896,7 +924,7 @@ export const Commands = [
}

if (!widgetUrl.startsWith("https://") && !widgetUrl.startsWith("http://")) {
return reject(_t("Please supply a https:// or http:// widget URL"));
return reject(newTranslatableError("Please supply a https:// or http:// widget URL"));
}
if (WidgetUtils.canUserModifyWidgets(roomId)) {
const userId = MatrixClientPeg.get().getUserId();
Expand All @@ -918,7 +946,7 @@ export const Commands = [

return success(WidgetUtils.setRoomWidget(roomId, widgetId, type, widgetUrl, name, data));
} else {
return reject(_t("You cannot modify widgets in this room."));
return reject(newTranslatableError("You cannot modify widgets in this room."));
}
},
category: CommandCategories.admin,
Expand All @@ -941,30 +969,34 @@ export const Commands = [
return success((async () => {
const device = cli.getStoredDevice(userId, deviceId);
if (!device) {
throw new Error(_t('Unknown (user, session) pair:') + ` (${userId}, ${deviceId})`);
throw newTranslatableError(
'Unknown (user, session) pair: (%(userId)s, %(deviceId)s)',
{ userId, deviceId },
);
}
const deviceTrust = await cli.checkDeviceTrust(userId, deviceId);

if (deviceTrust.isVerified()) {
if (device.getFingerprint() === fingerprint) {
throw new Error(_t('Session already verified!'));
throw newTranslatableError('Session already verified!');
} else {
throw new Error(_t('WARNING: Session already verified, but keys do NOT MATCH!'));
throw newTranslatableError('WARNING: Session already verified, but keys do NOT MATCH!');
}
}

if (device.getFingerprint() !== fingerprint) {
const fprint = device.getFingerprint();
throw new Error(
_t('WARNING: KEY VERIFICATION FAILED! The signing key for %(userId)s and session' +
throw newTranslatableError(
'WARNING: KEY VERIFICATION FAILED! The signing key for %(userId)s and session' +
' %(deviceId)s is "%(fprint)s" which does not match the provided key ' +
'"%(fingerprint)s". This could mean your communications are being intercepted!',
{
fprint,
userId,
deviceId,
fingerprint,
}));
},
);
}

await cli.setDeviceVerified(userId, deviceId, true);
Expand Down Expand Up @@ -1083,7 +1115,7 @@ export const Commands = [
if (isPhoneNumber) {
const results = await CallHandler.instance.pstnLookup(this.state.value);
if (!results || results.length === 0 || !results[0].userid) {
throw new Error("Unable to find Matrix ID for phone number");
throw newTranslatableError("Unable to find Matrix ID for phone number");
}
userId = results[0].userid;
}
Expand Down Expand Up @@ -1135,7 +1167,7 @@ export const Commands = [
runFn: function(roomId, args) {
const call = CallHandler.instance.getCallForRoom(roomId);
if (!call) {
return reject("No active call in this room");
return reject(newTranslatableError("No active call in this room"));
}
call.setRemoteOnHold(true);
return success();
Expand All @@ -1149,7 +1181,7 @@ export const Commands = [
runFn: function(roomId, args) {
const call = CallHandler.instance.getCallForRoom(roomId);
if (!call) {
return reject("No active call in this room");
return reject(newTranslatableError("No active call in this room"));
}
call.setRemoteOnHold(false);
return success();
Expand Down
3 changes: 3 additions & 0 deletions src/components/views/rooms/SendMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
let errText;
if (typeof error === 'string') {
errText = error;
} else if (error.translatedMessage) {
// Check for translatable errors (newTranslatableError)
errText = error.translatedMessage;
} else if (error.message) {
errText = error.message;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/languageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ interface ITranslatableError extends Error {
* @param {string} message Message to translate.
* @returns {Error} The constructed error.
*/
export function newTranslatableError(message: string) {
export function newTranslatableError(message: string, variables?: IVariables): ITranslatableError {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if we should shorten this to match other funcs, maybe _tError or similar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_tError seems reasonable 👍 but I'll leave that taste making to you for another PR

const error = new Error(message) as ITranslatableError;
error.translatedMessage = _t(message);
error.translatedMessage = _t(message, variables);
return error;
}

Expand Down