Implement space stickers & emoji

This commit is contained in:
Nicolas Werner 2022-09-01 13:25:11 +02:00
parent 01fd5e6b61
commit e144c5741f
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
7 changed files with 46 additions and 10 deletions

View File

@ -105,6 +105,7 @@ ApplicationWindow {
required property string displayName required property string displayName
required property bool fromAccountData required property bool fromAccountData
required property bool fromCurrentRoom required property bool fromCurrentRoom
required property bool fromSpace
required property string statekey required property string statekey
title: displayName title: displayName
@ -113,6 +114,8 @@ ApplicationWindow {
return qsTr("Private pack"); return qsTr("Private pack");
else if (fromCurrentRoom) else if (fromCurrentRoom)
return qsTr("Pack from this room"); return qsTr("Pack from this room");
else if (fromSpace)
return qsTr("Pack from parent community");
else else
return qsTr("Globally enabled pack"); return qsTr("Globally enabled pack");
} }

View File

@ -8,6 +8,7 @@
#include "Cache_p.h" #include "Cache_p.h"
#include <stdexcept> #include <stdexcept>
#include <unordered_set>
#include <variant> #include <variant>
#include <QCoreApplication> #include <QCoreApplication>
@ -3911,7 +3912,8 @@ Cache::getImagePacks(const std::string &room_id, std::optional<bool> stickers)
auto addPack = [&infos, stickers](const mtx::events::msc2545::ImagePack &pack, auto addPack = [&infos, stickers](const mtx::events::msc2545::ImagePack &pack,
const std::string &source_room, 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_sticker = pack.pack ? pack.pack->is_sticker() : true;
bool pack_is_emoji = pack.pack ? pack.pack->is_emoji() : true; bool pack_is_emoji = pack.pack ? pack.pack->is_emoji() : true;
bool pack_matches = bool pack_matches =
@ -3921,6 +3923,7 @@ Cache::getImagePacks(const std::string &room_id, std::optional<bool> stickers)
info.source_room = source_room; info.source_room = source_room;
info.state_key = state_key; info.state_key = state_key;
info.pack.pack = pack.pack; info.pack.pack = pack.pack;
info.from_space = from_space;
for (const auto &img : pack.images) { for (const auto &img : pack.images) {
if (stickers.has_value() && if (stickers.has_value() &&
@ -3942,7 +3945,7 @@ Cache::getImagePacks(const std::string &room_id, std::optional<bool> stickers)
auto tmp = auto tmp =
std::get_if<mtx::events::EphemeralEvent<mtx::events::msc2545::ImagePack>>(&*accountpack); std::get_if<mtx::events::EphemeralEvent<mtx::events::msc2545::ImagePack>>(&*accountpack);
if (tmp) if (tmp)
addPack(tmp->content, "", ""); addPack(tmp->content, "", "", false);
} }
// packs from rooms, that were enabled globally // packs from rooms, that were enabled globally
@ -3959,20 +3962,40 @@ Cache::getImagePacks(const std::string &room_id, std::optional<bool> stickers)
(void)d; (void)d;
if (auto pack = if (auto pack =
getStateEvent<mtx::events::msc2545::ImagePack>(txn, room_id2, state_id)) getStateEvent<mtx::events::msc2545::ImagePack>(txn, room_id2, state_id))
addPack(pack->content, room_id2, state_id); addPack(pack->content, room_id2, state_id, false);
} }
} }
} }
} }
// packs from current room std::function<void(const std::string &room_id)> addRoomAndCanonicalParents;
if (auto pack = getStateEvent<mtx::events::msc2545::ImagePack>(txn, room_id)) { std::unordered_set<std::string> visitedRooms;
addPack(pack->content, room_id, ""); addRoomAndCanonicalParents =
[this, &addRoomAndCanonicalParents, &addPack, &visitedRooms, &txn, &room_id](
const std::string &current_room) {
if (visitedRooms.count(current_room))
return;
else
visitedRooms.insert(current_room);
if (auto pack = getStateEvent<mtx::events::msc2545::ImagePack>(txn, current_room)) {
addPack(pack->content, current_room, "", current_room != room_id);
} }
for (const auto &pack : getStateEventsWithType<mtx::events::msc2545::ImagePack>(txn, room_id)) { for (const auto &pack :
addPack(pack.content, room_id, pack.state_key); getStateEventsWithType<mtx::events::msc2545::ImagePack>(txn, current_room)) {
addPack(pack.content, current_room, pack.state_key, current_room != room_id);
} }
for (const auto &parent :
getStateEventsWithType<mtx::events::state::space::Parent>(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; return infos;
} }

View File

@ -126,4 +126,5 @@ struct ImagePackInfo
mtx::events::msc2545::ImagePack pack; mtx::events::msc2545::ImagePack pack;
std::string source_room; std::string source_room;
std::string state_key; std::string state_key;
bool from_space = false;
}; };

View File

@ -36,6 +36,7 @@ ImagePackListModel::roleNames() const
{Roles::AvatarUrl, "avatarUrl"}, {Roles::AvatarUrl, "avatarUrl"},
{Roles::FromAccountData, "fromAccountData"}, {Roles::FromAccountData, "fromAccountData"},
{Roles::FromCurrentRoom, "fromCurrentRoom"}, {Roles::FromCurrentRoom, "fromCurrentRoom"},
{Roles::FromSpace, "fromSpace"},
{Roles::StateKey, "statekey"}, {Roles::StateKey, "statekey"},
{Roles::RoomId, "roomid"}, {Roles::RoomId, "roomid"},
}; };
@ -55,6 +56,8 @@ ImagePackListModel::data(const QModelIndex &index, int role) const
return pack->roomid().isEmpty(); return pack->roomid().isEmpty();
case Roles::FromCurrentRoom: case Roles::FromCurrentRoom:
return pack->roomid().toStdString() == this->room_id; return pack->roomid().toStdString() == this->room_id;
case Roles::FromSpace:
return pack->fromSpace();
case Roles::StateKey: case Roles::StateKey:
return pack->statekey(); return pack->statekey();
case Roles::RoomId: case Roles::RoomId:

View File

@ -21,6 +21,7 @@ public:
AvatarUrl, AvatarUrl,
FromAccountData, FromAccountData,
FromCurrentRoom, FromCurrentRoom,
FromSpace,
StateKey, StateKey,
RoomId, RoomId,
}; };

View File

@ -26,6 +26,7 @@ SingleImagePackModel::SingleImagePackModel(ImagePackInfo pack_, QObject *parent)
, statekey_(std::move(pack_.state_key)) , statekey_(std::move(pack_.state_key))
, old_statekey_(statekey_) , old_statekey_(statekey_)
, pack(std::move(pack_.pack)) , pack(std::move(pack_.pack))
, fromSpace_(pack_.from_space)
{ {
[[maybe_unused]] static auto imageInfoType = qRegisterMetaType<mtx::common::ImageInfo>(); [[maybe_unused]] static auto imageInfoType = qRegisterMetaType<mtx::common::ImageInfo>();

View File

@ -18,6 +18,7 @@ class SingleImagePackModel : public QAbstractListModel
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString roomid READ roomid CONSTANT) Q_PROPERTY(QString roomid READ roomid CONSTANT)
Q_PROPERTY(bool fromSpace READ fromSpace CONSTANT)
Q_PROPERTY(QString statekey READ statekey WRITE setStatekey NOTIFY statekeyChanged) Q_PROPERTY(QString statekey READ statekey WRITE setStatekey NOTIFY statekeyChanged)
Q_PROPERTY(QString attribution READ attribution WRITE setAttribution NOTIFY attributionChanged) Q_PROPERTY(QString attribution READ attribution WRITE setAttribution NOTIFY attributionChanged)
Q_PROPERTY(QString packname READ packname WRITE setPackname NOTIFY packnameChanged) Q_PROPERTY(QString packname READ packname WRITE setPackname NOTIFY packnameChanged)
@ -47,6 +48,7 @@ public:
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
QString roomid() const { return QString::fromStdString(roomid_); } QString roomid() const { return QString::fromStdString(roomid_); }
bool fromSpace() const { return fromSpace_; }
QString statekey() const { return QString::fromStdString(statekey_); } QString statekey() const { return QString::fromStdString(statekey_); }
QString packname() const { return QString::fromStdString(pack.pack->display_name); } QString packname() const { return QString::fromStdString(pack.pack->display_name); }
QString attribution() const { return QString::fromStdString(pack.pack->attribution); } QString attribution() const { return QString::fromStdString(pack.pack->attribution); }
@ -91,4 +93,6 @@ private:
mtx::events::msc2545::ImagePack pack; mtx::events::msc2545::ImagePack pack;
std::vector<std::string> shortcodes; std::vector<std::string> shortcodes;
bool fromSpace_ = false;
}; };