libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
roomevent.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2018 Kitsune Ral <kitsune-ral@users.sf.net>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include "event.h"
7
9
10#include <QtCore/QDateTime>
11
12namespace Quotient {
13
14constexpr inline auto EventIdKey = "event_id"_L1;
15constexpr inline auto RoomIdKey = "room_id"_L1;
16constexpr inline auto StateKeyKey = "state_key"_L1;
17constexpr inline auto RedactedCauseKey = "redacted_because"_L1;
18
19class RedactionEvent;
20class EncryptedEvent;
21
22// That check could look into Event and find most stuff already deleted...
23// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
24class QUOTIENT_API RoomEvent : public Event {
25public:
26 QUO_BASE_EVENT(RoomEvent, Event)
27
28 ~RoomEvent() override; // Don't inline this - see the private section
29
30 //! \brief A convenience function to get a display string for an event ID.
31 //! \return id() if the event id is not empty, otherwise transactionId();
32 //! this is useful to deal with pending and normal events uniformly.
33 //! \sa id(), transactionId()
34 QString displayId() const;
35
36 //! The event_id JSON value for the event.
37 QString id() const;
38
39 QDateTime originTimestamp() const;
40 QString roomId() const;
41 QString senderId() const;
42 bool isRedacted() const { return bool(_redactedBecause); }
43 const event_ptr_tt<RedactionEvent>& redactedBecause() const
44 {
45 return _redactedBecause;
46 }
47 QString redactionReason() const;
48
49 //! The transaction_id JSON value for the event.
50 QString transactionId() const;
51
52 // State events are special in Matrix; so isStateEvent() and stateKey() are here,
53 // as an exception. For other event types (including base types), Event::is<>() and
54 // Quotient::is<>() should be used
55
56 bool isStateEvent() const;
57
58 QString stateKey() const;
59
60 //! \brief Fill the pending event object with the room id
61 void setRoomId(const QString& roomId);
62 //! \brief Fill the pending event object with the sender id
63 void setSender(const QString& senderId);
64 //! \brief Fill the pending event object with the transaction id
65 //! \param txnId - transaction id, normally obtained from
66 //! Connection::generateTxnId()
67 void setTransactionId(const QString& txnId);
68
69 //! \brief Add an event id to locally created events after they are sent
70 //!
71 //! When a new event is created locally, it has no id; the homeserver
72 //! assigns it once the event is sent. This function allows to add the id
73 //! once the confirmation from the server is received. There should be no id
74 //! set previously in the event. It's the responsibility of the code calling
75 //! addId() to notify clients about the change; there's no signal or
76 //! callback for that in RoomEvent.
77 void addId(const QString& newId);
78
79 void setOriginalEvent(event_ptr_tt<EncryptedEvent>&& originalEvent);
80 const EncryptedEvent* originalEvent() const { return _originalEvent.get(); }
81 const QJsonObject encryptedJson() const;
82
83 //! \brief Determine whether the event is a reply to another message.
84 //!
85 //! \param includeFallbacks include thread fallback replies for non-threaded clients.
86 //!
87 //! \return true if this event is a reply, i.e. it has `"m.in_reply_to"`
88 //! event ID and is not a thread fallback (except where \p includeFallbacks is true);
89 //! false otherwise.
90 //!
91 //! \note It's possible to reply to another message in a thread so this function
92 //! will return true for a `"rel_type"` of `"m.thread"` if `"is_falling_back"`
93 //! is false.
94 bool isReply(bool includeFallbacks = false) const;
95
96 //! \brief The ID for the event being replied to.
97 //!
98 //! \param includeFallbacks include thread fallback replies for non-threaded clients.
99 //!
100 //!
101 //! \return The event ID for a reply, this includes threaded replies where `"rel_type"`
102 //! is `"m.thread"` and `"is_falling_back"` is false (except where \p includeFallbacks is true).
103 QString replyEventId(bool includeFallbacks = false) const;
104
105 //! \brief The EventRelation for this event.
106 //!
107 //! \return an EventRelation object which can be checked for type if it exists,
108 //! std::nullopt otherwise.
109 std::optional<EventRelation> relatesTo() const;
110
111protected:
112 explicit RoomEvent(const QJsonObject& json);
113 void dumpTo(QDebug dbg) const override;
114
115private:
116 // RedactionEvent is an incomplete type here so we cannot inline
117 // constructors using it and also destructors (with 'using', in particular).
118 event_ptr_tt<RedactionEvent> _redactedBecause;
119
120 event_ptr_tt<EncryptedEvent> _originalEvent;
121};
122using RoomEventPtr = event_ptr_tt<RoomEvent>;
123using RoomEvents = EventsArray<RoomEvent>;
124using RoomEventsRange = std::ranges::subrange<RoomEvents::iterator>;
125
126//! \brief Determine whether a given event type is that of a state event
127QUOTIENT_API bool isStateEvent(const QString& eventTypeId);
128
129} // namespace Quotient
130Q_DECLARE_METATYPE(Quotient::RoomEvent*)
131Q_DECLARE_METATYPE(const Quotient::RoomEvent*)
#define QUO_BASE_EVENT(CppType_, BaseCppType_,...)
Supply event metatype information in base event types.
Definition event.h:408
#define QUOTIENT_API