From 2642bd7dac10639f979a48af3b5ec2f6b407badf Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 4 Nov 2019 13:19:37 +0000 Subject: [PATCH] show when a fediverse Note is nothing but an attachment --- modules/fediverse/ap_utils.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/fediverse/ap_utils.py b/modules/fediverse/ap_utils.py index d21985df..e5ceacf5 100644 --- a/modules/fediverse/ap_utils.py +++ b/modules/fediverse/ap_utils.py @@ -1,4 +1,4 @@ -import urllib.parse +import os.path, urllib.parse import bs4 from src import IRCBot, utils from . import ap_actor @@ -88,6 +88,29 @@ def _normalise_note(content): return utils.parse.line_normalise(out) +def _content(note): + content = note.get("content", None) + attachment = note.get("attachment", []) + + if note.get("content", None): + return _normalise_note(content) + elif attachment: + type = attachment[0]["mediaType"].split("/", 1)[0] + filename = os.path.basename(attachment[0]["url"]) + + extension = None + if "." in filename: + filename, extension = filename.rsplit(".", 1) + if len(filename) > 20: + filename = "%s[...]" % filename[:20] + + if extension: + filename = "%s.%s" % (filename, extension) + else: + filename = "%s: %s" % (type, filename) + + return "<%s>" % filename + def format_note(actor, note, type="Create"): if type == "Announce": retoot_url = note @@ -98,13 +121,13 @@ def format_note(actor, note, type="Create"): original_tooter = ap_actor.Actor(retoot.data["attributedTo"]) original_tooter.load() retooted_user = "@%s@%s" % (original_tooter.username, retoot_instance) - retoot_content = _normalise_note(retoot.data["content"]) + retoot_content = _content(retoot.data) return (retoot.data.get("summary", None), "%s (boost %s): %s" % ( actor.username, retooted_user, retoot_content), retoot_url) elif type == "Create": - content = _normalise_note(note["content"]) + content = _content(note) url = note.get("url", note["id"]) return (note.get("summary", None),