2020-11-04 17:21:25 +00:00
|
|
|
/*
|
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
import React, { forwardRef, useContext } from "react";
|
2023-08-07 09:24:58 +01:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2020-11-04 17:21:25 +00:00
|
|
|
|
2024-07-04 16:50:07 +01:00
|
|
|
import type { RoomEncryptionEventContent } from "matrix-js-sdk/src/types";
|
2020-11-04 17:21:25 +00:00
|
|
|
import { _t } from "../../../languageHandler";
|
|
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
|
|
|
|
import EventTileBubble from "./EventTileBubble";
|
2020-11-04 17:50:59 +00:00
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
|
|
|
|
import DMRoomMap from "../../../utils/DMRoomMap";
|
2021-09-06 14:59:30 +01:00
|
|
|
import { objectHasDiff } from "../../../utils/objects";
|
2022-07-20 09:26:25 +02:00
|
|
|
import { isLocalRoom } from "../../../utils/localRoom/isLocalRoom";
|
2024-07-04 16:50:07 +01:00
|
|
|
import { MEGOLM_ENCRYPTION_ALGORITHM } from "../../../utils/crypto";
|
2020-11-04 17:21:25 +00:00
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
mxEvent: MatrixEvent;
|
2022-01-25 13:10:17 +00:00
|
|
|
timestamp?: JSX.Element;
|
2020-11-04 17:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-25 13:10:17 +00:00
|
|
|
const EncryptionEvent = forwardRef<HTMLDivElement, IProps>(({ mxEvent, timestamp }, ref) => {
|
2020-11-04 17:50:59 +00:00
|
|
|
const cli = useContext(MatrixClientContext);
|
2023-03-16 11:07:29 +00:00
|
|
|
const roomId = mxEvent.getRoomId()!;
|
2023-06-15 08:46:19 +01:00
|
|
|
const isRoomEncrypted = MatrixClientPeg.safeGet().isRoomEncrypted(roomId);
|
2020-11-04 17:50:59 +00:00
|
|
|
|
2024-07-04 16:50:07 +01:00
|
|
|
const prevContent = mxEvent.getPrevContent() as RoomEncryptionEventContent;
|
|
|
|
|
const content = mxEvent.getContent<RoomEncryptionEventContent>();
|
2021-09-06 14:59:30 +01:00
|
|
|
|
2021-09-08 12:53:13 +01:00
|
|
|
// if no change happened then skip rendering this, a shallow check is enough as all known fields are top-level.
|
|
|
|
|
if (!objectHasDiff(prevContent, content)) return null; // nop
|
|
|
|
|
|
2024-07-04 16:50:07 +01:00
|
|
|
if (content.algorithm === MEGOLM_ENCRYPTION_ALGORITHM && isRoomEncrypted) {
|
2020-11-04 17:50:59 +00:00
|
|
|
let subtitle: string;
|
|
|
|
|
const dmPartner = DMRoomMap.shared().getUserIdForRoomId(roomId);
|
2022-07-20 09:26:25 +02:00
|
|
|
const room = cli?.getRoom(roomId);
|
2024-07-04 16:50:07 +01:00
|
|
|
if (prevContent.algorithm === MEGOLM_ENCRYPTION_ALGORITHM) {
|
2023-10-02 13:52:27 +01:00
|
|
|
subtitle = _t("timeline|m.room.encryption|parameters_changed");
|
2021-09-06 14:59:30 +01:00
|
|
|
} else if (dmPartner) {
|
2023-03-16 11:07:29 +00:00
|
|
|
const displayName = room?.getMember(dmPartner)?.rawDisplayName || dmPartner;
|
2023-10-02 13:52:27 +01:00
|
|
|
subtitle = _t("timeline|m.room.encryption|enabled_dm", { displayName });
|
2023-03-16 11:07:29 +00:00
|
|
|
} else if (room && isLocalRoom(room)) {
|
2023-10-02 13:52:27 +01:00
|
|
|
subtitle = _t("timeline|m.room.encryption|enabled_local");
|
2020-11-04 17:50:59 +00:00
|
|
|
} else {
|
2023-10-02 13:52:27 +01:00
|
|
|
subtitle = _t("timeline|m.room.encryption|enabled");
|
2020-11-04 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-04 17:21:25 +00:00
|
|
|
return (
|
|
|
|
|
<EventTileBubble
|
|
|
|
|
className="mx_cryptoEvent mx_cryptoEvent_icon"
|
2023-08-22 18:47:33 +01:00
|
|
|
title={_t("common|encryption_enabled")}
|
2020-11-04 17:50:59 +00:00
|
|
|
subtitle={subtitle}
|
2022-01-25 13:10:17 +00:00
|
|
|
timestamp={timestamp}
|
2020-11-04 17:21:25 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2021-09-06 14:59:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isRoomEncrypted) {
|
2020-11-04 17:21:25 +00:00
|
|
|
return (
|
|
|
|
|
<EventTileBubble
|
|
|
|
|
className="mx_cryptoEvent mx_cryptoEvent_icon"
|
2023-08-22 18:47:33 +01:00
|
|
|
title={_t("common|encryption_enabled")}
|
2023-10-02 13:52:27 +01:00
|
|
|
subtitle={_t("timeline|m.room.encryption|disable_attempt")}
|
2022-01-25 13:10:17 +00:00
|
|
|
timestamp={timestamp}
|
2020-11-04 17:21:25 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<EventTileBubble
|
|
|
|
|
className="mx_cryptoEvent mx_cryptoEvent_icon mx_cryptoEvent_icon_warning"
|
2023-10-02 13:52:27 +01:00
|
|
|
title={_t("timeline|m.room.encryption|disabled")}
|
|
|
|
|
subtitle={_t("timeline|m.room.encryption|unsupported")}
|
2020-11-05 16:27:41 +00:00
|
|
|
ref={ref}
|
2022-01-25 13:10:17 +00:00
|
|
|
timestamp={timestamp}
|
2020-11-04 17:21:25 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2020-11-05 16:27:41 +00:00
|
|
|
});
|
2020-11-04 17:21:25 +00:00
|
|
|
|
|
|
|
|
export default EncryptionEvent;
|