aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorConfuSomu2023-07-15 15:52:49 +0200
committerConfuSomu2023-07-15 15:52:49 +0200
commite322946df7c33f76dbee46ec5b8e2118fbae8a26 (patch)
tree9a0fb14e794743c6baa87dcb61a694b5166141bc /src
parent0e84cb6c13b8b47ed0d05b21f14490bf6c4744f2 (diff)
downloadActorViewer-e322946df7c33f76dbee46ec5b8e2118fbae8a26.tar
ActorViewer-e322946df7c33f76dbee46ec5b8e2118fbae8a26.tar.gz
ActorViewer-e322946df7c33f76dbee46ec5b8e2118fbae8a26.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/archive_parser.cpp18
-rw-r--r--src/archive_parser.h4
2 files changed, 19 insertions, 3 deletions
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<QString, Archive::InitError> 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<APAttachmentFields> 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);
};