nheko/src/UsersModel.cpp

89 lines
3.2 KiB
C++
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
// SPDX-FileCopyrightText: 2023 Nheko Contributors
2021-03-05 00:35:15 +01:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2020-09-07 11:51:28 +02:00
#include "UsersModel.h"
2021-04-24 09:12:50 +02:00
#include <QUrl>
2020-09-07 11:51:28 +02:00
#include "Cache.h"
#include "Cache_p.h"
2020-09-07 11:51:28 +02:00
#include "CompletionModelRoles.h"
2021-04-24 09:12:50 +02:00
#include "UserSettingsPage.h"
2020-09-07 11:51:28 +02:00
UsersModel::UsersModel(const std::string &roomId, QObject *parent)
: QAbstractListModel(parent)
2020-11-20 02:38:08 +01:00
, room_id(roomId)
{
// 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 {
for (const auto &m : cache::roomMembers(roomId)) {
displayNames.push_back(QString::fromStdString(cache::displayName(room_id, m)));
userids.push_back(QString::fromStdString(m));
avatarUrls.push_back(
cache::avatarUrl(QString::fromStdString(room_id), QString::fromStdString(m)));
}
2021-09-18 00:22:33 +02:00
}
2020-11-20 02:38:08 +01:00
}
QHash<int, QByteArray>
UsersModel::roleNames() const
2020-09-07 11:51:28 +02:00
{
2021-09-18 00:22:33 +02:00
return {
{CompletionModel::CompletionRole, "completionRole"},
{CompletionModel::SearchRole, "searchRole"},
{CompletionModel::SearchRole2, "searchRole2"},
{Roles::DisplayName, "displayName"},
{Roles::AvatarUrl, "avatarUrl"},
{Roles::UserID, "userid"},
};
2020-09-07 11:51:28 +02:00
}
QVariant
UsersModel::data(const QModelIndex &index, int role) const
{
2021-09-18 00:22:33 +02:00
if (hasIndex(index.row(), index.column(), index.parent())) {
switch (role) {
case CompletionModel::CompletionRole:
if (UserSettings::instance()->markdown())
return QStringLiteral("[%1](https://matrix.to/#/%2)")
.arg(QString(displayNames[index.row()])
.replace("[", "\\[")
.replace("]", "\\]")
.toHtmlEscaped(),
2021-12-28 23:22:01 +01:00
QString(QUrl::toPercentEncoding(userids[index.row()])));
2021-09-18 00:22:33 +02: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:
return avatarUrls[index.row()];
2021-09-18 00:22:33 +02:00
case Roles::UserID:
return userids[index.row()].toHtmlEscaped();
2020-09-07 11:51:28 +02:00
}
2021-09-18 00:22:33 +02:00
}
return {};
2020-09-07 11:51:28 +02:00
}