File indexing completed on 2024-09-29 04:28:03

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"_ls].toObject()["display_name"_ls]),
0014             fromJson<Omittable<QUrl>>(json["pack"_ls].toObject()["avatar_url"_ls]),
0015             fromJson<Omittable<QStringList>>(json["pack"_ls].toObject()["usage"_ls]),
0016             fromJson<Omittable<QString>>(json["pack"_ls].toObject()["attribution"_ls]),
0017         };
0018     } else {
0019         pack = none;
0020     }
0021 
0022     const auto &keys = json["images"_ls].toObject().keys();
0023     for (const auto &k : keys) {
0024         Omittable<EventContent::ImageInfo> info;
0025         if (json["images"_ls][k].toObject().contains(QStringLiteral("info"))) {
0026             info = EventContent::ImageInfo(QUrl(json["images"_ls][k]["url"_ls].toString()), json["images"_ls][k]["info"_ls].toObject(), k);
0027         } else {
0028             info = none;
0029         }
0030         images += ImagePackImage{
0031             k,
0032             fromJson<QUrl>(json["images"_ls][k]["url"_ls].toString()),
0033             fromJson<Omittable<QString>>(json["images"_ls][k]["body"_ls]),
0034             info,
0035             fromJson<Omittable<QStringList>>(json["images"_ls][k]["usage"_ls]),
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"_ls] = *pack->displayName;
0046         }
0047         if (pack->usage) {
0048             QJsonArray usageJson;
0049             for (const auto &usage : *pack->usage) {
0050                 usageJson += usage;
0051             }
0052             packJson["usage"_ls] = usageJson;
0053         }
0054         if (pack->avatarUrl) {
0055             packJson["avatar_url"_ls] = pack->avatarUrl->toString();
0056         }
0057         if (pack->attribution) {
0058             packJson["attribution"_ls] = *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"_ls] = image.url.toString();
0067         if (image.body) {
0068             imageJson["body"_ls] = *image.body;
0069         }
0070         if (image.usage) {
0071             QJsonArray usageJson;
0072             for (const auto &usage : *image.usage) {
0073                 usageJson += usage;
0074             }
0075             imageJson["usage"_ls] = usageJson;
0076         }
0077         imagesJson[image.shortcode] = imageJson;
0078     }
0079     (*o)["images"_ls] = imagesJson;
0080 }