Changeset View
Changeset View
Standalone View
Standalone View
src/matrix-sticker-pack-list.cpp
| Show All 9 Lines | |||||
| #include <lager/lenses/at.hpp> | #include <lager/lenses/at.hpp> | ||||
| #include "matrix-sticker-pack.hpp" | #include "matrix-sticker-pack.hpp" | ||||
| #include "matrix-sticker-pack-list.hpp" | #include "matrix-sticker-pack-list.hpp" | ||||
| using namespace Kazv; | using namespace Kazv; | ||||
| static const std::string accountDataEventType = "im.ponies.user_emotes"; | static const std::string accountDataEventType = "im.ponies.user_emotes"; | ||||
| static const std::string roomStateEventType = "im.ponies.room_emotes"; | |||||
| static const std::string imagePackRoomsEventType = "im.ponies.emote_rooms"; | |||||
| using ImagePackRoomMap = immer::map<std::string/* room id */, immer::map<std::string/* */, json>>; | |||||
| lager::reader<immer::flex_vector<MatrixStickerPackSource>> getEventsFromClient(Client client) | lager::reader<immer::flex_vector<MatrixStickerPackSource>> getEventsFromClient(Client client) | ||||
| { | { | ||||
| lager::reader<Event> userEmotes = client.accountData()[accountDataEventType][lager::lenses::or_default]; | lager::reader<Event> userEmotes = client.accountData()[accountDataEventType][lager::lenses::or_default]; | ||||
| return userEmotes.map([](Event e) { | return lager::with( | ||||
| return immer::flex_vector<MatrixStickerPackSource>{ | userEmotes, | ||||
| {MatrixStickerPackSource::AccountData, accountDataEventType, e}, | client.accountData()[imagePackRoomsEventType][lager::lenses::or_default] | ||||
| }; | // The following jsonAtOr performs the validation for us: | ||||
| // if the content does not conform to the string-to-string-to-anything map, return an empty one | |||||
| .xform(eventContent | jsonAtOr("rooms", ImagePackRoomMap{})), | |||||
| client.rooms()).map([]( | |||||
| const Event &userEmotes, | |||||
| const ImagePackRoomMap &emoteRooms, | |||||
| const immer::map<std::string, RoomModel> &rooms) { | |||||
| // This transformation function takes O(|room sticker packs specified in account data|). | |||||
| auto res = immer::flex_vector<MatrixStickerPackSource>{ | |||||
| {MatrixStickerPackSource::AccountData, accountDataEventType, userEmotes} | |||||
| }.transient(); | |||||
| for (const auto &[roomId, stateKeyMap] : emoteRooms) { | |||||
| for (const auto &[stateKey, _] : stateKeyMap) { | |||||
| res.push_back(MatrixStickerPackSource{ | |||||
| MatrixStickerPackSource::RoomState, | |||||
| roomStateEventType, | |||||
| rooms[roomId].stateEvents[KeyOfState{roomStateEventType, stateKey}] | |||||
| }); | }); | ||||
| } | |||||
| } | |||||
| return res.persistent(); | |||||
| }).make(); | |||||
| } | } | ||||
| MatrixStickerPackList::MatrixStickerPackList(Client client, QObject *parent) | MatrixStickerPackList::MatrixStickerPackList(Client client, QObject *parent) | ||||
| : KazvAbstractListModel(parent) | : KazvAbstractListModel(parent) | ||||
| , m_client(client) | , m_client(client) | ||||
| , m_events(getEventsFromClient(m_client)) | , m_events(getEventsFromClient(m_client)) | ||||
| { | { | ||||
| initCountCursor(m_events.map([](const auto &events) { | initCountCursor(m_events.map([](const auto &events) { | ||||
| Show All 20 Lines | |||||