support !fedi with a URL to a Note

This commit is contained in:
jesopo 2019-10-04 13:06:29 +01:00
parent 8938e119b4
commit 472b5ba9f0
2 changed files with 43 additions and 22 deletions

View file

@ -53,8 +53,11 @@ class Module(ModuleManager.BaseModule):
@utils.kwarg("usage", "@<user>@<instance>") @utils.kwarg("usage", "@<user>@<instance>")
def fedi(self, event): def fedi(self, event):
account = None account = None
url = None
if not event["args"]: if not event["args"]:
account = event["user"].get_setting("fediverse", None) account = event["user"].get_setting("fediverse", None)
elif utils.http.REGEX_URL.match(event["args_split"][0]):
url = event["args_split"][0]
elif not "@" in event["args"]: elif not "@" in event["args"]:
target = event["args_split"][0] target = event["args_split"][0]
if event["server"].has_user_id(target): if event["server"].has_user_id(target):
@ -63,14 +66,40 @@ class Module(ModuleManager.BaseModule):
else: else:
account = event["args_split"][0] account = event["args_split"][0]
username = None note = None
instance = None type = "Create"
if account: if not url == None:
username, instance = ap_utils.split_username(account) note_page = ap_utils.activity_request(url)
if not note_page.content_type == ap_utils.ACTIVITY_TYPE:
raise utils.EventError("That's not a fediverse URL")
if not username or not instance: note = note_page.data
raise utils.EventError("Please provide @<user>@<instance>") actor = ap_actor.Actor(note["attributedTo"])
actor.load()
else:
username = None
instance = None
if account:
username, instance = ap_utils.split_username(account)
if not username or not instance:
raise utils.EventError("Please provide @<user>@<instance>")
actor, note = self._get_from_outbox(username, instance)
type = note["type"]
note = note["object"]
cw, out, url = ap_utils.format_note(actor, note, type)
shorturl = self.exports.get_one("shorturl")(event["server"], url,
context=event["target"])
if cw:
out = "CW: %s - %s" % (cw, shorturl)
else:
out = "%s - %s" % (out, shorturl)
event["stdout"].write(out)
def _get_from_outbox(self, username, instance):
actor_url = ap_utils.find_actor(username, instance) actor_url = ap_utils.find_actor(username, instance)
if not actor_url: if not actor_url:
@ -89,12 +118,4 @@ class Module(ModuleManager.BaseModule):
if not first_item: if not first_item:
raise utils.EventError("No toots found") raise utils.EventError("No toots found")
cw, out, url = ap_utils.format_note(actor, first_item) return actor, first_item
shorturl = self.exports.get_one("shorturl")(event["server"], url,
context=event["target"])
if cw:
out = "CW: %s - %s" % (cw, shorturl)
else:
out = "%s - %s" % (out, shorturl)
event["stdout"].write(out)

View file

@ -56,9 +56,9 @@ def find_actor(username, instance):
if link["type"] == ACTIVITY_TYPE: if link["type"] == ACTIVITY_TYPE:
return link["href"] return link["href"]
def format_note(actor, note): def format_note(actor, note, type="Create"):
if note["type"] == "Announce": if type == "Announce":
retoot_url = note["object"] retoot_url = note
retoot_instance = urllib.parse.urlparse(retoot_url).hostname retoot_instance = urllib.parse.urlparse(retoot_url).hostname
retoot = activity_request(retoot_url) retoot = activity_request(retoot_url)
@ -70,11 +70,11 @@ def format_note(actor, note):
return (retoot.data.get("summary", None), "%s (boost %s): %s - %s" % ( return (retoot.data.get("summary", None), "%s (boost %s): %s - %s" % (
actor.username, retooted_user, retoot_content), retoot_url) actor.username, retooted_user, retoot_content), retoot_url)
elif note["type"] == "Create": elif type == "Create":
content = utils.http.strip_html(note["object"]["content"]) content = utils.http.strip_html(note["content"])
url = note["object"]["id"] url = note["id"]
return (note["object"].get("summary", None), return (note.get("summary", None),
"%s: %s" % (actor.username, content), url) "%s: %s" % (actor.username, content), url)
return None, None, None return None, None, None