From 6fae36abc404ffb7e6ae29c9edceda5231400f0a Mon Sep 17 00:00:00 2001 From: CH Chethan Reddy <40890937+Chethan2k1@users.noreply.github.com> Date: Sun, 28 Jun 2020 21:01:34 +0530 Subject: [WIP] Add Caching for users --- src/Cache.cpp | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) (limited to 'src/Cache.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index 0c692d07..5afeab06 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -2882,6 +2882,115 @@ Cache::statusMessage(const std::string &user_id) return status_msg; } +void +to_json(json &j, const UserCache &info) +{ + j["user_id"] = info.user_id; + j["is_user_verified"] = info.is_user_verified; + j["cross_verified"] = info.cross_verified; + j["keys"] = info.keys; +} + +void +from_json(const json &j, UserCache &info) +{ + info.user_id = j.at("user_id"); + info.is_user_verified = j.at("is_user_verified"); + info.cross_verified = j.at("cross_verified").get>(); + info.keys = j.at("keys").get(); +} + +UserCache +Cache::getUserCache(const std::string &user_id) +{ + lmdb::val verifiedVal; + + auto txn = lmdb::txn::begin(env_); + auto db = getUserCacheDb(txn); + auto res = lmdb::dbi_get(txn, db, lmdb::val(user_id), verifiedVal); + + UserCache verified_state; + if (res) { + verified_state = json::parse(std::string(verifiedVal.data(), verifiedVal.size())); + } + + txn.commit(); + + return verified_state; +} + +//! be careful when using make sure is_user_verified is not changed +int +Cache::setUserCache(const std::string &user_id, const UserCache &body) +{ + auto txn = lmdb::txn::begin(env_); + auto db = getUserCacheDb(txn); + + auto res = lmdb::dbi_put(txn, db, lmdb::val(user_id), lmdb::val(json(body).dump())); + + txn.commit(); + + return res; +} + +int +Cache::deleteUserCache(const std::string &user_id) +{ + auto txn = lmdb::txn::begin(env_); + auto db = getUserCacheDb(txn); + auto res = lmdb::dbi_del(txn, db, lmdb::val(user_id), nullptr); + + txn.commit(); + + return res; +} + +void +to_json(json &j, const DeviceVerifiedCache &info) +{ + j["user_id"] = info.user_id; + j["device_verified"] = info.device_verified; +} + +void +from_json(const json &j, DeviceVerifiedCache &info) +{ + info.user_id = j.at("user_id"); + info.device_verified = j.at("device_verified").get>(); +} + +DeviceVerifiedCache +Cache::getVerifiedCache(const std::string &user_id) +{ + lmdb::val verifiedVal; + + auto txn = lmdb::txn::begin(env_); + auto db = getDeviceVerifiedDb(txn); + auto res = lmdb::dbi_get(txn, db, lmdb::val(user_id), verifiedVal); + + DeviceVerifiedCache verified_state; + if (res) { + verified_state = json::parse(std::string(verifiedVal.data(), verifiedVal.size())); + } + + txn.commit(); + + return verified_state; +} + +int +Cache::setVerifiedCache(const std::string &user_id, const DeviceVerifiedCache &body) +{ + auto txn = lmdb::txn::begin(env_); + auto db = getDeviceVerifiedDb(txn); + + auto res = lmdb::dbi_put(txn, db, lmdb::val(user_id), lmdb::val(json(body).dump())); + + txn.commit(); + + return res; +} + void to_json(json &j, const RoomInfo &info) { @@ -3063,6 +3172,35 @@ statusMessage(const std::string &user_id) { return instance_->statusMessage(user_id); } +UserCache +getUserCache(const std::string &user_id) +{ + return instance_->getUserCache(user_id); +} + +int +setUserCache(const std::string &user_id, const UserCache &body) +{ + return instance_->setUserCache(user_id, body); +} + +int +deleteUserCache(const std::string &user_id) +{ + return instance_->deleteUserCache(user_id); +} + +DeviceVerifiedCache +getVerifiedCache(const std::string &user_id) +{ + return instance_->getVerifiedCache(user_id); +} + +int +setVerifiedCache(const std::string &user_id, const DeviceVerifiedCache &body) +{ + return instance_->setVerifiedCache(user_id, body); +} //! Load saved data for the display names & avatars. void -- cgit 1.5.1