Fix potential crash from the room directory

This commit is contained in:
Nicolas Werner 2023-01-08 02:32:40 +01:00
parent fd949f0b83
commit 0b8709a0ea
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
2 changed files with 48 additions and 18 deletions

View file

@ -29,12 +29,6 @@ RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &serve
i++; i++;
} }
}); });
connect(this,
&RoomDirectoryModel::fetchedRoomsBatch,
this,
&RoomDirectoryModel::displayRooms,
Qt::QueuedConnection);
} }
QHash<int, QByteArray> QHash<int, QByteArray>
@ -170,22 +164,28 @@ RoomDirectoryModel::fetchMore(const QModelIndex &)
loadingMoreRooms_ = true; loadingMoreRooms_ = true;
emit loadingMoreRoomsChanged(); emit loadingMoreRoomsChanged();
auto job = QSharedPointer<FetchRoomsChunkFromDirectoryJob>::create();
connect(job.data(),
&FetchRoomsChunkFromDirectoryJob::fetchedRoomsBatch,
this,
&RoomDirectoryModel::displayRooms);
http::client()->post_public_rooms( http::client()->post_public_rooms(
req, req,
[requested_server, this, req](const mtx::responses::PublicRooms &res, [requested_server, job, req](const mtx::responses::PublicRooms &res,
mtx::http::RequestErr err) { mtx::http::RequestErr err) {
loadingMoreRooms_ = false;
emit loadingMoreRoomsChanged();
if (err) { if (err) {
nhlog::net()->error("Failed to retrieve rooms from mtxclient - {} - {} - {}", nhlog::net()->error("Failed to retrieve rooms from mtxclient - {} - {} - {}",
mtx::errors::to_string(err->matrix_error.errcode), mtx::errors::to_string(err->matrix_error.errcode),
err->matrix_error.error, err->matrix_error.error,
err->parse_error); err->parse_error);
} else if (req.filter.generic_search_term == this->userSearchString_ && } else {
req.since == this->prevBatch_ && requested_server == this->server_) {
nhlog::net()->debug("signalling chunk to GUI thread"); 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); requested_server);
@ -193,8 +193,19 @@ RoomDirectoryModel::fetchMore(const QModelIndex &)
void void
RoomDirectoryModel::displayRooms(std::vector<mtx::responses::PublicRoomsChunk> fetched_rooms, 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); nhlog::net()->debug("Prev batch: {} | Next batch: {}", prevBatch_, next_batch);
if (fetched_rooms.empty()) { if (fetched_rooms.empty()) {

View file

@ -13,6 +13,24 @@
#include <mtx/responses/public_rooms.hpp> #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 class RoomDirectoryModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
@ -55,8 +73,6 @@ public:
Q_INVOKABLE void joinRoom(const int &index = -1); Q_INVOKABLE void joinRoom(const int &index = -1);
signals: signals:
void fetchedRoomsBatch(std::vector<mtx::responses::PublicRoomsChunk> rooms,
const std::string &next_batch);
void loadingMoreRoomsChanged(); void loadingMoreRoomsChanged();
void reachedEndOfPaginationChanged(); void reachedEndOfPaginationChanged();
@ -67,7 +83,10 @@ public slots:
private slots: private slots:
void displayRooms(std::vector<mtx::responses::PublicRoomsChunk> rooms, 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: private:
bool canJoinRoom(const QString &room) const; bool canJoinRoom(const QString &room) const;