matrixion/src/MemberList.cpp

101 lines
2.7 KiB
C++
Raw Normal View History

2021-05-30 04:09:21 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "MemberList.h"
#include "Cache.h"
#include "ChatPage.h"
#include "Config.h"
#include "Logging.h"
#include "Utils.h"
#include "timeline/TimelineViewManager.h"
MemberList::MemberList(const QString &room_id, QObject *parent)
2021-05-30 04:09:21 +03:00
: QAbstractListModel{parent}
, room_id_{room_id}
{
try {
info_ = cache::singleRoomInfo(room_id_.toStdString());
} catch (const lmdb::error &) {
nhlog::db()->warn("failed to retrieve room info from cache: {}",
room_id_.toStdString());
}
try {
auto members = cache::getMembers(room_id_.toStdString());
addUsers(members);
numUsersLoaded_ = members.size();
2021-05-30 04:09:21 +03:00
} catch (const lmdb::error &e) {
nhlog::db()->critical("Failed to retrieve members from cache: {}", e.what());
}
}
void
MemberList::addUsers(const std::vector<RoomMember> &members)
{
2021-06-11 03:13:12 +03:00
beginInsertRows(
QModelIndex{}, m_memberList.count(), m_memberList.count() + members.size() - 1);
2021-05-30 04:09:21 +03:00
for (const auto &member : members)
m_memberList.push_back(
{member,
ChatPage::instance()->timelineManager()->rooms()->currentRoom()->avatarUrl(
member.user_id)});
endInsertRows();
}
QHash<int, QByteArray>
MemberList::roleNames() const
{
2021-07-19 21:31:08 +03:00
return {
{Mxid, "mxid"},
{DisplayName, "displayName"},
{AvatarUrl, "avatarUrl"},
};
2021-05-30 04:09:21 +03:00
}
QVariant
MemberList::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= (int)m_memberList.size() || index.row() < 0)
return {};
switch (role) {
case Mxid:
return m_memberList[index.row()].first.user_id;
case DisplayName:
return m_memberList[index.row()].first.display_name;
case AvatarUrl:
return m_memberList[index.row()].second;
default:
return {};
}
}
2021-06-11 03:13:12 +03:00
bool
MemberList::canFetchMore(const QModelIndex &) const
2021-05-30 04:09:21 +03:00
{
2021-06-11 03:13:12 +03:00
const size_t numMembers = rowCount();
if (numMembers > 1 && numMembers < info_.member_count)
return true;
else
return false;
2021-05-30 04:09:21 +03:00
}
void
MemberList::fetchMore(const QModelIndex &)
{
2021-07-19 21:49:57 +03:00
loadingMoreMembers_ = true;
emit loadingMoreMembersChanged();
auto members = cache::getMembers(room_id_.toStdString(), rowCount());
addUsers(members);
2021-07-18 01:24:04 +03:00
numUsersLoaded_ += members.size();
emit numUsersLoadedChanged();
2021-07-19 21:49:57 +03:00
loadingMoreMembers_ = false;
emit loadingMoreMembersChanged();
2021-05-30 04:09:21 +03:00
}