From e144c5741f36a27b4df44804db48a77446bcd75c Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Thu, 1 Sep 2022 13:25:11 +0200 Subject: Implement space stickers & emoji --- src/Cache.cpp | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'src/Cache.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index ede2e4e5..bf485325 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -8,6 +8,7 @@ #include "Cache_p.h" #include +#include #include #include @@ -3911,7 +3912,8 @@ Cache::getImagePacks(const std::string &room_id, std::optional stickers) auto addPack = [&infos, stickers](const mtx::events::msc2545::ImagePack &pack, const std::string &source_room, - const std::string &state_key) { + const std::string &state_key, + bool from_space) { bool pack_is_sticker = pack.pack ? pack.pack->is_sticker() : true; bool pack_is_emoji = pack.pack ? pack.pack->is_emoji() : true; bool pack_matches = @@ -3921,6 +3923,7 @@ Cache::getImagePacks(const std::string &room_id, std::optional stickers) info.source_room = source_room; info.state_key = state_key; info.pack.pack = pack.pack; + info.from_space = from_space; for (const auto &img : pack.images) { if (stickers.has_value() && @@ -3942,7 +3945,7 @@ Cache::getImagePacks(const std::string &room_id, std::optional stickers) auto tmp = std::get_if>(&*accountpack); if (tmp) - addPack(tmp->content, "", ""); + addPack(tmp->content, "", "", false); } // packs from rooms, that were enabled globally @@ -3959,19 +3962,39 @@ Cache::getImagePacks(const std::string &room_id, std::optional stickers) (void)d; if (auto pack = getStateEvent(txn, room_id2, state_id)) - addPack(pack->content, room_id2, state_id); + addPack(pack->content, room_id2, state_id, false); } } } } - // packs from current room - if (auto pack = getStateEvent(txn, room_id)) { - addPack(pack->content, room_id, ""); - } - for (const auto &pack : getStateEventsWithType(txn, room_id)) { - addPack(pack.content, room_id, pack.state_key); - } + std::function addRoomAndCanonicalParents; + std::unordered_set visitedRooms; + addRoomAndCanonicalParents = + [this, &addRoomAndCanonicalParents, &addPack, &visitedRooms, &txn, &room_id]( + const std::string ¤t_room) { + if (visitedRooms.count(current_room)) + return; + else + visitedRooms.insert(current_room); + + if (auto pack = getStateEvent(txn, current_room)) { + addPack(pack->content, current_room, "", current_room != room_id); + } + for (const auto &pack : + getStateEventsWithType(txn, current_room)) { + addPack(pack.content, current_room, pack.state_key, current_room != room_id); + } + + for (const auto &parent : + getStateEventsWithType(txn, current_room)) { + if (parent.content.canonical && parent.content.via && !parent.content.via->empty()) + addRoomAndCanonicalParents(parent.state_key); + } + }; + + // packs from current room and then iterate canonical space parents + addRoomAndCanonicalParents(room_id); return infos; } -- cgit 1.5.1