2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2023-01-02 06:25:33 +03:00
|
|
|
// SPDX-FileCopyrightText: 2023 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-09-07 12:51:28 +03:00
|
|
|
#include "UsersModel.h"
|
|
|
|
|
2021-04-24 10:12:50 +03:00
|
|
|
#include <QUrl>
|
|
|
|
|
2020-09-07 12:51:28 +03:00
|
|
|
#include "Cache.h"
|
2023-01-31 19:59:49 +03:00
|
|
|
#include "Cache_p.h"
|
2020-09-07 12:51:28 +03:00
|
|
|
#include "CompletionModelRoles.h"
|
2021-04-24 10:12:50 +03:00
|
|
|
#include "UserSettingsPage.h"
|
2020-09-07 12:51:28 +03:00
|
|
|
|
|
|
|
UsersModel::UsersModel(const std::string &roomId, QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
2020-11-20 04:38:08 +03:00
|
|
|
, room_id(roomId)
|
|
|
|
{
|
2023-01-31 19:59:49 +03:00
|
|
|
// obviously, "friends" isn't a room, but I felt this was the least invasive way
|
|
|
|
if (roomId == "friends") {
|
|
|
|
auto e = cache::client()->getAccountData(mtx::events::EventType::Direct);
|
|
|
|
if (e) {
|
|
|
|
if (auto event =
|
|
|
|
std::get_if<mtx::events::AccountDataEvent<mtx::events::account_data::Direct>>(
|
|
|
|
&e.value())) {
|
|
|
|
for (const auto &[userId, roomIds] : event->content.user_to_rooms) {
|
|
|
|
displayNames.push_back(
|
|
|
|
QString::fromStdString(cache::displayName(roomIds[0], userId)));
|
|
|
|
userids.push_back(QString::fromStdString(userId));
|
|
|
|
avatarUrls.push_back(cache::avatarUrl(QString::fromStdString(roomIds[0]),
|
|
|
|
QString::fromStdString(userId)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-31 23:04:59 +03:00
|
|
|
const auto start_at = std::chrono::steady_clock::now();
|
|
|
|
for (const auto &m : cache::getMembers(roomId, 0, -1)) {
|
|
|
|
displayNames.push_back(m.display_name);
|
|
|
|
userids.push_back(m.user_id);
|
|
|
|
avatarUrls.push_back(m.avatar_url);
|
2023-01-31 19:59:49 +03:00
|
|
|
}
|
2023-01-31 23:04:59 +03:00
|
|
|
const auto end_at = std::chrono::steady_clock::now();
|
|
|
|
const auto build_time = std::chrono::duration<double, std::milli>(end_at - start_at);
|
|
|
|
nhlog::ui()->debug("UsersModel: build data: {} ms", build_time.count());
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-11-20 04:38:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
UsersModel::roleNames() const
|
2020-09-07 12:51:28 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return {
|
|
|
|
{CompletionModel::CompletionRole, "completionRole"},
|
|
|
|
{CompletionModel::SearchRole, "searchRole"},
|
|
|
|
{CompletionModel::SearchRole2, "searchRole2"},
|
|
|
|
{Roles::DisplayName, "displayName"},
|
|
|
|
{Roles::AvatarUrl, "avatarUrl"},
|
|
|
|
{Roles::UserID, "userid"},
|
|
|
|
};
|
2020-09-07 12:51:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
UsersModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (hasIndex(index.row(), index.column(), index.parent())) {
|
|
|
|
switch (role) {
|
|
|
|
case CompletionModel::CompletionRole:
|
|
|
|
if (UserSettings::instance()->markdown())
|
2021-12-29 06:28:08 +03:00
|
|
|
return QStringLiteral("[%1](https://matrix.to/#/%2)")
|
2022-07-20 14:52:13 +03:00
|
|
|
.arg(QString(displayNames[index.row()])
|
|
|
|
.replace("[", "\\[")
|
|
|
|
.replace("]", "\\]")
|
|
|
|
.toHtmlEscaped(),
|
2021-12-29 01:22:01 +03:00
|
|
|
QString(QUrl::toPercentEncoding(userids[index.row()])));
|
2021-09-18 01:22:33 +03:00
|
|
|
else
|
|
|
|
return displayNames[index.row()];
|
|
|
|
case CompletionModel::SearchRole:
|
|
|
|
return displayNames[index.row()];
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Roles::DisplayName:
|
|
|
|
return displayNames[index.row()].toHtmlEscaped();
|
|
|
|
case CompletionModel::SearchRole2:
|
|
|
|
return userids[index.row()];
|
|
|
|
case Roles::AvatarUrl:
|
2023-01-31 19:59:49 +03:00
|
|
|
return avatarUrls[index.row()];
|
2021-09-18 01:22:33 +03:00
|
|
|
case Roles::UserID:
|
|
|
|
return userids[index.row()].toHtmlEscaped();
|
2020-09-07 12:51:28 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return {};
|
2020-09-07 12:51:28 +03:00
|
|
|
}
|