Fediverse: Add option format.statuses.showContentWithCW

This commit is contained in:
Valentin Lorentz 2020-05-10 13:20:17 +02:00
parent a52e7fa91b
commit 5908b86635
3 changed files with 72 additions and 23 deletions

View File

@ -57,8 +57,23 @@ conf.registerChannelValue(
registry.Boolean(
False,
_(
"""Determines whether the bot will output the
profile of @username@hostname accounts it sees in channel messages."""
"""Determines whether the bot will output the profile of
@username@hostname accounts it sees in channel messages."""
),
),
)
conf.registerGroup(Fediverse, "format")
conf.registerGroup(Fediverse.format, "statuses")
conf.registerChannelValue(
Fediverse.format.statuses,
"showContentWithCW",
registry.Boolean(
True,
_(
"""Determines whether the content of a status will be shown
when the status has a Content Warning."""
),
),
)

View File

@ -166,21 +166,35 @@ class Fediverse(callbacks.PluginRegexp):
hostname = urllib.parse.urlparse(actor["id"]).hostname
return "@%s@%s" % (actor["preferredUsername"], hostname)
def _format_status(self, irc, status):
def _format_status(self, irc, msg, status):
if status["type"] == "Create":
return self._format_status(irc, status["object"])
return self._format_status(irc, msg, status["object"])
elif status["type"] == "Note":
author_url = status["attributedTo"]
author = self._get_actor(irc, author_url)
cw = status.get("summary")
if cw:
return _("\x02%s (%s)\x02: [CW %s] %s") % (
if self.registryValue(
"format.statuses.showContentWithCW",
msg.channel,
irc.network,
):
# show CW and content
return _("\x02%s (%s)\x02: \x02[CW %s]\x02 %s") % (
author["name"],
self._format_actor_username(author),
cw,
utils.web.htmlToText(status["content"]),
)
else:
# show CW but not content
return _("\x02%s (%s)\x02: CW %s") % (
author["name"],
self._format_actor_username(author),
cw,
)
else:
# no CW, show content
return _("\x02%s (%s)\x02: %s") % (
author["name"],
self._format_actor_username(author),
@ -193,7 +207,7 @@ class Fediverse(callbacks.PluginRegexp):
status["object"], headers={"Accept": ap.ACTIVITY_MIMETYPE}
)
status = json.loads(content.decode())
return self._format_status(irc, status)
return self._format_status(irc, msg, status)
except ap.ActivityPubProtocolError as e:
return "<Could not fetch status: %s>" % e.args[0]
else:
@ -256,7 +270,8 @@ class Fediverse(callbacks.PluginRegexp):
return
irc.replies(
filter(
bool, (self._format_status(irc, status) for status in statuses)
bool,
(self._format_status(irc, msg, status) for status in statuses),
)
)
@ -278,7 +293,8 @@ class Fediverse(callbacks.PluginRegexp):
)
irc.replies(
filter(
bool, (self._format_status(irc, status) for status in statuses)
bool,
(self._format_status(irc, msg, status) for status in statuses),
)
)

View File

@ -240,17 +240,14 @@ OUTBOX_FIRSTPAGE_VALUE = {
},
},
{
"id": "https://example.org/users/someuser/statuses/1234/activity",
"id": "https://example.org/users/someuser/statuses/1235/activity",
"type": "Create",
"actor": "https://example.org/users/someuser",
"published": "2020-05-08T01:23:45Z",
"to": ["https://example.org/users/someuser/followers"],
"cc": [
"https://www.w3.org/ns/activitystreams#Public",
"https://example.com/users/FirstAuthor",
],
"cc": ["https://www.w3.org/ns/activitystreams#Public"],
"object": {
"id": "https://example.org/users/someuser/statuses/1234",
"id": "https://example.org/users/someuser/statuses/1235",
"type": "Note",
"summary": "This is a content warning",
"attributedTo": "https://example.org/users/someuser",
@ -378,15 +375,15 @@ class FediverseTestCase(ChannelPluginTestCase):
@contextlib.contextmanager
def mockRequests(self, expected_requests):
with Manager() as m:
expected_requests = m.list(expected_requests)
expected_requests = m.list(list(expected_requests))
original_getUrlContent = utils.web.getUrlContent
@functools.wraps(original_getUrlContent)
def newf(url, headers={}, data=None):
self.assertIsNone(data, "Unexpected POST")
self.assertIsNone(data, "Unexpected POST to %s" % url)
assert expected_requests, url
(expected_url, response) = expected_requests.pop(0)
self.assertEqual(url, expected_url, "Unexpected URL")
self.assertEqual(url, expected_url, "Unexpected URL: %s" % url)
if isinstance(response, bytes):
return response
@ -572,11 +569,32 @@ class FediverseTestCase(ChannelPluginTestCase):
"\x02someuser (@someuser@example.org)\x02: "
+ "@ FirstAuthor I am replying to you, "
+ "\x02someuser (@someuser@example.org)\x02: "
+ "[CW This is a content warning] "
+ "\x02[CW This is a content warning]\x02 "
+ "This is a status with a content warning, and "
+ "\x02Boosted User (@BoostedUser@example.net)\x02: "
+ "Status Content",
)
# The actors are cached from the previous request
expected_requests = [
(OUTBOX_URL, OUTBOX_DATA),
(OUTBOX_FIRSTPAGE_URL, OUTBOX_FIRSTPAGE_DATA),
(BOOSTED_URL, BOOSTED_DATA),
]
with self.mockRequests(expected_requests):
with conf.supybot.plugins.Fediverse.format.statuses.showContentWithCW.context(
False
):
self.assertResponse(
"statuses @someuser@example.org",
"\x02someuser (@someuser@example.org)\x02: "
+ "@ FirstAuthor I am replying to you, "
+ "\x02someuser (@someuser@example.org)\x02: "
+ "CW This is a content warning, and "
+ "\x02Boosted User (@BoostedUser@example.net)\x02: "
+ "Status Content",
)
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: