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: Rebase --- src/timeline/TimelineModel.cpp | 13 ++++++++++ src/timeline/TimelineModel.h | 4 +++ src/ui/UserProfile.cpp | 55 +++++++++++++++++++++++++++++++++++++++--- src/ui/UserProfile.h | 4 ++- 4 files changed, 71 insertions(+), 5 deletions(-) (limited to 'src') 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(); -- cgit 1.5.1