| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "UserDirectoryModel.h" |
| 6 | |
| 7 | #include <QSharedPointer> |
| 8 | |
| 9 | #include <mtx/responses/users.hpp> |
| 10 | |
| 11 | #include "Logging.h" |
| 12 | #include "MatrixClient.h" |
| 13 | |
| 14 | UserDirectoryModel::UserDirectoryModel(QObject *parent) |
| 15 | : QAbstractListModel{parent} |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | QHash<int, QByteArray> |
| 20 | UserDirectoryModel::roleNames() const |
| 21 | { |
| 22 | return { |
| 23 | {Roles::DisplayName, "displayName"}, |
| 24 | {Roles::Mxid, "userid"}, |
| 25 | {Roles::AvatarUrl, "avatarUrl"}, |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | UserDirectoryModel::setSearchString(const QString &f) |
| 31 | { |
| 32 | userSearchString_ = f.toStdString(); |
| 33 | nhlog::ui()->debug("Received user directory query: {}", userSearchString_); |
| 34 | beginResetModel(); |
| 35 | results_.clear(); |
| 36 | if (userSearchString_ == "") |
| 37 | nhlog::ui()->debug("Rejecting empty search string"); |
| 38 | else |
| 39 | canFetchMore_ = true; |
| 40 | endResetModel(); |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | UserDirectoryModel::fetchMore(const QModelIndex &) |
| 45 | { |
| 46 | if (!canFetchMore_) |
| 47 | return; |
| 48 | |
| 49 | nhlog::net()->debug("Fetching users from mtxclient..."); |
| 50 | std::string searchTerm = userSearchString_; |
| 51 | searchingUsers_ = true; |
| 52 | emit searchingUsersChanged(); |
| 53 | auto job = QSharedPointer<FetchUsersFromDirectoryJob>::create(); |
| 54 | connect(job.data(), |
| 55 | &FetchUsersFromDirectoryJob::fetchedSearchResults, |
| 56 | this, |
| 57 | &UserDirectoryModel::displaySearchResults); |
| 58 | http::client()->search_user_directory( |
| 59 | searchTerm, |
| 60 | [job, searchTerm](const mtx::responses::Users &res, mtx::http::RequestErr err) { |
| 61 | if (err) { |
| 62 | nhlog::net()->error("Failed to retrieve users from mtxclient - {} - {} - {}", |
| 63 | mtx::errors::to_string(err->matrix_error.errcode), |
| 64 | err->matrix_error.error, |
| 65 | err->parse_error); |
| 66 | } else { |
| 67 | emit job->fetchedSearchResults(res.results, searchTerm); |
| 68 | } |
| 69 | }, |
| 70 | 50); |
| 71 | } |
| 72 | |
| 73 | QVariant |
| 74 | UserDirectoryModel::data(const QModelIndex &index, int role) const |
| 75 | { |
| 76 | if (!index.isValid() || index.row() >= (int)results_.size() || index.row() < 0) |
| 77 | return {}; |
| 78 | switch (role) { |
| 79 | case Roles::DisplayName: |
| 80 | return QString::fromStdString(results_[index.row()].display_name); |
| 81 | case Roles::Mxid: |
| 82 | return QString::fromStdString(results_[index.row()].user_id); |
| 83 | case Roles::AvatarUrl: |
| 84 | return QString::fromStdString(results_[index.row()].avatar_url); |
| 85 | } |
| 86 | return {}; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | UserDirectoryModel::displaySearchResults(std::vector<mtx::responses::User> results, |
| 91 | const std::string &searchTerm) |
| 92 | { |
| 93 | if (searchTerm != this->userSearchString_) |
| 94 | return; |
| 95 | searchingUsers_ = false; |
| 96 | emit searchingUsersChanged(); |
| 97 | if (results.empty()) { |
| 98 | nhlog::net()->debug("mtxclient helper thread yielded no results!"); |
| 99 | return; |
| 100 | } |
| 101 | beginInsertRows(QModelIndex(), 0, static_cast<int>(results.size()) - 1); |
| 102 | results_ = results; |
| 103 | endInsertRows(); |
| 104 | canFetchMore_ = false; |
| 105 | } |
| 106 | |
| 107 | #include "moc_UserDirectoryModel.cpp" |