From 6a40e7d7f22db794ed97ac1768ab6e5f29aa1c12 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 1 Nov 2019 16:05:48 +0100 Subject: [PATCH] Add support for inline images --- blueprints/tasks.py | 12 +++++++++++- templates/utils.html | 4 ++-- utils/template_filters.py | 28 +++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/blueprints/tasks.py b/blueprints/tasks.py index cc40f2a..e47e218 100644 --- a/blueprints/tasks.py +++ b/blueprints/tasks.py @@ -7,6 +7,7 @@ from typing import Dict import flask import requests +from bs4 import BeautifulSoup from flask import current_app as app from little_boxes import activitypub as ap from little_boxes.activitypub import _to_list @@ -271,7 +272,9 @@ def select_video_to_cache(links): return videos[0] -@blueprint.route("/task/cache_attachments", methods=["POST"]) +@blueprint.route( + "/task/cache_attachments", methods=["POST"] +) # noqa: C910 # too complex def task_cache_attachments() -> _Response: task = p.parse(flask.request) app.logger.info(f"task={task!r}") @@ -286,6 +289,13 @@ def task_cache_attachments() -> _Response: else: obj = activity + if obj.content: + content_html = BeautifulSoup(obj.content, "html5lib") + for img in content_html.find_all("img"): + src = img.attrs.get("src") + if src: + Tasks.cache_attachment({"url": src}, iri) + if obj.has_type(ap.ActivityType.VIDEO): if isinstance(obj.url, list): # TODO: filter only videogt diff --git a/templates/utils.html b/templates/utils.html index e0b774e..928466a 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -137,7 +137,7 @@ {% if obj | has_type(['Article', 'Page']) %} {{ obj.name }} {{ obj | url_or_id | get_url }} {% elif obj | has_type('Question') %} - {{ obj.content | clean | replace_custom_emojis(obj) | code_highlight | safe }} + {{ obj.content | clean | update_inline_imgs | replace_custom_emojis(obj) | code_highlight | safe }}