| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "UsersModel.h" |
| 6 | |
| 7 | #include <QUrl> |
| 8 | |
| 9 | #include "Cache.h" |
| 10 | #include "Cache_p.h" |
| 11 | #include "CompletionModelRoles.h" |
| 12 | #include "Logging.h" |
| 13 | #include "UserSettingsPage.h" |
| 14 | #include "Utils.h" |
| 15 | |
| 16 | UsersModel::UsersModel(const std::string &roomId, QObject *parent) |
| 17 | : QAbstractListModel(parent) |
| 18 | , room_id(roomId) |
| 19 | { |
| 20 | // obviously, "friends" isn't a room, but I felt this was the least invasive way |
| 21 | if (roomId == "friends") { |
| 22 | auto e = cache::client()->getAccountData(mtx::events::EventType::Direct); |
| 23 | if (e) { |
| 24 | if (auto event = |
| 25 | std::get_if<mtx::events::AccountDataEvent<mtx::events::account_data::Direct>>( |
| 26 | &e.value())) { |
| 27 | for (const auto &[userId, roomIds] : event->content.user_to_rooms) { |
| 28 | if (roomIds.empty()) |
| 29 | continue; |
| 30 | |
| 31 | displayNames.push_back( |
| 32 | QString::fromStdString(cache::displayName(roomIds.at(0), userId))); |
| 33 | userids.push_back(QString::fromStdString(userId)); |
| 34 | avatarUrls.push_back(cache::avatarUrl(QString::fromStdString(roomIds.at(0)), |
| 35 | QString::fromStdString(userId))); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } else { |
| 40 | const auto start_at = std::chrono::steady_clock::now(); |
| 41 | for (const auto &m : cache::getMembers(roomId, 0, -1)) { |
| 42 | displayNames.push_back(m.display_name); |
| 43 | userids.push_back(m.user_id); |
| 44 | avatarUrls.push_back(m.avatar_url); |
| 45 | } |
| 46 | const auto end_at = std::chrono::steady_clock::now(); |
| 47 | const auto build_time = std::chrono::duration<double, std::milli>(end_at - start_at); |
| 48 | nhlog::ui()->debug("UsersModel: build data: {} ms", build_time.count()); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | QHash<int, QByteArray> |
| 53 | UsersModel::roleNames() const |
| 54 | { |
| 55 | return { |
| 56 | {CompletionModel::CompletionRole, "completionRole"}, |
| 57 | {CompletionModel::SearchRole, "searchRole"}, |
| 58 | {CompletionModel::SearchRole2, "searchRole2"}, |
| 59 | {Roles::DisplayName, "displayName"}, |
| 60 | {Roles::AvatarUrl, "avatarUrl"}, |
| 61 | {Roles::UserID, "userid"}, |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | QVariant |
| 66 | UsersModel::data(const QModelIndex &index, int role) const |
| 67 | { |
| 68 | if (hasIndex(index.row(), index.column(), index.parent())) { |
| 69 | switch (role) { |
| 70 | case CompletionModel::CompletionRole: |
| 71 | if (UserSettings::instance()->markdown()) |
| 72 | return QStringLiteral("[%1](https://matrix.to/#/%2)") |
| 73 | .arg(utils::escapeMentionMarkdown(QString(displayNames[index.row()])), |
| 74 | QString(QUrl::toPercentEncoding(userids[index.row()]))); |
| 75 | else |
| 76 | return displayNames[index.row()]; |
| 77 | case CompletionModel::SearchRole: |
| 78 | return displayNames[index.row()]; |
| 79 | case Qt::DisplayRole: |
| 80 | case Roles::DisplayName: |
| 81 | return displayNames[index.row()].toHtmlEscaped(); |
| 82 | case CompletionModel::SearchRole2: |
| 83 | return userids[index.row()]; |
| 84 | case Roles::AvatarUrl: |
| 85 | return avatarUrls[index.row()]; |
| 86 | case Roles::UserID: |
| 87 | return userids[index.row()].toHtmlEscaped(); |
| 88 | } |
| 89 | } |
| 90 | return {}; |
| 91 | } |