libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 James Graham <james.h.graham@protonmail.com>
2// SPDX-FileCopyrightText: 2026 Alexey Rusakov <kitsune@users.sf.net>
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#pragma once
6
7#include "eventitem.h"
8
9#include <QtCore/QHash>
10
11namespace Quotient {
12class Room;
13
14//! \brief Simple data structure representing a thread
15//!
16//! ThreadInfo objects are managed by ThreadInfos, which stores them keyed by thread root event ids.
17//! The thread root id is not stored in ThreadInfo itself to avoid redundancy.
18//! \sa ThreadInfos
19struct QUOTIENT_API ThreadInfo {
20 QString latestEventId;
21 int size = 0;
22 bool localUserParticipated = {};
23};
24
25//! \brief A manager of thread information in a room
26//!
27//! ThreadInfos encapsulates thread management logic, storing threads keyed by their root event id.
28//! Every time an event arrives to the timeline the room should send it to updateFrom() to update
29//! the relevant thread info if there's any thread involved. Otherwise this class behaves as
30//! a read-only `QHash<QString, ThreadInfo>`
31//! \sa ThreadInfo
32class QUOTIENT_API ThreadInfos : private QHash<QString, ThreadInfo> {
33public:
34 using base_type = QHash<QString, ThreadInfo>;
35 using const_iterator = base_type::const_iterator;
36 using key_type = base_type::key_type;
37 using mapped_type = base_type::mapped_type;
38
39 using UpdateResult = std::pair<ThreadInfo, bool>;
40
41 //! \brief Update thread state for an event that just arrived to the timeline
42 //!
43 //! This method handles the full logic of updating thread state when a new event arrives:
44 //! it figures out if any thread should be created or updated, and then creates or updates
45 //! the respective ThreadInfo object according to the data in the event.
46 //! \note As of this writing, the event reference itself is not stored, only the aggregated
47 //! statistics.
48 //! \return A pair consisting of a ThreadInfo object that was created/updated, and a boolean
49 //! flag that is `true` if that object was created, `false` if it was updated
50 UpdateResult updateFrom(const TimelineItem &eventItem);
51
52 // Exposing the relevant read-only API of QHash
53
54 base_type::const_iterator begin() const { return base_type::begin(); }
55 base_type::const_iterator end() const { return base_type::end(); }
56 base_type::const_iterator find(const key_type &k) const { return base_type::find(k); }
57 mapped_type operator[](const key_type &k) const { return base_type::operator[](k); }
58
59 using base_type::cbegin, base_type::cend, base_type::constFind, base_type::value;
60 using base_type::contains, base_type::size, base_type::isEmpty;
61
62private:
63 Room* room = nullptr;
64
65 friend class Room;
66 void setRoom(Room* r) { room = r; }
67};
68
69} // namespace Quotient
#define QUOTIENT_API