matrixion/src/InviteesModel.cpp

116 lines
3.1 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Nheko Contributors
2021-07-17 20:31:38 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2021-06-11 03:13:12 +03:00
#include "InviteesModel.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "mtx/responses/profile.hpp"
#include "timeline/TimelineModel.h"
2021-06-11 03:13:12 +03:00
InviteesModel::InviteesModel(TimelineModel *room, QObject *parent)
2023-02-27 17:08:26 +03:00
: QAbstractListModel{parent}
, room_{room}
2022-09-25 21:05:08 +03:00
{
}
2021-06-11 03:13:12 +03:00
void
InviteesModel::addUser(QString mxid, QString displayName, QString avatarUrl)
2021-06-11 03:13:12 +03:00
{
2023-10-14 00:28:57 +03:00
for (const auto &invitee : std::as_const(invitees_))
2021-11-13 23:05:37 +03:00
if (invitee->mxid_ == mxid)
return;
2021-09-18 01:22:33 +03:00
beginInsertRows(QModelIndex(), invitees_.count(), invitees_.count());
2021-06-11 03:13:12 +03:00
auto invitee = new Invitee{mxid, displayName, avatarUrl, this};
2021-09-18 01:22:33 +03:00
auto indexOfInvitee = invitees_.count();
connect(invitee, &Invitee::userInfoLoaded, this, [this, indexOfInvitee]() {
emit dataChanged(index(indexOfInvitee), index(indexOfInvitee));
});
2021-06-11 03:13:12 +03:00
2021-09-18 01:22:33 +03:00
invitees_.push_back(invitee);
2021-09-18 01:22:33 +03:00
endInsertRows();
emit countChanged();
2021-06-11 03:13:12 +03:00
}
void
InviteesModel::removeUser(QString mxid)
{
for (int i = 0; i < invitees_.length(); ++i) {
if (invitees_[i]->mxid_ == mxid) {
beginRemoveRows(QModelIndex(), i, i);
invitees_.removeAt(i);
endRemoveRows();
emit countChanged();
break;
}
}
}
2021-06-11 03:13:12 +03:00
QHash<int, QByteArray>
InviteesModel::roleNames() const
{
2021-09-18 01:22:33 +03:00
return {{Mxid, "mxid"}, {DisplayName, "displayName"}, {AvatarUrl, "avatarUrl"}};
2021-06-11 03:13:12 +03:00
}
QVariant
InviteesModel::data(const QModelIndex &index, int role) const
{
2021-09-18 01:22:33 +03:00
if (!index.isValid() || index.row() >= (int)invitees_.size() || index.row() < 0)
return {};
2021-06-11 03:13:12 +03:00
2021-09-18 01:22:33 +03:00
switch (role) {
case Mxid:
return invitees_[index.row()]->mxid_;
case DisplayName:
return invitees_[index.row()]->displayName_;
case AvatarUrl:
return invitees_[index.row()]->avatarUrl_;
default:
return {};
}
2021-06-11 03:13:12 +03:00
}
QStringList
InviteesModel::mxids()
{
2021-09-18 01:22:33 +03:00
QStringList mxidList;
mxidList.reserve(invitees_.size());
2023-10-14 00:28:57 +03:00
for (auto &invitee : std::as_const(invitees_))
mxidList.push_back(invitee->mxid_);
2021-09-18 01:22:33 +03:00
return mxidList;
2021-06-11 03:13:12 +03:00
}
Invitee::Invitee(QString mxid, QString displayName, QString avatarUrl, QObject *parent)
2021-06-11 03:13:12 +03:00
: QObject{parent}
, mxid_{std::move(mxid)}
2021-06-11 03:13:12 +03:00
{
// checking for empty avatarUrl will cause profiles that don't have an avatar
// to needlessly be loaded. Can we make sure we either provide both or none?
if (displayName == "" && avatarUrl == "") {
http::client()->get_profile(
mxid_.toStdString(),
[this](const mtx::responses::Profile &res, mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn("failed to retrieve profile info");
emit userInfoLoaded();
return;
}
displayName_ = QString::fromStdString(res.display_name);
avatarUrl_ = QString::fromStdString(res.avatar_url);
2021-06-11 03:13:12 +03:00
emit userInfoLoaded();
});
} else {
displayName_ = displayName;
avatarUrl_ = avatarUrl;
emit userInfoLoaded();
}
2021-06-11 03:13:12 +03:00
}
#include "moc_InviteesModel.cpp"