Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Display tags as special communities
Browse files Browse the repository at this point in the history
  • Loading branch information
elinorbgr committed Sep 14, 2018
1 parent 85a1b7a commit 96a1cd9
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 2 deletions.
Binary file added resources/icons/ui/tag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/ui/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/res.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<file>icons/ui/world.png</file>
<file>icons/ui/[email protected]</file>

<file>icons/ui/tag.png</file>
<file>icons/ui/[email protected]</file>

<file>icons/ui/edit.png</file>
<file>icons/ui/[email protected]</file>

Expand Down
21 changes: 21 additions & 0 deletions src/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,27 @@ Cache::roomsWithStateUpdates(const mtx::responses::Sync &res)
return rooms;
}

std::vector<std::string>
Cache::roomsWithTagUpdates(const mtx::responses::Sync &res)
{
using namespace mtx::events;

std::vector<std::string> rooms;
for (const auto &room : res.rooms.join) {
bool hasUpdates = false;
for (const auto &evt : room.second.account_data.events) {
if (evt.type() == typeid(Event<account_data::Tag>)) {
hasUpdates = true;
}
}

if (hasUpdates)
rooms.emplace_back(room.first);
}

return rooms;
}

RoomInfo
Cache::singleRoomInfo(const std::string &room_id)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,16 @@ class Cache : public QObject

RoomInfo singleRoomInfo(const std::string &room_id);
std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res);
std::vector<std::string> roomsWithTagUpdates(const mtx::responses::Sync &res);
std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms);
std::map<QString, RoomInfo> roomUpdates(const mtx::responses::Sync &sync)
{
return getRoomInfo(roomsWithStateUpdates(sync));
}
std::map<QString, RoomInfo> roomTagUpdates(const mtx::responses::Sync &sync)
{
return getRoomInfo(roomsWithTagUpdates(sync));
}

QVector<SearchResult> searchUsers(const std::string &room_id,
const std::string &query,
Expand Down
6 changes: 6 additions & 0 deletions src/ChatPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
});
});
connect(this, &ChatPage::syncRoomlist, room_list_, &RoomList::sync);
connect(this, &ChatPage::syncTags, communitiesList_, &CommunitiesList::syncTags);
connect(
this, &ChatPage::syncTopBar, this, [this](const std::map<QString, RoomInfo> &updates) {
if (updates.find(currentRoom()) != updates.end())
Expand Down Expand Up @@ -793,6 +794,7 @@ ChatPage::loadStateFromCache()

emit initializeEmptyViews(cache::client()->roomMessages());
emit initializeRoomList(cache::client()->roomInfo());
emit syncTags(cache::client()->roomInfo().toStdMap());

} catch (const mtx::crypto::olm_exception &e) {
nhlog::crypto()->critical("failed to restore olm account: {}", e.what());
Expand Down Expand Up @@ -1073,6 +1075,9 @@ ChatPage::trySync()
emit syncTopBar(updates);
emit syncRoomlist(updates);

auto tag_updates = cache::client()->roomTagUpdates(res);
emit syncTags(tag_updates);

cache::client()->deleteOldData();
} catch (const lmdb::map_full_error &e) {
nhlog::db()->error("lmdb is full: {}", e.what());
Expand Down Expand Up @@ -1205,6 +1210,7 @@ ChatPage::initialSyncHandler(const mtx::responses::Sync &res, mtx::http::Request

emit initializeViews(std::move(res.rooms));
emit initializeRoomList(cache::client()->roomInfo());
emit syncTags(cache::client()->roomInfo().toStdMap());
} catch (const lmdb::error &e) {
nhlog::db()->error("failed to save state after initial sync: {}", e.what());
startInitialSync();
Expand Down
1 change: 1 addition & 0 deletions src/ChatPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public slots:
void initializeEmptyViews(const std::map<QString, mtx::responses::Timeline> &msgs);
void syncUI(const mtx::responses::Rooms &rooms);
void syncRoomlist(const std::map<QString, RoomInfo> &updates);
void syncTags(const std::map<QString, RoomInfo> &updates);
void syncTopBar(const std::map<QString, RoomInfo> &updates);
void dropToLoginPageCb(const QString &msg);

Expand Down
53 changes: 52 additions & 1 deletion src/CommunitiesList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ CommunitiesList::CommunitiesList(QWidget *parent)
void
CommunitiesList::setCommunities(const mtx::responses::JoinedGroups &response)
{
communities_.clear();
// remove all non-tag communities
auto it = communities_.begin();
while (it != communities_.end()) {
if (it->second->is_tag()) {
++it;
} else {
it = communities_.erase(it);
}
}

addGlobalItem();

Expand All @@ -58,6 +66,49 @@ CommunitiesList::setCommunities(const mtx::responses::JoinedGroups &response)
emit communityChanged("world");
}

void
CommunitiesList::syncTags(const std::map<QString, RoomInfo> &info)
{
for (const auto &room : info)
setTagsForRoom(room.first, room.second.tags);
}

void
CommunitiesList::setTagsForRoom(const QString &room_id, const std::vector<std::string> &tags)
{
// create missing tag if any
for (const auto &tag: tags) {
QString name = QString("tag:") + QString::fromStdString(tag);
if (!communityExists(name)) {
addCommunity(std::string("tag:") + tag);
}
}
// update membership of the room for all tags
auto it = communities_.begin();
while (it != communities_.end()) {
// Skip if the community is not a tag
if (!it->second->is_tag()) {
++it;
continue;
}
// insert or remove the room from the tag as appropriate
std::string current_tag = it->first.right(it->first.size()-4).toStdString();
if (std::find(tags.begin(), tags.end(), current_tag) != tags.end()) {
// the room has this tag
it->second->addRoom(room_id);
} else {
// the room does not have this tag
it->second->delRoom(room_id);
}
// Check if the tag is now empty, if yes delete it
if (it->second->rooms().empty()) {
it = communities_.erase(it);
} else {
++it;
}
}
}

void
CommunitiesList::addCommunity(const std::string &group_id)
{
Expand Down
4 changes: 4 additions & 0 deletions src/CommunitiesList.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QSharedPointer>
#include <QVBoxLayout>

#include "Cache.h"
#include "CommunitiesListItem.h"
#include "ui/Theme.h"

Expand All @@ -20,6 +21,9 @@ class CommunitiesList : public QWidget
void removeCommunity(const QString &id) { communities_.erase(id); };
std::map<QString, bool> roomList(const QString &id) const;

void syncTags(const std::map<QString, RoomInfo> &info);
void setTagsForRoom(const QString &id, const std::vector<std::string> &tags);

signals:
void communityChanged(const QString &id);
void avatarRetrieved(const QString &id, const QPixmap &img);
Expand Down
5 changes: 4 additions & 1 deletion src/CommunitiesListItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ CommunitiesListItem::CommunitiesListItem(QString group_id, QWidget *parent)

if (groupId_ == "world")
avatar_ = QPixmap(":/icons/icons/ui/world.png");
if (groupId_.startsWith("tag:"))
avatar_ = QPixmap(":/icons/icons/ui/tag.png");
}

void
Expand Down Expand Up @@ -98,7 +100,8 @@ CommunitiesListItem::resolveName() const
{
if (!name_.isEmpty())
return name_;

if (groupId_.startsWith("tag:"))
return groupId_.right(groupId_.size()-4);
if (!groupId_.startsWith("+"))
return QString("Group"); // Group with no name or id.

Expand Down
4 changes: 4 additions & 0 deletions src/CommunitiesListItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ class CommunitiesListItem : public QWidget
void setAvatar(const QImage &img);

void setRooms(std::map<QString, bool> room_ids) { room_ids_ = std::move(room_ids); }
void addRoom(const QString &id) { room_ids_[id] = true; }
void delRoom(const QString &id) { room_ids_.erase(id); }
std::map<QString, bool> rooms() const { return room_ids_; }

bool is_tag() const { return groupId_.startsWith("tag:"); }

QColor highlightedBackgroundColor() const { return highlightedBackgroundColor_; }
QColor hoverBackgroundColor() const { return hoverBackgroundColor_; }
QColor backgroundColor() const { return backgroundColor_; }
Expand Down

0 comments on commit 96a1cd9

Please sign in to comment.