diff --git a/src/neochatconnection.cpp b/src/neochatconnection.cpp index d5170633b..cd15f366e 100644 --- a/src/neochatconnection.cpp +++ b/src/neochatconnection.cpp @@ -399,29 +399,6 @@ bool NeoChatConnection::directChatExists(Quotient::User *user) return directChats().contains(user); } -void NeoChatConnection::openOrCreateDirectChat(const QString &userId) -{ - if (auto user = this->user(userId)) { - openOrCreateDirectChat(user); - } else { - qWarning() << "openOrCreateDirectChat: Couldn't get user object for ID " << userId << ", unable to open/request direct chat."; - } -} - -void NeoChatConnection::openOrCreateDirectChat(User *user) -{ - const auto existing = directChats(); - - if (existing.contains(user)) { - const auto room = this->room(existing.value(user)); - if (room) { - RoomManager::instance().resolveResource(room->id()); - return; - } - } - requestDirectChat(user->id()); -} - qsizetype NeoChatConnection::directChatNotifications() const { qsizetype notifications = 0; diff --git a/src/neochatconnection.h b/src/neochatconnection.h index 28306c4fb..dafbf222f 100644 --- a/src/neochatconnection.h +++ b/src/neochatconnection.h @@ -154,20 +154,6 @@ public: */ Q_INVOKABLE bool directChatExists(Quotient::User *user); - /** - * @brief Join a direct chat with the given user ID. - * - * If a direct chat with the user doesn't exist one is created and then joined. - */ - Q_INVOKABLE void openOrCreateDirectChat(const QString &userId); - - /** - * @brief Join a direct chat with the given user object. - * - * If a direct chat with the user doesn't exist one is created and then joined. - */ - Q_INVOKABLE void openOrCreateDirectChat(Quotient::User *user); - /** * @brief Get the account data with \param type as a formatted JSON string. */ diff --git a/src/qml/Main.qml b/src/qml/Main.qml index 356fc27c1..9433165a5 100644 --- a/src/qml/Main.qml +++ b/src/qml/Main.qml @@ -255,9 +255,6 @@ Kirigami.ApplicationWindow { Connections { target: root.connection - function onDirectChatAvailable(directChat) { - RoomManager.resolveResource(directChat.id); - } function onNewKeyVerificationSession(session) { root.pageStack.pushDialogLayer(Qt.createComponent("org.kde.neochat", "KeyVerificationDialog"), { session: session diff --git a/src/qml/ManualUserDialog.qml b/src/qml/ManualUserDialog.qml index fbd91d45e..54a4f2e3f 100644 --- a/src/qml/ManualUserDialog.qml +++ b/src/qml/ManualUserDialog.qml @@ -38,7 +38,7 @@ Kirigami.Dialog { text: i18n("OK") icon.name: "dialog-ok" onTriggered: { - root.connection.openOrCreateDirectChat(userIdText.text); + root.connection.requestDirectChat(userIdText.text); root.accept(); } } diff --git a/src/qml/UserDetailDialog.qml b/src/qml/UserDetailDialog.qml index e3459eeb0..08c2a9acc 100644 --- a/src/qml/UserDetailDialog.qml +++ b/src/qml/UserDetailDialog.qml @@ -247,7 +247,7 @@ Kirigami.Dialog { text: root.connection.directChatExists(root.user) ? i18nc("%1 is the name of the user.", "Chat with %1", root.room ? root.room.member(root.user.id).htmlSafeDisplayName : QmlUtils.escapeString(root.user.displayName)) : i18n("Invite to private chat") icon.name: "document-send" onTriggered: { - root.connection.openOrCreateDirectChat(root.user.id); + root.connection.requestDirectChat(root.user.id); root.close(); } } diff --git a/src/qml/UserSearchPage.qml b/src/qml/UserSearchPage.qml index a327b8e25..98ce38d21 100644 --- a/src/qml/UserSearchPage.qml +++ b/src/qml/UserSearchPage.qml @@ -59,7 +59,7 @@ SearchPage { text: displayName onClicked: { - root.connection.openOrCreateDirectChat(userDelegate.userId); + root.connection.requestDirectChat(userDelegate.userId); root.closeDialog(); } diff --git a/src/roommanager.cpp b/src/roommanager.cpp index 212dabe5b..20e27125a 100644 --- a/src/roommanager.cpp +++ b/src/roommanager.cpp @@ -437,7 +437,19 @@ void RoomManager::setConnection(NeoChatConnection *connection) if (m_connection == connection) { return; } + + if (m_connection != nullptr) { + m_connection->disconnect(this); + } + m_connection = connection; + + if (m_connection != nullptr) { + connect(m_connection, &NeoChatConnection::directChatAvailable, this, [this](Quotient::Room *directChat) { + resolveResource(directChat->id()); + }); + } + Q_EMIT connectionChanged(); }