summary refs log tree commit diff
path: root/src/UserDirectoryModel.cpp
blob: 72a5a13e17aea2c6bea8b2fc9c22c6d71f89dd9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "UserDirectoryModel.h"

#include <QSharedPointer>

#include <mtx/responses/users.hpp>

#include "Logging.h"
#include "MatrixClient.h"

UserDirectoryModel::UserDirectoryModel(QObject *parent)
  : QAbstractListModel{parent}
{
}

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();
    if (userSearchString_ == "")
        nhlog::ui()->debug("Rejecting empty search string");
    else
        canFetchMore_ = true;
    endResetModel();
}

void
UserDirectoryModel::fetchMore(const QModelIndex &)
{
    if (!canFetchMore_)
        return;

    nhlog::net()->debug("Fetching users from mtxclient...");
    std::string searchTerm = userSearchString_;
    searchingUsers_        = true;
    emit searchingUsersChanged();
    auto job = QSharedPointer<FetchUsersFromDirectoryJob>::create();
    connect(job.data(),
            &FetchUsersFromDirectoryJob::fetchedSearchResults,
            this,
            &UserDirectoryModel::displaySearchResults);
    http::client()->search_user_directory(
      searchTerm,
      [job, searchTerm](const mtx::responses::Users &res, mtx::http::RequestErr err) {
          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 job->fetchedSearchResults(res.results, searchTerm);
          }
      },
      50);
}

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,
                                         const std::string &searchTerm)
{
    if (searchTerm != this->userSearchString_)
        return;
    searchingUsers_ = false;
    emit searchingUsersChanged();
    if (results.empty()) {
        nhlog::net()->debug("mtxclient helper thread yielded no results!");
        return;
    }
    beginInsertRows(QModelIndex(), 0, static_cast<int>(results.size()) - 1);
    results_ = results;
    endInsertRows();
    canFetchMore_ = false;
}