From e0c01b6fee5027211bd44a39860e40a2b7b226ad Mon Sep 17 00:00:00 2001 From: Black Hat Date: Sun, 29 Dec 2019 21:29:16 +0800 Subject: Add RoomPowerLevelEvent type --- lib/events/roompowerlevelsevent.cpp | 78 +++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 lib/events/roompowerlevelsevent.cpp (limited to 'lib') diff --git a/lib/events/roompowerlevelsevent.cpp b/lib/events/roompowerlevelsevent.cpp new file mode 100644 index 00000000..072f7a95 --- /dev/null +++ b/lib/events/roompowerlevelsevent.cpp @@ -0,0 +1,78 @@ +#include "roompowerlevelsevent.h" + +#include + +using namespace Quotient; + +inline QHash qVariantHashToQHash(const QVariantHash& vHash) { + QHash hash; + + QVariantHash::const_iterator i = vHash.constBegin(); + + while (i != vHash.constEnd()) { + hash.insert(i.key(), i.value().toInt()); + ++i; + } + + return hash; +} + +inline QJsonObject qHashToJson(const QHash& hash) { + QJsonObject json; + + QHash::const_iterator i = hash.constBegin(); + + while (i != hash.constEnd()) { + json.insert(i.key(), i.value()); + ++i; + } + + return json; +} + +PowerLevelsEventContent::PowerLevelsEventContent(const QJsonObject& json) : + invite(json["invite"_ls].toInt(50)), + kick(json["kick"_ls].toInt(50)), + ban(json["ban"_ls].toInt(50)), + redact(json["redact"_ls].toInt(50)), + events(qVariantHashToQHash(json["events"_ls].toVariant().toHash())), + eventsDefault(json["events_default"_ls].toInt(0)), + stateDefault(json["state_default"_ls].toInt(0)), + users(qVariantHashToQHash(json["users"_ls].toVariant().toHash())), + usersDefault(json["users_default"_ls].toInt(0)), + notifications(Notifications{json["notifications"_ls]["room"_ls].toInt(50)}) +{ +} + +void PowerLevelsEventContent::fillJson(QJsonObject* o) const { + o->insert(QStringLiteral("invite"), invite); + o->insert(QStringLiteral("kick"), kick); + o->insert(QStringLiteral("ban"), ban); + o->insert(QStringLiteral("redact"), redact); + o->insert(QStringLiteral("events"), qHashToJson(events)); + o->insert(QStringLiteral("events_default"), eventsDefault); + o->insert(QStringLiteral("state_default"), stateDefault); + o->insert(QStringLiteral("users"), qHashToJson(users)); + o->insert(QStringLiteral("users_default"), usersDefault); + o->insert(QStringLiteral("notifications"), QJsonObject{{"room", notifications.room}}); +} + +int RoomPowerLevelsEvent::powerLevelForEvent(const QString &eventId) { + auto e = events(); + + if (e.contains(eventId)) { + return e[eventId]; + } + + return eventsDefault(); +} + +int RoomPowerLevelsEvent::powerLevelForUser(const QString &userId) { + auto u = users(); + + if (u.contains(userId)) { + return u[userId]; + } + + return usersDefault(); +} -- cgit v1.2.3 From 52fdd715d605f201331a88ffb1d9caafcf86beb1 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Sun, 29 Dec 2019 21:43:35 +0800 Subject: Add missing file --- lib/events/roompowerlevelsevent.h | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 lib/events/roompowerlevelsevent.h (limited to 'lib') diff --git a/lib/events/roompowerlevelsevent.h b/lib/events/roompowerlevelsevent.h new file mode 100644 index 00000000..3a940380 --- /dev/null +++ b/lib/events/roompowerlevelsevent.h @@ -0,0 +1,75 @@ +#pragma once + +#include "eventcontent.h" +#include "stateevent.h" + +namespace Quotient { +class PowerLevelsEventContent : public EventContent::Base { +public: + struct Notifications { + int room; + }; + + explicit PowerLevelsEventContent(const QJsonObject& json); + + int invite; + int kick; + int ban; + + int redact; + + QHash events; + int eventsDefault; + int stateDefault; + + QHash users; + int usersDefault; + + Notifications notifications; + +protected: + void fillJson(QJsonObject* o) const override; +}; + +class RoomPowerLevelsEvent : public StateEvent { + Q_GADGET +public: + DEFINE_EVENT_TYPEID("m.room.power_levels", RoomPowerLevelsEvent) + + explicit RoomPowerLevelsEvent(const QJsonObject& obj) + : StateEvent(typeId(), obj) + {} + + int invite() { return content().invite; } + int kick() { return content().kick; } + int ban() { return content().ban; } + + int redact() { return content().redact; } + + QHash events() { return content().events; } + int eventsDefault() { return content().eventsDefault; } + int stateDefault() { return content().stateDefault; } + + QHash users() { return content().users; } + int usersDefault() { return content().usersDefault; } + + int roomNotification() { return content().notifications.room; } + + int powerLevelForEvent(const QString& eventId); + int powerLevelForUser(const QString& userId); + +private: +}; + +template <> +class EventFactory { +public: + static event_ptr_tt make(const QJsonObject& json, + const QString&) + { + return makeEvent(json); + } +}; + +REGISTER_EVENT_TYPE(RoomPowerLevelsEvent) +} // namespace Quotient -- cgit v1.2.3 From 00921320a407beaae62d01dbcf1550c3334a9258 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Mon, 30 Dec 2019 21:55:01 +0800 Subject: Simplify code --- lib/events/roompowerlevelsevent.cpp | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) (limited to 'lib') diff --git a/lib/events/roompowerlevelsevent.cpp b/lib/events/roompowerlevelsevent.cpp index 072f7a95..a6efaa63 100644 --- a/lib/events/roompowerlevelsevent.cpp +++ b/lib/events/roompowerlevelsevent.cpp @@ -4,41 +4,15 @@ using namespace Quotient; -inline QHash qVariantHashToQHash(const QVariantHash& vHash) { - QHash hash; - - QVariantHash::const_iterator i = vHash.constBegin(); - - while (i != vHash.constEnd()) { - hash.insert(i.key(), i.value().toInt()); - ++i; - } - - return hash; -} - -inline QJsonObject qHashToJson(const QHash& hash) { - QJsonObject json; - - QHash::const_iterator i = hash.constBegin(); - - while (i != hash.constEnd()) { - json.insert(i.key(), i.value()); - ++i; - } - - return json; -} - PowerLevelsEventContent::PowerLevelsEventContent(const QJsonObject& json) : invite(json["invite"_ls].toInt(50)), kick(json["kick"_ls].toInt(50)), ban(json["ban"_ls].toInt(50)), redact(json["redact"_ls].toInt(50)), - events(qVariantHashToQHash(json["events"_ls].toVariant().toHash())), + events(fromJson>(json["events"_ls])), eventsDefault(json["events_default"_ls].toInt(0)), stateDefault(json["state_default"_ls].toInt(0)), - users(qVariantHashToQHash(json["users"_ls].toVariant().toHash())), + users(fromJson>(json["users"_ls])), usersDefault(json["users_default"_ls].toInt(0)), notifications(Notifications{json["notifications"_ls]["room"_ls].toInt(50)}) { @@ -49,12 +23,12 @@ void PowerLevelsEventContent::fillJson(QJsonObject* o) const { o->insert(QStringLiteral("kick"), kick); o->insert(QStringLiteral("ban"), ban); o->insert(QStringLiteral("redact"), redact); - o->insert(QStringLiteral("events"), qHashToJson(events)); + o->insert(QStringLiteral("events"), Quotient::toJson(events)); o->insert(QStringLiteral("events_default"), eventsDefault); o->insert(QStringLiteral("state_default"), stateDefault); - o->insert(QStringLiteral("users"), qHashToJson(users)); + o->insert(QStringLiteral("users"), Quotient::toJson(users)); o->insert(QStringLiteral("users_default"), usersDefault); - o->insert(QStringLiteral("notifications"), QJsonObject{{"room", notifications.room}}); + o->insert(QStringLiteral("notifications"), {{"room", notifications.room}}); } int RoomPowerLevelsEvent::powerLevelForEvent(const QString &eventId) { -- cgit v1.2.3 From 2a54e66226863bdea4b0828675f38b75e4c7776f Mon Sep 17 00:00:00 2001 From: Black Hat Date: Wed, 1 Jan 2020 14:04:47 +0800 Subject: Mark methods as const. --- lib/events/roompowerlevelsevent.cpp | 4 ++-- lib/events/roompowerlevelsevent.h | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/events/roompowerlevelsevent.cpp b/lib/events/roompowerlevelsevent.cpp index a6efaa63..aff64cff 100644 --- a/lib/events/roompowerlevelsevent.cpp +++ b/lib/events/roompowerlevelsevent.cpp @@ -31,7 +31,7 @@ void PowerLevelsEventContent::fillJson(QJsonObject* o) const { o->insert(QStringLiteral("notifications"), {{"room", notifications.room}}); } -int RoomPowerLevelsEvent::powerLevelForEvent(const QString &eventId) { +int RoomPowerLevelsEvent::powerLevelForEvent(const QString &eventId) const { auto e = events(); if (e.contains(eventId)) { @@ -41,7 +41,7 @@ int RoomPowerLevelsEvent::powerLevelForEvent(const QString &eventId) { return eventsDefault(); } -int RoomPowerLevelsEvent::powerLevelForUser(const QString &userId) { +int RoomPowerLevelsEvent::powerLevelForUser(const QString &userId) const { auto u = users(); if (u.contains(userId)) { diff --git a/lib/events/roompowerlevelsevent.h b/lib/events/roompowerlevelsevent.h index 3a940380..b3405c56 100644 --- a/lib/events/roompowerlevelsevent.h +++ b/lib/events/roompowerlevelsevent.h @@ -40,23 +40,23 @@ public: : StateEvent(typeId(), obj) {} - int invite() { return content().invite; } - int kick() { return content().kick; } - int ban() { return content().ban; } + int invite() const { return content().invite; } + int kick() const { return content().kick; } + int ban() const { return content().ban; } - int redact() { return content().redact; } + int redact() const { return content().redact; } - QHash events() { return content().events; } - int eventsDefault() { return content().eventsDefault; } - int stateDefault() { return content().stateDefault; } + QHash events() const { return content().events; } + int eventsDefault() const { return content().eventsDefault; } + int stateDefault() const { return content().stateDefault; } - QHash users() { return content().users; } - int usersDefault() { return content().usersDefault; } + QHash users() const { return content().users; } + int usersDefault() const { return content().usersDefault; } - int roomNotification() { return content().notifications.room; } + int roomNotification() const { return content().notifications.room; } - int powerLevelForEvent(const QString& eventId); - int powerLevelForUser(const QString& userId); + int powerLevelForEvent(const QString& eventId) const; + int powerLevelForUser(const QString& userId) const; private: }; -- cgit v1.2.3 From d4034fb12b189156e135dcf0fb94960a1d8e9be9 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Wed, 1 Jan 2020 14:45:42 +0800 Subject: Add powerLevelForState() --- lib/events/roompowerlevelsevent.cpp | 10 ++++++++++ lib/events/roompowerlevelsevent.h | 1 + 2 files changed, 11 insertions(+) (limited to 'lib') diff --git a/lib/events/roompowerlevelsevent.cpp b/lib/events/roompowerlevelsevent.cpp index aff64cff..98588142 100644 --- a/lib/events/roompowerlevelsevent.cpp +++ b/lib/events/roompowerlevelsevent.cpp @@ -41,6 +41,16 @@ int RoomPowerLevelsEvent::powerLevelForEvent(const QString &eventId) const { return eventsDefault(); } +int RoomPowerLevelsEvent::powerLevelForState(const QString &eventId) const { + auto e = events(); + + if (e.contains(eventId)) { + return e[eventId]; + } + + return stateDefault(); +} + int RoomPowerLevelsEvent::powerLevelForUser(const QString &userId) const { auto u = users(); diff --git a/lib/events/roompowerlevelsevent.h b/lib/events/roompowerlevelsevent.h index b3405c56..f0f7207f 100644 --- a/lib/events/roompowerlevelsevent.h +++ b/lib/events/roompowerlevelsevent.h @@ -56,6 +56,7 @@ public: int roomNotification() const { return content().notifications.room; } int powerLevelForEvent(const QString& eventId) const; + int powerLevelForState(const QString& eventId) const; int powerLevelForUser(const QString& userId) const; private: -- cgit v1.2.3 From 285cff6584c48575d4ce49739d7006c21cc91a3f Mon Sep 17 00:00:00 2001 From: Black Hat Date: Wed, 1 Jan 2020 15:44:10 +0800 Subject: Fix linux build --- lib/events/roompowerlevelsevent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/events/roompowerlevelsevent.cpp b/lib/events/roompowerlevelsevent.cpp index 98588142..c2721595 100644 --- a/lib/events/roompowerlevelsevent.cpp +++ b/lib/events/roompowerlevelsevent.cpp @@ -28,7 +28,7 @@ void PowerLevelsEventContent::fillJson(QJsonObject* o) const { o->insert(QStringLiteral("state_default"), stateDefault); o->insert(QStringLiteral("users"), Quotient::toJson(users)); o->insert(QStringLiteral("users_default"), usersDefault); - o->insert(QStringLiteral("notifications"), {{"room", notifications.room}}); + o->insert(QStringLiteral("notifications"), QJsonObject{{"room", notifications.room}}); } int RoomPowerLevelsEvent::powerLevelForEvent(const QString &eventId) const { -- cgit v1.2.3 From 3accf7dfd82e8e14a7f90a8debee921f2f428679 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Wed, 1 Jan 2020 16:11:13 +0800 Subject: Fix linux build --- lib/events/roompowerlevelsevent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/events/roompowerlevelsevent.cpp b/lib/events/roompowerlevelsevent.cpp index c2721595..0a401752 100644 --- a/lib/events/roompowerlevelsevent.cpp +++ b/lib/events/roompowerlevelsevent.cpp @@ -14,7 +14,7 @@ PowerLevelsEventContent::PowerLevelsEventContent(const QJsonObject& json) : stateDefault(json["state_default"_ls].toInt(0)), users(fromJson>(json["users"_ls])), usersDefault(json["users_default"_ls].toInt(0)), - notifications(Notifications{json["notifications"_ls]["room"_ls].toInt(50)}) + notifications(Notifications{json["notifications"_ls].toObject()["room"_ls].toInt(50)}) { } -- cgit v1.2.3 From ac48199de174aa8cca52e703a3e65034fcd5db07 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Sun, 12 Jan 2020 04:29:14 -0800 Subject: Use parsed RoomPowerLevelsEvent in room upgrading --- lib/room.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/room.cpp b/lib/room.cpp index 696a5f1b..b29f6f48 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -52,6 +52,7 @@ #include "events/roomtombstoneevent.h" #include "events/simplestateevents.h" #include "events/typingevent.h" +#include "events/roompowerlevelsevent.h" #include "jobs/downloadfilejob.h" #include "jobs/mediathumbnailjob.h" #include "jobs/postreadmarkersjob.h" @@ -656,21 +657,12 @@ bool Room::canSwitchVersions() const // TODO, #276: m.room.power_levels const auto* plEvt = - d->currentState.value({ QStringLiteral("m.room.power_levels"), {} }); + d->getCurrentState(); if (!plEvt) return true; - const auto plJson = plEvt->contentJson(); - const auto currentUserLevel = - plJson.value("users"_ls) - .toObject() - .value(localUser()->id()) - .toInt(plJson.value("users_default"_ls).toInt()); - const auto tombstonePowerLevel = - plJson.value("events"_ls) - .toObject() - .value("m.room.tombstone"_ls) - .toInt(plJson.value("state_default"_ls).toInt()); + const auto currentUserLevel = plEvt->powerLevelForUser(localUser()->id()); + const auto tombstonePowerLevel = plEvt->powerLevelForState("m.room.tombstone"_ls); return currentUserLevel >= tombstonePowerLevel; } -- cgit v1.2.3