summary refs log tree commit diff
path: root/src/Cache.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2020-10-27 17:45:28 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2020-10-27 17:45:28 +0100
commit12fff7408ea7539d778a641bbf1746693d30ee2a (patch)
treeaf9ef46791e7f2e5dcd480f4c1cc91a97ee95a71 /src/Cache.cpp
parentFix small scope issue (diff)
downloadnheko-12fff7408ea7539d778a641bbf1746693d30ee2a.tar.xz
Optimize build
Diffstat (limited to 'src/Cache.cpp')
-rw-r--r--src/Cache.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp
index 993fbfe7..f187af62 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -3377,6 +3377,46 @@ Cache::markUserKeysOutOfDate(lmdb::txn &txn,
 }
 
 void
+Cache::query_keys(const std::string &user_id,
+                  std::function<void(const UserKeyCache &, mtx::http::RequestErr)> cb)
+{
+        auto cache_ = cache::userKeys(user_id);
+
+        if (cache_.has_value()) {
+                if (!cache_->updated_at.empty() && cache_->updated_at == cache_->last_changed) {
+                        cb(cache_.value(), {});
+                        return;
+                }
+        }
+
+        mtx::requests::QueryKeys req;
+        req.device_keys[user_id] = {};
+
+        std::string last_changed;
+        if (cache_)
+                last_changed = cache_->last_changed;
+        req.token = last_changed;
+
+        http::client()->query_keys(req,
+                                   [cb, user_id, last_changed](const mtx::responses::QueryKeys &res,
+                                                               mtx::http::RequestErr err) {
+                                           if (err) {
+                                                   nhlog::net()->warn(
+                                                     "failed to query device keys: {},{}",
+                                                     err->matrix_error.errcode,
+                                                     static_cast<int>(err->status_code));
+                                                   cb({}, err);
+                                                   return;
+                                           }
+
+                                           cache::updateUserKeys(last_changed, res);
+
+                                           auto keys = cache::userKeys(user_id);
+                                           cb(keys.value_or(UserKeyCache{}), err);
+                                   });
+}
+
+void
 to_json(json &j, const VerificationCache &info)
 {
         j["device_verified"] = info.device_verified;