From 7cda502edd511c447790c02b787e0eb7748593b6 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 25 Nov 2020 22:45:33 +0100 Subject: [PATCH] Add some db logging and clean up avatar urls a bit --- src/Cache.cpp | 25 ++++++++++++------------- src/Cache.h | 2 +- src/Cache_p.h | 5 +---- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index 3a6b0ca9..bd0f3733 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -1164,9 +1164,7 @@ Cache::saveState(const mtx::responses::Sync &res) RoomInfo updatedInfo; updatedInfo.name = getRoomName(txn, statesdb, membersdb).toStdString(); updatedInfo.topic = getRoomTopic(txn, statesdb).toStdString(); - updatedInfo.avatar_url = - getRoomAvatarUrl(txn, statesdb, membersdb, QString::fromStdString(room.first)) - .toStdString(); + updatedInfo.avatar_url = getRoomAvatarUrl(txn, statesdb, membersdb).toStdString(); updatedInfo.version = getRoomVersion(txn, statesdb).toStdString(); // Process the account_data associated with this room @@ -1881,10 +1879,7 @@ Cache::invites() } QString -Cache::getRoomAvatarUrl(lmdb::txn &txn, - lmdb::dbi &statesdb, - lmdb::dbi &membersdb, - const QString &room_id) +Cache::getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb) { using namespace mtx::events; using namespace mtx::events::state; @@ -1912,14 +1907,17 @@ Cache::getRoomAvatarUrl(lmdb::txn &txn, auto cursor = lmdb::cursor::open(txn, membersdb); std::string user_id; std::string member_data; + std::string fallback_url; // Resolve avatar for 1-1 chats. while (cursor.get(user_id, member_data, MDB_NEXT)) { - if (user_id == localUserId_.toStdString()) - continue; try { MemberInfo m = json::parse(member_data); + if (user_id == localUserId_.toStdString()) { + fallback_url = m.avatar_url; + continue; + } cursor.close(); return QString::fromStdString(m.avatar_url); @@ -1931,7 +1929,7 @@ Cache::getRoomAvatarUrl(lmdb::txn &txn, cursor.close(); // Default case when there is only one member. - return avatarUrl(room_id, localUserId_); + return QString::fromStdString(fallback_url); } QString @@ -2291,7 +2289,8 @@ Cache::getMember(const std::string &room_id, const std::string &user_id) MemberInfo m = json::parse(std::string_view(info.data(), info.size())); return m; } - } catch (...) { + } catch (std::exception &e) { + nhlog::db()->warn("Failed to read member ({}): {}", user_id, e.what()); } return std::nullopt; } @@ -3841,9 +3840,9 @@ getRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb) return instance_->getRoomTopic(txn, statesdb); } QString -getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb, const QString &room_id) +getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb) { - return instance_->getRoomAvatarUrl(txn, statesdb, membersdb, room_id); + return instance_->getRoomAvatarUrl(txn, statesdb, membersdb); } QString diff --git a/src/Cache.h b/src/Cache.h index 573ab700..8cbb0006 100644 --- a/src/Cache.h +++ b/src/Cache.h @@ -92,7 +92,7 @@ QString getRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb); //! Retrieve the room avatar's url if any. QString -getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb, const QString &room_id); +getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb); //! Retrieve the version of the room if any. QString getRoomVersion(lmdb::txn &txn, lmdb::dbi &statesdb); diff --git a/src/Cache_p.h b/src/Cache_p.h index ffa7d509..9c919fb5 100644 --- a/src/Cache_p.h +++ b/src/Cache_p.h @@ -89,10 +89,7 @@ public: //! Retrieve the topic of the room if any. QString getRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb); //! Retrieve the room avatar's url if any. - QString getRoomAvatarUrl(lmdb::txn &txn, - lmdb::dbi &statesdb, - lmdb::dbi &membersdb, - const QString &room_id); + QString getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb); //! Retrieve the version of the room if any. QString getRoomVersion(lmdb::txn &txn, lmdb::dbi &statesdb);