From 55d5692856dfeb8d53281bc2da2015c4d17bbdd5 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 16 Sep 2019 10:51:59 +0100 Subject: [PATCH] refactor AP Note stringifying out to ap_utils.py --- modules/fediverse/__init__.py | 39 +++++------------------------------ modules/fediverse/ap_utils.py | 22 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/modules/fediverse/__init__.py b/modules/fediverse/__init__.py index 93f75338..ea16c5e9 100644 --- a/modules/fediverse/__init__.py +++ b/modules/fediverse/__init__.py @@ -74,37 +74,8 @@ class Module(ModuleManager.BaseModule): if not items: raise utils.EventError("No toots found") - first_item = items[0] - if first_item["type"] == "Announce": - retoot_url = first_item["object"] - retoot_instance = urllib.parse.urlparse(retoot_url).hostname - retoot = utils.http.request(retoot_url, - headers=ACTIVITY_HEADERS, json=True, useragent=USERAGENT) - - original_tooter = ap_actor.Actor(retoot.data["attributedTo"]) - original_tooter.load() - - original_tooter = utils.http.request(original_tooter_url, - headers=ACTIVITY_HEADERS, json=True, useragent=USERAGENT) - - retooted_user = "@%s@%s" % (original_tooter.username, - retoot_instance) - - shorturl = self.exports.get_one("shorturl")( - event["server"], retoot_url) - retoot_content = utils.http.strip_html( - retoot.data["content"]) - - event["stdout"].write("%s (boost %s): %s - %s" % ( - actor.username, retooted_user, retoot_content, - shorturl)) - - elif first_item["type"] == "Create": - content = utils.http.strip_html( - first_item["object"]["content"]) - url = first_item["object"]["id"] - shorturl = self.exports.get_one("shorturl")( - event["server"], url) - - event["stdout"].write("%s: %s - %s" % (actor.username, - content, shorturl)) + out, url = ap_utils.format_note(actor, items[0]) + shorturl = self.exports.get_one("shorturl")(event["server"], url, + context=event["target"]) + out = "%s - %s" % (out, shorturl) + event["stdout"].write(out) diff --git a/modules/fediverse/ap_utils.py b/modules/fediverse/ap_utils.py index 61a59888..42fa2caf 100644 --- a/modules/fediverse/ap_utils.py +++ b/modules/fediverse/ap_utils.py @@ -54,3 +54,25 @@ def find_actor(username, instance): if link["type"] == ACTIVITY_TYPE: return link["href"] +def format_note(actor, note): + if note["type"] == "Announce": + retoot_url = note["object"] + retoot_instance = urllib.parse.urlparse(retoot_url).hostname + retoot = activity_request(retoot_url) + + original_tooter = ap_actor.Actor(retoot.data["attributedTo"]) + original_tooter.load() + original_tooter = activity_request(original_tooter_url) + retooted_user = "@%s@%s" % (original_tooter.username, retoot_instance) + retoot_content = utils.http.strip_html(retoot.data["content"]) + + return "%s (boost %s): %s - %s" % ( + actor.username, retooted_user, retoot_content), retoot_url + + elif note["type"] == "Create": + content = utils.http.strip_html(note["object"]["content"]) + url = note["object"]["id"] + + return "%s: %s" % (actor.username, content), url + + return None, None