summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2023-10-23 23:58:53 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2023-10-23 23:58:53 +0200
commitfce026725e52b59a79e34dcfb70953173f3cd3be (patch)
tree57f5bc5ab630cd20846b0e590063c80208cbc12a
parentprevent overscroll in roomlist and communities list (diff)
parentFix license lint... (diff)
downloadnheko-fce026725e52b59a79e34dcfb70953173f3cd3be.tar.xz
Merge branch 'ignore-users' of github.com:NepNep21/nheko into ignore-users
-rw-r--r--CMakeLists.txt1
-rw-r--r--resources/qml/dialogs/IgnoredUsers.qml69
-rw-r--r--resources/qml/dialogs/UserProfile.qml32
-rw-r--r--src/timeline/TimelineModel.cpp12
-rw-r--r--src/timeline/TimelineModel.h4
-rw-r--r--src/timeline/TimelineViewManager.cpp40
-rw-r--r--src/timeline/TimelineViewManager.h8
-rw-r--r--src/ui/UserProfile.cpp36
-rw-r--r--src/ui/UserProfile.h3
9 files changed, 200 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 996d3aba..67eff75e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -777,6 +777,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..ff1e528e
--- /dev/null
+++ b/resources/qml/dialogs/IgnoredUsers.qml
@@ -0,0 +1,69 @@
+// 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 var profile
+
+    title: qsTr("Ignored users")
+    flags: Qt.WindowCloseButtonHint | Qt.WindowTitleHint
+    height: 650
+    width: 420
+    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
+        spacing: Nheko.paddingMedium
+
+        model: TimelineManager.ignoredUsers
+        header: ColumnLayout {
+            Text {
+                Layout.fillWidth: true
+                Layout.maximumWidth: view.width
+                wrapMode: Text.Wrap
+                color: palette.text
+                text: qsTr("Ignoring a user hides their messages (they can still see yours!).")
+            }
+
+            Item { Layout.preferredHeight: Nheko.paddingLarge }
+        }
+        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..3d65d52a 100644
--- a/resources/qml/dialogs/UserProfile.qml
+++ b/resources/qml/dialogs/UserProfile.qml
@@ -292,6 +292,19 @@ ApplicationWindow {
                 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
                     image: ":/icons/icons/ui/refresh.svg"
                     hoverEnabled: true
                     ToolTip.visible: hovered
@@ -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, { 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 e8a0a507..d85a9516 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<int, QByteArray>
@@ -2221,6 +2223,16 @@ TimelineModel::scrollTimerEvent()
 }
 
 void
+TimelineModel::handleIgnoredUser(const QString &id, const std::optional<QString> &err)
+{
+    if (err) {
+        MainWindow::instance()->showNotification(tr("Failed to ignore \"%1\": %2").arg(id, *err));
+    } else {
+        this->clearTimeline();
+    }
+}
+
+void
 TimelineModel::requestKeyForEvent(const QString &id)
 {
     auto encrypted_event = events.get(id.toStdString(), "", false);
diff --git a/src/timeline/TimelineModel.h b/src/timeline/TimelineModel.h
index 23c3c802..eefe921f 100644
--- a/src/timeline/TimelineModel.h
+++ b/src/timeline/TimelineModel.h
@@ -463,6 +463,7 @@ public slots:
 private slots:
     void addPendingMessage(mtx::events::collections::TimelineEvents event);
     void scrollTimerEvent();
+    void handleIgnoredUser(const QString &id, const std::optional<QString> &err);
 
 signals:
     void dataAtIdChanged(QString id);
@@ -512,6 +513,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<QString> &err);
+
 private:
     template<typename T>
     void sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::EventType eventType);
diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp
index b8bd679b..e2616c14 100644
--- a/src/timeline/TimelineViewManager.cpp
+++ b/src/timeline/TimelineViewManager.cpp
@@ -12,6 +12,7 @@
 #include <QString>
 
 #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,41 @@ TimelineViewManager::fixImageRendering(QQuickTextDocument *t, QQuickItem *i)
         QObject::connect(t->textDocument(), SIGNAL(imagesLoaded()), i, SLOT(updateWholeDocument()));
     }
 }
+
+using IgnoredUsers = mtx::events::EphemeralEvent<mtx::events::account_data::IgnoredUsers>;
+
+static QVector<QString>
+convertIgnoredToQt(const IgnoredUsers &ev)
+{
+    QVector<QString> users;
+    for (const mtx::events::account_data::IgnoredUser &user : ev.content.users) {
+        users.push_back(QString::fromStdString(user.id));
+    }
+
+    return users;
+}
+
+QVector<QString>
+TimelineViewManager::getIgnoredUsers()
+{
+    const auto cache = cache::client()->getAccountData(mtx::events::EventType::IgnoredUsers);
+    if (!cache) {
+        return {};
+    }
+
+    return convertIgnoredToQt(std::get<IgnoredUsers>(*cache));
+}
+
+void
+TimelineViewManager::processIgnoredUsers(const mtx::responses::AccountData &data)
+{
+    for (const mtx::events::collections::RoomAccountDataEvents::variant &ev : data.events) {
+        if (!std::holds_alternative<IgnoredUsers>(ev)) {
+            continue;
+        }
+        const auto &ignoredEv = std::get<IgnoredUsers>(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..6a825b6f 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<QString> ignoredUsers READ getIgnoredUsers NOTIFY ignoredUsersChanged)
 
 public:
     TimelineViewManager(CallManager *callManager, ChatPage *parent = nullptr);
@@ -62,6 +63,10 @@ public:
         return instance_;
     }
 
+    static TimelineViewManager *instance() { return TimelineViewManager::instance_; }
+
+    QVector<QString> getIgnoredUsers();
+
     void sync(const mtx::responses::Sync &sync_);
 
     VerificationManager *verificationManager() { return verificationManager_; }
@@ -113,6 +118,7 @@ signals:
                           QString url,
                           double originalWidth,
                           double proportionalHeight);
+    void ignoredUsersChanged(const QVector<QString> &ignoredUsers);
 
 public slots:
     void updateReadReceipts(const QString &room_id, const std::vector<QString> &event_ids);
@@ -154,4 +160,6 @@ private:
     QHash<QPair<QString, quint64>, 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 80def409..3b2375ad 100644
--- a/src/ui/UserProfile.cpp
+++ b/src/ui/UserProfile.cpp
@@ -225,6 +225,38 @@ UserProfile::refreshDevices()
 }
 
 void
+UserProfile::ignoredStatus(const QString &id, const bool ignore)
+{
+    auto old = TimelineViewManager::instance()->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<mtx::events::account_data::IgnoredUser> 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 if (e) {
+            emit this->unignoredUserError(id, QString::fromStdString(e->matrix_error.error));
+        }
+    });
+}
+
+void
 UserProfile::fetchDeviceList(const QString &userID)
 {
     if (!cache::client() || !cache::client()->isDatabaseReady())
@@ -345,10 +377,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..1affe8bd 100644
--- a/src/ui/UserProfile.h
+++ b/src/ui/UserProfile.h
@@ -184,7 +184,7 @@ public:
     Q_INVOKABLE void refreshDevices();
     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 +201,7 @@ signals:
     void displayError(const QString &errorMessage);
     void globalUsernameRetrieved(const QString &globalUser);
     void devicesChanged();
+    void unignoredUserError(const QString &id, const QVariant &err);
 
     // internal
     void verificationStatiChanged();