From c3d21c43df2562eceeadcab006578cff6f7c25a1 Mon Sep 17 00:00:00 2001 From: MarkusRost <2701034-MarkusRost@users.noreply.gitlab.com> Date: Thu, 6 Oct 2022 09:05:33 +0000 Subject: [PATCH] Allow pre_hooks to change the event type #245 --- docs/API spec.md | 2 +- src/discussions.py | 7 ++++--- src/rcgcdw.py | 18 ++++++++++++------ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/docs/API spec.md b/docs/API spec.md index faafaba..4c0aed8 100644 --- a/docs/API spec.md +++ b/docs/API spec.md @@ -35,7 +35,7 @@ common functions that can be used to interact with the script and the wiki. ### Formatter **Path**: `src.api.formatter` _Formatter module implements two decorators: `embed` and `compact`. Both of them can take the following keyword arguments:_ -- `event` - string - event type for formatter, in case the event is a [log event](https://www.mediawiki.org/wiki/Manual:Log_actions) it's constructed by taking log_type and combining it with log_action with / character in between (for example `upload/overwrite`). If the event however is not a log event but action like edit, the type will consist only of `type` value. +- `event` - string - event type for formatter, in case the event is a [log event](https://www.mediawiki.org/wiki/Manual:Log_actions) it's constructed by taking log_type and combining it with log_action with / character in between (for example `upload/overwrite`). If the event however is not a log event but action like edit, the type will consist only of `type` value. (Pre hooks may receive or set this to an empty string for events that will be ignored.) - `aliases` - list[str] - list of strings containing all the events given event should alias for, it helps in case you want the same function be used for multiple event types. Both `event` and `aliases` arguments are optional in formatters. However, every formatter needs to have some kind of event specified. If it's not specified in the decorator, a fallback method will be used which constructs event type in format `{func.__module__}/{func.__name__.split("_", 1)[1]}`, in other terms taking the name of the file in which formatter is defined as first part and entire function name after first _ character as second part. Note that this fallback works only for log events. diff --git a/src/discussions.py b/src/discussions.py index be6a156..2e9622d 100644 --- a/src/discussions.py +++ b/src/discussions.py @@ -122,12 +122,13 @@ def parse_discussion_post(post, comment_pages): except KeyError: discussion_logger.error("Could not parse paths for article comment, here is the content of comment_pages: {}, ignoring...".format(comment_pages)) raise ArticleCommentError - event_type = f"discussion/{post_type.lower()}" - context.event = event_type + context.event = f"discussion/{post_type.lower()}" context.set_comment_page(comment_page) run_hooks(pre_hooks, context, post) + if not context.event: + return try: - discord_message = default_message(event_type, formatter_hooks)(context, post) + discord_message = default_message(context.event, formatter_hooks)(context, post) except NoFormatter: return except: diff --git a/src/rcgcdw.py b/src/rcgcdw.py index e9c237b..4f43aaf 100644 --- a/src/rcgcdw.py +++ b/src/rcgcdw.py @@ -206,8 +206,10 @@ def rc_processor(change, changed_categories): if ("actionhidden" in change or "suppressed" in change) and "suppressed" not in settings["ignored"]: # if event is hidden using suppression context.event = "suppressed" run_hooks(pre_hooks, context, change) + if not context.event: + return try: - discord_message: Optional[DiscordMessage] = default_message("suppressed", formatter_hooks)(context, change) + discord_message: Optional[DiscordMessage] = default_message(context.event, formatter_hooks)(context, change) except NoFormatter: return except: @@ -243,23 +245,25 @@ def rc_processor(change, changed_categories): return context.event = identification_string run_hooks(pre_hooks, context, change) + if not context.event: + return try: - discord_message: Optional[DiscordMessage] = default_message(identification_string, formatter_hooks)(context, change) + discord_message: Optional[DiscordMessage] = default_message(context.event, formatter_hooks)(context, change) except: if settings.get("error_tolerance", 1) > 0: discord_message: Optional[DiscordMessage] = None # It's handled by send_to_discord, we still want other code to run else: raise - if identification_string in ("delete/delete", "delete/delete_redir") and AUTO_SUPPRESSION_ENABLED: # TODO Move it into a hook? + if context.event in ("delete/delete", "delete/delete_redir") and AUTO_SUPPRESSION_ENABLED: # TODO Move it into a hook? delete_messages(dict(pageid=change.get("pageid"))) - elif identification_string == "delete/event" and AUTO_SUPPRESSION_ENABLED: + elif context.event == "delete/event" and AUTO_SUPPRESSION_ENABLED: logparams = change.get('logparams', {"ids": []}) if settings["appearance"]["mode"] == "embed": redact_messages(logparams.get("ids", []), 1, logparams.get("new", {})) else: for logid in logparams.get("ids", []): delete_messages(dict(logid=logid)) - elif identification_string == "delete/revision" and AUTO_SUPPRESSION_ENABLED: + elif context.event == "delete/revision" and AUTO_SUPPRESSION_ENABLED: logparams = change.get('logparams', {"ids": []}) if logparams.get("type", "") in ("revision", "logging", "oldimage"): if settings["appearance"]["mode"] == "embed": @@ -282,8 +286,10 @@ def abuselog_processing(entry): context = Context(settings["appearance"]["mode"], "abuselog", settings.get("abuselog_webhookURL", settings["webhookURL"]), client, formatters_i18n, settings) context.event = action run_hooks(pre_hooks, context, entry) + if not context.event: + return try: - discord_message: Optional[DiscordMessage] = default_message(action, formatter_hooks)(context, entry) + discord_message: Optional[DiscordMessage] = default_message(context.event, formatter_hooks)(context, entry) except NoFormatter: return except: