From c583e31b165c0248876f0d43a1c1b0de3533d51d Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sat, 18 Jan 2025 10:50:46 -0500 Subject: [PATCH] Add how many rooms you have in common to the user detail dialog Eventually this will be expanded into an actual list you can look through, but this can at least give you an idea the number of rooms this user shares with you. If the user doesn't share any rooms with you (e.g. they left) then the label is hidden. --- src/CMakeLists.txt | 2 + src/models/commonroomsmodel.cpp | 74 +++++++++++++++++++++++++++++++++ src/models/commonroomsmodel.h | 58 ++++++++++++++++++++++++++ src/qml/UserDetailDialog.qml | 15 +++++++ 4 files changed, 149 insertions(+) create mode 100644 src/models/commonroomsmodel.cpp create mode 100644 src/models/commonroomsmodel.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f0a73c74e..9f4b3d509 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -196,6 +196,8 @@ add_library(neochat STATIC models/messagecontentfiltermodel.h models/pinnedmessagemodel.cpp models/pinnedmessagemodel.h + models/commonroomsmodel.cpp + models/commonroomsmodel.h ) set_source_files_properties(qml/OsmLocationPlugin.qml PROPERTIES diff --git a/src/models/commonroomsmodel.cpp b/src/models/commonroomsmodel.cpp new file mode 100644 index 000000000..e3a896d68 --- /dev/null +++ b/src/models/commonroomsmodel.cpp @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: 2025 Joshua Goins +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "commonroomsmodel.h" +#include "jobs/neochatgetcommonroomsjob.h" + +#include + +using namespace Quotient; + +CommonRoomsModel::CommonRoomsModel(QObject *parent) + : QAbstractListModel(parent) +{ +} + +NeoChatConnection *CommonRoomsModel::connection() const +{ + return m_connection; +} + +void CommonRoomsModel::setConnection(NeoChatConnection *connection) +{ + m_connection = connection; + Q_EMIT connectionChanged(); + reload(); +} + +QString CommonRoomsModel::userId() const +{ + return m_userId; +} + +void CommonRoomsModel::setUserId(const QString &userId) +{ + m_userId = userId; + Q_EMIT userIdChanged(); + reload(); +} + +QVariant CommonRoomsModel::data(const QModelIndex &index, int roleName) const +{ + Q_UNUSED(index) + Q_UNUSED(roleName) + return {}; +} + +int CommonRoomsModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return m_commonRooms.size(); +} + +void CommonRoomsModel::reload() +{ + if (!m_connection || m_userId.isEmpty()) { + return; + } + + if (!m_connection->canCheckMutualRooms()) { + return; + } + + m_connection->callApi(m_userId).then([this](const auto job) { + const auto &replyData = job->jsonData(); + beginResetModel(); + for (const auto &roomId : replyData[u"joined"_s].toArray()) { + m_commonRooms.push_back(roomId.toString()); + } + endResetModel(); + Q_EMIT countChanged(); + }); +} + +#include "moc_commonroomsmodel.cpp" diff --git a/src/models/commonroomsmodel.h b/src/models/commonroomsmodel.h new file mode 100644 index 000000000..18e681e99 --- /dev/null +++ b/src/models/commonroomsmodel.h @@ -0,0 +1,58 @@ +// SPDX-FileCopyrightText: 2025 Joshua Goins +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +#include "neochatconnection.h" +#include "neochatroom.h" + +#include +#include + +/** + * @brief Model to show the common or mutual rooms between you and another user. + */ +class CommonRoomsModel : public QAbstractListModel +{ + Q_OBJECT + QML_ELEMENT + Q_PROPERTY(NeoChatConnection *connection WRITE setConnection READ connection NOTIFY connectionChanged REQUIRED) + Q_PROPERTY(QString userId WRITE setUserId READ userId NOTIFY userIdChanged REQUIRED) + Q_PROPERTY(int count READ rowCount NOTIFY countChanged) + +public: + enum Roles { + TextRole = Qt::DisplayRole, + LongitudeRole, + LatitudeRole, + AssetRole, + AuthorRole, + }; + Q_ENUM(Roles) + + explicit CommonRoomsModel(QObject *parent = nullptr); + + [[nodiscard]] NeoChatConnection *connection() const; + void setConnection(NeoChatConnection *connection); + + [[nodiscard]] QString userId() const; + void setUserId(const QString &userId); + + [[nodiscard]] QVariant data(const QModelIndex &index, int roleName) const override; + [[nodiscard]] Q_INVOKABLE int rowCount(const QModelIndex &parent = {}) const override; + +Q_SIGNALS: + void connectionChanged(); + void userIdChanged(); + void countChanged(); + +private: + void reload(); + + QPointer m_connection; + QString m_userId; + QList m_commonRooms; +}; diff --git a/src/qml/UserDetailDialog.qml b/src/qml/UserDetailDialog.qml index f89784d2e..aa7e66b75 100644 --- a/src/qml/UserDetailDialog.qml +++ b/src/qml/UserDetailDialog.qml @@ -56,6 +56,8 @@ Kirigami.Dialog { ColumnLayout { Layout.fillWidth: true + spacing: 0 + Kirigami.Heading { level: 1 Layout.fillWidth: true @@ -71,6 +73,19 @@ Kirigami.Dialog { textFormat: TextEdit.PlainText text: root.user.id } + + QQC2.Label { + property CommonRoomsModel model: CommonRoomsModel { + connection: root.connection + userId: root.user.id + } + + text: i18ncp("@info", "One mutual room", "%1 mutual rooms", model.count) + color: Kirigami.Theme.disabledTextColor + visible: model.count > 0 + + Layout.topMargin: Kirigami.Units.smallSpacing + } } QQC2.AbstractButton { Layout.minimumHeight: avatar.height * 0.75