diff options
Diffstat (limited to 'lib/events')
-rw-r--r-- | lib/events/roompowerlevelsevent.cpp | 62 | ||||
-rw-r--r-- | lib/events/roompowerlevelsevent.h | 76 |
2 files changed, 138 insertions, 0 deletions
diff --git a/lib/events/roompowerlevelsevent.cpp b/lib/events/roompowerlevelsevent.cpp new file mode 100644 index 00000000..0a401752 --- /dev/null +++ b/lib/events/roompowerlevelsevent.cpp @@ -0,0 +1,62 @@ +#include "roompowerlevelsevent.h" + +#include <QJsonDocument> + +using namespace Quotient; + +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(fromJson<QHash<QString, int>>(json["events"_ls])), + eventsDefault(json["events_default"_ls].toInt(0)), + stateDefault(json["state_default"_ls].toInt(0)), + users(fromJson<QHash<QString, int>>(json["users"_ls])), + usersDefault(json["users_default"_ls].toInt(0)), + notifications(Notifications{json["notifications"_ls].toObject()["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"), Quotient::toJson(events)); + o->insert(QStringLiteral("events_default"), eventsDefault); + o->insert(QStringLiteral("state_default"), stateDefault); + o->insert(QStringLiteral("users"), Quotient::toJson(users)); + o->insert(QStringLiteral("users_default"), usersDefault); + o->insert(QStringLiteral("notifications"), QJsonObject{{"room", notifications.room}}); +} + +int RoomPowerLevelsEvent::powerLevelForEvent(const QString &eventId) const { + auto e = events(); + + if (e.contains(eventId)) { + return e[eventId]; + } + + 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(); + + if (u.contains(userId)) { + return u[userId]; + } + + return usersDefault(); +} diff --git a/lib/events/roompowerlevelsevent.h b/lib/events/roompowerlevelsevent.h new file mode 100644 index 00000000..f0f7207f --- /dev/null +++ b/lib/events/roompowerlevelsevent.h @@ -0,0 +1,76 @@ +#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<QString, int> events; + int eventsDefault; + int stateDefault; + + QHash<QString, int> users; + int usersDefault; + + Notifications notifications; + +protected: + void fillJson(QJsonObject* o) const override; +}; + +class RoomPowerLevelsEvent : public StateEvent<PowerLevelsEventContent> { + Q_GADGET +public: + DEFINE_EVENT_TYPEID("m.room.power_levels", RoomPowerLevelsEvent) + + explicit RoomPowerLevelsEvent(const QJsonObject& obj) + : StateEvent(typeId(), obj) + {} + + int invite() const { return content().invite; } + int kick() const { return content().kick; } + int ban() const { return content().ban; } + + int redact() const { return content().redact; } + + QHash<QString, int> events() const { return content().events; } + int eventsDefault() const { return content().eventsDefault; } + int stateDefault() const { return content().stateDefault; } + + QHash<QString, int> users() const { return content().users; } + int usersDefault() const { return content().usersDefault; } + + 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: +}; + +template <> +class EventFactory<RoomPowerLevelsEvent> { +public: + static event_ptr_tt<RoomPowerLevelsEvent> make(const QJsonObject& json, + const QString&) + { + return makeEvent<RoomPowerLevelsEvent>(json); + } +}; + +REGISTER_EVENT_TYPE(RoomPowerLevelsEvent) +} // namespace Quotient |