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

Commit

Permalink
Process and store tag list per room
Browse files Browse the repository at this point in the history
  • Loading branch information
elinorbgr committed Sep 13, 2018
1 parent bf4d559 commit 85a1b7a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,8 @@ Cache::notifyForReadReceipts(lmdb::txn &txn, const std::string &room_id)
void
Cache::saveState(const mtx::responses::Sync &res)
{
using namespace mtx::events;

auto txn = lmdb::txn::begin(env_);

setNextBatchToken(txn, res.next_batch);
Expand All @@ -865,6 +867,35 @@ Cache::saveState(const mtx::responses::Sync &res)
getRoomAvatarUrl(txn, statesdb, membersdb, QString::fromStdString(room.first))
.toStdString();

// Process the account_data associated with this room
bool has_new_tags = false;
for (const auto &evt : room.second.account_data.events) {
// for now only fetch tag events
if (evt.type() == typeid(Event<account_data::Tag>)) {
auto tags_evt = boost::get<Event<account_data::Tag>>(evt);
has_new_tags = true;
for (const auto &tag : tags_evt.content.tags) {
updatedInfo.tags.push_back(tag.first);
}
}
}
if (!has_new_tags) {
// retrieve the old tags, they haven't changed
lmdb::val data;
if (lmdb::dbi_get(txn, roomsDb_, lmdb::val(room.first), data)) {
try {
RoomInfo tmp =
json::parse(std::string(data.data(), data.size()));
updatedInfo.tags = tmp.tags;
} catch (const json::exception &e) {
nhlog::db()->warn(
"failed to parse room info: room_id ({}), {}",
room.first,
std::string(data.data(), data.size()));
}
}
}

lmdb::dbi_put(
txn, roomsDb_, lmdb::val(room.first), lmdb::val(json(updatedInfo).dump()));

Expand Down
8 changes: 8 additions & 0 deletions src/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ struct RoomInfo
bool guest_access = false;
//! Metadata describing the last message in the timeline.
DescInfo msgInfo;
//! The list of tags associated with this room
std::vector<std::string> tags;
};

inline void
Expand All @@ -128,6 +130,9 @@ to_json(json &j, const RoomInfo &info)

if (info.member_count != 0)
j["member_count"] = info.member_count;

if (info.tags.size() != 0)
j["tags"] = info.tags;
}

inline void
Expand All @@ -142,6 +147,9 @@ from_json(const json &j, RoomInfo &info)

if (j.count("member_count"))
info.member_count = j.at("member_count");

if (j.count("tags"))
info.tags = j.at("tags").get<std::vector<std::string>>();
}

//! Basic information per member;
Expand Down

0 comments on commit 85a1b7a

Please sign in to comment.