Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add joinrule api and joinrulesevent #616

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,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
Expand Down
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: 2022 Carl Schwan <[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();
}
17 changes: 17 additions & 0 deletions lib/events/joinrulesevent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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;
};
Comment on lines +8 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a rather basic implementation of m.room.join_rules, missing the safeguards that I would expect it to have. At the very least, joinRule should be an enum (hopefully we don't get a tide of new join rules in the next year, as we've just added a bunch). Even better, allow could actually be made a vector of AllowCondition structures defined in the spec. I realise this is more work than just copying code from NeoChat - if you don't feel like doing it, let me know and I will help.

} // 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 @@ -539,6 +540,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 @@ -2311,6 +2317,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(JoinRulesEvent::TypeId);
auto currentPl = plEvent->powerLevelForUser(localUser()->id());
if (currentPl < pl) {
qWarning() << "Power level too low to set join rules";
return;
}
setState(JoinRulesEvent::TypeId, "", 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 @@ -237,6 +238,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 @@ -842,6 +844,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 @@ -949,6 +952,7 @@ public Q_SLOTS:
* Not triggered when display name changes.
*/
void namesChanged(Quotient::Room* room);
void joinRuleChanged();
KitsuneRal marked this conversation as resolved.
Show resolved Hide resolved
void displaynameAboutToChange(Quotient::Room* room);
void displaynameChanged(Quotient::Room* room, QString oldName);
void pinnedEventsChanged();
Expand Down