summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2023-01-08 02:32:40 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2023-01-08 02:32:40 +0100
commit0b8709a0ead4bdc71666f09860a058c90e3c7a1a (patch)
tree0aa4d04c2f626aff091ff6aad534e5a885d50468
parentSpeedup quick switcher (diff)
downloadnheko-0b8709a0ead4bdc71666f09860a058c90e3c7a1a.tar.xz
Fix potential crash from the room directory
-rw-r--r--src/RoomDirectoryModel.cpp41
-rw-r--r--src/RoomDirectoryModel.h25
2 files changed, 48 insertions, 18 deletions
diff --git a/src/RoomDirectoryModel.cpp b/src/RoomDirectoryModel.cpp

index c6d45995..fbfed03d 100644 --- a/src/RoomDirectoryModel.cpp +++ b/src/RoomDirectoryModel.cpp
@@ -29,12 +29,6 @@ RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &serve i++; } }); - - connect(this, - &RoomDirectoryModel::fetchedRoomsBatch, - this, - &RoomDirectoryModel::displayRooms, - Qt::QueuedConnection); } QHash<int, QByteArray> @@ -170,22 +164,28 @@ RoomDirectoryModel::fetchMore(const QModelIndex &) loadingMoreRooms_ = true; emit loadingMoreRoomsChanged(); + auto job = QSharedPointer<FetchRoomsChunkFromDirectoryJob>::create(); + connect(job.data(), + &FetchRoomsChunkFromDirectoryJob::fetchedRoomsBatch, + this, + &RoomDirectoryModel::displayRooms); + http::client()->post_public_rooms( req, - [requested_server, this, req](const mtx::responses::PublicRooms &res, - mtx::http::RequestErr err) { - loadingMoreRooms_ = false; - emit loadingMoreRoomsChanged(); - + [requested_server, job, req](const mtx::responses::PublicRooms &res, + mtx::http::RequestErr err) { if (err) { nhlog::net()->error("Failed to retrieve rooms from mtxclient - {} - {} - {}", mtx::errors::to_string(err->matrix_error.errcode), err->matrix_error.error, err->parse_error); - } else if (req.filter.generic_search_term == this->userSearchString_ && - req.since == this->prevBatch_ && requested_server == this->server_) { + } else { nhlog::net()->debug("signalling chunk to GUI thread"); - emit fetchedRoomsBatch(res.chunk, res.next_batch); + emit job->fetchedRoomsBatch(res.chunk, + res.next_batch, + req.filter.generic_search_term, + requested_server, + req.since); } }, requested_server); @@ -193,8 +193,19 @@ RoomDirectoryModel::fetchMore(const QModelIndex &) void RoomDirectoryModel::displayRooms(std::vector<mtx::responses::PublicRoomsChunk> fetched_rooms, - const std::string &next_batch) + const std::string &next_batch, + const std::string &search_term, + const std::string &server, + const std::string &since) { + if (search_term != this->userSearchString_ || since != this->prevBatch_ || + server != this->server_) { + return; + } + + loadingMoreRooms_ = false; + emit loadingMoreRoomsChanged(); + nhlog::net()->debug("Prev batch: {} | Next batch: {}", prevBatch_, next_batch); if (fetched_rooms.empty()) { diff --git a/src/RoomDirectoryModel.h b/src/RoomDirectoryModel.h
index 54938561..2c6a5bca 100644 --- a/src/RoomDirectoryModel.h +++ b/src/RoomDirectoryModel.h
@@ -13,6 +13,24 @@ #include <mtx/responses/public_rooms.hpp> +class FetchRoomsChunkFromDirectoryJob final : public QObject +{ + Q_OBJECT + +public: + explicit FetchRoomsChunkFromDirectoryJob(QObject *p = nullptr) + : QObject(p) + { + } + +signals: + void fetchedRoomsBatch(std::vector<mtx::responses::PublicRoomsChunk> rooms, + const std::string &next_batch, + const std::string &search_term, + const std::string &server, + const std::string &since); +}; + class RoomDirectoryModel : public QAbstractListModel { Q_OBJECT @@ -55,8 +73,6 @@ public: Q_INVOKABLE void joinRoom(const int &index = -1); signals: - void fetchedRoomsBatch(std::vector<mtx::responses::PublicRoomsChunk> rooms, - const std::string &next_batch); void loadingMoreRoomsChanged(); void reachedEndOfPaginationChanged(); @@ -67,7 +83,10 @@ public slots: private slots: void displayRooms(std::vector<mtx::responses::PublicRoomsChunk> rooms, - const std::string &next_batch); + const std::string &next_batch, + const std::string &search_term, + const std::string &server, + const std::string &since); private: bool canJoinRoom(const QString &room) const;