diff --git a/src/Cache.cpp b/src/Cache.cpp
index b27a8b37..6c746d4b 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -3146,6 +3146,36 @@ Cache::joinedRooms()
return room_ids;
}
+std::map<std::string, RoomInfo>
+Cache::getCommonRooms(const std::string &user_id)
+{
+ std::map<std::string, RoomInfo> result;
+
+ auto txn = ro_txn(env_);
+
+ std::string_view room_id;
+ std::string_view room_data;
+ std::string_view member_info;
+
+ auto roomsCursor = lmdb::cursor::open(txn, roomsDb_);
+ while (roomsCursor.get(room_id, room_data, MDB_NEXT)) {
+ try {
+ if (getMembersDb(txn, std::string(room_id)).get(txn, user_id, member_info)) {
+ RoomInfo tmp = nlohmann::json::parse(std::move(room_data)).get<RoomInfo>();
+ result.emplace(std::string(room_id), std::move(tmp));
+ }
+ } catch (std::exception &e) {
+ nhlog::db()->warn("Failed to read common room for member ({}) in room ({}): {}",
+ user_id,
+ room_id,
+ e.what());
+ }
+ }
+ roomsCursor.close();
+
+ return result;
+}
+
std::optional<MemberInfo>
Cache::getMember(const std::string &room_id, const std::string &user_id)
{
diff --git a/src/Cache_p.h b/src/Cache_p.h
index 5a4f9afb..38cadfc9 100644
--- a/src/Cache_p.h
+++ b/src/Cache_p.h
@@ -64,6 +64,7 @@ public:
crypto::Trust roomVerificationStatus(const std::string &room_id);
std::vector<std::string> joinedRooms();
+ std::map<std::string, RoomInfo> getCommonRooms(const std::string &user_id);
QMap<QString, RoomInfo> roomInfo(bool withInvites = true);
QHash<QString, RoomInfo> invites();
diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp
index 66a68bb8..80def409 100644
--- a/src/ui/UserProfile.cpp
+++ b/src/ui/UserProfile.cpp
@@ -58,6 +58,12 @@ UserProfile::UserProfile(const QString &roomid,
emit verificationStatiChanged();
});
fetchDeviceList(this->userid_);
+
+ if (userid != utils::localUser())
+ sharedRooms_ =
+ new RoomInfoModel(cache::client()->getCommonRooms(userid.toStdString()), this);
+ else
+ sharedRooms_ = new RoomInfoModel({}, this);
}
QHash<int, QByteArray>
@@ -102,12 +108,53 @@ DeviceInfoModel::reset(const std::vector<DeviceInfo> &deviceList)
endResetModel();
}
+RoomInfoModel::RoomInfoModel(const std::map<std::string, RoomInfo> &raw, QObject *parent)
+ : QAbstractListModel(parent)
+{
+ for (const auto &e : raw)
+ roomInfos_.push_back(e);
+}
+
+QHash<int, QByteArray>
+RoomInfoModel::roleNames() const
+{
+ return {
+ {RoomId, "roomId"},
+ {RoomName, "roomName"},
+ {AvatarUrl, "avatarUrl"},
+ };
+}
+
+QVariant
+RoomInfoModel::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid() || index.row() >= (int)roomInfos_.size() || index.row() < 0)
+ return {};
+
+ switch (role) {
+ case RoomId:
+ return QString::fromStdString(roomInfos_[index.row()].first);
+ case RoomName:
+ return QString::fromStdString(roomInfos_[index.row()].second.name);
+ case AvatarUrl:
+ return QString::fromStdString(roomInfos_[index.row()].second.avatar_url);
+ default:
+ return {};
+ }
+}
+
DeviceInfoModel *
UserProfile::deviceList()
{
return &this->deviceList_;
}
+RoomInfoModel *
+UserProfile::sharedRooms()
+{
+ return this->sharedRooms_;
+}
+
QString
UserProfile::userid()
{
diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h
index d8423ffa..a880f320 100644
--- a/src/ui/UserProfile.h
+++ b/src/ui/UserProfile.h
@@ -119,6 +119,30 @@ private:
friend class UserProfile;
};
+class RoomInfoModel final : public QAbstractListModel
+{
+ Q_OBJECT
+public:
+ enum Roles
+ {
+ RoomId,
+ RoomName,
+ AvatarUrl,
+ };
+
+ explicit RoomInfoModel(const std::map<std::string, RoomInfo> &, QObject *parent = nullptr);
+ QHash<int, QByteArray> roleNames() const override;
+ int rowCount(const QModelIndex &parent = QModelIndex()) const override
+ {
+ (void)parent;
+ return (int)roomInfos_.size();
+ }
+ QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+
+private:
+ std::vector<std::pair<std::string, RoomInfo>> roomInfos_;
+};
+
class UserProfile final : public QObject
{
Q_OBJECT
@@ -126,6 +150,7 @@ class UserProfile final : public QObject
Q_PROPERTY(QString userid READ userid CONSTANT)
Q_PROPERTY(QString avatarUrl READ avatarUrl NOTIFY avatarUrlChanged)
Q_PROPERTY(DeviceInfoModel *deviceList READ deviceList NOTIFY devicesChanged)
+ Q_PROPERTY(RoomInfoModel *sharedRooms READ sharedRooms CONSTANT)
Q_PROPERTY(bool isGlobalUserProfile READ isGlobalUserProfile CONSTANT)
Q_PROPERTY(int userVerified READ getUserStatus NOTIFY userStatusChanged)
Q_PROPERTY(bool isLoading READ isLoading NOTIFY loadingChanged)
@@ -139,6 +164,7 @@ public:
TimelineModel *parent = nullptr);
DeviceInfoModel *deviceList();
+ RoomInfoModel *sharedRooms();
QString userid();
QString displayName();
@@ -198,4 +224,5 @@ private:
bool isLoading_ = false;
TimelineViewManager *manager;
TimelineModel *model;
+ RoomInfoModel *sharedRooms_ = nullptr;
};
|