2021-07-17 20:31:38 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-06-11 03:13:12 +03:00
|
|
|
#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)
|
|
|
|
{
|
|
|
|
beginInsertRows(QModelIndex(), invitees_.count(), invitees_.count());
|
|
|
|
|
|
|
|
auto invitee = new Invitee{mxid, this};
|
2021-07-21 02:20:55 +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
|
|
|
|
|
|
|
invitees_.push_back(invitee);
|
2021-07-19 19:31:20 +03:00
|
|
|
|
2021-07-21 02:20:55 +03:00
|
|
|
endInsertRows();
|
2021-07-19 19:31:20 +03:00
|
|
|
emit countChanged();
|
2021-06-11 03:13:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
InviteesModel::roleNames() const
|
|
|
|
{
|
|
|
|
return {{Mxid, "mxid"}, {DisplayName, "displayName"}, {AvatarUrl, "avatarUrl"}};
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
InviteesModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid() || index.row() >= (int)invitees_.size() || index.row() < 0)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList
|
|
|
|
InviteesModel::mxids()
|
|
|
|
{
|
|
|
|
QStringList mxidList;
|
|
|
|
for (int i = 0; i < invitees_.length(); ++i)
|
|
|
|
mxidList.push_back(invitees_[i]->mxid_);
|
|
|
|
return mxidList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Invitee::Invitee(const QString &mxid, QObject *parent)
|
|
|
|
: QObject{parent}
|
|
|
|
, mxid_{mxid}
|
|
|
|
{
|
|
|
|
http::client()->get_profile(
|
|
|
|
mxid_.toStdString(),
|
|
|
|
[this](const mtx::responses::Profile &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
2021-07-21 02:21:04 +03:00
|
|
|
nhlog::net()->warn("failed to retrieve profile info");
|
|
|
|
emit userInfoLoaded();
|
2021-06-11 03:13:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
displayName_ = QString::fromStdString(res.display_name);
|
|
|
|
avatarUrl_ = QString::fromStdString(res.avatar_url);
|
|
|
|
|
|
|
|
emit userInfoLoaded();
|
|
|
|
});
|
|
|
|
}
|