diff --git a/src/Cache.cc b/src/Cache.cc
index c1f25f63..614e8a90 100644
--- a/src/Cache.cc
+++ b/src/Cache.cc
@@ -987,7 +987,8 @@ Cache::getTimelineMessages(lmdb::txn &txn, const std::string &room_id)
if (obj.count("event") == 0 || obj.count("token") == 0)
continue;
- mtx::events::collections::TimelineEvent event = obj.at("event");
+ mtx::events::collections::TimelineEvent event;
+ mtx::events::collections::from_json(obj.at("event"), event);
index += 1;
@@ -1058,7 +1059,8 @@ Cache::getLastMessageInfo(lmdb::txn &txn, const std::string &room_id)
if (obj.count("event") == 0)
continue;
- mtx::events::collections::TimelineEvent event = obj.at("event");
+ mtx::events::collections::TimelineEvent event;
+ mtx::events::collections::from_json(obj.at("event"), event);
cursor.close();
return utils::getMessageDescription(
diff --git a/src/ChatPage.cc b/src/ChatPage.cc
index 336ea7c3..7f6306f6 100644
--- a/src/ChatPage.cc
+++ b/src/ChatPage.cc
@@ -501,31 +501,8 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
connect(room_list_, &RoomList::roomAvatarChanged, this, &ChatPage::updateTopBarAvatar);
- // connect(http::client(),
- // SIGNAL(getOwnCommunitiesResponse(QList<QString>)),
- // this,
- // SLOT(updateOwnCommunitiesInfo(QList<QString>)));
- // connect(http::client(),
- // &MatrixClient::communityProfileRetrieved,
- // this,
- // [this](QString communityId, QJsonObject profile) {
- // communities_[communityId]->parseProfile(profile);
- // });
- // connect(http::client(),
- // &MatrixClient::communityRoomsRetrieved,
- // this,
- // [this](QString communityId, QJsonObject rooms) {
- // communities_[communityId]->parseRooms(rooms);
-
- // if (communityId == current_community_) {
- // if (communityId == "world") {
- // room_list_->setFilterRooms(false);
- // } else {
- // room_list_->setRoomFilter(
- // communities_[communityId]->getRoomList());
- // }
- // }
- // });
+ connect(
+ this, &ChatPage::updateGroupsInfo, communitiesList_, &CommunitiesList::setCommunities);
connect(this, &ChatPage::leftRoom, this, &ChatPage::removeRoom);
connect(this, &ChatPage::notificationsRetrieved, this, &ChatPage::sendDesktopNotifications);
@@ -533,13 +510,13 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
connect(communitiesList_,
&CommunitiesList::communityChanged,
this,
- [this](const QString &communityId) {
- current_community_ = communityId;
+ [this](const QString &groupId) {
+ current_community_ = groupId;
- if (communityId == "world")
+ if (groupId == "world")
room_list_->setFilterRooms(false);
else
- room_list_->setRoomFilter(communities_[communityId]->getRoomList());
+ room_list_->setRoomFilter(communitiesList_->roomList(groupId));
});
connect(¬ificationsManager,
@@ -759,18 +736,6 @@ ChatPage::updateTopBarAvatar(const QString &roomid, const QPixmap &img)
}
void
-ChatPage::updateOwnCommunitiesInfo(const QList<QString> &own_communities)
-{
- for (int i = 0; i < own_communities.size(); i++) {
- QSharedPointer<Community> community = QSharedPointer<Community>(new Community());
-
- communities_[own_communities[i]] = community;
- }
-
- communitiesList_->setCommunities(communities_);
-}
-
-void
ChatPage::changeTopRoomInfo(const QString &room_id)
{
if (room_id.isEmpty()) {
@@ -1335,7 +1300,18 @@ ChatPage::getProfileInfo()
QImage::fromData(QByteArray(data.data(), data.size())));
});
});
- // TODO http::client()->getOwnCommunities();
+
+ http::v2::client()->joined_groups(
+ [this](const mtx::responses::JoinedGroups &res, mtx::http::RequestErr err) {
+ if (err) {
+ nhlog::net()->critical("failed to retrieve joined groups: {} {}",
+ static_cast<int>(err->status_code),
+ err->matrix_error.error);
+ return;
+ }
+
+ emit updateGroupsInfo(res);
+ });
}
void
diff --git a/src/CommunitiesList.cc b/src/CommunitiesList.cc
index 39e9a7fe..b2742f59 100644
--- a/src/CommunitiesList.cc
+++ b/src/CommunitiesList.cc
@@ -40,50 +40,81 @@ CommunitiesList::CommunitiesList(QWidget *parent)
scrollArea_->setWidget(scrollAreaContents_);
topLayout_->addWidget(scrollArea_);
- // connect(http::client(),
- // &MatrixClient::communityProfileRetrieved,
- // this,
- // [this](QString communityId, QJsonObject profile) {
- // fetchCommunityAvatar(communityId, profile["avatar_url"].toString());
- // });
connect(
this, &CommunitiesList::avatarRetrieved, this, &CommunitiesList::updateCommunityAvatar);
}
void
-CommunitiesList::setCommunities(const std::map<QString, QSharedPointer<Community>> &communities)
+CommunitiesList::setCommunities(const mtx::responses::JoinedGroups &response)
{
communities_.clear();
addGlobalItem();
- for (const auto &community : communities) {
- addCommunity(community.second, community.first);
-
- // http::client()->fetchCommunityProfile(community.first);
- // http::client()->fetchCommunityRooms(community.first);
- }
+ for (const auto &group : response.groups)
+ addCommunity(group);
communities_["world"]->setPressedState(true);
emit communityChanged("world");
}
void
-CommunitiesList::addCommunity(QSharedPointer<Community> community, const QString &community_id)
+CommunitiesList::addCommunity(const std::string &group_id)
{
- CommunitiesListItem *list_item =
- new CommunitiesListItem(community, community_id, scrollArea_);
+ const auto id = QString::fromStdString(group_id);
+
+ CommunitiesListItem *list_item = new CommunitiesListItem(id, scrollArea_);
+ communities_.emplace(id, QSharedPointer<CommunitiesListItem>(list_item));
+ contentsLayout_->insertWidget(contentsLayout_->count() - 1, list_item);
- communities_.emplace(community_id, QSharedPointer<CommunitiesListItem>(list_item));
+ connect(this,
+ &CommunitiesList::groupProfileRetrieved,
+ this,
+ [this](const QString &id, const mtx::responses::GroupProfile &profile) {
+ if (communities_.find(id) == communities_.end())
+ return;
- fetchCommunityAvatar(community_id, community->getAvatar().toString());
+ communities_.at(id)->setName(QString::fromStdString(profile.name));
- contentsLayout_->insertWidget(contentsLayout_->count() - 1, list_item);
+ if (!profile.avatar_url.empty())
+ fetchCommunityAvatar(id,
+ QString::fromStdString(profile.avatar_url));
+ });
+ connect(this,
+ &CommunitiesList::groupRoomsRetrieved,
+ this,
+ [this](const QString &id, const std::vector<QString> &rooms) {
+ if (communities_.find(id) == communities_.end())
+ return;
+ communities_.at(id)->setRooms(rooms);
+ });
connect(list_item,
&CommunitiesListItem::clicked,
this,
&CommunitiesList::highlightSelectedCommunity);
+
+ http::v2::client()->group_profile(
+ group_id, [id, this](const mtx::responses::GroupProfile &res, mtx::http::RequestErr err) {
+ if (err) {
+ return;
+ }
+
+ emit groupProfileRetrieved(id, res);
+ });
+
+ http::v2::client()->group_rooms(
+ group_id, [id, this](const nlohmann::json &res, mtx::http::RequestErr err) {
+ if (err) {
+ return;
+ }
+
+ std::vector<QString> room_ids;
+ for (const auto &room : res.at("chunk"))
+ room_ids.push_back(QString::fromStdString(room.at("room_id")));
+
+ emit groupRoomsRetrieved(id, room_ids);
+ });
}
void
@@ -94,7 +125,7 @@ CommunitiesList::updateCommunityAvatar(const QString &community_id, const QPixma
return;
}
- communities_.find(community_id)->second->setAvatar(img.toImage());
+ communities_.at(community_id)->setAvatar(img.toImage());
}
void
@@ -153,3 +184,12 @@ CommunitiesList::fetchCommunityAvatar(const QString &id, const QString &avatarUr
emit avatarRetrieved(id, pix);
});
}
+
+std::vector<QString>
+CommunitiesList::roomList(const QString &id) const
+{
+ if (communityExists(id))
+ return communities_.at(id)->rooms();
+
+ return {};
+}
diff --git a/src/CommunitiesListItem.cc b/src/CommunitiesListItem.cc
index e5127050..df6c5393 100644
--- a/src/CommunitiesListItem.cc
+++ b/src/CommunitiesListItem.cc
@@ -4,12 +4,9 @@
#include "RippleOverlay.h"
#include "Utils.h"
-CommunitiesListItem::CommunitiesListItem(QSharedPointer<Community> community,
- QString community_id,
- QWidget *parent)
+CommunitiesListItem::CommunitiesListItem(QString group_id, QWidget *parent)
: QWidget(parent)
- , community_(community)
- , communityId_(community_id)
+ , groupId_(group_id)
{
setMouseTracking(true);
setAttribute(Qt::WA_Hover);
@@ -20,8 +17,8 @@ CommunitiesListItem::CommunitiesListItem(QSharedPointer<Community> community,
rippleOverlay_->setClipPath(path);
rippleOverlay_->setClipping(true);
- if (communityId_ == "world")
- communityAvatar_ = QPixmap(":/icons/icons/ui/world.svg");
+ if (groupId_ == "world")
+ avatar_ = QPixmap(":/icons/icons/ui/world.svg");
}
void
@@ -41,7 +38,7 @@ CommunitiesListItem::mousePressEvent(QMouseEvent *event)
return;
}
- emit clicked(communityId_);
+ emit clicked(groupId_);
setPressedState(true);
@@ -70,12 +67,12 @@ CommunitiesListItem::paintEvent(QPaintEvent *)
else
p.fillRect(rect(), backgroundColor_);
- if (communityAvatar_.isNull()) {
+ if (avatar_.isNull()) {
QFont font;
font.setPixelSize(conf::roomlist::fonts::communityBubble);
p.setFont(font);
- p.drawLetterAvatar(utils::firstChar(community_->getName()),
+ p.drawLetterAvatar(utils::firstChar(resolveName()),
avatarFgColor_,
avatarBgColor_,
width(),
@@ -84,7 +81,7 @@ CommunitiesListItem::paintEvent(QPaintEvent *)
} else {
p.save();
- p.drawAvatar(communityAvatar_, width(), height(), IconSize);
+ p.drawAvatar(avatar_, width(), height(), IconSize);
p.restore();
}
}
@@ -92,6 +89,20 @@ CommunitiesListItem::paintEvent(QPaintEvent *)
void
CommunitiesListItem::setAvatar(const QImage &img)
{
- communityAvatar_ = utils::scaleImageToPixmap(img, IconSize);
+ avatar_ = utils::scaleImageToPixmap(img, IconSize);
update();
}
+
+QString
+CommunitiesListItem::resolveName() const
+{
+ if (!name_.isEmpty())
+ return name_;
+
+ if (!groupId_.startsWith("+"))
+ return QString("Group"); // Group with no name or id.
+
+ // Extract the localpart of the group.
+ auto firstPart = groupId_.split(':').at(0);
+ return firstPart.right(firstPart.size() - 1);
+}
diff --git a/src/Community.cc b/src/Community.cc
deleted file mode 100644
index d563c151..00000000
--- a/src/Community.cc
+++ /dev/null
@@ -1,33 +0,0 @@
-#include "include/Community.h"
-
-#include <QJsonArray>
-#include <QJsonValue>
-
-void
-Community::parseProfile(const QJsonObject &profile)
-{
- if (profile["name"].type() == QJsonValue::Type::String)
- name_ = profile["name"].toString();
- else
- name_ = "Unnamed Community"; // TODO: what is correct here?
-
- if (profile["avatar_url"].type() == QJsonValue::Type::String)
- avatar_ = QUrl(profile["avatar_url"].toString());
-
- if (profile["short_description"].type() == QJsonValue::Type::String)
- short_description_ = profile["short_description"].toString();
-
- if (profile["long_description"].type() == QJsonValue::Type::String)
- long_description_ = profile["long_description"].toString();
-}
-
-void
-Community::parseRooms(const QJsonObject &rooms)
-{
- rooms_.clear();
-
- for (auto const &room : rooms["chunk"].toArray()) {
- if (room.toObject().contains("room_id"))
- rooms_.emplace_back(room.toObject()["room_id"].toString());
- }
-}
diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc
index d4ab8e33..9e69dbbd 100644
--- a/src/MatrixClient.cc
+++ b/src/MatrixClient.cc
@@ -31,8 +31,11 @@ init()
qRegisterMetaType<mtx::responses::Notifications>();
qRegisterMetaType<mtx::responses::Rooms>();
qRegisterMetaType<mtx::responses::Sync>();
+ qRegisterMetaType<mtx::responses::JoinedGroups>();
+ qRegisterMetaType<mtx::responses::GroupProfile>();
qRegisterMetaType<std::string>();
qRegisterMetaType<std::vector<std::string>>();
+ qRegisterMetaType<std::vector<QString>>();
}
} // namespace http
|