Skip to content

Commit fabb3da

Browse files
committed
Connection::userIds() and Quotient::isGuestUserId()
The former one is a replacement of Connection::users() that was removed in #746 with no alternative provided; the latter one allows to check MXIDs without having to create a User object, as User objects are not something readily available from Connection (and besides, they were never necessary to check if a user id is that of a guest).
1 parent 5ac4d1f commit fabb3da

File tree

6 files changed

+23
-0
lines changed

6 files changed

+23
-0
lines changed

Quotient/connection.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,8 @@ void Connection::removeFromIgnoredUsers(const QString& userId)
13021302
}
13031303
}
13041304

1305+
QStringList Connection::userIds() const { return d->userMap.keys(); }
1306+
13051307
const ConnectionData* Connection::connectionData() const
13061308
{
13071309
return d->data.get();

Quotient/connection.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ class QUOTIENT_API Connection : public QObject {
283283
//! \sa ignoredUsersListChanged
284284
Q_INVOKABLE void removeFromIgnoredUsers(const QString& userId);
285285

286+
//! \brief Get the entire list of users known to the current user on this homeserver
287+
//! \note Be mindful that this can easily count thousands or tens of thousands, and use
288+
//! sparingly; when in a room context, always use Room::members() instead
289+
Q_INVOKABLE QStringList userIds() const;
290+
286291
//! Get the base URL of the homeserver to connect to
287292
QUrl homeserver() const;
288293
//! Get the domain name used for ids/aliases on the server

Quotient/connection_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Q_DECL_HIDDEN Quotient::Connection::Private {
3939
QHash<QString, QString> roomAliasMap;
4040
QVector<QString> roomIdsToForget;
4141
QVector<QString> pendingStateRoomIds;
42+
// It's an ordered map to make Connection::userIds() return an already sorted list
4243
QMap<QString, User*> userMap;
4344
std::unordered_map<QString, Avatar> userAvatarMap;
4445
DirectChatsMap directChats;

Quotient/util.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ int Quotient::minorVersion()
128128

129129
int Quotient::patchVersion() { return Quotient_VERSION_PATCH; }
130130

131+
bool Quotient::isGuestUserId(const UserId& uId)
132+
{
133+
static const QRegularExpression guestMxIdRe{ QStringLiteral("^@\\d+:") };
134+
return guestMxIdRe.match(uId).hasMatch();
135+
}
136+
131137
bool Quotient::HomeserverData::checkMatrixSpecVersion(QStringView targetVersion) const
132138
{
133139
// TODO: Replace this naïve implementation with something smarter that can check things like

Quotient/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ using UserId = QString;
429429
using RoomId = QString;
430430
using EventId = QString;
431431

432+
QUOTIENT_API bool isGuestUserId(const UserId& uId);
433+
432434
struct QUOTIENT_API HomeserverData {
433435
QUrl baseUrl;
434436
QStringList supportedSpecVersions;

autotests/utiltests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestUtils : public QObject {
2525
Q_OBJECT
2626
private Q_SLOTS:
2727
void testLinkifyUrl();
28+
void testIsGuestUserId();
2829

2930
private:
3031
void testLinkified(QString original, const QString& expected, const int sourceLine) const
@@ -69,5 +70,11 @@ void TestUtils::testLinkifyUrl()
6970
#undef T
7071
}
7172

73+
void TestUtils::testIsGuestUserId()
74+
{
75+
QVERIFY(isGuestUserId("@123:example.org"_ls));
76+
QVERIFY(!isGuestUserId("@normal:example.org"_ls));
77+
}
78+
7279
QTEST_APPLESS_MAIN(TestUtils)
7380
#include "utiltests.moc"

0 commit comments

Comments
 (0)