Skip to content

Commit

Permalink
feat: add joinrule api and joinrulesevent
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Feb 6, 2023
1 parent e20f13f commit f08e95f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/events/joinrulesevent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2019 Kitsune Ral <[email protected]>
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "joinrulesevent.h"

using namespace Quotient;

QString JoinRulesEvent::joinRule() const
{
return fromJson<QString>(contentJson()["join_rule"_ls]);
}

QJsonArray JoinRulesEvent::allow() const
{
return contentJson()["allow"_ls].toArray();
}
18 changes: 18 additions & 0 deletions lib/events/joinrulesevent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2021 Carl Schwan <[email protected]>
// 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
18 changes: 18 additions & 0 deletions lib/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -544,6 +545,11 @@ QString Room::predecessorId() const
return {};
}

QString Room::joinRule() const
{
return currentState().get<JoinRulesEvent>()->joinRule();
}

Room* Room::predecessor(JoinStates statesFilter) const
{
if (const auto& predId = predecessorId(); !predId.isEmpty())
Expand Down Expand Up @@ -2306,6 +2312,18 @@ void Room::setName(const QString& newName)
setState<RoomNameEvent>(newName);
}

void Room::setJoinRule(const QString& joinRule)
{
auto plEvent = currentState().get<RoomPowerLevelsEvent>();
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<RoomCanonicalAliasEvent>(newAlias, altAliases());
Expand Down
4 changes: 4 additions & 0 deletions lib/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f08e95f

Please sign in to comment.