Fi lineendings

This commit is contained in:
Nicolas Werner 2021-08-17 14:22:37 +02:00
parent 028bcd5b7c
commit 995b62122a
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
2 changed files with 307 additions and 307 deletions

View file

@ -1,209 +1,209 @@
// SPDX-FileCopyrightText: 2021 Nheko Contributors // SPDX-FileCopyrightText: 2021 Nheko Contributors
// //
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
#include "RoomDirectoryModel.h" #include "RoomDirectoryModel.h"
#include "Cache.h" #include "Cache.h"
#include "ChatPage.h" #include "ChatPage.h"
#include <algorithm> #include <algorithm>
RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &server) RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &server)
: QAbstractListModel(parent) : QAbstractListModel(parent)
, server_(server) , server_(server)
{ {
connect(ChatPage::instance(), &ChatPage::newRoom, this, [this](const QString &roomid) { connect(ChatPage::instance(), &ChatPage::newRoom, this, [this](const QString &roomid) {
auto roomid_ = roomid.toStdString(); auto roomid_ = roomid.toStdString();
int i = 0; int i = 0;
for (const auto &room : publicRoomsData_) { for (const auto &room : publicRoomsData_) {
if (room.room_id == roomid_) { if (room.room_id == roomid_) {
emit dataChanged(index(i), index(i), {Roles::CanJoin}); emit dataChanged(index(i), index(i), {Roles::CanJoin});
break; break;
} }
i++; i++;
} }
}); });
connect(this, connect(this,
&RoomDirectoryModel::fetchedRoomsBatch, &RoomDirectoryModel::fetchedRoomsBatch,
this, this,
&RoomDirectoryModel::displayRooms, &RoomDirectoryModel::displayRooms,
Qt::QueuedConnection); Qt::QueuedConnection);
} }
QHash<int, QByteArray> QHash<int, QByteArray>
RoomDirectoryModel::roleNames() const RoomDirectoryModel::roleNames() const
{ {
return { return {
{Roles::Name, "name"}, {Roles::Name, "name"},
{Roles::Id, "roomid"}, {Roles::Id, "roomid"},
{Roles::AvatarUrl, "avatarUrl"}, {Roles::AvatarUrl, "avatarUrl"},
{Roles::Topic, "topic"}, {Roles::Topic, "topic"},
{Roles::MemberCount, "numMembers"}, {Roles::MemberCount, "numMembers"},
{Roles::Previewable, "canPreview"}, {Roles::Previewable, "canPreview"},
{Roles::CanJoin, "canJoin"}, {Roles::CanJoin, "canJoin"},
}; };
} }
void void
RoomDirectoryModel::resetDisplayedData() RoomDirectoryModel::resetDisplayedData()
{ {
beginResetModel(); beginResetModel();
prevBatch_ = ""; prevBatch_ = "";
nextBatch_ = ""; nextBatch_ = "";
canFetchMore_ = true; canFetchMore_ = true;
publicRoomsData_.clear(); publicRoomsData_.clear();
endResetModel(); endResetModel();
} }
void void
RoomDirectoryModel::setMatrixServer(const QString &s) RoomDirectoryModel::setMatrixServer(const QString &s)
{ {
server_ = s.toStdString(); server_ = s.toStdString();
nhlog::ui()->debug("Received matrix server: {}", server_); nhlog::ui()->debug("Received matrix server: {}", server_);
resetDisplayedData(); resetDisplayedData();
} }
void void
RoomDirectoryModel::setSearchTerm(const QString &f) RoomDirectoryModel::setSearchTerm(const QString &f)
{ {
userSearchString_ = f.toStdString(); userSearchString_ = f.toStdString();
nhlog::ui()->debug("Received user query: {}", userSearchString_); nhlog::ui()->debug("Received user query: {}", userSearchString_);
resetDisplayedData(); resetDisplayedData();
} }
bool bool
RoomDirectoryModel::canJoinRoom(const QString &room) const RoomDirectoryModel::canJoinRoom(const QString &room) const
{ {
return !room.isEmpty() && cache::getRoomInfo({room.toStdString()}).empty(); return !room.isEmpty() && cache::getRoomInfo({room.toStdString()}).empty();
} }
std::vector<std::string> std::vector<std::string>
RoomDirectoryModel::getViasForRoom(const std::vector<std::string> &aliases) RoomDirectoryModel::getViasForRoom(const std::vector<std::string> &aliases)
{ {
std::vector<std::string> vias; std::vector<std::string> vias;
vias.reserve(aliases.size()); vias.reserve(aliases.size());
std::transform(aliases.begin(), std::transform(aliases.begin(),
aliases.end(), aliases.end(),
std::back_inserter(vias), std::back_inserter(vias),
[](const auto &alias) { return alias.substr(alias.find(":") + 1); }); [](const auto &alias) { return alias.substr(alias.find(":") + 1); });
return vias; return vias;
} }
void void
RoomDirectoryModel::joinRoom(const int &index) RoomDirectoryModel::joinRoom(const int &index)
{ {
if (index >= 0 && static_cast<size_t>(index) < publicRoomsData_.size()) { if (index >= 0 && static_cast<size_t>(index) < publicRoomsData_.size()) {
const auto &chunk = publicRoomsData_[index]; const auto &chunk = publicRoomsData_[index];
nhlog::ui()->debug("'Joining room {}", chunk.room_id); nhlog::ui()->debug("'Joining room {}", chunk.room_id);
ChatPage::instance()->joinRoomVia(chunk.room_id, getViasForRoom(chunk.aliases)); ChatPage::instance()->joinRoomVia(chunk.room_id, getViasForRoom(chunk.aliases));
} }
} }
QVariant QVariant
RoomDirectoryModel::data(const QModelIndex &index, int role) const RoomDirectoryModel::data(const QModelIndex &index, int role) const
{ {
if (hasIndex(index.row(), index.column(), index.parent())) { if (hasIndex(index.row(), index.column(), index.parent())) {
const auto &room_chunk = publicRoomsData_[index.row()]; const auto &room_chunk = publicRoomsData_[index.row()];
switch (role) { switch (role) {
case Roles::Name: case Roles::Name:
return QString::fromStdString(room_chunk.name); return QString::fromStdString(room_chunk.name);
case Roles::Id: case Roles::Id:
return QString::fromStdString(room_chunk.room_id); return QString::fromStdString(room_chunk.room_id);
case Roles::AvatarUrl: case Roles::AvatarUrl:
return QString::fromStdString(room_chunk.avatar_url); return QString::fromStdString(room_chunk.avatar_url);
case Roles::Topic: case Roles::Topic:
return QString::fromStdString(room_chunk.topic); return QString::fromStdString(room_chunk.topic);
case Roles::MemberCount: case Roles::MemberCount:
return QVariant::fromValue(room_chunk.num_joined_members); return QVariant::fromValue(room_chunk.num_joined_members);
case Roles::Previewable: case Roles::Previewable:
return QVariant::fromValue(room_chunk.world_readable); return QVariant::fromValue(room_chunk.world_readable);
case Roles::CanJoin: case Roles::CanJoin:
return canJoinRoom(QString::fromStdString(room_chunk.room_id)); return canJoinRoom(QString::fromStdString(room_chunk.room_id));
} }
} }
return {}; return {};
} }
void void
RoomDirectoryModel::fetchMore(const QModelIndex &) RoomDirectoryModel::fetchMore(const QModelIndex &)
{ {
if (!canFetchMore_) if (!canFetchMore_)
return; return;
nhlog::net()->debug("Fetching more rooms from mtxclient..."); nhlog::net()->debug("Fetching more rooms from mtxclient...");
mtx::requests::PublicRooms req; mtx::requests::PublicRooms req;
req.limit = limit_; req.limit = limit_;
req.since = prevBatch_; req.since = prevBatch_;
req.filter.generic_search_term = userSearchString_; req.filter.generic_search_term = userSearchString_;
// req.third_party_instance_id = third_party_instance_id; // req.third_party_instance_id = third_party_instance_id;
auto requested_server = server_; auto requested_server = server_;
reachedEndOfPagination_ = false; reachedEndOfPagination_ = false;
emit reachedEndOfPaginationChanged(); emit reachedEndOfPaginationChanged();
loadingMoreRooms_ = true; loadingMoreRooms_ = true;
emit loadingMoreRoomsChanged(); emit loadingMoreRoomsChanged();
http::client()->post_public_rooms( http::client()->post_public_rooms(
req, req,
[requested_server, this, req](const mtx::responses::PublicRooms &res, [requested_server, this, req](const mtx::responses::PublicRooms &res,
mtx::http::RequestErr err) { mtx::http::RequestErr err) {
loadingMoreRooms_ = false; loadingMoreRooms_ = false;
emit loadingMoreRoomsChanged(); emit loadingMoreRoomsChanged();
if (err) { if (err) {
nhlog::net()->error( nhlog::net()->error(
"Failed to retrieve rooms from mtxclient - {} - {} - {}", "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 if (req.filter.generic_search_term == this->userSearchString_ &&
req.since == this->prevBatch_ && requested_server == this->server_) { 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 fetchedRoomsBatch(res.chunk, res.next_batch);
} }
}, },
requested_server); requested_server);
} }
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)
{ {
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()) {
nhlog::net()->error("mtxclient helper thread yielded empty chunk!"); nhlog::net()->error("mtxclient helper thread yielded empty chunk!");
return; return;
} }
beginInsertRows(QModelIndex(), beginInsertRows(QModelIndex(),
static_cast<int>(publicRoomsData_.size()), static_cast<int>(publicRoomsData_.size()),
static_cast<int>(publicRoomsData_.size() + fetched_rooms.size()) - 1); static_cast<int>(publicRoomsData_.size() + fetched_rooms.size()) - 1);
this->publicRoomsData_.insert( this->publicRoomsData_.insert(
this->publicRoomsData_.end(), fetched_rooms.begin(), fetched_rooms.end()); this->publicRoomsData_.end(), fetched_rooms.begin(), fetched_rooms.end());
endInsertRows(); endInsertRows();
if (next_batch.empty()) { if (next_batch.empty()) {
canFetchMore_ = false; canFetchMore_ = false;
reachedEndOfPagination_ = true; reachedEndOfPagination_ = true;
emit reachedEndOfPaginationChanged(); emit reachedEndOfPaginationChanged();
} }
prevBatch_ = next_batch; prevBatch_ = next_batch;
nhlog::ui()->debug("Finished loading rooms"); nhlog::ui()->debug("Finished loading rooms");
} }

View file

@ -1,98 +1,98 @@
// SPDX-FileCopyrightText: 2021 Nheko Contributors // SPDX-FileCopyrightText: 2021 Nheko Contributors
// //
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
#pragma once #pragma once
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QHash> #include <QHash>
#include <QString> #include <QString>
#include <string> #include <string>
#include <vector> #include <vector>
#include "MatrixClient.h" #include "MatrixClient.h"
#include <mtx/responses/public_rooms.hpp> #include <mtx/responses/public_rooms.hpp>
#include <mtxclient/http/errors.hpp> #include <mtxclient/http/errors.hpp>
#include "Logging.h" #include "Logging.h"
namespace mtx::http { namespace mtx::http {
using RequestErr = const std::optional<mtx::http::ClientError> &; using RequestErr = const std::optional<mtx::http::ClientError> &;
} }
namespace mtx::responses { namespace mtx::responses {
struct PublicRooms; struct PublicRooms;
} }
class RoomDirectoryModel : public QAbstractListModel class RoomDirectoryModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool loadingMoreRooms READ loadingMoreRooms NOTIFY loadingMoreRoomsChanged) Q_PROPERTY(bool loadingMoreRooms READ loadingMoreRooms NOTIFY loadingMoreRoomsChanged)
Q_PROPERTY(bool reachedEndOfPagination READ reachedEndOfPagination NOTIFY Q_PROPERTY(bool reachedEndOfPagination READ reachedEndOfPagination NOTIFY
reachedEndOfPaginationChanged) reachedEndOfPaginationChanged)
public: public:
explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &server = ""); explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &server = "");
enum Roles enum Roles
{ {
Name = Qt::UserRole, Name = Qt::UserRole,
Id, Id,
AvatarUrl, AvatarUrl,
Topic, Topic,
MemberCount, MemberCount,
Previewable, Previewable,
CanJoin, CanJoin,
}; };
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
inline int rowCount(const QModelIndex &parent = QModelIndex()) const override inline int rowCount(const QModelIndex &parent = QModelIndex()) const override
{ {
(void)parent; (void)parent;
return static_cast<int>(publicRoomsData_.size()); return static_cast<int>(publicRoomsData_.size());
} }
bool canFetchMore(const QModelIndex &) const override { return canFetchMore_; } bool canFetchMore(const QModelIndex &) const override { return canFetchMore_; }
bool loadingMoreRooms() const { return loadingMoreRooms_; } bool loadingMoreRooms() const { return loadingMoreRooms_; }
bool reachedEndOfPagination() const { return reachedEndOfPagination_; } bool reachedEndOfPagination() const { return reachedEndOfPagination_; }
void fetchMore(const QModelIndex &) override; void fetchMore(const QModelIndex &) override;
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, void fetchedRoomsBatch(std::vector<mtx::responses::PublicRoomsChunk> rooms,
const std::string &next_batch); const std::string &next_batch);
void loadingMoreRoomsChanged(); void loadingMoreRoomsChanged();
void reachedEndOfPaginationChanged(); void reachedEndOfPaginationChanged();
public slots: public slots:
void setMatrixServer(const QString &s = ""); void setMatrixServer(const QString &s = "");
void setSearchTerm(const QString &f); void setSearchTerm(const QString &f);
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);
private: private:
bool canJoinRoom(const QString &room) const; bool canJoinRoom(const QString &room) const;
static constexpr size_t limit_ = 50; static constexpr size_t limit_ = 50;
std::string server_; std::string server_;
std::string userSearchString_; std::string userSearchString_;
std::string prevBatch_; std::string prevBatch_;
std::string nextBatch_; std::string nextBatch_;
bool canFetchMore_{true}; bool canFetchMore_{true};
bool loadingMoreRooms_{false}; bool loadingMoreRooms_{false};
bool reachedEndOfPagination_{false}; bool reachedEndOfPagination_{false};
std::vector<mtx::responses::PublicRoomsChunk> publicRoomsData_; std::vector<mtx::responses::PublicRoomsChunk> publicRoomsData_;
std::vector<std::string> getViasForRoom(const std::vector<std::string> &room); std::vector<std::string> getViasForRoom(const std::vector<std::string> &room);
void resetDisplayedData(); void resetDisplayedData();
}; };