show when a fediverse Note is nothing but an attachment
This commit is contained in:
parent
516884acb8
commit
2642bd7dac
1 changed files with 26 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
import urllib.parse
|
import os.path, urllib.parse
|
||||||
import bs4
|
import bs4
|
||||||
from src import IRCBot, utils
|
from src import IRCBot, utils
|
||||||
from . import ap_actor
|
from . import ap_actor
|
||||||
|
@ -88,6 +88,29 @@ def _normalise_note(content):
|
||||||
|
|
||||||
return utils.parse.line_normalise(out)
|
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"):
|
def format_note(actor, note, type="Create"):
|
||||||
if type == "Announce":
|
if type == "Announce":
|
||||||
retoot_url = note
|
retoot_url = note
|
||||||
|
@ -98,13 +121,13 @@ def format_note(actor, note, type="Create"):
|
||||||
original_tooter = ap_actor.Actor(retoot.data["attributedTo"])
|
original_tooter = ap_actor.Actor(retoot.data["attributedTo"])
|
||||||
original_tooter.load()
|
original_tooter.load()
|
||||||
retooted_user = "@%s@%s" % (original_tooter.username, retoot_instance)
|
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" % (
|
return (retoot.data.get("summary", None), "%s (boost %s): %s" % (
|
||||||
actor.username, retooted_user, retoot_content), retoot_url)
|
actor.username, retooted_user, retoot_content), retoot_url)
|
||||||
|
|
||||||
elif type == "Create":
|
elif type == "Create":
|
||||||
content = _normalise_note(note["content"])
|
content = _content(note)
|
||||||
url = note.get("url", note["id"])
|
url = note.get("url", note["id"])
|
||||||
|
|
||||||
return (note.get("summary", None),
|
return (note.get("summary", None),
|
||||||
|
|
Loading…
Reference in a new issue