1 | // SPDX-FileCopyrightText: 2019 Alexey Andreyev <aa13q@ya.ru> |
2 | // SPDX-License-Identifier: LGPL-2.1-or-later |
3 | |
4 | #include "encryptedevent.h" |
5 | #include <Quotient/e2ee/e2ee_common.h> |
6 | |
7 | using namespace Quotient; |
8 | |
9 | EncryptedEvent::EncryptedEvent(const QJsonObject& ciphertexts, |
10 | const QString& senderKey) |
11 | : RoomEvent(basicJson(matrixType: TypeId, content: { { AlgorithmKeyL, OlmV1Curve25519AesSha2AlgoKey }, |
12 | { CiphertextKey, ciphertexts }, |
13 | { SenderKeyKey, senderKey } })) |
14 | {} |
15 | |
16 | EncryptedEvent::EncryptedEvent(const QByteArray& ciphertext, |
17 | const QString& senderKey, |
18 | const QString& deviceId, const QString& sessionId) |
19 | : RoomEvent(basicJson(matrixType: TypeId, content: { { AlgorithmKeyL, MegolmV1AesSha2AlgoKey }, |
20 | { CiphertextKey, QString::fromLatin1(ba: ciphertext) }, |
21 | { DeviceIdKey, deviceId }, |
22 | { SenderKeyKey, senderKey }, |
23 | { SessionIdKey, sessionId } })) |
24 | {} |
25 | |
26 | EncryptedEvent::EncryptedEvent(const QJsonObject& obj) : RoomEvent(obj) {} |
27 | |
28 | QString EncryptedEvent::algorithm() const |
29 | { |
30 | return contentPart<QString>(key: AlgorithmKeyL); |
31 | } |
32 | |
33 | RoomEventPtr EncryptedEvent::createDecrypted(const QString &decrypted) const |
34 | { |
35 | auto eventObject = QJsonDocument::fromJson(json: decrypted.toUtf8()).object(); |
36 | eventObject["event_id" _ls] = id(); |
37 | eventObject["sender" _ls] = senderId(); |
38 | eventObject["origin_server_ts" _ls] = originTimestamp().toMSecsSinceEpoch(); |
39 | if (const auto relatesToJson = contentPart<QJsonObject>(key: "m.relates_to"_ls ); |
40 | !relatesToJson.isEmpty()) { |
41 | auto content = eventObject["content" _ls].toObject(); |
42 | content["m.relates_to" _ls] = relatesToJson; |
43 | eventObject["content" _ls] = content; |
44 | } |
45 | if (const auto redactsJson = unsignedPart<QString>(key: "redacts"_ls ); |
46 | !redactsJson.isEmpty()) { |
47 | auto unsign = eventObject["unsigned" _ls].toObject(); |
48 | unsign["redacts" _ls] = redactsJson; |
49 | eventObject["unsigned" _ls] = unsign; |
50 | } |
51 | return loadEvent<RoomEvent>(fullJson: eventObject); |
52 | } |
53 | |
54 | void EncryptedEvent::setRelation(const QJsonObject& relation) |
55 | { |
56 | auto content = contentJson(); |
57 | content["m.relates_to" _ls] = relation; |
58 | editJson()["content" _ls] = content; |
59 | } |
60 | |