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( registry.Boolean(
False, False,
_( _(
"""Determines whether the bot will output the """Determines whether the bot will output the profile of
profile of @username@hostname accounts it sees in channel messages.""" @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 hostname = urllib.parse.urlparse(actor["id"]).hostname
return "@%s@%s" % (actor["preferredUsername"], hostname) return "@%s@%s" % (actor["preferredUsername"], hostname)
def _format_status(self, irc, status): def _format_status(self, irc, msg, status):
if status["type"] == "Create": if status["type"] == "Create":
return self._format_status(irc, status["object"]) return self._format_status(irc, msg, status["object"])
elif status["type"] == "Note": elif status["type"] == "Note":
author_url = status["attributedTo"] author_url = status["attributedTo"]
author = self._get_actor(irc, author_url) author = self._get_actor(irc, author_url)
cw = status.get("summary") cw = status.get("summary")
if cw: if cw:
return _("\x02%s (%s)\x02: [CW %s] %s") % ( if self.registryValue(
author["name"], "format.statuses.showContentWithCW",
self._format_actor_username(author), msg.channel,
cw, irc.network,
utils.web.htmlToText(status["content"]), ):
) # 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: else:
# no CW, show content
return _("\x02%s (%s)\x02: %s") % ( return _("\x02%s (%s)\x02: %s") % (
author["name"], author["name"],
self._format_actor_username(author), self._format_actor_username(author),
@ -193,7 +207,7 @@ class Fediverse(callbacks.PluginRegexp):
status["object"], headers={"Accept": ap.ACTIVITY_MIMETYPE} status["object"], headers={"Accept": ap.ACTIVITY_MIMETYPE}
) )
status = json.loads(content.decode()) status = json.loads(content.decode())
return self._format_status(irc, status) return self._format_status(irc, msg, status)
except ap.ActivityPubProtocolError as e: except ap.ActivityPubProtocolError as e:
return "<Could not fetch status: %s>" % e.args[0] return "<Could not fetch status: %s>" % e.args[0]
else: else:
@ -256,7 +270,8 @@ class Fediverse(callbacks.PluginRegexp):
return return
irc.replies( irc.replies(
filter( 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( irc.replies(
filter( 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", "type": "Create",
"actor": "https://example.org/users/someuser", "actor": "https://example.org/users/someuser",
"published": "2020-05-08T01:23:45Z", "published": "2020-05-08T01:23:45Z",
"to": ["https://example.org/users/someuser/followers"], "to": ["https://example.org/users/someuser/followers"],
"cc": [ "cc": ["https://www.w3.org/ns/activitystreams#Public"],
"https://www.w3.org/ns/activitystreams#Public",
"https://example.com/users/FirstAuthor",
],
"object": { "object": {
"id": "https://example.org/users/someuser/statuses/1234", "id": "https://example.org/users/someuser/statuses/1235",
"type": "Note", "type": "Note",
"summary": "This is a content warning", "summary": "This is a content warning",
"attributedTo": "https://example.org/users/someuser", "attributedTo": "https://example.org/users/someuser",
@ -378,15 +375,15 @@ class FediverseTestCase(ChannelPluginTestCase):
@contextlib.contextmanager @contextlib.contextmanager
def mockRequests(self, expected_requests): def mockRequests(self, expected_requests):
with Manager() as m: with Manager() as m:
expected_requests = m.list(expected_requests) expected_requests = m.list(list(expected_requests))
original_getUrlContent = utils.web.getUrlContent original_getUrlContent = utils.web.getUrlContent
@functools.wraps(original_getUrlContent) @functools.wraps(original_getUrlContent)
def newf(url, headers={}, data=None): def newf(url, headers={}, data=None):
self.assertIsNone(data, "Unexpected POST") self.assertIsNone(data, "Unexpected POST to %s" % url)
assert expected_requests, url assert expected_requests, url
(expected_url, response) = expected_requests.pop(0) (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): if isinstance(response, bytes):
return response return response
@ -572,11 +569,32 @@ class FediverseTestCase(ChannelPluginTestCase):
"\x02someuser (@someuser@example.org)\x02: " "\x02someuser (@someuser@example.org)\x02: "
+ "@ FirstAuthor I am replying to you, " + "@ FirstAuthor I am replying to you, "
+ "\x02someuser (@someuser@example.org)\x02: " + "\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 " + "This is a status with a content warning, and "
+ "\x02Boosted User (@BoostedUser@example.net)\x02: " + "\x02Boosted User (@BoostedUser@example.net)\x02: "
+ "Status Content", + "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: # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: