diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp
index 08219a38..3872294a 100644
--- a/src/ui/UserProfile.cpp
+++ b/src/ui/UserProfile.cpp
@@ -7,6 +7,8 @@
#include "mtx/responses/crypto.hpp"
#include "timeline/TimelineModel.h"
#include "timeline/TimelineViewManager.h"
+#include <mtx/responses.hpp>
+#include <mtx/responses/common.hpp>
UserProfile::UserProfile(QString roomid,
QString userid,
@@ -44,6 +46,23 @@ UserProfile::UserProfile(QString roomid,
}
deviceList_.reset(deviceList_.deviceList_);
});
+
+ connect(this,
+ &UserProfile::globalUsernameRetrieved,
+ this,
+ &UserProfile::setGlobalUsername,
+ Qt::QueuedConnection);
+
+ http::client()->get_profile(
+ userid_.toStdString(),
+ [this](const mtx::responses::Profile &res, mtx::http::RequestErr err) {
+ if (err) {
+ nhlog::net()->warn("failed to retrieve own profile info");
+ return;
+ }
+
+ emit globalUsernameRetrieved(QString::fromStdString(res.display_name));
+ });
}
QHash<int, QByteArray>
@@ -97,7 +116,7 @@ UserProfile::userid()
QString
UserProfile::displayName()
{
- return cache::displayName(roomid_, userid_);
+ return isGlobalUserProfile() ? globalUsername : cache::displayName(roomid_, userid_);
}
QString
@@ -107,6 +126,12 @@ UserProfile::avatarUrl()
}
bool
+UserProfile::isGlobalUserProfile() const
+{
+ return roomid_ == "";
+}
+
+bool
UserProfile::getUserStatus()
{
return isUserVerified;
@@ -214,6 +239,40 @@ UserProfile::startChat()
}
void
+UserProfile::changeUsername(QString username)
+{
+ if (isGlobalUserProfile()) {
+ // change global
+ http::client()->set_displayname(
+ username.toStdString(), [this](mtx::http::RequestErr err) {
+ if (err) {
+ nhlog::net()->warn("could not change username");
+ return;
+ }
+ });
+ } else {
+ // change room username
+ mtx::events::state::Member member;
+ member.display_name = username.toStdString();
+ member.avatar_url =
+ cache::avatarUrl(roomid_,
+ QString::fromStdString(http::client()->user_id().to_string()))
+ .toStdString();
+ member.membership = mtx::events::state::Membership::Join;
+
+ http::client()->send_state_event(
+ roomid_.toStdString(),
+ http::client()->user_id().to_string(),
+ member,
+ [](mtx::responses::EventId, mtx::http::RequestErr err) {
+ if (err)
+ nhlog::net()->error("Failed to set room displayname: {}",
+ err->matrix_error.error);
+ });
+ }
+}
+
+void
UserProfile::verify(QString device)
{
if (!device.isEmpty())
@@ -228,3 +287,10 @@ UserProfile::unverify(QString device)
{
cache::markDeviceUnverified(userid_.toStdString(), device.toStdString());
}
+
+void
+UserProfile::setGlobalUsername(const QString &globalUser)
+{
+ globalUsername = globalUser;
+ emit displayNameChanged();
+}
\ No newline at end of file
|