File indexing completed on 2024-05-12 16:25:02

0001 // SPDX-FileCopyrightText: 2021-2023 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #include "imagepackevent.h"
0005 #include <QJsonObject>
0006 
0007 using namespace Quotient;
0008 
0009 ImagePackEventContent::ImagePackEventContent(const QJsonObject &json)
0010 {
0011     if (json.contains(QStringLiteral("pack"))) {
0012         pack = ImagePackEventContent::Pack{
0013             fromJson<Omittable<QString>>(json["pack"].toObject()["display_name"]),
0014             fromJson<Omittable<QUrl>>(json["pack"].toObject()["avatar_url"]),
0015             fromJson<Omittable<QStringList>>(json["pack"].toObject()["usage"]),
0016             fromJson<Omittable<QString>>(json["pack"].toObject()["attribution"]),
0017         };
0018     } else {
0019         pack = none;
0020     }
0021 
0022     const auto &keys = json["images"].toObject().keys();
0023     for (const auto &k : keys) {
0024         Omittable<EventContent::ImageInfo> info;
0025         if (json["images"][k].toObject().contains(QStringLiteral("info"))) {
0026             info = EventContent::ImageInfo(QUrl(json["images"][k]["url"].toString()), json["images"][k]["info"].toObject(), k);
0027         } else {
0028             info = none;
0029         }
0030         images += ImagePackImage{
0031             k,
0032             fromJson<QUrl>(json["images"][k]["url"].toString()),
0033             fromJson<Omittable<QString>>(json["images"][k]["body"]),
0034             info,
0035             fromJson<Omittable<QStringList>>(json["images"][k]["usage"]),
0036         };
0037     }
0038 }
0039 
0040 void ImagePackEventContent::fillJson(QJsonObject *o) const
0041 {
0042     if (pack) {
0043         QJsonObject packJson;
0044         if (pack->displayName) {
0045             packJson["display_name"] = *pack->displayName;
0046         }
0047         if (pack->usage) {
0048             QJsonArray usageJson;
0049             for (const auto &usage : *pack->usage) {
0050                 usageJson += usage;
0051             }
0052             packJson["usage"] = usageJson;
0053         }
0054         if (pack->avatarUrl) {
0055             packJson["avatar_url"] = pack->avatarUrl->toString();
0056         }
0057         if (pack->attribution) {
0058             packJson["attribution"] = *pack->attribution;
0059         }
0060         (*o)["pack"_ls] = packJson;
0061     }
0062 
0063     QJsonObject imagesJson;
0064     for (const auto &image : images) {
0065         QJsonObject imageJson;
0066         imageJson["url"] = image.url.toString();
0067         if (image.body) {
0068             imageJson["body"] = *image.body;
0069         }
0070         if (image.usage) {
0071             QJsonArray usageJson;
0072             for (const auto &usage : *image.usage) {
0073                 usageJson += usage;
0074             }
0075             imageJson["usage"] = usageJson;
0076         }
0077         imagesJson[image.shortcode] = imageJson;
0078     }
0079     (*o)["images"_ls] = imagesJson;
0080 }