Debian Codegraph @

src/InviteesModel.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "InviteesModel.h"
6
7 #include "Logging.h"
8 #include "MatrixClient.h"
9 #include "mtx/responses/profile.hpp"
10 #include "timeline/TimelineModel.h"
11
12 InviteesModel::InviteesModel(TimelineModel *room, QObject *parent)
13 : QAbstractListModel{parent}
14 , room_{room}
15 {
16 }
17
18 void
19 InviteesModel::addUser(QString mxid, QString displayName, QString avatarUrl)
20 {
21 for (const auto &invitee : std::as_const(invitees_))
22 if (invitee->mxid_ == mxid)
23 return;
24
25 beginInsertRows(QModelIndex(), invitees_.count(), invitees_.count());
26
27 auto invitee = new Invitee{mxid, displayName, avatarUrl, this};
28 auto indexOfInvitee = invitees_.count();
29 connect(invitee, &Invitee::userInfoLoaded, this, [this, indexOfInvitee]() {
30 emit dataChanged(index(indexOfInvitee), index(indexOfInvitee));
31 });
32
33 invitees_.push_back(invitee);
34
35 endInsertRows();
36 emit countChanged();
37 }
38
39 void
40 InviteesModel::removeUser(QString mxid)
41 {
42 for (int i = 0; i < invitees_.length(); ++i) {
43 if (invitees_[i]->mxid_ == mxid) {
44 beginRemoveRows(QModelIndex(), i, i);
45 invitees_.removeAt(i);
46 endRemoveRows();
47 emit countChanged();
48 break;
49 }
50 }
51 }
52
53 QHash<int, QByteArray>
54 InviteesModel::roleNames() const
55 {
56 return {{Mxid, "mxid"}, {DisplayName, "displayName"}, {AvatarUrl, "avatarUrl"}};
57 }
58
59 QVariant
60 InviteesModel::data(const QModelIndex &index, int role) const
61 {
62 if (!index.isValid() || index.row() >= (int)invitees_.size() || index.row() < 0)
63 return {};
64
65 switch (role) {
66 case Mxid:
67 return invitees_[index.row()]->mxid_;
68 case DisplayName:
69 return invitees_[index.row()]->displayName_;
70 case AvatarUrl:
71 return invitees_[index.row()]->avatarUrl_;
72 default:
73 return {};
74 }
75 }
76
77 QStringList
78 InviteesModel::mxids()
79 {
80 QStringList mxidList;
81 mxidList.reserve(invitees_.size());
82 for (auto &invitee : std::as_const(invitees_))
83 mxidList.push_back(invitee->mxid_);
84 return mxidList;
85 }
86
87 Invitee::Invitee(QString mxid, QString displayName, QString avatarUrl, QObject *parent)
88 : QObject{parent}
89 , mxid_{std::move(mxid)}
90 {
91 // checking for empty avatarUrl will cause profiles that don't have an avatar
92 // to needlessly be loaded. Can we make sure we either provide both or none?
93 if (displayName == "" && avatarUrl == "") {
94 http::client()->get_profile(
95 mxid_.toStdString(),
96 [this](const mtx::responses::Profile &res, mtx::http::RequestErr err) {
97 if (err) {
98 nhlog::net()->warn("failed to retrieve profile info");
99 emit userInfoLoaded();
100 return;
101 }
102
103 displayName_ = QString::fromStdString(res.display_name);
104 avatarUrl_ = QString::fromStdString(res.avatar_url);
105
106 emit userInfoLoaded();
107 });
108 } else {
109 displayName_ = displayName;
110 avatarUrl_ = avatarUrl;
111 emit userInfoLoaded();
112 }
113 }
114
115 #include "moc_InviteesModel.cpp"