From 8c202e0719bb417f8e495cd4761799c6a694a26f Mon Sep 17 00:00:00 2001 From: ConfuSomu Date: Thu, 5 Jan 2023 21:57:33 -0500 Subject: Display image attachments in status info --- src/archive_parser.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++ src/archive_parser.h | 2 ++ src/templates/attachment.html | 4 ++++ src/templates/status_info.html | 1 + 4 files changed, 56 insertions(+) create mode 100644 src/templates/attachment.html diff --git a/src/archive_parser.cpp b/src/archive_parser.cpp index d9d4cf1..30e613a 100644 --- a/src/archive_parser.cpp +++ b/src/archive_parser.cpp @@ -5,6 +5,7 @@ #include #include #include +#include Archive::Archive(QString outbox_filename, ArchiveType archive_type) : outbox_filename(outbox_filename), archive_type(archive_type) {} @@ -166,6 +167,22 @@ QString* get_html_status_info_template() { return html; } +QString* get_html_status_info_attachment_template() { + static QString* html = new QString(); + + if (html->isNull()) { + QFile file("src/templates/attachment.html"); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + html->append("Error loading attachment template."); + return html; + } + + html->append(file.readAll()); + } + + return html; +} + QString get_html_status_object_as_list(QJsonObject obj, QString element_name) { QString text; @@ -184,6 +201,36 @@ QString get_html_status_object_as_list(QJsonObject obj, QString element_name) { return text; } +QString Archive::get_html_status_attachments(QJsonValueRef attachments_ref) { + QString text; + QJsonArray attachments = attachments_ref.toArray(); + + int i = 1; + for (auto attachment_ref : attachments) { + QString att_html(*get_html_status_info_attachment_template()); + QJsonObject attachment = attachment_ref.toObject(); + + if (attachment.contains("mediaType") and attachment["mediaType"].toString().startsWith("image/")) { + att_html.replace("{{id}}", QString::number(i)); + + if (attachment.contains("url")) { + QString url = attachment["url"].toString(); + QFileInfo path(outbox_filename); + url.prepend(path.absolutePath()); + att_html.replace("{{path}}", url); + } + + if (attachment.contains("name")) + att_html.replace("{{alt-text}}", attachment["name"].toString()); + + text.append(att_html); + } + ++i; + } + + return text; +} + // status_index is assumed to be a valid index QString Archive::get_html_status_info(int status_index) { QString info_text(*get_html_status_info_template()); @@ -211,6 +258,8 @@ QString Archive::get_html_status_info(int status_index) { info_text.replace("{{summary}}", summary_text); } + info_text.replace("{{attachment-div}}", get_html_status_attachments(activity["attachment"])); + if (activity.contains("url") and activity["url"].isString()) info_text.replace("{{url-status}}", activity["url"].toString()); diff --git a/src/archive_parser.h b/src/archive_parser.h index 98fd2b8..0a1d5f3 100644 --- a/src/archive_parser.h +++ b/src/archive_parser.h @@ -37,4 +37,6 @@ private: QJsonObject *outbox_json = nullptr; QJsonArray *outbox_items = nullptr; + + QString get_html_status_attachments(QJsonValueRef attachments_ref); }; diff --git a/src/templates/attachment.html b/src/templates/attachment.html new file mode 100644 index 0000000..4c7731c --- /dev/null +++ b/src/templates/attachment.html @@ -0,0 +1,4 @@ +

Attachment {{id}}


+ +

Alt: {{alt-text}}

+
diff --git a/src/templates/status_info.html b/src/templates/status_info.html index a081463..a51c066 100644 --- a/src/templates/status_info.html +++ b/src/templates/status_info.html @@ -15,5 +15,6 @@
CW: {{summary}}
{{content}}
+
{{attachment-div}}
-- cgit v1.2.3-54-g00ecf