aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--room.cpp29
-rw-r--r--room.h6
2 files changed, 35 insertions, 0 deletions
diff --git a/room.cpp b/room.cpp
index bc8aaf1d..9caaf97e 100644
--- a/room.cpp
+++ b/room.cpp
@@ -20,6 +20,7 @@
#include <QtCore/QHash>
#include <QtCore/QJsonArray>
+#include <QtCore/QStringBuilder> // for efficient string concats (operator%)
#include <QtCore/QDebug>
#include "connection.h"
@@ -266,6 +267,34 @@ void Room::memberRenamed(User* user, QString oldName)
d->renameMember(user, oldName);
}
+QString Room::roomMembername(User *u) const
+{
+ // See the CS spec, section 11.2.2.3
+
+ QString username = u->name();
+ if (username.isEmpty())
+ return u->id();
+
+ // Get the list of users with the same display name. Most likely,
+ // there'll be one, but there's a chance there are more.
+ auto namesakes = d->membersMap.values(username);
+ if (namesakes.size() == 1)
+ return username;
+
+ // We expect a user to be a member of the room - but technically it is
+ // possible to invoke roomMemberName() even for non-members. In such case
+ // we return the name _with_ id, to stay on a safe side.
+ if ( !namesakes.contains(u) )
+ {
+ qWarning()
+ << "Room::roomMemberName(): user" << u->id()
+ << "is not a member of the room" << id();
+ }
+
+ // In case of more than one namesake, disambiguate with user id.
+ return username % " <" % u->id() % ">";
+}
+
void Room::addMessage(Event* event)
{
processMessageEvent(event);
diff --git a/room.h b/room.h
index 72a1f249..261ce44d 100644
--- a/room.h
+++ b/room.h
@@ -53,6 +53,12 @@ namespace QMatrixClient
Q_INVOKABLE QList<User*> users() const;
+ /**
+ * @brief Produces a disambiguated name for a given user in
+ * the context of the room.
+ */
+ Q_INVOKABLE QString roomMembername(User* u) const;
+
Q_INVOKABLE void addMessage( Event* event );
Q_INVOKABLE void addInitialState( State* state );
Q_INVOKABLE void updateData( const SyncRoomData& data );