fix users avatars

This commit is contained in:
Andrei Yankovich 2021-03-14 11:51:21 +03:00
parent 14cd487e89
commit d6815f1cd5
18 changed files with 90 additions and 38 deletions

View File

@ -129,7 +129,7 @@ bool HanoiServer::workWirthUserData(const UserData *obj,
return false;
}
return sendData(userData.data(), sender->networkAddress());
return sendData(userData.data(), sender->networkAddress(), oldHeader);
}
void HanoiServer::updateUserInWorld(const UserData *data) {

View File

@ -45,7 +45,7 @@ BackEnd::BackEnd(QQmlApplicationEngine *engine):
_recordsTable = new RecordListModel(this);
_world = new RecordListModel(this);
_imageProvider = new HanoiImageProvider(&_profile);
_imageProvider = new HanoiImageProvider(_client->getUsersCache());
_dataConverter = new DataConverter;
_loginModel->setComponents(LoginView::Nickname |
@ -321,7 +321,7 @@ void BackEnd::setNewAvatar(QString pathToAvatar) {
img.save(&buf, "PNG");
buf.close();
_profile.setAvatar(arr);
_client->setNewAvatar(_profile.getId().toString(), arr);
}

View File

@ -54,6 +54,20 @@ QSharedPointer<UserData> DataConverter::toUserDataPtr(const LocalUser &input) {
return result;
}
QSharedPointer<LocalUser> DataConverter::toLocalUser(const QSharedPointer<UserData> &input) {
return toLocalUser(*input.data());
}
QSharedPointer<LocalUser> DataConverter::toLocalUser(const UserData &input) {
auto result = QSharedPointer<LocalUser>::create();
result->setId(input.getId());
result->setUpdateTime(input.updateTime());
result->setUserData(input.userData());
result->setToken(input.getSignToken());
return result;
}
QH::PKG::UserMember DataConverter::toUserMember(const QSharedPointer<LocalUser> &input) {
return toUserMember(*input.data());
}

View File

@ -27,6 +27,9 @@ public:
static QSharedPointer<UserData> toUserDataPtr(const QSharedPointer<LocalUser>& input);
static QSharedPointer<UserData> toUserDataPtr(const LocalUser& input);
static QSharedPointer<LocalUser> toLocalUser(const QSharedPointer<UserData>& input);
static QSharedPointer<LocalUser> toLocalUser(const UserData& input);
static QH::PKG::UserMember toUserMember(const QSharedPointer<LocalUser>& input);
static QH::PKG::UserMember toUserMember(const LocalUser& input);

View File

@ -159,13 +159,18 @@ bool HanoiClient::isOnlineAndLoginned(const QSharedPointer<LocalUser> &data) {
}
void HanoiClient::updateLocalCache(const QSharedPointer<LocalUser>& localUser) {
_usersCache[localUser->getId().toString()] = localUser;
emit userDataChanged(localUser);
}
bool HanoiClient::workWithUserData(const QSharedPointer<UserData>& obj) {
auto userId = getMember().getId();
QSharedPointer<LocalUser> localUser;
if (userId == obj->getId()) {
localUser = getLocalUser(obj->getId().toString());
QSharedPointer<LocalUser> localUser = getLocalUser(obj->getId().toString());
if (obj->updateTime() > localUser->updateTime()) {
localUser->setUserData(obj->userData());
@ -178,18 +183,21 @@ bool HanoiClient::workWithUserData(const QSharedPointer<UserData>& obj) {
QmlNotificationService::NotificationData::Normal);
}
emit userDataChanged(localUser);
} else {
localUser = QSharedPointer<LocalUser>::create();
localUser->setUserData(obj->userData());
_usersCache[obj->getId().toString()] = localUser;
updateLocalCache(DataConverter::toLocalUser(obj));
}
emit userDataChanged(localUser);
return true;
}
const QHash<QString, QSharedPointer<LocalUser>>*
HanoiClient::getUsersCache() const {
return &_usersCache;
}
bool HanoiClient::setNewAvatar(const QString &userId, const QByteArray &image) {
auto profile = LocalUser();
@ -204,6 +212,7 @@ bool HanoiClient::setNewAvatar(const QString &userId, const QByteArray &image) {
}
obj->setAvatar(image);
updateLocalCache(obj);
if (isOnlineAndLoginned(obj)) {
return sendUserData(DataConverter::toUserDataPtr(obj));
@ -224,6 +233,9 @@ bool HanoiClient::subscribeToWorld() {
}
bool HanoiClient::getUserData(const QString& userId) {
if (userId.isEmpty())
return false;
if (_usersCache.contains(userId)) {
emit userDataChanged(_usersCache[userId]);
return true;
@ -270,6 +282,8 @@ bool HanoiClient::setProfile(const QString &userId,
*selectedProfileData = user;
}
_usersCache[user->getId().toString()] = user;
emit userDataChanged(user);
resetUser();
@ -278,6 +292,7 @@ bool HanoiClient::setProfile(const QString &userId,
login(userMember);
}
return true;
}

View File

@ -64,6 +64,13 @@ public:
*/
bool getUserData(const QString &userId);
/**
* @brief getUsersCache return a pointer to the cache of users.
* @return
*/
const QHash<QString, QSharedPointer<LocalUser> >*
getUsersCache() const;
protected:
QStringList SQLSources() const override;
QH::HostAddress serverAddress() const override;
@ -89,6 +96,7 @@ private:
bool isOnlineAndLoginned(const QSharedPointer<LocalUser>& data);
bool workWithUserData(const QSharedPointer<UserData> & userData);
void updateLocalCache(const QSharedPointer<LocalUser> &localUser);
QHash<QString, QSharedPointer<LocalUser>> _usersCache;
QSharedPointer<WorldClient> _world;

View File

@ -12,13 +12,13 @@
#include <QThread>
#include <QThreadPool>
HanoiImageProvider::HanoiImageProvider(const LocalUser *user) {
HanoiImageProvider::HanoiImageProvider(const QHash<QString, QSharedPointer<LocalUser> > *cache) {
_pool = new QThreadPool();
_currentUser = user;
_cache = cache;
}
void HanoiImageProvider::stop() {
_currentUser = nullptr;
_cache = nullptr;
}
HanoiImageProvider::~HanoiImageProvider() {
@ -28,15 +28,15 @@ HanoiImageProvider::~HanoiImageProvider() {
QQuickImageResponse *HanoiImageProvider::requestImageResponse(const QString &id,
const QSize &requestedSize) {
AsyncImageResponse *response = new AsyncImageResponse(id, requestedSize, _currentUser);
AsyncImageResponse *response = new AsyncImageResponse(id, requestedSize, _cache);
_pool->start(response);
return response;
}
AsyncImageResponse::AsyncImageResponse(const QString &id, const QSize &requestedSize,
const LocalUser *client)
: m_id(id), m_requestedSize(requestedSize), m_texture(0), _currentUser(client) {
const QHash<QString, QSharedPointer<LocalUser> > *cache)
: m_id(id), m_requestedSize(requestedSize), m_texture(0), _cache(cache) {
setAutoDelete(false);
}
@ -45,12 +45,18 @@ QQuickTextureFactory *AsyncImageResponse::textureFactory() const {
}
void AsyncImageResponse::run() {
if (!_currentUser) {
if (!_cache) {
emit finished();
return;
}
QByteArray imageData = _currentUser->avatarData();
auto ids = m_id.split(":");
QByteArray imageData;
if (ids.size() && _cache->contains(ids[0])) {
imageData = _cache->value(ids[0])->avatarData();
}
QImage image;
if (imageData.size()) {

View File

@ -17,7 +17,9 @@ class LocalUser;
class AsyncImageResponse : public QQuickImageResponse, public QRunnable
{
public:
AsyncImageResponse(const QString &id, const QSize &requestedSize, const LocalUser *client);
AsyncImageResponse(const QString &id,
const QSize &requestedSize,
const QHash<QString, QSharedPointer<LocalUser>>* cache);
QQuickTextureFactory *textureFactory() const override;
@ -28,14 +30,14 @@ class AsyncImageResponse : public QQuickImageResponse, public QRunnable
QQuickTextureFactory *m_texture;
private:
const LocalUser *_currentUser = nullptr;
const QHash<QString, QSharedPointer<LocalUser>>* _cache = nullptr;
};
class HanoiImageProvider: public QQuickAsyncImageProvider
{
public:
HanoiImageProvider(const LocalUser *user);
HanoiImageProvider(const QHash<QString, QSharedPointer<LocalUser>> *cache);
void stop();
~HanoiImageProvider() override;
@ -43,7 +45,7 @@ public:
private:
QThreadPool *_pool = nullptr;
const LocalUser *_currentUser = nullptr;
const QHash<QString, QSharedPointer<LocalUser>>* _cache = nullptr;
};
#endif // HANOIIMAGEPROVIDER_H

View File

@ -191,7 +191,7 @@
<translation>Local user accept new data from the server.</translation>
</message>
<message>
<location filename="../hanoiclient.cpp" line="352"/>
<location filename="../hanoiclient.cpp" line="363"/>
<source>Internal Error, server send invalid data, and this data can&apos;t be saved into local database.</source>
<translation>Internal Error, server send invalid data, and this data can&apos;t be saved into local database.</translation>
</message>

View File

@ -202,7 +202,7 @@
<translation>NBEWデータを受け入れます</translation>
</message>
<message>
<location filename="../hanoiclient.cpp" line="352"/>
<location filename="../hanoiclient.cpp" line="363"/>
<source>Internal Error, server send invalid data, and this data can&apos;t be saved into local database.</source>
<translation></translation>
</message>

View File

@ -202,7 +202,7 @@
<translation>Локальный пользователь принял новые данные с сервера.</translation>
</message>
<message>
<location filename="../hanoiclient.cpp" line="352"/>
<location filename="../hanoiclient.cpp" line="363"/>
<source>Internal Error, server send invalid data, and this data can&apos;t be saved into local database.</source>
<translation>Внутренняя ошибка, сервер отправил неверные данные, и эти данные не могут быть сохранены в локальной базе данных.</translation>
</message>

View File

@ -203,7 +203,7 @@ Bu seviye için minimum adımlar: %2</translation>
<translation>yerel kullanıcı sunucudan yeni verileri kabul eder.</translation>
</message>
<message>
<location filename="../hanoiclient.cpp" line="352"/>
<location filename="../hanoiclient.cpp" line="363"/>
<source>Internal Error, server send invalid data, and this data can&apos;t be saved into local database.</source>
<translation>Dahili Hata, sunucu geçersiz veri gönderdi ve bu veriler yerel veritabanına kaydedilemez.</translation>
</message>

View File

@ -203,7 +203,7 @@
<translation>локальний користувач приймає нові дані з сервера.</translation>
</message>
<message>
<location filename="../hanoiclient.cpp" line="352"/>
<location filename="../hanoiclient.cpp" line="363"/>
<source>Internal Error, server send invalid data, and this data can&apos;t be saved into local database.</source>
<translation>Внутрішня помилка, сервер надсилає недійсні дані, і ці дані не можна зберегти в локальній базі даних.</translation>
</message>

View File

@ -163,6 +163,8 @@ ProfileData *LocalUser::userData() {
void LocalUser::setUserData(const ProfileData &userData) {
_userData = userData;
_avatarHash = qHash(_userData._avatar);
}
QH::AccessToken LocalUser::token() const {
@ -205,11 +207,13 @@ void LocalUser::setRecord(int record) {
}
void LocalUser::setAvatar(const QByteArray& avatar) {
if (_userData._avatar == avatar)
auto hash = qHash(avatar);
if (_avatarHash == hash)
return;
_userData._avatar = avatar;
_avatarHash = qHash(avatar);
_avatarHash = hash;
emit avatarChanged(_avatarHash);
}

View File

@ -22,7 +22,7 @@ GridLayout {
Layout.rowSpan: 1
Image {
id: userAvatar
source: "image://HanoiImages/" + ((userModel)? userModel.avatarHash: "")
source: "image://HanoiImages/" + ((userModel)? userModel.userId + ":" + userModel.avatarHash: "")
anchors.fill: parent
fillMode: Image.PreserveAspectFit

View File

@ -46,6 +46,10 @@ Item {
id: privateRoot
function showUser(id) {
if (backEnd)
backEnd.selectUserFromWorldTable(id);
preview.open()
}
}

2
Heart

@ -1 +1 @@
Subproject commit ec8c160ed3dc6dda9f88a564279b8fa97db6d936
Subproject commit 7b4fab59425e740d0deabcf4fe95dec6c094146d

View File

@ -26,12 +26,8 @@ parts:
- libasn1-8-heimdal
- libgssapi3-heimdal
- libldap-2.4-2
after: [libsgl, hanoi-tower-wrapper, qtlib]
after: [libsgl, qtlib]
hanoi-tower-wrapper:
plugin: dump
source: QtNetworkProtocol/QuasarAppLib/Etalons/snap/wrapper
qtlib:
plugin: dump