From 6bb64e2ba63726093aeb0a7cb85a1bf28c63e52f Mon Sep 17 00:00:00 2001 From: MarkusRost <2701034-MarkusRost@users.noreply.gitlab.com> Date: Tue, 4 Oct 2022 07:29:56 +0000 Subject: [PATCH 1/3] Improved abuse log feed messages *Includes translation changes* Made the filter name link to the entry details (#256) Made embeds layout match other actions Added edit count to IP users Clarified `blockautopromote` result --- extensions/base/abusefilter.py | 72 +++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/extensions/base/abusefilter.py b/extensions/base/abusefilter.py index edae5a1..fac98c3 100644 --- a/extensions/base/abusefilter.py +++ b/extensions/base/abusefilter.py @@ -22,7 +22,7 @@ from src.api.util import embed_helper, sanitize_to_url, parse_mediawiki_changes, # Order results from most drastic first to less drastic last abuselog_results = ["degroup", "blockautopromote", "rangeblock", "block", "disallow", "throttle", "warn", "tag", ""] -abusefilter_results = lambda string, _, default: {"degroup": _("**Removed from privileged groups**"), "blockautopromote": _("Removed autoconfirmed group"), "rangeblock": _("**IP range blocked**"), "block": _("**Blocked user**"), "disallow": _("Disallowed the action"), "throttle": _("Throttled actions"), "warn": _("Warning issued"), "tag": _("Tagged the edit"), "": _("None")}.get(string, default) +abusefilter_results = lambda string, _, default: {"degroup": _("**Removed from privileged groups**"), "blockautopromote": _("**Removed autopromoted groups**"), "rangeblock": _("**IP range blocked**"), "block": _("**Blocked user**"), "disallow": _("Disallowed the action"), "throttle": _("Throttled actions"), "warn": _("Warning issued"), "tag": _("Tagged the edit"), "": _("None")}.get(string, default) abusefilter_actions = lambda string, _, default: {"edit": _("Edit"), "upload": _("Upload"), "move": _("Move"), "stashupload": _("Stash upload"), "delete": _("Deletion"), "createaccount": _("Account creation"), "autocreateaccount": _("Auto account creation")}.get(string, default) logger = logging.getLogger("extensions.base") @@ -40,16 +40,15 @@ def abuselog_action(results): return action -def abuse_filter_format_user(change, settings): - author = change["user"] - if settings.get("hide_ips", False): - try: - ipaddress.ip_address(change["user"]) - except ValueError: - pass - else: - author = _("Unregistered user") - return author +def abuse_filter_is_ip(change): + is_ip = False + try: + ipaddress.ip_address(change["user"]) + except ValueError: + pass + else: + is_ip = True + return is_ip @formatter.embed(event="abuselog") @@ -57,10 +56,42 @@ def embed_abuselog(ctx: Context, change: dict): results = change["result"].split(",") action = abuselog_action(results) embed = DiscordMessage(ctx.message_type, action, ctx.webhook_url) - author = abuse_filter_format_user(change, ctx.settings) - embed["title"] = ctx._("{user} triggered \"{abuse_filter}\"").format(user=author, abuse_filter=sanitize_to_markdown(change["filter"])) - embed.add_field(ctx._("Performed"), abusefilter_actions(change["action"], ctx._, change["action"])) - embed.add_field(ctx._("Title"), sanitize_to_markdown(change.get("title", ctx._("Unknown")))) + author = change["user"] + author_url = ctx.client.create_article_path("User:{}".format(sanitize_to_url(change["user"]))) + if abuse_filter_is_ip(change): + author_url = ctx.client.create_article_path("Special:Contributions/{user}".format(user=sanitize_to_url(change["user"]))) + ip_mapper = ctx.client.get_ipmapper() + logger.debug("current user: {} with cache of IPs: {}".format(change["user"], ip_mapper.keys())) + if change["user"] not in list(ip_mapper.keys()): + try: + contibs = ctx.client.make_api_request( + "?action=query&format=json&list=usercontribs&uclimit=max&ucuser={user}&ucstart={timestamp}&ucprop=".format( + user=sanitize_to_url(change["user"]), timestamp=change["timestamp"]), "query", + "usercontribs") + except (ServerError, MediaWikiError): + logger.warning("WARNING: Something went wrong when checking amount of contributions for given IP address") + if ctx.settings.get("hide_ips", False): + author = ctx._("Unregistered user") + else: + author = change["user"] + "(?)" + else: + ip_mapper[change["user"]] = len(contibs) + logger.debug("Current params user {} and state of map_ips {}".format(change["user"], ip_mapper)) + if ctx.settings.get("hide_ips", False): + author = ctx._("Unregistered user") + else: + author = "{author} ({contribs})".format(author=change["user"], contribs=len(contibs)) + else: + logger.debug("Current params user {} and state of map_ips {}".format(change["user"], ip_mapper)) + author = "{author} ({amount})".format( + author=change["user"] if ctx.settings.get("hide_ips", False) is False else ctx._("Unregistered user"), + amount=ip_mapper[change["user"]]) + embed.set_author(author, author_url) + embed["title"] = sanitize_to_markdown(change["filter"]) + embed["url"] = ctx.client.create_article_path("Special:AbuseLog/{entry}".format(entry=change["id"])) + embed.add_field(ctx._("Title"), "[{target}]({target_url})".format(target=change.get("title", ctx._("Unknown")), + target_url=clean_link(ctx.client.create_article_path(sanitize_to_url(change.get("title", ctx._("Unknown")))))), inline=True) + embed.add_field(ctx._("Performed"), abusefilter_actions(change["action"], ctx._, change["action"]), inline=True) embed.add_field(ctx._("Action taken"), ctx._(", ").join([abusefilter_results(result, ctx._, result) for result in results])) return embed @@ -69,10 +100,15 @@ def embed_abuselog(ctx: Context, change: dict): def compact_abuselog(ctx: Context, change: dict): results = change["result"].split(",") action = abuselog_action(results) - author_url = clean_link(ctx.client.create_article_path("User:{user}".format(user=change["user"]))) - author = abuse_filter_format_user(change, ctx.settings) - message = ctx._("[{author}]({author_url}) triggered *{abuse_filter}*, performing the action \"{action}\" on *[{target}]({target_url})* - action taken: {result}.").format( + author = change["user"] + author_url = clean_link(ctx.client.create_article_path("User:{user}".format(user=sanitize_to_url(change["user"])))) + if abuse_filter_is_ip(change): + author_url = clean_link(ctx.client.create_article_path("Special:Contributions/{user}".format(user=sanitize_to_url(change["user"])))) + if ctx.settings.get("hide_ips", False): + author = ctx._("Unregistered user") + message = ctx._("[{author}]({author_url}) triggered *[{abuse_filter}]({details_url})*, performing the action \"{action}\" on *[{target}]({target_url})* - action taken: {result}.").format( author=author, author_url=author_url, abuse_filter=sanitize_to_markdown(change["filter"]), + details_url=clean_link(ctx.client.create_article_path("Special:AbuseLog/{entry}".format(entry=change["id"]))), action=abusefilter_actions(change["action"], ctx._, change["action"]), target=change.get("title", ctx._("Unknown")), target_url=clean_link(ctx.client.create_article_path(sanitize_to_url(change.get("title", ctx._("Unknown"))))), result=ctx._(", ").join([abusefilter_results(result, ctx._, result) for result in results])) From 805dc858f602072693a693234c3bd6d84b0d6b6d Mon Sep 17 00:00:00 2001 From: MarkusRost <2701034-MarkusRost@users.noreply.gitlab.com> Date: Tue, 4 Oct 2022 11:34:43 +0000 Subject: [PATCH 2/3] Modified util author functions to support abuselog --- extensions/base/abusefilter.py | 39 ++-------------------------------- src/api/util.py | 8 +++---- 2 files changed, 6 insertions(+), 41 deletions(-) diff --git a/extensions/base/abusefilter.py b/extensions/base/abusefilter.py index fac98c3..2eedb2a 100644 --- a/extensions/base/abusefilter.py +++ b/extensions/base/abusefilter.py @@ -56,43 +56,13 @@ def embed_abuselog(ctx: Context, change: dict): results = change["result"].split(",") action = abuselog_action(results) embed = DiscordMessage(ctx.message_type, action, ctx.webhook_url) - author = change["user"] - author_url = ctx.client.create_article_path("User:{}".format(sanitize_to_url(change["user"]))) - if abuse_filter_is_ip(change): - author_url = ctx.client.create_article_path("Special:Contributions/{user}".format(user=sanitize_to_url(change["user"]))) - ip_mapper = ctx.client.get_ipmapper() - logger.debug("current user: {} with cache of IPs: {}".format(change["user"], ip_mapper.keys())) - if change["user"] not in list(ip_mapper.keys()): - try: - contibs = ctx.client.make_api_request( - "?action=query&format=json&list=usercontribs&uclimit=max&ucuser={user}&ucstart={timestamp}&ucprop=".format( - user=sanitize_to_url(change["user"]), timestamp=change["timestamp"]), "query", - "usercontribs") - except (ServerError, MediaWikiError): - logger.warning("WARNING: Something went wrong when checking amount of contributions for given IP address") - if ctx.settings.get("hide_ips", False): - author = ctx._("Unregistered user") - else: - author = change["user"] + "(?)" - else: - ip_mapper[change["user"]] = len(contibs) - logger.debug("Current params user {} and state of map_ips {}".format(change["user"], ip_mapper)) - if ctx.settings.get("hide_ips", False): - author = ctx._("Unregistered user") - else: - author = "{author} ({contribs})".format(author=change["user"], contribs=len(contibs)) - else: - logger.debug("Current params user {} and state of map_ips {}".format(change["user"], ip_mapper)) - author = "{author} ({amount})".format( - author=change["user"] if ctx.settings.get("hide_ips", False) is False else ctx._("Unregistered user"), - amount=ip_mapper[change["user"]]) - embed.set_author(author, author_url) embed["title"] = sanitize_to_markdown(change["filter"]) embed["url"] = ctx.client.create_article_path("Special:AbuseLog/{entry}".format(entry=change["id"])) embed.add_field(ctx._("Title"), "[{target}]({target_url})".format(target=change.get("title", ctx._("Unknown")), target_url=clean_link(ctx.client.create_article_path(sanitize_to_url(change.get("title", ctx._("Unknown")))))), inline=True) embed.add_field(ctx._("Performed"), abusefilter_actions(change["action"], ctx._, change["action"]), inline=True) embed.add_field(ctx._("Action taken"), ctx._(", ").join([abusefilter_results(result, ctx._, result) for result in results])) + embed_helper(ctx, embed, change, is_anon=abuse_filter_is_ip(change), set_desc=False) return embed @@ -100,12 +70,7 @@ def embed_abuselog(ctx: Context, change: dict): def compact_abuselog(ctx: Context, change: dict): results = change["result"].split(",") action = abuselog_action(results) - author = change["user"] - author_url = clean_link(ctx.client.create_article_path("User:{user}".format(user=sanitize_to_url(change["user"])))) - if abuse_filter_is_ip(change): - author_url = clean_link(ctx.client.create_article_path("Special:Contributions/{user}".format(user=sanitize_to_url(change["user"])))) - if ctx.settings.get("hide_ips", False): - author = ctx._("Unregistered user") + author, author_url = compact_author(ctx, change, is_anon=abuse_filter_is_ip(change)) message = ctx._("[{author}]({author_url}) triggered *[{abuse_filter}]({details_url})*, performing the action \"{action}\" on *[{target}]({target_url})* - action taken: {result}.").format( author=author, author_url=author_url, abuse_filter=sanitize_to_markdown(change["filter"]), details_url=clean_link(ctx.client.create_article_path("Special:AbuseLog/{entry}".format(entry=change["id"]))), diff --git a/src/api/util.py b/src/api/util.py index 18d9c5e..91d74e5 100644 --- a/src/api/util.py +++ b/src/api/util.py @@ -79,12 +79,12 @@ def compact_summary(ctx: Context) -> str: return " *({})*".format(ctx.parsedcomment) return "" -def compact_author(ctx: Context, change: dict) -> (Optional[str], Optional[str]): +def compact_author(ctx: Context, change: dict, is_anon=None) -> (Optional[str], Optional[str]): """Returns link to the author and the author itself respecting the settings""" author, author_url = None, None if ctx.event != "suppressed": author_url = clean_link(ctx.client.create_article_path("User:{user}".format(user=sanitize_to_url(change["user"])))) - if "anon" in change: + if ("anon" in change if is_anon is None else is_anon): if settings.get("hide_ips", False): author = _("Unregistered user") else: @@ -94,7 +94,7 @@ def compact_author(ctx: Context, change: dict) -> (Optional[str], Optional[str]) return author, author_url -def embed_helper(ctx: Context, message: DiscordMessage, change: dict, set_user=True, set_edit_meta=True, set_desc=True) -> None: +def embed_helper(ctx: Context, message: DiscordMessage, change: dict, set_user=True, set_edit_meta=True, set_desc=True, is_anon=None) -> None: """Helps in preparing common edit/log fields for events. Passed arguments automatically become saturated with needed data. All automatic setups can be disabled by setting relevant variable to False @@ -105,7 +105,7 @@ def embed_helper(ctx: Context, message: DiscordMessage, change: dict, set_user=T setting default description (to ctx.parsedcomment)""" if set_user: author = None - if "anon" in change: + if ("anon" in change if is_anon is None else is_anon): author_url = ctx.client.create_article_path("Special:Contributions/{user}".format(user=sanitize_to_url(change["user"]))) ip_mapper = ctx.client.get_ipmapper() logger.debug("current user: {} with cache of IPs: {}".format(change["user"], ip_mapper.keys())) From 957aad3f501e50c8805804fb0679c167f8c58b03 Mon Sep 17 00:00:00 2001 From: Frisk Date: Tue, 4 Oct 2022 17:47:16 +0200 Subject: [PATCH 3/3] Bunch of code style fixups --- src/api/util.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/util.py b/src/api/util.py index 91d74e5..2930828 100644 --- a/src/api/util.py +++ b/src/api/util.py @@ -79,12 +79,12 @@ def compact_summary(ctx: Context) -> str: return " *({})*".format(ctx.parsedcomment) return "" -def compact_author(ctx: Context, change: dict, is_anon=None) -> (Optional[str], Optional[str]): +def compact_author(ctx: Context, change: dict, is_anon: Optional[bool] = None) -> (Optional[str], Optional[str]): """Returns link to the author and the author itself respecting the settings""" author, author_url = None, None if ctx.event != "suppressed": author_url = clean_link(ctx.client.create_article_path("User:{user}".format(user=sanitize_to_url(change["user"])))) - if ("anon" in change if is_anon is None else is_anon): + if "anon" in change if is_anon is None else is_anon: if settings.get("hide_ips", False): author = _("Unregistered user") else: @@ -94,7 +94,7 @@ def compact_author(ctx: Context, change: dict, is_anon=None) -> (Optional[str], return author, author_url -def embed_helper(ctx: Context, message: DiscordMessage, change: dict, set_user=True, set_edit_meta=True, set_desc=True, is_anon=None) -> None: +def embed_helper(ctx: Context, message: DiscordMessage, change: dict, set_user=True, set_edit_meta=True, set_desc=True, is_anon: Optional[bool] = None) -> None: """Helps in preparing common edit/log fields for events. Passed arguments automatically become saturated with needed data. All automatic setups can be disabled by setting relevant variable to False @@ -105,7 +105,7 @@ def embed_helper(ctx: Context, message: DiscordMessage, change: dict, set_user=T setting default description (to ctx.parsedcomment)""" if set_user: author = None - if ("anon" in change if is_anon is None else is_anon): + if "anon" in change if is_anon is None else is_anon: author_url = ctx.client.create_article_path("Special:Contributions/{user}".format(user=sanitize_to_url(change["user"]))) ip_mapper = ctx.client.get_ipmapper() logger.debug("current user: {} with cache of IPs: {}".format(change["user"], ip_mapper.keys()))