2022-12-15 12:24:33 -05:00
|
|
|
/*
|
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
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
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";
|
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-04-25 09:11:41 +01:00
|
|
|
function getErrorMessage(mxEvent: MatrixEvent, isVerified: boolean | undefined): string {
|
|
|
|
|
if (mxEvent.isEncryptedDisabledForUnverifiedDevices) return _t("timeline|decryption_failure|blocked");
|
|
|
|
|
switch (mxEvent.decryptionFailureReason) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
return _t("timeline|decryption_failure|unable_to_decrypt");
|
2023-02-22 11:39:09 +01:00
|
|
|
}
|
|
|
|
|
|
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);
|
2023-07-07 13:37:26 +01:00
|
|
|
return (
|
|
|
|
|
<div className="mx_DecryptionFailureBody mx_EventTile_content" 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>;
|