From d1ba9fd878ce4fedcd9a8f21875e53d322653e7e Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Thu, 27 Jul 2023 22:04:34 -0300 Subject: [PATCH 01/10] Rebase --- CMakeLists.txt | 1 + resources/qml/dialogs/IgnoredUsers.qml | 65 ++++++++++++++++++++++++++ resources/qml/dialogs/UserProfile.qml | 32 +++++++++++++ src/timeline/TimelineModel.cpp | 13 ++++++ src/timeline/TimelineModel.h | 4 ++ src/ui/UserProfile.cpp | 55 ++++++++++++++++++++-- src/ui/UserProfile.h | 4 +- 7 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 resources/qml/dialogs/IgnoredUsers.qml diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a2fece2..dd356ae9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -772,6 +772,7 @@ set(QML_SOURCES resources/qml/dialogs/AllowedRoomsSettingsDialog.qml resources/qml/dialogs/RoomSettings.qml resources/qml/dialogs/UserProfile.qml + resources/qml/dialogs/IgnoredUsers.qml resources/qml/emoji/StickerPicker.qml resources/qml/pages/LoginPage.qml resources/qml/pages/RegisterPage.qml diff --git a/resources/qml/dialogs/IgnoredUsers.qml b/resources/qml/dialogs/IgnoredUsers.qml new file mode 100644 index 00000000..714cb67e --- /dev/null +++ b/resources/qml/dialogs/IgnoredUsers.qml @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: Nheko Contributors +// +// SPDX-License-Identifier: GPL-3.0-or-later +import QtQml 2.15 +import QtQuick 2.15 +import QtQuick.Controls 2.15 +import QtQuick.Layouts 2.15 +import QtQuick.Window 2.15 +import im.nheko 1.0 + +Window { + id: ignoredUsers + required property list users + required property var profile + + title: qsTr("Ignored users") + flags: Qt.WindowCloseButtonHint | Qt.WindowTitleHint + height: 650 + width: 420 + minimumHeight: 420 + color: palette.window + + Connections { + target: profile + function onUnignoredUser(id, err) { + if (err) { + const text = qsTr("Failed to unignore \"%1\": %2").arg(id).arg(err) + MainWindow.showNotification(text) + } else { + users = Array.from(users).filter(user => user !== id) + } + } + } + + ListView { + id: view + width: ignoredUsers.width + height: ignoredUsers.height + Layout.leftMargin: Nheko.paddingMedium + Layout.rightMargin: Nheko.paddingMedium + spacing: Nheko.paddingMedium + + model: users + delegate: RowLayout { + width: view.width + Text { + Layout.fillWidth: true + Layout.alignment: Qt.AlignLeft + elide: Text.ElideRight + color: palette.text + text: modelData + } + + ImageButton { + Layout.preferredHeight: 24 + Layout.preferredWidth: 24 + image: ":/icons/icons/ui/delete.svg" + hoverEnabled: true + ToolTip.visible: hovered + ToolTip.text: qsTr("Stop Ignoring.") + onClicked: profile.ignoredStatus(modelData, false) + } + } + } +} \ No newline at end of file diff --git a/resources/qml/dialogs/UserProfile.qml b/resources/qml/dialogs/UserProfile.qml index b54b52a4..4df27a3f 100644 --- a/resources/qml/dialogs/UserProfile.qml +++ b/resources/qml/dialogs/UserProfile.qml @@ -289,6 +289,19 @@ ApplicationWindow { visible: !profile.isGlobalUserProfile && profile.room.permissions.canBan() } + ImageButton { + Layout.preferredHeight: 24 + Layout.preferredWidth: 24 + image: ":/icons/icons/ui/volume-off-indicator.svg" + hoverEnabled: true + ToolTip.visible: hovered + ToolTip.text: qsTr("Ignore the user.") + onClicked: { + profile.ignoredStatus(profile.userid, true) + } + visible: !profile.isSelf && !profile.isGlobalUserProfile + } + ImageButton { Layout.preferredHeight: 24 Layout.preferredWidth: 24 @@ -299,6 +312,25 @@ ApplicationWindow { onClicked: profile.refreshDevices() } + ImageButton { + Layout.preferredHeight: 24 + Layout.preferredWidth: 24 + image: ":/icons/icons/ui/volume-off-indicator.svg" + hoverEnabled: true + ToolTip.visible: hovered + ToolTip.text: qsTr("Ignored users.") + onClicked: { + var component = Qt.createComponent("IgnoredUsers.qml") + if (component.status == Component.Ready) { + var window = component.createObject(userProfileDialog, {users: profile.getIgnoredUsers(), profile: profile}) + window.show() + timelineRoot.destroyOnClose(window) + } else { + console.error("Failed to create component: " + component.errorString()); + } + } + visible: profile.isSelf && profile.isGlobalUserProfile + } } TabBar { diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index b2a036c5..ce136e35 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -521,6 +521,8 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj cache::client()->updateState(room_id_.toStdString(), events_, true); this->syncState({std::move(events_.events)}); }); + + connect(this, &TimelineModel::ignoredUser, this, &TimelineModel::handleIgnoredUser); } QHash @@ -2108,6 +2110,17 @@ TimelineModel::scrollTimerEvent() } } +void +TimelineModel::handleIgnoredUser(const QString &id, const std::optional &err) +{ + if (err) { + MainWindow::instance()->showNotification( + tr("Failed to ignore \"%1\": %2").arg(id).arg(*err)); + } else { + this->clearTimeline(); + } +} + void TimelineModel::requestKeyForEvent(const QString &id) { diff --git a/src/timeline/TimelineModel.h b/src/timeline/TimelineModel.h index fccc99eb..c8947891 100644 --- a/src/timeline/TimelineModel.h +++ b/src/timeline/TimelineModel.h @@ -454,6 +454,7 @@ public slots: private slots: void addPendingMessage(mtx::events::collections::TimelineEvents event); void scrollTimerEvent(); + void handleIgnoredUser(const QString &id, const std::optional &err); signals: void dataAtIdChanged(QString id); @@ -503,6 +504,9 @@ signals: void fetchedMore(); + // The user may close the profile window before we receive a response, so handle it here + void ignoredUser(const QString &id, const std::optional &err); + private: template void sendEncryptedMessage(mtx::events::RoomEvent msg, mtx::events::EventType eventType); diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 80def409..5146ff26 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -224,6 +224,57 @@ UserProfile::refreshDevices() fetchDeviceList(this->userid_); } +QVector +UserProfile::getIgnoredUsers() +{ + QVector vec; + const std::optional optEv = + cache::client()->getAccountData(mtx::events::EventType::IgnoredUsers); + if (optEv) { + const auto &ev = + std::get>(*optEv) + .content; + for (const mtx::events::account_data::IgnoredUser &user : ev.users) { + vec.append(QString::fromStdString(user.id)); + } + } + + return vec; +} + +void +UserProfile::ignoredStatus(const QString &id, const bool ignore) +{ + auto old = this->getIgnoredUsers(); + if (ignore) { + if (old.contains(id)) { + emit this->room()->ignoredUser(id, tr("Already ignored")); + return; + } + old.append(id); + } else { + old.removeOne(id); + } + + std::vector content; + for (const QString &item : old) { + const mtx::events::account_data::IgnoredUser data{.id = item.toStdString()}; + content.push_back(data); + } + + const mtx::events::account_data::IgnoredUsers payload{.users{content}}; + + http::client()->put_account_data(payload, [this, id, ignore](mtx::http::RequestErr e) { + if (ignore) { + emit this->room()->ignoredUser( + id, e ? std::optional(QString::fromStdString(e->matrix_error.error)) : std::nullopt); + } else { + emit this->unignoredUser( + id, e ? QVariant(QString::fromStdString(e->matrix_error.error)) : QVariant()); + } + }); +} + void UserProfile::fetchDeviceList(const QString &userID) { @@ -345,10 +396,6 @@ UserProfile::banUser() ChatPage::instance()->banUser(roomid_, this->userid_, QLatin1String("")); } -// void ignoreUser(){ - -// } - void UserProfile::kickUser() { diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index d8e06aa1..2908b57e 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -182,9 +182,10 @@ public: Q_INVOKABLE void unverify(const QString &device = QLatin1String("")); Q_INVOKABLE void fetchDeviceList(const QString &userID); Q_INVOKABLE void refreshDevices(); + Q_INVOKABLE QVector getIgnoredUsers(); Q_INVOKABLE void banUser(); Q_INVOKABLE void signOutDevice(const QString &deviceID); - // Q_INVOKABLE void ignoreUser(); + Q_INVOKABLE void ignoredStatus(const QString &id, const bool ignore); Q_INVOKABLE void kickUser(); Q_INVOKABLE void startChat(); Q_INVOKABLE void startChat(bool encryptionEnabled); @@ -201,6 +202,7 @@ signals: void displayError(const QString &errorMessage); void globalUsernameRetrieved(const QString &globalUser); void devicesChanged(); + void unignoredUser(const QString &id, const QVariant &err); // internal void verificationStatiChanged(); From 563bb0a651f37de4f26b0df06c1361bf8063a9b6 Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Fri, 4 Aug 2023 03:30:45 -0300 Subject: [PATCH 02/10] UI improvements --- resources/qml/dialogs/IgnoredUsers.qml | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/resources/qml/dialogs/IgnoredUsers.qml b/resources/qml/dialogs/IgnoredUsers.qml index 714cb67e..51feb9cb 100644 --- a/resources/qml/dialogs/IgnoredUsers.qml +++ b/resources/qml/dialogs/IgnoredUsers.qml @@ -34,13 +34,23 @@ Window { ListView { id: view - width: ignoredUsers.width - height: ignoredUsers.height - Layout.leftMargin: Nheko.paddingMedium - Layout.rightMargin: Nheko.paddingMedium + anchors.fill: parent spacing: Nheko.paddingMedium model: users + header: ColumnLayout { + Text { + Layout.fillWidth: true + Layout.maximumWidth: view.width + // Review request: Wrapping occurs with default width/font values, would it be better design to increase the window width? + wrapMode: Text.Wrap + color: palette.text + text: qsTr("Ignoring a user hides their messages (they can still see yours!).") + } + + // Review request: Is there a better way to do this? + Item { Layout.preferredHeight: Nheko.paddingLarge } + } delegate: RowLayout { width: view.width Text { From 6a6384babe8138568a01f28e915de52e011bc952 Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Sat, 5 Aug 2023 06:47:02 -0300 Subject: [PATCH 03/10] Move comments to proper reviews --- resources/qml/dialogs/IgnoredUsers.qml | 2 -- 1 file changed, 2 deletions(-) diff --git a/resources/qml/dialogs/IgnoredUsers.qml b/resources/qml/dialogs/IgnoredUsers.qml index 51feb9cb..65574338 100644 --- a/resources/qml/dialogs/IgnoredUsers.qml +++ b/resources/qml/dialogs/IgnoredUsers.qml @@ -42,13 +42,11 @@ Window { Text { Layout.fillWidth: true Layout.maximumWidth: view.width - // Review request: Wrapping occurs with default width/font values, would it be better design to increase the window width? wrapMode: Text.Wrap color: palette.text text: qsTr("Ignoring a user hides their messages (they can still see yours!).") } - // Review request: Is there a better way to do this? Item { Layout.preferredHeight: Nheko.paddingLarge } } delegate: RowLayout { From 4eaba191de4fc1ae1709b90c97ec4030397f1d7c Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Thu, 7 Sep 2023 18:10:04 -0300 Subject: [PATCH 04/10] Use properties --- resources/qml/dialogs/IgnoredUsers.qml | 13 +++------ resources/qml/dialogs/UserProfile.qml | 2 +- src/timeline/TimelineViewManager.cpp | 38 ++++++++++++++++++++++++++ src/timeline/TimelineViewManager.h | 11 ++++++++ src/ui/UserProfile.cpp | 25 ++--------------- src/ui/UserProfile.h | 3 +- 6 files changed, 58 insertions(+), 34 deletions(-) diff --git a/resources/qml/dialogs/IgnoredUsers.qml b/resources/qml/dialogs/IgnoredUsers.qml index 65574338..bdd24a57 100644 --- a/resources/qml/dialogs/IgnoredUsers.qml +++ b/resources/qml/dialogs/IgnoredUsers.qml @@ -10,7 +10,6 @@ import im.nheko 1.0 Window { id: ignoredUsers - required property list users required property var profile title: qsTr("Ignored users") @@ -22,13 +21,9 @@ Window { Connections { target: profile - function onUnignoredUser(id, err) { - if (err) { - const text = qsTr("Failed to unignore \"%1\": %2").arg(id).arg(err) - MainWindow.showNotification(text) - } else { - users = Array.from(users).filter(user => user !== id) - } + function onUnignoredUserError(id, err) { + const text = qsTr("Failed to unignore \"%1\": %2").arg(id).arg(err) + MainWindow.showNotification(text) } } @@ -37,7 +32,7 @@ Window { anchors.fill: parent spacing: Nheko.paddingMedium - model: users + model: TimelineManager.ignoredUsers header: ColumnLayout { Text { Layout.fillWidth: true diff --git a/resources/qml/dialogs/UserProfile.qml b/resources/qml/dialogs/UserProfile.qml index 4df27a3f..3d65d52a 100644 --- a/resources/qml/dialogs/UserProfile.qml +++ b/resources/qml/dialogs/UserProfile.qml @@ -322,7 +322,7 @@ ApplicationWindow { onClicked: { var component = Qt.createComponent("IgnoredUsers.qml") if (component.status == Component.Ready) { - var window = component.createObject(userProfileDialog, {users: profile.getIgnoredUsers(), profile: profile}) + var window = component.createObject(userProfileDialog, { profile: profile}) window.show() timelineRoot.destroyOnClose(window) } else { diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp index b8bd679b..3e623d7a 100644 --- a/src/timeline/TimelineViewManager.cpp +++ b/src/timeline/TimelineViewManager.cpp @@ -12,6 +12,7 @@ #include #include "Cache.h" +#include "Cache_p.h" #include "ChatPage.h" #include "CombinedImagePackModel.h" #include "CommandCompleter.h" @@ -210,6 +211,7 @@ TimelineViewManager::sync(const mtx::responses::Sync &sync_) this->rooms_->sync(sync_); this->communities_->sync(sync_); this->presenceEmitter->sync(sync_.presence); + this->processIgnoredUsers(sync_.account_data); if (isInitialSync_) { this->isInitialSync_ = false; @@ -560,3 +562,39 @@ TimelineViewManager::fixImageRendering(QQuickTextDocument *t, QQuickItem *i) QObject::connect(t->textDocument(), SIGNAL(imagesLoaded()), i, SLOT(updateWholeDocument())); } } + +using IgnoredUsers = mtx::events::EphemeralEvent; + +static QVector convertIgnoredToQt(const IgnoredUsers &ev) { + QVector users; + for (const mtx::events::account_data::IgnoredUser &user : ev.content.users) { + users.push_back(QString::fromStdString(user.id)); + } + + return users; +} + +QVector +TimelineViewManager::getIgnoredUsers() +{ + const auto cache = cache::client()->getAccountData(mtx::events::EventType::IgnoredUsers); + if (!cache) { + return {}; + } + + return convertIgnoredToQt(std::get(*cache)); +} + +void +TimelineViewManager::processIgnoredUsers(const mtx::responses::AccountData &data) +{ + for (const mtx::events::collections::RoomAccountDataEvents::variant &ev : data.events) { + if (!std::holds_alternative(ev)) { + continue; + } + const auto &ignoredEv = std::get(ev); + + emit this->ignoredUsersChanged(convertIgnoredToQt(ignoredEv)); + break; + } +} \ No newline at end of file diff --git a/src/timeline/TimelineViewManager.h b/src/timeline/TimelineViewManager.h index f3bd04a2..74832c02 100644 --- a/src/timeline/TimelineViewManager.h +++ b/src/timeline/TimelineViewManager.h @@ -39,6 +39,7 @@ class TimelineViewManager final : public QObject Q_PROPERTY( bool isInitialSync MEMBER isInitialSync_ READ isInitialSync NOTIFY initialSyncChanged) Q_PROPERTY(bool isConnected READ isConnected NOTIFY isConnectedChanged) + Q_PROPERTY(QVector ignoredUsers READ getIgnoredUsers NOTIFY ignoredUsersChanged) public: TimelineViewManager(CallManager *callManager, ChatPage *parent = nullptr); @@ -62,6 +63,13 @@ public: return instance_; } + static TimelineViewManager *instance() + { + return TimelineViewManager::instance_; + } + + QVector getIgnoredUsers(); + void sync(const mtx::responses::Sync &sync_); VerificationManager *verificationManager() { return verificationManager_; } @@ -113,6 +121,7 @@ signals: QString url, double originalWidth, double proportionalHeight); + void ignoredUsersChanged(const QVector &ignoredUsers); public slots: void updateReadReceipts(const QString &room_id, const std::vector &event_ids); @@ -154,4 +163,6 @@ private: QHash, QColor> userColors; inline static TimelineViewManager *instance_ = nullptr; + + void processIgnoredUsers(const mtx::responses::AccountData &data); }; diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 5146ff26..3b2375ad 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -224,28 +224,10 @@ UserProfile::refreshDevices() fetchDeviceList(this->userid_); } -QVector -UserProfile::getIgnoredUsers() -{ - QVector vec; - const std::optional optEv = - cache::client()->getAccountData(mtx::events::EventType::IgnoredUsers); - if (optEv) { - const auto &ev = - std::get>(*optEv) - .content; - for (const mtx::events::account_data::IgnoredUser &user : ev.users) { - vec.append(QString::fromStdString(user.id)); - } - } - - return vec; -} - void UserProfile::ignoredStatus(const QString &id, const bool ignore) { - auto old = this->getIgnoredUsers(); + auto old = TimelineViewManager::instance()->getIgnoredUsers(); if (ignore) { if (old.contains(id)) { emit this->room()->ignoredUser(id, tr("Already ignored")); @@ -268,9 +250,8 @@ UserProfile::ignoredStatus(const QString &id, const bool ignore) if (ignore) { emit this->room()->ignoredUser( id, e ? std::optional(QString::fromStdString(e->matrix_error.error)) : std::nullopt); - } else { - emit this->unignoredUser( - id, e ? QVariant(QString::fromStdString(e->matrix_error.error)) : QVariant()); + } else if (e) { + emit this->unignoredUserError(id, QString::fromStdString(e->matrix_error.error)); } }); } diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index 2908b57e..1affe8bd 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -182,7 +182,6 @@ public: Q_INVOKABLE void unverify(const QString &device = QLatin1String("")); Q_INVOKABLE void fetchDeviceList(const QString &userID); Q_INVOKABLE void refreshDevices(); - Q_INVOKABLE QVector getIgnoredUsers(); Q_INVOKABLE void banUser(); Q_INVOKABLE void signOutDevice(const QString &deviceID); Q_INVOKABLE void ignoredStatus(const QString &id, const bool ignore); @@ -202,7 +201,7 @@ signals: void displayError(const QString &errorMessage); void globalUsernameRetrieved(const QString &globalUser); void devicesChanged(); - void unignoredUser(const QString &id, const QVariant &err); + void unignoredUserError(const QString &id, const QVariant &err); // internal void verificationStatiChanged(); From 44cf096111388264531fe5750a3c5caa59be7203 Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Thu, 7 Sep 2023 19:17:03 -0300 Subject: [PATCH 05/10] Lint + clazy --- src/timeline/TimelineModel.cpp | 2 +- src/timeline/TimelineViewManager.cpp | 8 +++++--- src/timeline/TimelineViewManager.h | 5 +---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index ce136e35..a384d7c2 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -2115,7 +2115,7 @@ TimelineModel::handleIgnoredUser(const QString &id, const std::optional { if (err) { MainWindow::instance()->showNotification( - tr("Failed to ignore \"%1\": %2").arg(id).arg(*err)); + tr("Failed to ignore \"%1\": %2").arg(id, *err)); } else { this->clearTimeline(); } diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp index 3e623d7a..e2616c14 100644 --- a/src/timeline/TimelineViewManager.cpp +++ b/src/timeline/TimelineViewManager.cpp @@ -565,7 +565,9 @@ TimelineViewManager::fixImageRendering(QQuickTextDocument *t, QQuickItem *i) using IgnoredUsers = mtx::events::EphemeralEvent; -static QVector convertIgnoredToQt(const IgnoredUsers &ev) { +static QVector +convertIgnoredToQt(const IgnoredUsers &ev) +{ QVector users; for (const mtx::events::account_data::IgnoredUser &user : ev.content.users) { users.push_back(QString::fromStdString(user.id)); @@ -586,14 +588,14 @@ TimelineViewManager::getIgnoredUsers() } void -TimelineViewManager::processIgnoredUsers(const mtx::responses::AccountData &data) +TimelineViewManager::processIgnoredUsers(const mtx::responses::AccountData &data) { for (const mtx::events::collections::RoomAccountDataEvents::variant &ev : data.events) { if (!std::holds_alternative(ev)) { continue; } const auto &ignoredEv = std::get(ev); - + emit this->ignoredUsersChanged(convertIgnoredToQt(ignoredEv)); break; } diff --git a/src/timeline/TimelineViewManager.h b/src/timeline/TimelineViewManager.h index 74832c02..6a825b6f 100644 --- a/src/timeline/TimelineViewManager.h +++ b/src/timeline/TimelineViewManager.h @@ -63,10 +63,7 @@ public: return instance_; } - static TimelineViewManager *instance() - { - return TimelineViewManager::instance_; - } + static TimelineViewManager *instance() { return TimelineViewManager::instance_; } QVector getIgnoredUsers(); From c4fb9ac14519848a5476942585923acbbe687fec Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Thu, 7 Sep 2023 19:22:13 -0300 Subject: [PATCH 06/10] Fix lint after clazy fix --- src/timeline/TimelineModel.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index a384d7c2..e13b56d7 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -2114,8 +2114,7 @@ void TimelineModel::handleIgnoredUser(const QString &id, const std::optional &err) { if (err) { - MainWindow::instance()->showNotification( - tr("Failed to ignore \"%1\": %2").arg(id, *err)); + MainWindow::instance()->showNotification(tr("Failed to ignore \"%1\": %2").arg(id, *err)); } else { this->clearTimeline(); } From 7c9fed91b82c174c40aefdce009090dd142b76f9 Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Thu, 7 Sep 2023 19:44:10 -0300 Subject: [PATCH 07/10] Fix license lint... --- resources/qml/dialogs/IgnoredUsers.qml | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/qml/dialogs/IgnoredUsers.qml b/resources/qml/dialogs/IgnoredUsers.qml index bdd24a57..ff1e528e 100644 --- a/resources/qml/dialogs/IgnoredUsers.qml +++ b/resources/qml/dialogs/IgnoredUsers.qml @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: Nheko Contributors // // SPDX-License-Identifier: GPL-3.0-or-later + import QtQml 2.15 import QtQuick 2.15 import QtQuick.Controls 2.15 From a0a49b6c2a3cf48cb4e979a92cd2065a51bff775 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 24 Oct 2023 01:12:01 +0200 Subject: [PATCH 08/10] Cleanup ignore user functionality slightly --- resources/qml/dialogs/UserProfile.qml | 9 ++--- src/ChatPage.cpp | 53 ++++++++++++++++++++++-- src/timeline/TimelineModel.cpp | 12 ------ src/timeline/TimelineModel.h | 6 --- src/timeline/TimelineViewManager.h | 3 +- src/ui/UserProfile.cpp | 58 +++++++++++++++++++-------- src/ui/UserProfile.h | 7 +++- 7 files changed, 102 insertions(+), 46 deletions(-) diff --git a/resources/qml/dialogs/UserProfile.qml b/resources/qml/dialogs/UserProfile.qml index 3d65d52a..989c2bab 100644 --- a/resources/qml/dialogs/UserProfile.qml +++ b/resources/qml/dialogs/UserProfile.qml @@ -295,11 +295,10 @@ ApplicationWindow { image: ":/icons/icons/ui/volume-off-indicator.svg" hoverEnabled: true ToolTip.visible: hovered - ToolTip.text: qsTr("Ignore the user.") - onClicked: { - profile.ignoredStatus(profile.userid, true) - } - visible: !profile.isSelf && !profile.isGlobalUserProfile + ToolTip.text: profile.ignored ? qsTr("Unignore the user.") : qsTr("Ignore the user.") + buttonTextColor: profile.ignored ? Nheko.theme.red : palette.buttonText + onClicked: profile.ignored = !profile.ignored + visible: !profile.isSelf } ImageButton { diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 90d542dd..e63135a9 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -6,6 +6,9 @@ #include #include +#include +#include + #include #include "AvatarProvider.h" @@ -21,7 +24,6 @@ #include "encryption/DeviceVerificationFlow.h" #include "encryption/Olm.h" #include "ui/RoomSummary.h" -#include "ui/Theme.h" #include "ui/UserProfile.h" #include "voip/CallManager.h" @@ -29,8 +31,6 @@ #include "timeline/TimelineViewManager.h" -#include "blurhash.hpp" - ChatPage *ChatPage::instance_ = nullptr; static constexpr int CHECK_CONNECTIVITY_INTERVAL = 15'000; static constexpr int RETRY_TIMEOUT = 5'000; @@ -765,6 +765,23 @@ ChatPage::handleSyncResponse(const mtx::responses::Sync &res, const std::string // Ensure that we have enough one-time keys available. ensureOneTimeKeyCount(res.device_one_time_keys_count, res.device_unused_fallback_key_types); + std::optional oldIgnoredUsers; + if (auto ignoreEv = std::ranges::find_if( + res.account_data.events, + [](const mtx::events::collections::RoomAccountDataEvents &e) { + return std::holds_alternative< + mtx::events::AccountDataEvent>(e); + }); + ignoreEv != res.account_data.events.end()) { + if (auto oldEv = cache::client()->getAccountData(mtx::events::EventType::IgnoredUsers)) + oldIgnoredUsers = + std::get>( + *oldEv) + .content; + else + oldIgnoredUsers = mtx::events::account_data::IgnoredUsers{}; + } + // TODO: fine grained error handling try { cache::client()->saveState(res); @@ -773,6 +790,36 @@ ChatPage::handleSyncResponse(const mtx::responses::Sync &res, const std::string auto updates = cache::getRoomInfo(cache::client()->roomsWithStateUpdates(res)); emit syncUI(std::move(res)); + + // if the ignored users changed, clear timeline of all affected rooms. + if (oldIgnoredUsers) { + if (auto newEv = + cache::client()->getAccountData(mtx::events::EventType::IgnoredUsers)) { + std::vector changedUsers{}; + std::ranges::set_symmetric_difference( + oldIgnoredUsers->users, + std::get>( + *newEv) + .content.users, + std::back_inserter(changedUsers), + {}, + &mtx::events::account_data::IgnoredUser::id, + &mtx::events::account_data::IgnoredUser::id); + + std::unordered_set roomsToReload; + for (const auto &user : changedUsers) { + auto commonRooms = cache::client()->getCommonRooms(user.id); + for (const auto &room : commonRooms) + roomsToReload.insert(room.first); + } + + for (const auto &room : roomsToReload) { + if (auto model = + view_manager_->rooms()->getRoomById(QString::fromStdString(room))) + model->clearTimeline(); + } + } + } } catch (const lmdb::map_full_error &e) { nhlog::db()->error("lmdb is full: {}", e.what()); cache::deleteOldData(); diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index d85a9516..e8a0a507 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -521,8 +521,6 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj cache::client()->updateState(room_id_.toStdString(), events_, true); this->syncState({std::move(events_.events)}); }); - - connect(this, &TimelineModel::ignoredUser, this, &TimelineModel::handleIgnoredUser); } QHash @@ -2222,16 +2220,6 @@ TimelineModel::scrollTimerEvent() } } -void -TimelineModel::handleIgnoredUser(const QString &id, const std::optional &err) -{ - if (err) { - MainWindow::instance()->showNotification(tr("Failed to ignore \"%1\": %2").arg(id, *err)); - } else { - this->clearTimeline(); - } -} - void TimelineModel::requestKeyForEvent(const QString &id) { diff --git a/src/timeline/TimelineModel.h b/src/timeline/TimelineModel.h index eefe921f..4ffd61ec 100644 --- a/src/timeline/TimelineModel.h +++ b/src/timeline/TimelineModel.h @@ -18,8 +18,6 @@ #include "CacheStructs.h" #include "EventStore.h" #include "InputBar.h" -#include "InviteesModel.h" -#include "MemberList.h" #include "Permissions.h" #include "ReadReceiptsModel.h" #include "ui/RoomSummary.h" @@ -463,7 +461,6 @@ public slots: private slots: void addPendingMessage(mtx::events::collections::TimelineEvents event); void scrollTimerEvent(); - void handleIgnoredUser(const QString &id, const std::optional &err); signals: void dataAtIdChanged(QString id); @@ -513,9 +510,6 @@ signals: void fetchedMore(); - // The user may close the profile window before we receive a response, so handle it here - void ignoredUser(const QString &id, const std::optional &err); - private: template void sendEncryptedMessage(mtx::events::RoomEvent msg, mtx::events::EventType eventType); diff --git a/src/timeline/TimelineViewManager.h b/src/timeline/TimelineViewManager.h index 6a825b6f..b4e176cd 100644 --- a/src/timeline/TimelineViewManager.h +++ b/src/timeline/TimelineViewManager.h @@ -11,7 +11,8 @@ #include #include -#include "ReadReceiptsModel.h" +#include "InviteesModel.h" +#include "MemberList.h" #include "timeline/CommunitiesModel.h" #include "timeline/PresenceEmitter.h" #include "timeline/RoomlistModel.h" diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 3b2375ad..1b66a97d 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -11,11 +11,11 @@ #include "Cache_p.h" #include "ChatPage.h" #include "Logging.h" +#include "MainWindow.h" +#include "MatrixClient.h" #include "UserProfile.h" #include "Utils.h" -#include "encryption/DeviceVerificationFlow.h" #include "encryption/VerificationManager.h" -#include "mtx/responses/crypto.hpp" #include "timeline/TimelineModel.h" #include "timeline/TimelineViewManager.h" #include "ui/UIA.h" @@ -64,6 +64,19 @@ UserProfile::UserProfile(const QString &roomid, new RoomInfoModel(cache::client()->getCommonRooms(userid.toStdString()), this); else sharedRooms_ = new RoomInfoModel({}, this); + + connect(ChatPage::instance(), &ChatPage::syncUI, this, [this](const mtx::responses::Sync &res) { + if (auto ignoreEv = std::ranges::find_if( + res.account_data.events, + [](const mtx::events::collections::RoomAccountDataEvents &e) { + return std::holds_alternative< + mtx::events::AccountDataEvent>(e); + }); + ignoreEv != res.account_data.events.end()) { + // doesn't matter much if it was actually us + emit ignoredChanged(); + } + }); } QHash @@ -224,34 +237,45 @@ UserProfile::refreshDevices() fetchDeviceList(this->userid_); } +bool +UserProfile::ignored() const +{ + auto old = TimelineViewManager::instance()->getIgnoredUsers(); + return old.contains(userid_); +} + void -UserProfile::ignoredStatus(const QString &id, const bool ignore) +UserProfile::setIgnored(bool ignore) { auto old = TimelineViewManager::instance()->getIgnoredUsers(); if (ignore) { - if (old.contains(id)) { - emit this->room()->ignoredUser(id, tr("Already ignored")); + if (old.contains(userid_)) { + emit ignoredChanged(); return; } - old.append(id); + old.append(userid_); } else { - old.removeOne(id); + if (!old.contains(userid_)) { + emit ignoredChanged(); + return; + } + old.removeAll(userid_); } std::vector content; - for (const QString &item : old) { - const mtx::events::account_data::IgnoredUser data{.id = item.toStdString()}; - content.push_back(data); + for (const QString &item : std::as_const(old)) { + content.emplace_back(item.toStdString()); } - const mtx::events::account_data::IgnoredUsers payload{.users{content}}; + mtx::events::account_data::IgnoredUsers payload{.users{content}}; + + auto userid = userid_; - http::client()->put_account_data(payload, [this, id, ignore](mtx::http::RequestErr e) { - if (ignore) { - emit this->room()->ignoredUser( - id, e ? std::optional(QString::fromStdString(e->matrix_error.error)) : std::nullopt); - } else if (e) { - emit this->unignoredUserError(id, QString::fromStdString(e->matrix_error.error)); + http::client()->put_account_data(payload, [userid](mtx::http::RequestErr e) { + if (e) { + MainWindow::instance()->showNotification( + tr("Failed to ignore \"%1\": %2") + .arg(userid, QString::fromStdString(e->matrix_error.error))); } }); } diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index 1affe8bd..bc5b6a35 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -157,6 +157,7 @@ class UserProfile final : public QObject Q_PROPERTY(int userVerified READ getUserStatus NOTIFY userStatusChanged) Q_PROPERTY(bool isLoading READ isLoading NOTIFY loadingChanged) Q_PROPERTY(bool userVerificationEnabled READ userVerificationEnabled NOTIFY userStatusChanged) + Q_PROPERTY(bool ignored READ ignored WRITE setIgnored NOTIFY ignoredChanged) Q_PROPERTY(bool isSelf READ isSelf CONSTANT) Q_PROPERTY(TimelineModel *room READ room CONSTANT) public: @@ -184,7 +185,6 @@ public: Q_INVOKABLE void refreshDevices(); Q_INVOKABLE void banUser(); Q_INVOKABLE void signOutDevice(const QString &deviceID); - Q_INVOKABLE void ignoredStatus(const QString &id, const bool ignore); Q_INVOKABLE void kickUser(); Q_INVOKABLE void startChat(); Q_INVOKABLE void startChat(bool encryptionEnabled); @@ -193,6 +193,9 @@ public: Q_INVOKABLE void changeAvatar(); Q_INVOKABLE void openGlobalProfile(); + void setIgnored(bool ignored); + bool ignored() const; + signals: void userStatusChanged(); void loadingChanged(); @@ -201,7 +204,7 @@ signals: void displayError(const QString &errorMessage); void globalUsernameRetrieved(const QString &globalUser); void devicesChanged(); - void unignoredUserError(const QString &id, const QVariant &err); + void ignoredChanged(); // internal void verificationStatiChanged(); From 0b28e7934d4647288d4704d6fa44259d47b79782 Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:46:04 -0300 Subject: [PATCH 09/10] Move dialog to settings page and add avatar image to delegate --- resources/qml/dialogs/IgnoredUsers.qml | 23 ++++++++++++----------- resources/qml/dialogs/UserProfile.qml | 20 -------------------- resources/qml/pages/UserSettingsPage.qml | 18 ++++++++++++++++++ src/UserSettingsPage.cpp | 6 ++++++ src/UserSettingsPage.h | 2 ++ 5 files changed, 38 insertions(+), 31 deletions(-) diff --git a/resources/qml/dialogs/IgnoredUsers.qml b/resources/qml/dialogs/IgnoredUsers.qml index ff1e528e..6527eb18 100644 --- a/resources/qml/dialogs/IgnoredUsers.qml +++ b/resources/qml/dialogs/IgnoredUsers.qml @@ -11,7 +11,6 @@ import im.nheko 1.0 Window { id: ignoredUsers - required property var profile title: qsTr("Ignored users") flags: Qt.WindowCloseButtonHint | Qt.WindowTitleHint @@ -20,14 +19,6 @@ Window { minimumHeight: 420 color: palette.window - Connections { - target: profile - function onUnignoredUserError(id, err) { - const text = qsTr("Failed to unignore \"%1\": %2").arg(id).arg(err) - MainWindow.showNotification(text) - } - } - ListView { id: view anchors.fill: parent @@ -46,7 +37,17 @@ Window { Item { Layout.preferredHeight: Nheko.paddingLarge } } delegate: RowLayout { + property var profile: TimelineManager.getGlobalUserProfile(modelData) + width: view.width + + Avatar { + enabled: false + displayName: profile.displayName + userid: profile.userid + url: profile.avatarUrl.replace("mxc://", "image://MxcImage/") + } + Text { Layout.fillWidth: true Layout.alignment: Qt.AlignLeft @@ -62,8 +63,8 @@ Window { hoverEnabled: true ToolTip.visible: hovered ToolTip.text: qsTr("Stop Ignoring.") - onClicked: profile.ignoredStatus(modelData, false) + onClicked: profile.ignored = false } } } -} \ No newline at end of file +} diff --git a/resources/qml/dialogs/UserProfile.qml b/resources/qml/dialogs/UserProfile.qml index 989c2bab..6cf747e3 100644 --- a/resources/qml/dialogs/UserProfile.qml +++ b/resources/qml/dialogs/UserProfile.qml @@ -310,26 +310,6 @@ ApplicationWindow { ToolTip.text: qsTr("Refresh device list.") onClicked: profile.refreshDevices() } - - ImageButton { - Layout.preferredHeight: 24 - Layout.preferredWidth: 24 - image: ":/icons/icons/ui/volume-off-indicator.svg" - hoverEnabled: true - ToolTip.visible: hovered - ToolTip.text: qsTr("Ignored users.") - onClicked: { - var component = Qt.createComponent("IgnoredUsers.qml") - if (component.status == Component.Ready) { - var window = component.createObject(userProfileDialog, { profile: profile}) - window.show() - timelineRoot.destroyOnClose(window) - } else { - console.error("Failed to create component: " + component.errorString()); - } - } - visible: profile.isSelf && profile.isGlobalUserProfile - } } TabBar { diff --git a/resources/qml/pages/UserSettingsPage.qml b/resources/qml/pages/UserSettingsPage.qml index 7159a2f6..2dc4684d 100644 --- a/resources/qml/pages/UserSettingsPage.qml +++ b/resources/qml/pages/UserSettingsPage.qml @@ -233,6 +233,24 @@ Rectangle { } } + DelegateChoice { + roleValue: UserSettingsModel.ManageIgnoredUsers + Button { + text: qsTr("MANAGE") + onClicked: { + var dialog = ignoredUsersDialog.createObject(); + dialog.show(); + destroyOnClose(dialog); + } + + Component { + id: ignoredUsersDialog + + IgnoredUsers {} + } + } + } + DelegateChoice { Text { text: model.value diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index 4a25880c..49b267f7 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -1043,6 +1043,8 @@ UserSettingsModel::data(const QModelIndex &index, int role) const return tr("Read receipts"); case HiddenTimelineEvents: return tr("Hidden events"); + case IgnoredUsers: + return tr("Ignored users"); case DesktopNotifications: return tr("Desktop notifications"); case AlertOnNotification: @@ -1486,6 +1488,8 @@ UserSettingsModel::data(const QModelIndex &index, int role) const return tr("Regularly redact expired events as specified in the event expiration " "configuration. Since this is currently not executed server side, you need " "to have one client running this regularly."); + case IgnoredUsers: + return tr("Manage your ignored users."); } } else if (role == Type) { switch (index.row()) { @@ -1572,6 +1576,8 @@ UserSettingsModel::data(const QModelIndex &index, int role) const return KeyStatus; case HiddenTimelineEvents: return ConfigureHiddenEvents; + case IgnoredUsers: + return ManageIgnoredUsers; } } else if (role == ValueLowerBound) { switch (index.row()) { diff --git a/src/UserSettingsPage.h b/src/UserSettingsPage.h index 2bae068a..2cf8e5ab 100644 --- a/src/UserSettingsPage.h +++ b/src/UserSettingsPage.h @@ -508,6 +508,7 @@ class UserSettingsModel : public QAbstractListModel MessageVisibilitySection, ExpireEvents, HiddenTimelineEvents, + IgnoredUsers, NotificationsSection, DesktopNotifications, @@ -566,6 +567,7 @@ public: SessionKeyImportExport, XSignKeysRequestDownload, ConfigureHiddenEvents, + ManageIgnoredUsers, }; Q_ENUM(Types); From ad68cd8b4c5eb05c807f261f7ba7e62475e41ceb Mon Sep 17 00:00:00 2001 From: NepNep21 <43792621+NepNep21@users.noreply.github.com> Date: Wed, 25 Oct 2023 19:38:04 -0300 Subject: [PATCH 10/10] Switch to X icon and add close button --- resources/qml/dialogs/IgnoredUsers.qml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/resources/qml/dialogs/IgnoredUsers.qml b/resources/qml/dialogs/IgnoredUsers.qml index 6527eb18..2d8cc920 100644 --- a/resources/qml/dialogs/IgnoredUsers.qml +++ b/resources/qml/dialogs/IgnoredUsers.qml @@ -23,6 +23,7 @@ Window { id: view anchors.fill: parent spacing: Nheko.paddingMedium + footerPositioning: ListView.OverlayFooter model: TimelineManager.ignoredUsers header: ColumnLayout { @@ -59,12 +60,24 @@ Window { ImageButton { Layout.preferredHeight: 24 Layout.preferredWidth: 24 - image: ":/icons/icons/ui/delete.svg" + image: ":/icons/icons/ui/dismiss.svg" hoverEnabled: true ToolTip.visible: hovered ToolTip.text: qsTr("Stop Ignoring.") onClicked: profile.ignored = false } } + footer: DialogButtonBox { + z: 2 + width: view.width + alignment: Qt.AlignRight + standardButtons: DialogButtonBox.Ok + onAccepted: ignoredUsers.close() + + background: Rectangle { + anchors.fill: parent + color: palette.window + } + } } }