2022-12-15 12:24:33 -05:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2024-04-25 09:11:41 +01:00
|
|
|
Copyright 2022-2024 The Matrix.org Foundation C.I.C.
|
2022-12-15 12:24:33 -05:00
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-12-15 12:24:33 -05:00
|
|
|
*/
|
|
|
|
|
|
2024-09-30 13:39:25 +01:00
|
|
|
import classNames from "classnames";
|
2024-04-25 09:11:41 +01:00
|
|
|
import React, { forwardRef, ForwardRefExoticComponent, useContext } from "react";
|
2023-02-22 11:39:09 +01:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2024-04-25 09:11:41 +01:00
|
|
|
import { DecryptionFailureCode } from "matrix-js-sdk/src/crypto-api";
|
2024-10-14 17:54:15 +01:00
|
|
|
import { WarningIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
|
2022-12-15 12:24:33 -05:00
|
|
|
|
|
|
|
|
import { _t } from "../../../languageHandler";
|
|
|
|
|
import { IBodyProps } from "./IBodyProps";
|
2024-04-25 09:11:41 +01:00
|
|
|
import { LocalDeviceVerificationStateContext } from "../../../contexts/LocalDeviceVerificationStateContext";
|
2022-12-15 12:24:33 -05:00
|
|
|
|
2024-09-30 13:39:25 +01:00
|
|
|
function getErrorMessage(mxEvent: MatrixEvent, isVerified: boolean | undefined): string | React.JSX.Element {
|
2024-04-25 09:11:41 +01:00
|
|
|
switch (mxEvent.decryptionFailureReason) {
|
2024-07-10 10:22:59 +01:00
|
|
|
case DecryptionFailureCode.MEGOLM_KEY_WITHHELD_FOR_UNVERIFIED_DEVICE:
|
|
|
|
|
return _t("timeline|decryption_failure|blocked");
|
|
|
|
|
|
2024-04-25 09:11:41 +01:00
|
|
|
case DecryptionFailureCode.HISTORICAL_MESSAGE_NO_KEY_BACKUP:
|
|
|
|
|
return _t("timeline|decryption_failure|historical_event_no_key_backup");
|
|
|
|
|
|
|
|
|
|
case DecryptionFailureCode.HISTORICAL_MESSAGE_BACKUP_UNCONFIGURED:
|
|
|
|
|
if (isVerified === false) {
|
|
|
|
|
// The user seems to have a key backup, so prompt them to verify in the hope that doing so will
|
|
|
|
|
// mean we can restore from backup and we'll get the key for this message.
|
|
|
|
|
return _t("timeline|decryption_failure|historical_event_unverified_device");
|
|
|
|
|
}
|
|
|
|
|
// otherwise, use the default.
|
|
|
|
|
break;
|
2024-04-29 13:18:57 -04:00
|
|
|
|
|
|
|
|
case DecryptionFailureCode.HISTORICAL_MESSAGE_USER_NOT_JOINED:
|
2024-09-30 13:39:25 +01:00
|
|
|
// TODO: event should be hidden instead of showing this error.
|
|
|
|
|
// To be revisited as part of https://github.com/element-hq/element-meta/issues/2449
|
2024-04-29 13:18:57 -04:00
|
|
|
return _t("timeline|decryption_failure|historical_event_user_not_joined");
|
2024-09-30 13:39:25 +01:00
|
|
|
|
|
|
|
|
case DecryptionFailureCode.SENDER_IDENTITY_PREVIOUSLY_VERIFIED:
|
|
|
|
|
return (
|
|
|
|
|
<span>
|
2024-10-14 17:54:15 +01:00
|
|
|
<WarningIcon className="mx_Icon mx_Icon_16" />
|
2024-09-30 13:39:25 +01:00
|
|
|
{_t("timeline|decryption_failure|sender_identity_previously_verified")}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
case DecryptionFailureCode.UNSIGNED_SENDER_DEVICE:
|
|
|
|
|
// TODO: event should be hidden instead of showing this error.
|
|
|
|
|
// To be revisited as part of https://github.com/element-hq/element-meta/issues/2449
|
|
|
|
|
return _t("timeline|decryption_failure|sender_unsigned_device");
|
2024-04-25 09:11:41 +01:00
|
|
|
}
|
|
|
|
|
return _t("timeline|decryption_failure|unable_to_decrypt");
|
2023-02-22 11:39:09 +01:00
|
|
|
}
|
|
|
|
|
|
2024-09-30 13:39:25 +01:00
|
|
|
/** Get an extra CSS class, specific to the decryption failure reason */
|
|
|
|
|
function errorClassName(mxEvent: MatrixEvent): string | null {
|
|
|
|
|
switch (mxEvent.decryptionFailureReason) {
|
|
|
|
|
case DecryptionFailureCode.SENDER_IDENTITY_PREVIOUSLY_VERIFIED:
|
|
|
|
|
return "mx_DecryptionFailureVerifiedIdentityChanged";
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 12:24:33 -05:00
|
|
|
// A placeholder element for messages that could not be decrypted
|
2024-04-25 09:11:41 +01:00
|
|
|
export const DecryptionFailureBody = forwardRef<HTMLDivElement, IBodyProps>(({ mxEvent }, ref): React.JSX.Element => {
|
|
|
|
|
const verificationState = useContext(LocalDeviceVerificationStateContext);
|
2024-09-30 13:39:25 +01:00
|
|
|
const classes = classNames("mx_DecryptionFailureBody", "mx_EventTile_content", errorClassName(mxEvent));
|
|
|
|
|
|
2023-07-07 13:37:26 +01:00
|
|
|
return (
|
2024-09-30 13:39:25 +01:00
|
|
|
<div className={classes} ref={ref}>
|
2024-04-25 09:11:41 +01:00
|
|
|
{getErrorMessage(mxEvent, verificationState)}
|
2023-07-07 13:37:26 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}) as ForwardRefExoticComponent<IBodyProps>;
|