#include "mtx/responses/media.hpp" #include #include using json = nlohmann::json; namespace mtx { namespace responses { void from_json(const json &obj, ContentURI &res) { res.content_uri = obj.at("content_uri").get(); } template static void extractOptionalAttribute(const json &obj, const char *name, std::optional &out) { if (obj.contains(name)) { const auto &field = obj.at(name); // Matrix's OpenGraph proxying does not standardize numbers to be actual json numbers, they // can be (and, at least on synapse, sometimes are) strings if constexpr (std::is_same_v) { if (field.type() == json::value_t::string) { out = static_cast(std::stol(field.get())); return; } } out = field.get(); } } void from_json(const json &obj, URLPreview &res) { res.title = obj.at("og:title").get(); res.url = obj.at("og:url").get(); extractOptionalAttribute(obj, "og:site_name", res.site_name); extractOptionalAttribute(obj, "og:description", res.description); extractOptionalAttribute(obj, "og:image:type", res.image.type); extractOptionalAttribute(obj, "og:image:width", res.image.width); extractOptionalAttribute(obj, "og:image:height", res.image.height); extractOptionalAttribute(obj, "og:image:alt", res.image.alt); res.image.size = obj.at("matrix:image:size").get(); res.image.url = obj.at("og:image").get(); } } }