Support abuse log entries with multiple results

This commit is contained in:
MarkusRost 2022-09-27 09:04:48 +00:00
parent 49cd47d77b
commit e5b8a81dd9

View file

@ -21,7 +21,10 @@ from src.api.context import Context
from src.api.util import embed_helper, sanitize_to_url, parse_mediawiki_changes, clean_link, compact_author, \
create_article_path, sanitize_to_markdown
abusefilter_translatable = lambda string, _, default: {"edit": _("Edit"), "upload": _("Upload"), "move": _("Move"), "stashupload": _("Stash upload"), "delete": _("Deletion"), "createaccount": _("Account creation"), "autocreateaccount": _("Auto account creation"), "": _("None"), "warn": _("Warning issued"), "block": _("**Blocked user**"), "tag": _("Tagged the edit"), "disallow": _("Disallowed the action"), "rangeblock": _("**IP range blocked**"), "throttle": _("Throttled actions"), "blockautopromote": _("Removed autoconfirmed group"), "degroup": _("**Removed from privileged groups**")}.get(string, default)
# 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_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")
@ -29,6 +32,15 @@ logger = logging.getLogger("extensions.base")
# Processing Abuselog LOG events, separate from RC logs
def abuselog_action(results):
action = "unknown"
for result in abuselog_results:
if result in results:
action = "abuselog/{}".format(result)
break
return action
def abuse_filter_format_user(change, settings):
author = change["user"]
if settings.get("hide_ips", False):
@ -43,26 +55,28 @@ def abuse_filter_format_user(change, settings):
@formatter.embed(event="abuselog")
def embed_abuselog(ctx: Context, change: dict):
action = "abuselog/{}".format(change["result"])
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_translatable(change["action"], ctx._, ctx._("Unknown")))
embed.add_field(ctx._("Action taken"), abusefilter_translatable(change["result"], ctx._, ctx._("Unknown")))
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"))))
embed.add_field(ctx._("Action taken"), ctx._(", ").join([abusefilter_results(result, ctx._, result) for result in results]))
return embed
@formatter.compact(event="abuselog")
def compact_abuselog(ctx: Context, change: dict):
action = "abuselog/{}".format(change["result"])
results = change["result"].split(",")
action = abuselog_action(results)
author_url = clean_link(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=author, author_url=author_url, abuse_filter=sanitize_to_markdown(change["filter"]),
action=abusefilter_translatable(change["action"], ctx._, ctx._("Unknown")), target=change.get("title", ctx._("Unknown")),
action=abusefilter_actions(change["action"], ctx._, change["action"]), target=change.get("title", ctx._("Unknown")),
target_url=clean_link(create_article_path(sanitize_to_url(change.get("title", ctx._("Unknown"))))),
result=abusefilter_translatable(change["result"], ctx._, ctx._("Unknown")))
result=ctx._(", ").join([abusefilter_results(result, ctx._, result) for result in results])
return DiscordMessage(ctx.message_type, action, ctx.webhook_url, content=message)
# abusefilter/modify - AbuseFilter filter modification