You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nheko/src/InviteesModel.cpp

103 lines
2.5 KiB

3 years ago
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
4 years ago
#include "InviteesModel.h"
#include "Cache.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "mtx/responses/profile.hpp"
InviteesModel::InviteesModel(QObject *parent)
: QAbstractListModel{parent}
{}
void
InviteesModel::addUser(QString mxid)
{
for (const auto &invitee : qAsConst(invitees_))
if (invitee->mxid_ == mxid)
return;
beginInsertRows(QModelIndex(), invitees_.count(), invitees_.count());
4 years ago
auto invitee = new Invitee{mxid, this};
auto indexOfInvitee = invitees_.count();
connect(invitee, &Invitee::userInfoLoaded, this, [this, indexOfInvitee]() {
emit dataChanged(index(indexOfInvitee), index(indexOfInvitee));
});
4 years ago
invitees_.push_back(invitee);
endInsertRows();
emit countChanged();
4 years ago
}
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;
}
}
}
4 years ago
QHash<int, QByteArray>
InviteesModel::roleNames() const
{
return {{Mxid, "mxid"}, {DisplayName, "displayName"}, {AvatarUrl, "avatarUrl"}};
4 years ago
}
QVariant
InviteesModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= (int)invitees_.size() || index.row() < 0)
return {};
4 years ago
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 {};
}
4 years ago
}
QStringList
InviteesModel::mxids()
{
QStringList mxidList;
mxidList.reserve(invitees_.size());
for (auto &invitee : qAsConst(invitees_))
mxidList.push_back(invitee->mxid_);
return mxidList;
4 years ago
}
Invitee::Invitee(QString mxid, QObject *parent)
4 years ago
: QObject{parent}
, mxid_{std::move(mxid)}
4 years ago
{
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;
}
4 years ago
displayName_ = QString::fromStdString(res.display_name);
avatarUrl_ = QString::fromStdString(res.avatar_url);
4 years ago
emit userInfoLoaded();
});
4 years ago
}