Debian Codegraph @

mtxclient/include/mtx/events/mscs/image_packs.hpp

raw symbols SCIP
1 #pragma once
2
3 /// @file
4 /// @brief Image packs from [MSC2545](https://github.com/matrix-org/matrix-doc/pull/2545)
5
6 #include <bitset>
7 #include <cstdint>
8 #include <map>
9 #include <optional>
10 #include <string>
11
12 #if __has_include(<nlohmann/json_fwd.hpp>)
13 #include <nlohmann/json_fwd.hpp>
14 #else
15 #include <nlohmann/json.hpp>
16 #endif
17
18 #include "mtx/events/common.hpp"
19
20 namespace mtx {
21 namespace events {
22
23 //! Custom emotes and stickers MSC
24 namespace msc2545 {
25 //! How a pack or image is intended to be used.
26 enum PackUsage : uint8_t
27 {
28 Sticker,
29 Emoji,
30 };
31
32 //! A single image in a pack
33 struct PackImage
34 {
35 //! The mxc uri of this image.
36 std::string url;
37 //! The body to be sent in a sticker. May be empty.
38 std::string body;
39
40 //! Info as used in stickers about size, etc.
41 std::optional<mtx::common::ImageInfo> info;
42
43 //! What the images are used for. Indexed by PackUsage
44 std::bitset<2> usage;
45
46 //! If this overrides the pack level usage definition.
47 [[nodiscard]] bool overrides_usage() const { return usage.any(); }
48
49 //! If this can be used as an emoji/emojicon.
50 [[nodiscard]] bool is_emoji() const { return usage.test(PackUsage::Emoji); }
51 //! If this can be used as a sticker.
52 [[nodiscard]] bool is_sticker() const { return usage.test(PackUsage::Sticker); }
53 };
54
55 //! A pack of stickers and/or emoticons.
56 struct ImagePack
57 {
58 //! The stickers/emoticons in this pack. Indexed by slug.
59 std::map<std::string, PackImage> images;
60
61 //! Information about an image pack.
62 struct PackDescription
63 {
64 //! The name of this pack to be presented to the user.
65 std::string display_name;
66 //! An optional avatar/preview of the pack.
67 std::string avatar_url;
68 //! Attribution for this pack, i.e. where it is from originally.
69 std::string attribution;
70
71 //! What the images are used for. Indexed by PackUsage
72 std::bitset<2> usage;
73
74 //! If this can be used as an emoji/emojicon.
75 [[nodiscard]] bool is_emoji() const { return usage.none() || usage.test(PackUsage::Emoji); }
76 //! If this can be used as a sticker.
77 [[nodiscard]] bool is_sticker() const
78 {
79 return usage.none() || usage.test(PackUsage::Sticker);
80 }
81 };
82
83 //! Information about this pack
84 std::optional<PackDescription> pack;
85
86 friend void from_json(const nlohmann::json &obj, ImagePack &content);
87 friend void to_json(nlohmann::json &obj, const ImagePack &content);
88 };
89
90 //! The image packs from rooms enabled by this user to be available globally.
91 struct ImagePackRooms
92 {
93 // A map from room_id to state key to an arbitrary object (which currently is unused).
94 std::map<std::string, std::map<std::string, std::string>> rooms;
95
96 friend void from_json(const nlohmann::json &obj, ImagePackRooms &content);
97 friend void to_json(nlohmann::json &obj, const ImagePackRooms &content);
98 };
99
100 } // namespace msc2545
101 } // namespace events
102 } // namespace mtx