From 73dbd3c8dd78b41c8d93392eac74231e5b4a20d5 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 14 Jul 2018 12:08:16 +0300 Subject: Re-enable groups --- src/CommunitiesList.cc | 82 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 21 deletions(-) (limited to 'src/CommunitiesList.cc') 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> &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, const QString &community_id) +CommunitiesList::addCommunity(const std::string &group_id) { - CommunitiesListItem *list_item = - new CommunitiesListItem(community, community_id, scrollArea_); - - communities_.emplace(community_id, QSharedPointer(list_item)); - - fetchCommunityAvatar(community_id, community->getAvatar().toString()); + const auto id = QString::fromStdString(group_id); + CommunitiesListItem *list_item = new CommunitiesListItem(id, scrollArea_); + communities_.emplace(id, QSharedPointer(list_item)); contentsLayout_->insertWidget(contentsLayout_->count() - 1, list_item); + connect(this, + &CommunitiesList::groupProfileRetrieved, + this, + [this](const QString &id, const mtx::responses::GroupProfile &profile) { + if (communities_.find(id) == communities_.end()) + return; + + communities_.at(id)->setName(QString::fromStdString(profile.name)); + + if (!profile.avatar_url.empty()) + fetchCommunityAvatar(id, + QString::fromStdString(profile.avatar_url)); + }); + connect(this, + &CommunitiesList::groupRoomsRetrieved, + this, + [this](const QString &id, const std::vector &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 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 +CommunitiesList::roomList(const QString &id) const +{ + if (communityExists(id)) + return communities_.at(id)->rooms(); + + return {}; +} -- cgit 1.5.1