From 0834b246ad1f802696e053aa465f4172326a431f Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Mon, 8 May 2017 19:44:01 +0300 Subject: [PATCH] Initial support for display names --- include/TimelineViewManager.h | 3 +++ src/ChatPage.cc | 14 ++++++++++++++ src/MatrixClient.cc | 7 +++---- src/TimelineItem.cc | 15 ++++++++++----- src/TimelineViewManager.cc | 9 +++++++++ 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/include/TimelineViewManager.h b/include/TimelineViewManager.h index 3c539305..20f40951 100644 --- a/include/TimelineViewManager.h +++ b/include/TimelineViewManager.h @@ -41,7 +41,10 @@ public: static QString chooseRandomColor(); static QString getUserColor(const QString &userid); + static QString displayName(const QString &userid); + static QMap NICK_COLORS; + static QMap DISPLAY_NAMES; signals: void unreadMessages(QString roomid, int count); diff --git a/src/ChatPage.cc b/src/ChatPage.cc index fbaf9ddd..8a9b29b0 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -23,6 +23,7 @@ #include "ChatPage.h" #include "Sync.h" +#include "TimelineViewManager.h" #include "UserInfoWidget.h" #include "AliasesEventContent.h" @@ -31,6 +32,7 @@ #include "CreateEventContent.h" #include "HistoryVisibilityEventContent.h" #include "JoinRulesEventContent.h" +#include "MemberEventContent.h" #include "NameEventContent.h" #include "PowerLevelsEventContent.h" #include "TopicEventContent.h" @@ -320,6 +322,18 @@ void ChatPage::updateRoomState(RoomState &room_state, const QJsonArray &events) room_state.name = name; break; } + case events::EventType::RoomMember: { + events::StateEvent member; + member.deserialize(event); + + auto display_name = member.content().displayName(); + + if (display_name.isEmpty()) + display_name = member.stateKey(); + + TimelineViewManager::DISPLAY_NAMES.insert(member.stateKey(), display_name); + break; + } case events::EventType::RoomPowerLevels: { events::StateEvent power_levels; power_levels.deserialize(event); diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index b14dcf92..86d43b1f 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -483,15 +483,14 @@ void MatrixClient::sendTextMessage(const QString &roomid, const QString &msg) no void MatrixClient::initialSync() noexcept { - QJsonArray excluded_event_types = { - QString("m.room.member"), + QJsonArray excluded_presence = { + QString("m.presence"), }; QJsonObject filter{{"room", QJsonObject{{"timeline", QJsonObject{{"limit", 70}}}, - {"state", QJsonObject{{"not_types", excluded_event_types}}}, {"ephemeral", QJsonObject{{"limit", 0}}}}}, - {"presence", QJsonObject{{"limit", 0}}}}; + {"presence", QJsonObject{{"not_types", excluded_presence}}}}; QUrlQuery query; query.addQueryItem("full_state", "true"); diff --git a/src/TimelineItem.cc b/src/TimelineItem.cc index 8d5e503a..c16293b8 100644 --- a/src/TimelineItem.cc +++ b/src/TimelineItem.cc @@ -20,6 +20,7 @@ #include "ImageItem.h" #include "TimelineItem.h" +#include "TimelineViewManager.h" namespace events = matrix::events; namespace msgs = matrix::events::messages; @@ -28,7 +29,7 @@ TimelineItem::TimelineItem(const QString &userid, const QString &color, const QS : QWidget(parent) { generateTimestamp(QDateTime::currentDateTime()); - generateBody(userid, color, body); + generateBody(TimelineViewManager::displayName(userid), color, body); setupLayout(); } @@ -45,7 +46,7 @@ TimelineItem::TimelineItem(ImageItem *image, const events::MessageEventsetMargin(0); @@ -87,7 +88,7 @@ TimelineItem::TimelineItem(const events::MessageEvent &event, bool body = "" + body + ""; if (with_sender) - generateBody(event.sender(), color, body); + generateBody(TimelineViewManager::displayName(event.sender()), color, body); else generateBody(body); @@ -103,7 +104,7 @@ TimelineItem::TimelineItem(const events::MessageEvent &event, bool w generateTimestamp(timestamp); if (with_sender) - generateBody(event.sender(), color, body); + generateBody(TimelineViewManager::displayName(event.sender()), color, body); else generateBody(body); @@ -131,7 +132,11 @@ void TimelineItem::generateBody(const QString &body) void TimelineItem::generateBody(const QString &userid, const QString &color, const QString &body) { - auto sender = userid.split(":")[0].split("@")[1]; + auto sender = userid; + + // TODO: Fix this by using a UserId type. + if (userid.split(":")[0].split("@").size() > 1) + sender = userid.split(":")[0].split("@")[1]; content_label_ = new QLabel(this); content_label_->setWordWrap(true); diff --git a/src/TimelineViewManager.cc b/src/TimelineViewManager.cc index bf3dd997..84bf20b2 100644 --- a/src/TimelineViewManager.cc +++ b/src/TimelineViewManager.cc @@ -129,6 +129,7 @@ void TimelineViewManager::setHistoryView(const QString &room_id) } QMap TimelineViewManager::NICK_COLORS; +QMap TimelineViewManager::DISPLAY_NAMES; QString TimelineViewManager::chooseRandomColor() { @@ -198,3 +199,11 @@ QString TimelineViewManager::getUserColor(const QString &userid) return color; } + +QString TimelineViewManager::displayName(const QString &userid) +{ + if (DISPLAY_NAMES.contains(userid)) + return DISPLAY_NAMES.value(userid); + + return userid; +}