summary refs log tree commit diff
path: root/src/Cache.cc
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-05-16 23:30:50 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-05-16 23:30:50 +0300
commit791a9d0a4d04a6340e04a303d5e055acfd8ed53a (patch)
tree130969532fa174118224861a20029c4282fc7e92 /src/Cache.cc
parentAdd menu to modify the name & topic of the room (diff)
downloadnheko-791a9d0a4d04a6340e04a303d5e055acfd8ed53a.tar.xz
Hide the edit menu when the user doesn't have enough power
Diffstat (limited to 'src/Cache.cc')
-rw-r--r--src/Cache.cc39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/Cache.cc b/src/Cache.cc

index 31e86a69..c055ab05 100644 --- a/src/Cache.cc +++ b/src/Cache.cc
@@ -15,6 +15,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <limits> #include <stdexcept> #include <QByteArray> @@ -1223,6 +1224,44 @@ Cache::isNotificationSent(const std::string &event_id) return res; } +bool +Cache::hasEnoughPowerLevel(const std::vector<mtx::events::EventType> &eventTypes, + const std::string &room_id, + const std::string &user_id) +{ + using namespace mtx::events; + using namespace mtx::events::state; + + auto txn = lmdb::txn::begin(env_); + auto db = getStatesDb(txn, room_id); + + uint16_t min_event_level = std::numeric_limits<uint16_t>::max(); + uint16_t user_level = std::numeric_limits<uint16_t>::min(); + + lmdb::val event; + bool res = lmdb::dbi_get(txn, db, lmdb::val(to_string(EventType::RoomPowerLevels)), event); + + if (res) { + try { + StateEvent<PowerLevels> msg = + json::parse(std::string(event.data(), event.size())); + + user_level = msg.content.user_level(user_id); + + for (const auto &ty : eventTypes) + min_event_level = + std::min(min_event_level, + (uint16_t)msg.content.state_level(to_string(ty))); + } catch (const json::exception &e) { + qWarning() << "hasEnoughPowerLevel: " << e.what(); + } + } + + txn.commit(); + + return user_level >= min_event_level; +} + QHash<QString, QString> Cache::DisplayNames; QHash<QString, QString> Cache::AvatarUrls;