Fediverse: Add support for descriptions with line breaks

This commit is contained in:
Valentin Lorentz 2022-07-20 18:09:41 +02:00
parent 2df2bc28d0
commit 34f8842273
3 changed files with 21 additions and 10 deletions

View File

@ -50,6 +50,10 @@ _ = PluginInternationalization("Fediverse")
_username_regexp = re.compile("@(?P<localuser>[^@ ]+)@(?P<hostname>[^@ ]+)")
def html_to_text(html):
return utils.web.htmlToText(html).split("\n", 1)[0].strip()
class FediverseHttp(httpserver.SupyHTTPServerCallback):
name = "minimal ActivityPub server"
defaultResponse = _(
@ -236,7 +240,9 @@ class Fediverse(callbacks.PluginRegexp):
if author.get("id"):
return self._format_author(irc, author["id"])
elif isinstance(author, list):
return format("%L", [self._format_author(irc, item) for item in author])
return format(
"%L", [self._format_author(irc, item) for item in author]
)
else:
return "<unknown>"
@ -245,7 +251,9 @@ class Fediverse(callbacks.PluginRegexp):
return self._format_status(irc, msg, status["object"])
elif status["type"] == "Note":
cw = status.get("summary")
author_fullname = self._format_author(irc, status.get("attributedTo"))
author_fullname = self._format_author(
irc, status.get("attributedTo")
)
if cw:
if self.registryValue(
"format.statuses.showContentWithCW",
@ -258,7 +266,7 @@ class Fediverse(callbacks.PluginRegexp):
% (
author_fullname,
cw,
utils.web.htmlToText(status["content"]),
html_to_text(status["content"]),
)
]
else:
@ -270,7 +278,7 @@ class Fediverse(callbacks.PluginRegexp):
_("%s: %s")
% (
author_fullname,
utils.web.htmlToText(status["content"]),
html_to_text(status["content"]),
)
]
@ -288,13 +296,15 @@ class Fediverse(callbacks.PluginRegexp):
except ap.ActivityPubProtocolError as e:
return "<Could not fetch status: %s>" % e.args[0]
elif status["type"] == "Video":
author_fullname = self._format_author(irc, status.get("attributedTo"))
author_fullname = self._format_author(
irc, status.get("attributedTo")
)
return format(
_("\x02%s\x02 (%T) by %s: %s"),
status["name"],
abs(parse_xsd_duration(status["duration"]).total_seconds()),
author_fullname,
status["content"],
html_to_text(status["content"]),
)
else:
assert False, "Unknown status type %s: %r" % (
@ -313,14 +323,14 @@ class Fediverse(callbacks.PluginRegexp):
_("%s: %s")
% (
self._format_actor_fullname(actor),
utils.web.htmlToText(actor["summary"]),
html_to_text(actor["summary"]),
)
)
def _format_profile(self, irc, msg, actor):
return _("%s: %s") % (
self._format_actor_fullname(actor),
utils.web.htmlToText(actor["summary"]),
html_to_text(actor["summary"]),
)
def usernameSnarfer(self, irc, msg, match):

View File

@ -446,7 +446,8 @@ class NetworklessFediverseTestCase(BaseFediverseTestCase):
"status https://example.org/w/gABde9e210FGHre",
"\x02name of video\x02 (1 hour, 26 minutes, and 0 seconds) "
"by \x02chocobozzz\x02 (@chocobozzz@peertube.cpy.re) "
"and \x02someuser\x02 (@someuser@example.org): description of video"
"and \x02someuser\x02 (@someuser@example.org): "
"description of the video with a second line",
)
def testStatusUrlSnarferDisabled(self):

View File

@ -535,7 +535,7 @@ PEERTUBE_VIDEO_VALUE = {
"originallyPublishedAt": None,
"updated": "2022-07-13T07:03:12.373Z",
"mediaType": "text/markdown",
"content": "description of video",
"content": "description of <strong>the</strong> video\r\nwith a second line",
"support": None,
"subtitleLanguage": [],
"icon": [