From 752e7f4d9a0437d342e3da7dcd69fc523d888e5e Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Wed, 16 Nov 2022 02:49:40 +0100 Subject: [PATCH] Implement changing join rules --- src/neochatroom.cpp | 14 ++++++++++++++ src/neochatroom.h | 4 +++- src/qml/RoomSettings/Security.qml | 24 ++++++++++++++++++++---- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index f8657283f..a4c767252 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -874,6 +874,20 @@ QString NeoChatRoom::joinRule() const return getCurrentState()->joinRule(); } +void NeoChatRoom::setJoinRule(const QString &joinRule) +{ + if (!canSendState("m.room.join_rules")) { + qWarning() << "Power level too low to set join rules"; + return; + } +#ifdef QUOTIENT_07 + setState("m.room.join_rules", "", QJsonObject{{"join_rule", joinRule}}); +#else + setState(QJsonObject{{"type", "m.room.join_rules"}, {"state_key", ""}, {"content", QJsonObject{{"join_rule", joinRule}}}}); +#endif + // Not emitting joinRuleChanged() here, since that would override the change in the UI with the *current* value, which is not the *new* value. +} + QCoro::Task NeoChatRoom::doDeleteMessagesByUser(const QString &user) { QStringList events; diff --git a/src/neochatroom.h b/src/neochatroom.h index 05100bfc7..0b755ec6f 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -49,7 +49,7 @@ class NeoChatRoom : public Quotient::Room Q_PROPERTY(bool readMarkerLoaded READ readMarkerLoaded NOTIFY readMarkerLoadedChanged) Q_PROPERTY(QDateTime lastActiveTime READ lastActiveTime NOTIFY lastActiveTimeChanged) Q_PROPERTY(bool isInvite READ isInvite NOTIFY isInviteChanged) - Q_PROPERTY(QString joinRule READ joinRule CONSTANT) + Q_PROPERTY(QString joinRule READ joinRule WRITE setJoinRule NOTIFY joinRuleChanged) Q_PROPERTY(QString htmlSafeDisplayName READ htmlSafeDisplayName NOTIFY displayNameChanged) Q_PROPERTY(PushNotificationState::State pushNotificationState MEMBER m_currentPushNotificationState WRITE setPushNotificationState NOTIFY pushNotificationStateChanged) @@ -112,6 +112,7 @@ public: bool isEventHighlighted(const Quotient::RoomEvent *e) const; [[nodiscard]] QString joinRule() const; + void setJoinRule(const QString &joinRule); [[nodiscard]] bool hasFileUploading() const { @@ -254,6 +255,7 @@ Q_SIGNALS: void chatBoxEditIdChanged(); void chatBoxAttachmentPathChanged(); void canEncryptRoomChanged(); + void joinRuleChanged(); public Q_SLOTS: void uploadFile(const QUrl &url, const QString &body = QString()); diff --git a/src/qml/RoomSettings/Security.qml b/src/qml/RoomSettings/Security.qml index b4c9dce3f..624e4c215 100644 --- a/src/qml/RoomSettings/Security.qml +++ b/src/qml/RoomSettings/Security.qml @@ -24,7 +24,10 @@ Kirigami.ScrollablePage { text: i18nc("@option:check", "Private (invite only)") Kirigami.FormData.label: i18nc("@option:check", "Access:") checked: room.joinRule === "invite" - enabled: false + enabled: room.canSendState("m.room.join_rules") + onCheckedChanged: if (checked) { + room.joinRule = "invite"; + } } QQC2.Label { text: i18n("Only invited people can join.") @@ -33,19 +36,32 @@ Kirigami.ScrollablePage { QQC2.RadioButton { text: i18nc("@option:check", "Space members") checked: room.joinRule === "restricted" - enabled: false + enabled: room.canSendState("m.room.join_rules") && ["8", "9", "10"].includes(room.version) && false + onCheckedChanged: if (checked) { + room.joinRule = "restricted"; + } } QQC2.Label { text: i18n("Anyone in a space can find and join.") + wrapMode: Qt.Wrap + width: root.width font: Kirigami.Theme.smallFont } + QQC2.Label { + font: Kirigami.Theme.smallFont + visible: !["8", "9", "10"].includes(room.version) + text: i18n("You need to upgrade this room to a newer version to enable this setting.") + } QQC2.RadioButton { text: i18nc("@option:check", "Public") checked: room.joinRule === "public" - enabled: false + enabled: room.canSendState("m.room.join_rules") + onCheckedChanged: if (checked) { + room.joinRule = "public"; + } } QQC2.Label { - text: i18nc("@option:check", "Anyone can find and join.") + room.joinRule + text: i18nc("@option:check", "Anyone can find and join.") font: Kirigami.Theme.smallFont } }