diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e860387b..063df33bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -162,6 +162,7 @@ list(APPEND lib_SRCS lib/events/roomkeyevent.h lib/events/stickerevent.h lib/events/filesourceinfo.h lib/events/filesourceinfo.cpp + lib/events/joinrulesevent.h lib/events/joinrulesevent.cpp lib/jobs/requestdata.h lib/jobs/requestdata.cpp lib/jobs/basejob.h lib/jobs/basejob.cpp lib/jobs/syncjob.h lib/jobs/syncjob.cpp diff --git a/lib/events/joinrulesevent.cpp b/lib/events/joinrulesevent.cpp new file mode 100644 index 000000000..36bbdcccf --- /dev/null +++ b/lib/events/joinrulesevent.cpp @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: 2019 Kitsune Ral +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "joinrulesevent.h" + +using namespace Quotient; + +QString JoinRulesEvent::joinRule() const +{ + return fromJson(contentJson()["join_rule"_ls]); +} + +QJsonArray JoinRulesEvent::allow() const +{ + return contentJson()["allow"_ls].toArray(); +} diff --git a/lib/events/joinrulesevent.h b/lib/events/joinrulesevent.h new file mode 100644 index 000000000..fc9888166 --- /dev/null +++ b/lib/events/joinrulesevent.h @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2021 Carl Schwan +// SPDX-License-Identifier: LGPL-2.1-or-later + +#pragma once + +#include "stateevent.h" +namespace Quotient { +class JoinRulesEvent : public StateEvent { +public: + QUO_EVENT(JoinRulesEvent, "m.room.join_rules") + + explicit JoinRulesEvent(const QJsonObject& obj) : StateEvent(obj) {} + + QString joinRule() const; + QJsonArray allow() const; +}; +REGISTER_EVENT_TYPE(JoinRulesEvent) +} // namespace Quotient diff --git a/lib/room.cpp b/lib/room.cpp index 159d81629..9f2c37518 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -37,6 +37,7 @@ #include "events/callevents.h" #include "events/encryptionevent.h" +#include "events/joinrulesevent.h" #include "events/reactionevent.h" #include "events/receiptevent.h" #include "events/redactionevent.h" @@ -544,6 +545,11 @@ QString Room::predecessorId() const return {}; } +QString Room::joinRule() const +{ + return currentState().get()->joinRule(); +} + Room* Room::predecessor(JoinStates statesFilter) const { if (const auto& predId = predecessorId(); !predId.isEmpty()) @@ -2306,6 +2312,18 @@ void Room::setName(const QString& newName) setState(newName); } +void Room::setJoinRule(const QString& joinRule) +{ + auto plEvent = currentState().get(); + auto pl = plEvent->powerLevelForState("m.room.join_rules"); + auto currentPl = plEvent->powerLevelForUser(localUser()->id()); + if (currentPl < pl) { + qWarning() << "Power level too low to set join rules"; + return; + } + setState("m.room.join_rules", "", QJsonObject{ { "join_rule", joinRule } }); +} + void Room::setCanonicalAlias(const QString& newAlias) { setState(newAlias, altAliases()); diff --git a/lib/room.h b/lib/room.h index e2d6b869a..2a9295ab7 100644 --- a/lib/room.h +++ b/lib/room.h @@ -117,6 +117,7 @@ class QUOTIENT_API Room : public QObject { Q_PROPERTY(Connection* connection READ connection CONSTANT) Q_PROPERTY(User* localUser READ localUser CONSTANT) Q_PROPERTY(QString id READ id CONSTANT) + Q_PROPERTY(QString joinRule READ joinRule WRITE setJoinRule NOTIFY joinRuleChanged) Q_PROPERTY(QString version READ version NOTIFY baseStateLoaded) Q_PROPERTY(bool isUnstable READ isUnstable NOTIFY stabilityUpdated) Q_PROPERTY(QString predecessorId READ predecessorId NOTIFY baseStateLoaded) @@ -235,6 +236,7 @@ class QUOTIENT_API Room : public QObject { QString version() const; bool isUnstable() const; QString predecessorId() const; + [[nodiscard]] QString joinRule() const; /// Room predecessor /** This function validates that the predecessor has a tombstone and * the tombstone refers to the current room. If that's not the case, @@ -836,6 +838,7 @@ public Q_SLOTS: const QString& stateKey, const QJsonObject& contentJson); void setName(const QString& newName); + void setJoinRule(const QString &joinRule); void setCanonicalAlias(const QString& newAlias); void setPinnedEvents(const QStringList& events); /// Set room aliases on the user's current server @@ -943,6 +946,7 @@ public Q_SLOTS: * Not triggered when display name changes. */ void namesChanged(Quotient::Room* room); + void joinRuleChanged(); void displaynameAboutToChange(Quotient::Room* room); void displaynameChanged(Quotient::Room* room, QString oldName); void pinnedEventsChanged();