1 | // SPDX-FileCopyrightText: 2018 Kitsune Ral <kitsune-ral@users.sf.net> |
2 | // SPDX-License-Identifier: LGPL-2.1-or-later |
3 | |
4 | #include "roomevent.h" |
5 | |
6 | #include "../logging_categories_p.h" |
7 | #include "redactionevent.h" |
8 | |
9 | #ifdef Quotient_E2EE_ENABLED |
10 | #include "encryptedevent.h" |
11 | #endif |
12 | |
13 | using namespace Quotient; |
14 | |
15 | RoomEvent::RoomEvent(const QJsonObject& json) : Event(json) |
16 | { |
17 | if (const auto redaction = unsignedPart<QJsonObject>(key: RedactedCauseKey); |
18 | !redaction.isEmpty()) |
19 | _redactedBecause = loadEvent<RedactionEvent>(fullJson: redaction); |
20 | } |
21 | |
22 | RoomEvent::~RoomEvent() = default; // Let the smart pointer do its job |
23 | |
24 | QString RoomEvent::id() const { return fullJson()[EventIdKey].toString(); } |
25 | |
26 | QDateTime RoomEvent::originTimestamp() const |
27 | { |
28 | return Quotient::fromJson<QDateTime>(jv: fullJson()["origin_server_ts" _ls]); |
29 | } |
30 | |
31 | QString RoomEvent::roomId() const |
32 | { |
33 | return fullJson()[RoomIdKey].toString(); |
34 | } |
35 | |
36 | QString RoomEvent::senderId() const |
37 | { |
38 | return fullJson()[SenderKey].toString(); |
39 | } |
40 | |
41 | QString RoomEvent::redactionReason() const |
42 | { |
43 | return isRedacted() ? _redactedBecause->reason() : QString {}; |
44 | } |
45 | |
46 | QString RoomEvent::transactionId() const |
47 | { |
48 | return unsignedPart<QString>(key: "transaction_id"_ls ); |
49 | } |
50 | |
51 | QString RoomEvent::stateKey() const |
52 | { |
53 | return fullJson()[StateKeyKey].toString(); |
54 | } |
55 | |
56 | void RoomEvent::setRoomId(const QString& roomId) |
57 | { |
58 | editJson().insert(key: RoomIdKey, value: roomId); |
59 | } |
60 | |
61 | void RoomEvent::setSender(const QString& senderId) |
62 | { |
63 | editJson().insert(key: SenderKey, value: senderId); |
64 | } |
65 | |
66 | void RoomEvent::setTransactionId(const QString& txnId) |
67 | { |
68 | auto unsignedData = fullJson()[UnsignedKey].toObject(); |
69 | unsignedData.insert(QStringLiteral("transaction_id" ), value: txnId); |
70 | editJson().insert(key: UnsignedKey, value: unsignedData); |
71 | Q_ASSERT(transactionId() == txnId); |
72 | } |
73 | |
74 | void RoomEvent::addId(const QString& newId) |
75 | { |
76 | Q_ASSERT(id().isEmpty()); |
77 | Q_ASSERT(!newId.isEmpty()); |
78 | editJson().insert(key: EventIdKey, value: newId); |
79 | qCDebug(EVENTS) << "Event txnId -> id:" << transactionId() << "->" << id(); |
80 | Q_ASSERT(id() == newId); |
81 | } |
82 | |
83 | void RoomEvent::dumpTo(QDebug dbg) const |
84 | { |
85 | Event::dumpTo(dbg); |
86 | dbg << " (made at " << originTimestamp().toString(format: Qt::ISODate) << ')'; |
87 | } |
88 | |
89 | #ifdef Quotient_E2EE_ENABLED |
90 | void RoomEvent::setOriginalEvent(event_ptr_tt<EncryptedEvent>&& originalEvent) |
91 | { |
92 | _originalEvent = std::move(originalEvent); |
93 | } |
94 | |
95 | const QJsonObject RoomEvent::encryptedJson() const |
96 | { |
97 | if(!_originalEvent) { |
98 | return {}; |
99 | } |
100 | return _originalEvent->fullJson(); |
101 | } |
102 | #endif |
103 | |