From e322946df7c33f76dbee46ec5b8e2118fbae8a26 Mon Sep 17 00:00:00 2001 From: ConfuSomu Date: Sat, 15 Jul 2023 15:52:49 +0200 Subject: Account for "@context" in outbox.json being array "@context" in the outbox.json file is not always a string and sometimes might be an array with multiple elements. This patch implements a json_check_item method which checks for the existence of "https://www.w3.org/ns/activitystreams" in both cases. --- src/archive_parser.cpp | 18 +++++++++++++++--- src/archive_parser.h | 4 ++++ 2 files changed, 19 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/archive_parser.cpp b/src/archive_parser.cpp index fbc8aa0..25d1b76 100644 --- a/src/archive_parser.cpp +++ b/src/archive_parser.cpp @@ -42,10 +42,9 @@ std::variant Archive::init() { return JsonNotObject; // Do some more throughful checks to make sure that the JSON is actually valid and is a Mastodon data export (the only type supported currently) - if (not (outbox_json->contains("@context") and outbox_json->value("@context").isString() and - (outbox_json->value("@context").toString() == "https://www.w3.org/ns/activitystreams"))) + if (not json_check_item(outbox_json->value("@context"), "https://www.w3.org/ns/activitystreams")) return JsonNotActivityStream; - if (outbox_json->contains("orderedItems") and outbox_json->value("orderedItems").isArray()) { + if (outbox_json->value("orderedItems").isArray()) { outbox_items = new QJsonArray(outbox_json->value("orderedItems").toArray()); // we'll need it during Archive's lifetime } else return JsonParseError; @@ -349,3 +348,16 @@ void Archive::find_attachment_dir(QString example_attachment) { } // If the attachment directory wasn't found, it will be searched for next attachment url parsing as attachment_dir_have_to_find wasn't touched (and is still true) } + +bool Archive::json_check_item(const QJsonValue& value, const QString& item) { + // This allows us to avoid having an "and" condition that has QJsonObject::contains() and simplifies the if statement to only having this current function + if (value.type() == QJsonValue::Undefined) return false; + + if (value.toString() == item) + return true; + else if (value.isArray()) { + QJsonArray array = value.toArray(); + return array.contains(item); + } else + return false; +} diff --git a/src/archive_parser.h b/src/archive_parser.h index 7377120..7f41878 100644 --- a/src/archive_parser.h +++ b/src/archive_parser.h @@ -49,4 +49,8 @@ private: QStringList get_status_object_language_list(QJsonObject obj, QString element_name); std::vector get_status_attachments_list(QJsonValueRef attachments_ref); void find_attachment_dir(QString example_attachment); + + // Check the `object`, which may be a simple string or a QJsonArray, for the existance of a string `item`. + // Returns true if the `item` exists, else false. + bool json_check_item(const QJsonValue& object, const QString& item); }; -- cgit v1.2.3-54-g00ecf