Use the forget function to leave a room everywhere

Use the forget function to leave a room everywhere this is both for consistency and to reduce dependencies. This way no dependency on RoomManager is required to leave a room and since in all cases they have an object they can just call the function.
This commit is contained in:
James Graham
2025-05-20 17:35:34 +01:00
parent f0d2c19393
commit a1ca768711
13 changed files with 59 additions and 42 deletions

View File

@@ -32,6 +32,7 @@
#include "neochatroom.h" #include "neochatroom.h"
#include "notificationsmanager.h" #include "notificationsmanager.h"
#include "proxycontroller.h" #include "proxycontroller.h"
#include "roommanager.h"
#if defined(Q_OS_WIN) || defined(Q_OS_MAC) #if defined(Q_OS_WIN) || defined(Q_OS_MAC)
#include "trayicon.h" #include "trayicon.h"
@@ -230,6 +231,7 @@ void Controller::initConnection(NeoChatConnection *connection)
m_notificationsManager.handleNotifications(connection); m_notificationsManager.handleNotifications(connection);
}); });
connect(this, &Controller::globalUrlPreviewDefaultChanged, connection, &NeoChatConnection::globalUrlPreviewEnabledChanged); connect(this, &Controller::globalUrlPreviewDefaultChanged, connection, &NeoChatConnection::globalUrlPreviewEnabledChanged);
connect(connection, &NeoChatConnection::roomAboutToBeLeft, &RoomManager::instance(), &RoomManager::roomLeft);
Q_EMIT connectionAdded(connection); Q_EMIT connectionAdded(connection);
} }

View File

@@ -276,7 +276,7 @@ void NotificationsManager::postInviteNotification(NeoChatRoom *rawRoom)
if (inAnyOfOurRooms) { if (inAnyOfOurRooms) {
doPostInviteNotification(room); doPostInviteNotification(room);
} else { } else {
room->leaveRoom(); room->forget();
} }
} }
}); });
@@ -330,14 +330,14 @@ void NotificationsManager::doPostInviteNotification(QPointer<NeoChatRoom> room)
if (!room) { if (!room) {
return; return;
} }
RoomManager::instance().leaveRoom(room); room->forget();
notification->close(); notification->close();
}); });
connect(rejectAndIgnoreAction, &KNotificationAction::activated, this, [room, notification]() { connect(rejectAndIgnoreAction, &KNotificationAction::activated, this, [room, notification]() {
if (!room) { if (!room) {
return; return;
} }
RoomManager::instance().leaveRoom(room); room->forget();
room->connection()->addToIgnoredUsers(room->invitingUserId()); room->connection()->addToIgnoredUsers(room->invitingUserId());
notification->close(); notification->close();
}); });

View File

@@ -28,7 +28,7 @@ Kirigami.PromptDialog {
text: i18nc("@action:button", "Leave Room") text: i18nc("@action:button", "Leave Room")
QQC2.DialogButtonBox.buttonRole: QQC2.DialogButtonBox.AcceptRole QQC2.DialogButtonBox.buttonRole: QQC2.DialogButtonBox.AcceptRole
icon.name: "arrow-left-symbolic" icon.name: "arrow-left-symbolic"
onClicked: RoomManager.leaveRoom(root.room) onClicked: root.room.forget();
} }
} }
} }

View File

@@ -108,7 +108,7 @@ ColumnLayout {
icon.name: "dialog-cancel-symbolic" icon.name: "dialog-cancel-symbolic"
text: i18nc("@action:button Reject this invite", "Reject Invite") text: i18nc("@action:button Reject this invite", "Reject Invite")
onClicked: RoomManager.leaveRoom(root.currentRoom) onClicked: root.currentRoom.forget()
} }
} }
@@ -123,7 +123,7 @@ ColumnLayout {
text: i18nc("@action:button Block the user", "Block %1", root.invitingMember.displayName) text: i18nc("@action:button Block the user", "Block %1", root.invitingMember.displayName)
onClicked: { onClicked: {
RoomManager.leaveRoom(root.currentRoom); root.currentRoom.forget()
root.currentRoom.connection.addToIgnoredUsers(root.currentRoom.invitingUserId); root.currentRoom.connection.addToIgnoredUsers(root.currentRoom.invitingUserId);
} }
} }

View File

@@ -139,8 +139,6 @@ Kirigami.Page {
anchors.fill: parent anchors.fill: parent
sourceComponent: SpaceHomePage { sourceComponent: SpaceHomePage {
room: root.currentRoom room: root.currentRoom
onRequestLeaveRoom: room => RoomManager.leaveRoom(room);
} }
} }

View File

@@ -448,29 +448,27 @@ void RoomManager::knockRoom(NeoChatConnection *account, const QString &roomAlias
Qt::SingleShotConnection); Qt::SingleShotConnection);
} }
void RoomManager::roomLeft(const QString &id)
{
if (id.isEmpty()) {
return;
}
if (m_currentRoom && m_currentRoom->id() == id) {
setCurrentRoom({});
}
if (m_currentSpaceId == id) {
setCurrentSpace({});
}
}
bool RoomManager::visitNonMatrix(const QUrl &url) bool RoomManager::visitNonMatrix(const QUrl &url)
{ {
UrlHelper().openUrl(url); UrlHelper().openUrl(url);
return true; return true;
} }
void RoomManager::leaveRoom(NeoChatRoom *room)
{
if (!room) {
return;
}
if (m_currentRoom && m_currentRoom->id() == room->id()) {
setCurrentRoom({});
}
if (m_currentSpaceId == room->id()) {
setCurrentSpace({});
}
room->forget();
}
ChatDocumentHandler *RoomManager::chatDocumentHandler() const ChatDocumentHandler *RoomManager::chatDocumentHandler() const
{ {
return m_chatDocumentHandler; return m_chatDocumentHandler;

View File

@@ -195,11 +195,6 @@ public:
*/ */
Q_INVOKABLE void loadInitialRoom(); Q_INVOKABLE void loadInitialRoom();
/**
* @brief Leave the room and close it if it is open.
*/
Q_INVOKABLE void leaveRoom(NeoChatRoom *room);
/** /**
* @brief Knock a room. * @brief Knock a room.
* *
@@ -208,6 +203,13 @@ public:
*/ */
void knockRoom(NeoChatConnection *account, const QString &roomAliasOrId, const QString &reason, const QStringList &viaServers); void knockRoom(NeoChatConnection *account, const QString &roomAliasOrId, const QString &reason, const QStringList &viaServers);
/**
* @brief Cleanup after the given room is left.
*
* This ensures that the current room and space are not set to the left room.
*/
void roomLeft(const QString &id);
/** /**
* @brief Show a media item maximized. * @brief Show a media item maximized.
* *

View File

@@ -29,7 +29,7 @@ QStringList rainbowColors{"#ff2b00"_L1, "#ff5500"_L1, "#ff8000"_L1, "#ffaa00"_L1
auto leaveRoomLambda = [](const QString &text, NeoChatRoom *room, ChatBarCache *) { auto leaveRoomLambda = [](const QString &text, NeoChatRoom *room, ChatBarCache *) {
if (text.isEmpty()) { if (text.isEmpty()) {
Q_EMIT room->showMessage(MessageType::Information, i18n("Leaving this room.")); Q_EMIT room->showMessage(MessageType::Information, i18n("Leaving this room."));
room->connection()->leaveRoom(room); room->forget();
} else { } else {
QRegularExpression roomRegex(uR"(^[#!][^:]+:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?)"_s); QRegularExpression roomRegex(uR"(^[#!][^:]+:\w(?:\w|\.|-)*\.\w+(?::\d{1,5})?)"_s);
auto regexMatch = roomRegex.match(text); auto regexMatch = roomRegex.match(text);
@@ -38,13 +38,13 @@ auto leaveRoomLambda = [](const QString &text, NeoChatRoom *room, ChatBarCache *
i18nc("'<text>' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text)); i18nc("'<text>' does not look like a room id or alias.", "'%1' does not look like a room id or alias.", text));
return QString(); return QString();
} }
auto leaving = room->connection()->room(text); auto leaving = dynamic_cast<NeoChatRoom *>(room->connection()->room(text));
if (!leaving) { if (!leaving) {
leaving = room->connection()->roomByAlias(text); leaving = dynamic_cast<NeoChatRoom *>(room->connection()->roomByAlias(text));
} }
if (leaving) { if (leaving) {
Q_EMIT room->showMessage(MessageType::Information, i18nc("Leaving room <roomname>.", "Leaving room %1.", text)); Q_EMIT room->showMessage(MessageType::Information, i18nc("Leaving room <roomname>.", "Leaving room %1.", text));
room->connection()->leaveRoom(leaving); leaving->forget();
} else { } else {
Q_EMIT room->showMessage(MessageType::Information, i18nc("Room <roomname> not found", "Room %1 not found.", text)); Q_EMIT room->showMessage(MessageType::Information, i18nc("Room <roomname> not found", "Room %1 not found.", text));
} }

View File

@@ -9,6 +9,7 @@
#include "neochatroom.h" #include "neochatroom.h"
#include "spacehierarchycache.h" #include "spacehierarchycache.h"
#include <Quotient/connection.h>
#include <Quotient/jobs/basejob.h> #include <Quotient/jobs/basejob.h>
#include <Quotient/quotient_common.h> #include <Quotient/quotient_common.h>
#include <qt6keychain/keychain.h> #include <qt6keychain/keychain.h>
@@ -386,6 +387,13 @@ void NeoChatConnection::createSpace(const QString &name, const QString &topic, c
}); });
} }
Quotient::ForgetRoomJob *NeoChatConnection::forgetRoom(const QString &id)
{
Q_EMIT roomAboutToBeLeft(id);
return Connection::forgetRoom(id);
}
bool NeoChatConnection::directChatExists(Quotient::User *user) bool NeoChatConnection::directChatExists(Quotient::User *user)
{ {
return directChats().contains(user); return directChats().contains(user);

View File

@@ -150,6 +150,14 @@ public:
*/ */
Q_INVOKABLE void createSpace(const QString &name, const QString &topic, const QString &parent = {}, bool setChildParent = false); Q_INVOKABLE void createSpace(const QString &name, const QString &topic, const QString &parent = {}, bool setChildParent = false);
/**
* @brief Send /forget to the server and delete room locally.
*
* @note This wraps around the Quotient::Connection::forgetRoom() to allow
* roomAboutToBeLeft() to be emitted.
*/
Quotient::ForgetRoomJob *forgetRoom(const QString &id);
/** /**
* @brief Whether a direct chat with the user exists. * @brief Whether a direct chat with the user exists.
*/ */
@@ -224,6 +232,11 @@ Q_SIGNALS:
*/ */
void errorOccured(const QString &error); void errorOccured(const QString &error);
/**
* @brief The given room ID is about to be forgotten.
*/
void roomAboutToBeLeft(const QString &id);
private: private:
static bool m_globalUrlPreviewDefault; static bool m_globalUrlPreviewDefault;
static PushRuleAction::Action m_defaultAction; static PushRuleAction::Action m_defaultAction;

View File

@@ -305,8 +305,9 @@ void NeoChatRoom::forget()
roomIds += predecessor->id(); roomIds += predecessor->id();
} }
const auto neochatConnection = dynamic_cast<NeoChatConnection *>(connection());
for (const auto &id : roomIds) { for (const auto &id : roomIds) {
connection()->forgetRoom(id); neochatConnection->forgetRoom(id);
} }
} }

View File

@@ -72,6 +72,6 @@ KirigamiComponents.ConvergentContextMenu {
QQC2.Action { QQC2.Action {
text: i18nc("'Space' is a matrix space", "Leave Space") text: i18nc("'Space' is a matrix space", "Leave Space")
icon.name: "go-previous" icon.name: "go-previous"
onTriggered: RoomManager.leaveRoom(room) onTriggered: root.room.forget()
} }
} }

View File

@@ -19,11 +19,6 @@ ColumnLayout {
*/ */
required property NeoChatRoom room required property NeoChatRoom room
/**
* @brief Request to leave the given room.
*/
signal requestLeaveRoom(NeoChatRoom room)
anchors.fill: parent anchors.fill: parent
spacing: 0 spacing: 0
@@ -102,7 +97,7 @@ ColumnLayout {
QQC2.Button { QQC2.Button {
text: i18nc("@action:button", "Leave this space") text: i18nc("@action:button", "Leave this space")
icon.name: "go-previous" icon.name: "go-previous"
onClicked: root.requestLeaveRoom(root.room) onClicked: root.room.forget()
} }
Item { Item {
Layout.fillWidth: true Layout.fillWidth: true