mirror of https://github.com/Nheko-Reborn/nheko
parent
6529240be8
commit
b599f5c0c6
@ -0,0 +1,87 @@ |
||||
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
||||
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "UserDirectoryModel.h" |
||||
|
||||
#include "Cache.h" |
||||
#include "Logging.h" |
||||
#include "MatrixClient.h" |
||||
#include "mtx/responses/users.hpp" |
||||
|
||||
UserDirectoryModel::UserDirectoryModel(QObject *parent) |
||||
: QAbstractListModel{parent} |
||||
{ |
||||
connect(this, |
||||
&UserDirectoryModel::fetchedSearchResults, |
||||
this, |
||||
&UserDirectoryModel::displaySearchResults, |
||||
Qt::QueuedConnection); |
||||
} |
||||
|
||||
QHash<int, QByteArray> |
||||
UserDirectoryModel::roleNames() const |
||||
{ |
||||
return { |
||||
{Roles::DisplayName, "displayName"}, |
||||
{Roles::Mxid, "userid"}, |
||||
{Roles::AvatarUrl, "avatarUrl"}, |
||||
}; |
||||
} |
||||
|
||||
void |
||||
UserDirectoryModel::setSearchString(const QString &f) |
||||
{ |
||||
userSearchString_ = f.toStdString(); |
||||
nhlog::ui()->debug("Received user directory query: {}", userSearchString_); |
||||
beginResetModel(); |
||||
results_.clear(); |
||||
endResetModel(); |
||||
searchingUsers_ = true; |
||||
emit searchingUsersChanged(); |
||||
http::client()->search_user_directory( |
||||
userSearchString_, |
||||
[this](const mtx::responses::Users &res, mtx::http::RequestErr err) { |
||||
searchingUsers_ = false; |
||||
emit searchingUsersChanged(); |
||||
|
||||
if (err) { |
||||
nhlog::net()->error("Failed to retrieve users from mtxclient - {} - {} - {}", |
||||
mtx::errors::to_string(err->matrix_error.errcode), |
||||
err->matrix_error.error, |
||||
err->parse_error); |
||||
} else { |
||||
emit fetchedSearchResults(res.results); |
||||
} |
||||
}, |
||||
-1); |
||||
} |
||||
|
||||
QVariant |
||||
UserDirectoryModel::data(const QModelIndex &index, int role) const |
||||
{ |
||||
if (!index.isValid() || index.row() >= (int)results_.size() || index.row() < 0) |
||||
return {}; |
||||
switch (role) { |
||||
case Roles::DisplayName: |
||||
return QString::fromStdString(results_[index.row()].display_name); |
||||
case Roles::Mxid: |
||||
return QString::fromStdString(results_[index.row()].user_id); |
||||
case Roles::AvatarUrl: |
||||
return QString::fromStdString(results_[index.row()].avatar_url); |
||||
} |
||||
return {}; |
||||
} |
||||
|
||||
void |
||||
UserDirectoryModel::displaySearchResults(std::vector<mtx::responses::User> results) |
||||
{ |
||||
results_ = results; |
||||
if (results_.empty()) { |
||||
nhlog::net()->error("mtxclient helper thread yielded empty chunk!"); |
||||
return; |
||||
} |
||||
beginInsertRows(QModelIndex(), 0, static_cast<int>(results_.size()) - 1); |
||||
endInsertRows(); |
||||
} |
@ -0,0 +1,55 @@ |
||||
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
||||
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once |
||||
|
||||
#include <QAbstractListModel> |
||||
#include <QString> |
||||
#include <string> |
||||
#include <vector> |
||||
|
||||
#include <mtx/responses/users.hpp> |
||||
|
||||
class UserDirectoryModel : public QAbstractListModel |
||||
{ |
||||
Q_OBJECT |
||||
|
||||
Q_PROPERTY(bool searchingUsers READ searchingUsers NOTIFY searchingUsersChanged) |
||||
|
||||
public: |
||||
explicit UserDirectoryModel(QObject *parent = nullptr); |
||||
|
||||
enum Roles |
||||
{ |
||||
DisplayName, |
||||
Mxid, |
||||
AvatarUrl, |
||||
}; |
||||
QHash<int, QByteArray> roleNames() const override; |
||||
|
||||
QVariant data(const QModelIndex &index, int role) const override; |
||||
|
||||
inline int rowCount(const QModelIndex &parent = QModelIndex()) const override |
||||
{ |
||||
(void)parent; |
||||
return static_cast<int>(results_.size()); |
||||
} |
||||
|
||||
private: |
||||
std::vector<mtx::responses::User> results_; |
||||
std::string userSearchString_; |
||||
bool searchingUsers_{false}; |
||||
|
||||
signals: |
||||
void searchingUsersChanged(); |
||||
void fetchedSearchResults(std::vector<mtx::responses::User> results); |
||||
|
||||
public slots: |
||||
void setSearchString(const QString &f); |
||||
bool searchingUsers() const { return searchingUsers_; } |
||||
|
||||
private slots: |
||||
void displaySearchResults(std::vector<mtx::responses::User> results); |
||||
}; |
Loading…
Reference in new issue