libQuotient
A Qt library for building matrix clients
Loading...
Searching...
No Matches
roommember.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
7#include "uri.h"
8#include "avatar.h"
9
10#include <QtCore/QObject>
11#include <QtQmlIntegration/qqmlintegration.h>
12
13namespace Quotient {
14class Room;
15class RoomMemberEvent;
16
17//! \brief Representation of a user state in a room
18//!
19//! The class is intentionally a read-only data object that is effectively a wrapper around an
20//! `m.room.member` event for the desired user. This is designed to provide the data in a format
21//! ready for visualizing a user (avatar or name) in the context of the room it was generated in.
22//! This means that if a user has set a unique name or avatar for a particular room that is what
23//! will be returned.
24//!
25//! \note The RoomMember class is not intended for interacting with the user's profile.
26//! For that a Quotient::User object should be obtained from a Quotient::Connection as that
27//! has the support functions for modifying profile information.
28//! \warning RoomMember is a gadget class and should not be kept between syncs. It does not track
29//! changes of the member state therefore some member changes (i.e. leaving the room) may
30//! render a RoomMember dangling, when calling any of its methods leads to undefined
31//! behaviour.
32//! \sa User
37
53
54public:
55 RoomMember() = default;
56
57 explicit RoomMember(const Room* room, const RoomMemberEvent* member);
58
59 bool isEmpty() const { return _member == nullptr; }
60
61 bool operator==(const RoomMember& other) const;
62
63 //! @brief Get unique stable user id
64 //!
65 //! The Matrix user ID is generated by the server and is never changed.
66 QString id() const;
67
68 //! @brief The matrix.to URI for the user
69 //!
70 //! Typically used when you want to visit a user (see
71 //! Quotient::UriResolverBase::visitResource()).
72 //!
73 //! @sa Quotient::UriResolverBase::visitResource()
74 Uri uri() const;
75
76 //! Whether this member is the local user
77 bool isLocalMember() const;
78
79 //! The membership state of the member
81
82 //! \brief The raw unmodified display name for the user in the given room
83 //!
84 //! The value will be empty if no display name has been set.
85 //!
86 //! \warning This value is not sanitized or HTML escape so use appropriately.
87 //! For ready to display values use displayName() or fullName() for
88 //! plain text and htmlSafeDisplayName() or htmlSafeFullName() fo
89 //! rich text.
90 //!
91 //! \sa displayName(), htmlSafeDisplayName(), fullName(), htmlSafeFullName()
92 QString name() const;
93
94 //! \brief Get the user display name ready for display
95 //!
96 //! This function always aims to return something that can be displayed in a
97 //! UI, so if no display name is set the user's Matrix ID will be returned.
98 //!
99 //! The output is sanitized and suitable for a plain text field. For a rich
100 //! field use htmlSafeDisplayName().
101 //!
102 //! \sa htmlSafeDisplayName()
104
105 //! \brief Get the user display name ready for display
106 //!
107 //! This function always aims to return something that can be displayed in a
108 //! UI, so if no display name is set the user's Matrix ID will be returned.
109 //!
110 //! The output is sanitized and html escaped ready for a rich text field. For
111 //! a plain field use displayName().
112 //!
113 //! \sa displayName()
115
116 //! \brief Get user name and id in a single string
117 //!
118 //! This function always aims to return something that can be displayed in a
119 //! UI, so if no display name is set the just user's Matrix ID will be returned.
120 //! The constructed string follows the format 'name (id)' which the spec
121 //! recommends for users disambiguation in a room context and in other places.
122 //!
123 //! The output is sanitized and suitable for a plain text field. For a rich
124 //! field use htmlSafeFullName().
125 //!
126 //! \sa htmlSafeFullName()
128
129 //! \brief Get user name and id in a single string
130 //!
131 //! This function always aims to return something that can be displayed in a
132 //! UI, so if no display name is set the just user's Matrix ID will be returned.
133 //! The constructed string follows the format 'name (id)' which the spec
134 //! recommends for users disambiguation in a room context and in other places.
135 //!
136 //! The output is sanitized and html escaped ready for a rich text field. For
137 //! a plain field use fullName().
138 //!
139 //! \sa fullName()
141
142 //! \brief Get the disambiguated user name
143 //!
144 //! This function always aims to return something that can be displayed in a
145 //! UI, so if no display name is set the just user's Matrix ID will be returned.
146 //! The output is equivalent to fullName() if there is another user in the room
147 //! with the same name. Otherwise it is equivalent to displayName().
148 //!
149 //! The output is sanitized and suitable for a plain text field. For a rich
150 //! field use htmlSafeDisambiguatedName().
151 //!
152 //! \sa htmlSafeDisambiguatedName(), fullName(), displayName()
154
155 //! \brief Get the disambiguated user name
156 //!
157 //! This function always aims to return something that can be displayed in a
158 //! UI, so if no display name is set the just user's Matrix ID will be returned.
159 //! The output is equivalent to htmlSafeFullName() if there is another user in the room
160 //! with the same name. Otherwise it is equivalent to htmlSafeDisplayName().
161 //!
162 //! The output is sanitized and html escaped ready for a rich text field. For
163 //! a plain field use disambiguatedName().
164 //!
165 //! \sa disambiguatedName(), htmlSafeFullName(), htmlSafeDisplayName()
167
168 //! \brief Check whether the name or id of the member contains a substring
169 //!
170 //! This is useful for a predicate to filter room members.
171 //! \sa MemberMatcher
173
174 //! \brief Hue color component of this user based on the user's Matrix ID
175 //!
176 //! The implementation is based on XEP-0392:
177 //! https://xmpp.org/extensions/xep-0392.html
178 //! Naming and ranges are the same as QColor's hue methods:
179 //! https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
180 int hue() const;
181
182 //! \brief HueF color component of this user based on the user's Matrix ID
183 //!
184 //! The implementation is based on XEP-0392:
185 //! https://xmpp.org/extensions/xep-0392.html
186 //! Naming and ranges are the same as QColor's hue methods:
187 //! https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
188 qreal hueF() const;
189
190 //! \brief Color based on the user's Matrix ID
191 //!
192 //! See https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal
193 //! for the methodology.
194 QColor color() const;
195
196 const Avatar& avatarObject() const;
197
198 //! \brief The mxc URL as a string for the user avatar in the room
199 //!
200 //! This can be empty if none set.
202
203 //! \brief The mxc URL for the user avatar in the room
204 //!
205 //! This can be empty if none set.
207
209
211
212 //! \brief The power level of the member.
213 //!
214 //! This is in the context of the current room. Will return the default power
215 //! level for the room if not specifically set.
216 int powerLevel() const;
217
218private:
219 const Room* _room = nullptr;
220 const RoomMemberEvent* _member = nullptr;
221
222 qreal _hueF = 0;
223};
224
225//! \brief A factory to get a functional object matching room members against a substring
226//!
227//! This is a convenience wrapper to use RoomMember::matches() in standard algorithms.
228inline auto memberMatcher(auto substr, Qt::CaseSensitivity cs = Qt::CaseSensitive)
229{
230#ifdef __cpp_lib_bind_back
231 return std::bind_back(&RoomMember::matches, substr, cs);
232#else
233 return [substr, cs](const RoomMember& m) { return m.matches(substr, cs); };
234#endif
235}
236
238 bool operator()(const RoomMember& u1, const RoomMember& u2) const
239 {
240 return operator()(u1.displayName(), u2.displayName());
241 }
243};
244
245} // namespace Quotient
Representation of a user state in a room.
Definition roommember.h:33
auto memberMatcher(auto substr, Qt::CaseSensitivity cs=Qt::CaseSensitive)
A factory to get a functional object matching room members against a substring.
Definition roommember.h:228
#define QUOTIENT_API