summary refs log tree commit diff
path: root/src/Cache.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2020-06-08 01:45:24 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2020-06-08 01:59:05 +0200
commit96f4169be97715e6b6b45663492e3791ba21ae09 (patch)
tree618753677e69ac590aa2cad267dfd7d31c574bfd /src/Cache.cpp
parentSmooth scaling for images (diff)
downloadnheko-96f4169be97715e6b6b45663492e3791ba21ae09.tar.xz
Show presence and set custom status messages
Diffstat (limited to 'src/Cache.cpp')
-rw-r--r--src/Cache.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp
index 009cbabc..d9d1134e 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -952,6 +952,8 @@ Cache::saveState(const mtx::responses::Sync &res)
 
         saveInvites(txn, res.rooms.invite);
 
+        savePresence(txn, res.presence);
+
         removeLeftRooms(txn, res.rooms.leave);
 
         txn.commit();
@@ -1037,6 +1039,21 @@ Cache::saveInvite(lmdb::txn &txn,
         }
 }
 
+void
+Cache::savePresence(
+  lmdb::txn &txn,
+  const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presenceUpdates)
+{
+        for (const auto &update : presenceUpdates) {
+                auto presenceDb = getPresenceDb(txn);
+
+                lmdb::dbi_put(txn,
+                              presenceDb,
+                              lmdb::val(update.sender),
+                              lmdb::val(json(update.content).dump()));
+        }
+}
+
 std::vector<std::string>
 Cache::roomsWithStateUpdates(const mtx::responses::Sync &res)
 {
@@ -2254,6 +2271,50 @@ Cache::removeAvatarUrl(const QString &room_id, const QString &user_id)
         AvatarUrls.remove(fmt);
 }
 
+mtx::presence::PresenceState
+Cache::presenceState(const std::string &user_id)
+{
+        lmdb::val presenceVal;
+
+        auto txn = lmdb::txn::begin(env_);
+        auto db  = getPresenceDb(txn);
+        auto res = lmdb::dbi_get(txn, db, lmdb::val(user_id), presenceVal);
+
+        mtx::presence::PresenceState state = mtx::presence::offline;
+
+        if (res) {
+                mtx::events::presence::Presence presence =
+                  json::parse(std::string(presenceVal.data(), presenceVal.size()));
+                state = presence.presence;
+        }
+
+        txn.commit();
+
+        return state;
+}
+
+std::string
+Cache::statusMessage(const std::string &user_id)
+{
+        lmdb::val presenceVal;
+
+        auto txn = lmdb::txn::begin(env_);
+        auto db  = getPresenceDb(txn);
+        auto res = lmdb::dbi_get(txn, db, lmdb::val(user_id), presenceVal);
+
+        std::string status_msg;
+
+        if (res) {
+                mtx::events::presence::Presence presence =
+                  json::parse(std::string(presenceVal.data(), presenceVal.size()));
+                status_msg = presence.status_msg;
+        }
+
+        txn.commit();
+
+        return status_msg;
+}
+
 void
 to_json(json &j, const RoomInfo &info)
 {
@@ -2425,6 +2486,17 @@ insertAvatarUrl(const QString &room_id, const QString &user_id, const QString &a
         instance_->insertAvatarUrl(room_id, user_id, avatar_url);
 }
 
+mtx::presence::PresenceState
+presenceState(const std::string &user_id)
+{
+        return instance_->presenceState(user_id);
+}
+std::string
+statusMessage(const std::string &user_id)
+{
+        return instance_->statusMessage(user_id);
+}
+
 //! Load saved data for the display names & avatars.
 void
 populateMembers()