summary refs log tree commit diff
path: root/src/Cache.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2021-12-30 04:54:03 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2021-12-30 04:54:03 +0100
commit9a9dbda571a674ecfbf23533c64ec9cb095479ef (patch)
tree3e4ac7250ec9bedc7285c5aec144eb496a08e62e /src/Cache.cpp
parentConvert blurhash image format on worker thread (diff)
downloadnheko-9a9dbda571a674ecfbf23533c64ec9cb095479ef.tar.xz
Update presence dynamically and reduce allocations
Diffstat (limited to 'src/Cache.cpp')
-rw-r--r--src/Cache.cpp52
1 files changed, 10 insertions, 42 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp
index b197353e..eec5b79f 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -3897,53 +3897,26 @@ Cache::avatarUrl(const QString &room_id, const QString &user_id)
     return QString();
 }
 
-mtx::presence::PresenceState
-Cache::presenceState(const std::string &user_id)
+mtx::events::presence::Presence
+Cache::presence(const std::string &user_id)
 {
     if (user_id.empty())
         return {};
 
     std::string_view presenceVal;
 
-    auto txn = lmdb::txn::begin(env_);
-    auto db  = getPresenceDb(txn);
-    auto res = db.get(txn, user_id, presenceVal);
-
-    mtx::presence::PresenceState state = mtx::presence::offline;
-
-    if (res) {
-        mtx::events::presence::Presence presence =
-          json::parse(std::string_view(presenceVal.data(), presenceVal.size()));
-        state = presence.presence;
-    }
-
-    txn.commit();
-
-    return state;
-}
-
-std::string
-Cache::statusMessage(const std::string &user_id)
-{
-    if (user_id.empty())
-        return {};
-
-    std::string_view presenceVal;
-
-    auto txn = lmdb::txn::begin(env_);
+    auto txn = ro_txn(env_);
     auto db  = getPresenceDb(txn);
     auto res = db.get(txn, user_id, presenceVal);
 
-    std::string status_msg;
+    mtx::events::presence::Presence presence_{};
+    presence_.presence = mtx::presence::PresenceState::offline;
 
     if (res) {
-        mtx::events::presence::Presence presence = json::parse(presenceVal);
-        status_msg                               = presence.status_msg;
+        presence_ = json::parse(std::string_view(presenceVal.data(), presenceVal.size()));
     }
 
-    txn.commit();
-
-    return status_msg;
+    return presence_;
 }
 
 void
@@ -4747,17 +4720,12 @@ avatarUrl(const QString &room_id, const QString &user_id)
     return instance_->avatarUrl(room_id, user_id);
 }
 
-mtx::presence::PresenceState
-presenceState(const std::string &user_id)
+mtx::events::presence::Presence
+presence(const std::string &user_id)
 {
     if (!instance_)
         return {};
-    return instance_->presenceState(user_id);
-}
-std::string
-statusMessage(const std::string &user_id)
-{
-    return instance_->statusMessage(user_id);
+    return instance_->presence(user_id);
 }
 
 // user cache stores user keys