mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-29 14:18:49 +03:00
update room and global avatar through user profile
This commit is contained in:
parent
0ebb2947ef
commit
c3e02240bf
3 changed files with 98 additions and 13 deletions
|
@ -31,7 +31,7 @@ ApplicationWindow {
|
||||||
displayName: profile.displayName
|
displayName: profile.displayName
|
||||||
userid: profile.userid
|
userid: profile.userid
|
||||||
Layout.alignment: Qt.AlignHCenter
|
Layout.alignment: Qt.AlignHCenter
|
||||||
onClicked: TimelineManager.openImageOverlay(TimelineManager.timeline.avatarUrl(userid), TimelineManager.timeline.data.id)
|
onClicked: profile.isSelf ? profile.changeAvatar() : TimelineManager.openImageOverlay(TimelineManager.timeline.avatarUrl(userid), TimelineManager.timeline.data.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
TextInput {
|
TextInput {
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
#include "UserProfile.h"
|
#include <QFileDialog>
|
||||||
|
#include <QImageReader>
|
||||||
|
#include <QMimeDatabase>
|
||||||
|
#include <QStandardPaths>
|
||||||
|
|
||||||
#include "Cache_p.h"
|
#include "Cache_p.h"
|
||||||
#include "ChatPage.h"
|
#include "ChatPage.h"
|
||||||
#include "DeviceVerificationFlow.h"
|
#include "DeviceVerificationFlow.h"
|
||||||
#include "Logging.h"
|
#include "Logging.h"
|
||||||
|
#include "UserProfile.h"
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
#include "mtx/responses/crypto.hpp"
|
#include "mtx/responses/crypto.hpp"
|
||||||
#include "timeline/TimelineModel.h"
|
#include "timeline/TimelineModel.h"
|
||||||
#include "timeline/TimelineViewManager.h"
|
#include "timeline/TimelineViewManager.h"
|
||||||
#include <mtx/responses.hpp>
|
|
||||||
#include <mtx/responses/common.hpp>
|
|
||||||
|
|
||||||
UserProfile::UserProfile(QString roomid,
|
UserProfile::UserProfile(QString roomid,
|
||||||
QString userid,
|
QString userid,
|
||||||
|
@ -260,15 +263,7 @@ UserProfile::changeUsername(QString username)
|
||||||
.toStdString();
|
.toStdString();
|
||||||
member.membership = mtx::events::state::Membership::Join;
|
member.membership = mtx::events::state::Membership::Join;
|
||||||
|
|
||||||
http::client()->send_state_event(
|
updateRoomMemberState(std::move(member));
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,3 +289,87 @@ UserProfile::setGlobalUsername(const QString &globalUser)
|
||||||
globalUsername = globalUser;
|
globalUsername = globalUser;
|
||||||
emit displayNameChanged();
|
emit displayNameChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
UserProfile::changeAvatar()
|
||||||
|
{
|
||||||
|
const QString picturesFolder =
|
||||||
|
QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
|
||||||
|
const QString fileName = QFileDialog::getOpenFileName(
|
||||||
|
nullptr, tr("Select an avatar"), picturesFolder, tr("All Files (*)"));
|
||||||
|
|
||||||
|
if (fileName.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
QMimeDatabase db;
|
||||||
|
QMimeType mime = db.mimeTypeForFile(fileName, QMimeDatabase::MatchContent);
|
||||||
|
|
||||||
|
const auto format = mime.name().split("/")[0];
|
||||||
|
|
||||||
|
QFile file{fileName, this};
|
||||||
|
if (format != "image") {
|
||||||
|
// displayErrorMessage(tr("The selected file is not an image"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
|
// displayErrorMessage(tr("Error while reading file: %1").arg(file.errorString()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto bin = file.peek(file.size());
|
||||||
|
const auto payload = std::string(bin.data(), bin.size());
|
||||||
|
const auto dimensions = QImageReader(&file).size();
|
||||||
|
|
||||||
|
// First we need to create a new mxc URI
|
||||||
|
// (i.e upload media to the Matrix content repository) for the new avatar.
|
||||||
|
http::client()->upload(
|
||||||
|
payload,
|
||||||
|
mime.name().toStdString(),
|
||||||
|
QFileInfo(fileName).fileName().toStdString(),
|
||||||
|
[this,
|
||||||
|
dimensions,
|
||||||
|
payload,
|
||||||
|
mimetype = mime.name().toStdString(),
|
||||||
|
size = payload.size(),
|
||||||
|
room_id = roomid_.toStdString(),
|
||||||
|
content = std::move(bin)](const mtx::responses::ContentURI &res,
|
||||||
|
mtx::http::RequestErr err) {
|
||||||
|
if (err) {
|
||||||
|
nhlog::ui()->error("Failed to upload image", err->matrix_error.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isGlobalUserProfile()) {
|
||||||
|
http::client()->set_avatar_url(
|
||||||
|
res.content_uri, [](mtx::http::RequestErr err) {
|
||||||
|
if (err) {
|
||||||
|
nhlog::ui()->error("Failed to set user avatar url",
|
||||||
|
err->matrix_error.error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// change room username
|
||||||
|
mtx::events::state::Member member;
|
||||||
|
member.display_name = cache::displayName(roomid_, userid_).toStdString();
|
||||||
|
member.avatar_url = res.content_uri;
|
||||||
|
member.membership = mtx::events::state::Membership::Join;
|
||||||
|
|
||||||
|
updateRoomMemberState(std::move(member));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
UserProfile::updateRoomMemberState(mtx::events::state::Member member)
|
||||||
|
{
|
||||||
|
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 update room member state : ",
|
||||||
|
err->matrix_error.error);
|
||||||
|
});
|
||||||
|
}
|
|
@ -4,6 +4,8 @@
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
#include <mtx/responses.hpp>
|
||||||
|
#include <mtx/responses/common.hpp>
|
||||||
|
|
||||||
namespace verification {
|
namespace verification {
|
||||||
Q_NAMESPACE
|
Q_NAMESPACE
|
||||||
|
@ -112,6 +114,7 @@ public:
|
||||||
Q_INVOKABLE void kickUser();
|
Q_INVOKABLE void kickUser();
|
||||||
Q_INVOKABLE void startChat();
|
Q_INVOKABLE void startChat();
|
||||||
Q_INVOKABLE void changeUsername(QString username);
|
Q_INVOKABLE void changeUsername(QString username);
|
||||||
|
Q_INVOKABLE void changeAvatar();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void userStatusChanged();
|
void userStatusChanged();
|
||||||
|
@ -121,6 +124,9 @@ signals:
|
||||||
protected slots:
|
protected slots:
|
||||||
void setGlobalUsername(const QString &globalUser);
|
void setGlobalUsername(const QString &globalUser);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void updateRoomMemberState(mtx::events::state::Member member);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString roomid_, userid_;
|
QString roomid_, userid_;
|
||||||
QString globalUsername;
|
QString globalUsername;
|
||||||
|
|
Loading…
Reference in a new issue