Add feed_type to Context, pre-hooks full context

This commit is contained in:
MarkusRost 2022-06-14 06:53:42 +00:00
parent b19706fcfd
commit ab60fd3728
4 changed files with 12 additions and 9 deletions

View file

@ -112,9 +112,10 @@ Context can consist of the following fields:
- `client` - [Client](#Client) object - `client` - [Client](#Client) object
- `webhook_url` - string - webhook url for given formatter - `webhook_url` - string - webhook url for given formatter
- `message_type` - string - can be either `embed` or `compact` - `message_type` - string - can be either `embed` or `compact`
- `feed_type` - string - type of the feed, can be either `recentchanges`, `abuselog` or `discussion`
- `event` - string - action called, should be the same as formatter event action
- `categories` - {"new": set(), "removed": set()} - each set containing strings of added or removed categories for given page - `categories` - {"new": set(), "removed": set()} - each set containing strings of added or removed categories for given page
- `parsedcomment` - string - contains escaped and Markdown parsed summary (parsed_comment) of a log/edit action - `parsedcomment` - string - contains escaped and Markdown parsed summary (parsed_comment) of a log/edit action
- `event` - string - action called, should be the same as formatter event action
- `comment_page` - dict - containing `fullUrl` and `article` with strings both to full article url and its name - `comment_page` - dict - containing `fullUrl` and `article` with strings both to full article url and its name
### Util ### Util

View file

@ -22,10 +22,11 @@ if TYPE_CHECKING:
class Context: class Context:
"""Context object containing client and some metadata regarding specific formatter call, """Context object containing client and some metadata regarding specific formatter call,
they are mainly used as a bridge between part that fetches the changes and API's formatters""" they are mainly used as a bridge between part that fetches the changes and API's formatters"""
def __init__(self, message_type: str, webhook_url: str, client: Client): def __init__(self, message_type: str, feed_type: str, webhook_url: str, client: Client):
self.client = client self.client = client
self.webhook_url = webhook_url self.webhook_url = webhook_url
self.message_type = message_type self.message_type = message_type
self.feed_type = feed_type
self.categories = None self.categories = None
self.parsedcomment = None self.parsedcomment = None
self.event = None self.event = None
@ -41,4 +42,4 @@ class Context:
self.comment_page = page self.comment_page = page
def __str__(self): def __str__(self):
return f"<Context message_type={self.message_type} event={self.event} webhook_url={self.webhook_url}" return f"<Context message_type={self.message_type} feed_type={self.feed_type} event={self.event} webhook_url={self.webhook_url}"

View file

@ -107,7 +107,7 @@ def parse_discussion_post(post, comment_pages):
"""Initial post recognition & handling""" """Initial post recognition & handling"""
global client global client
post_type = post["_embedded"]["thread"][0]["containerType"] post_type = post["_embedded"]["thread"][0]["containerType"]
context = Context(display_mode, webhook_url, client) context = Context(display_mode, "discussion", webhook_url, client)
# Filter posts by forum # Filter posts by forum
if post_type == "FORUM" and settings["fandom_discussions"].get("show_forums", []): if post_type == "FORUM" and settings["fandom_discussions"].get("show_forums", []):
if not post["forumName"] in settings["fandom_discussions"]["show_forums"]: if not post["forumName"] in settings["fandom_discussions"]["show_forums"]:

View file

@ -202,10 +202,10 @@ def rc_processor(change, changed_categories):
metadata = DiscordMessageMetadata("POST", rev_id=change.get("revid", None), log_id=change.get("logid", None), metadata = DiscordMessageMetadata("POST", rev_id=change.get("revid", None), log_id=change.get("logid", None),
page_id=change.get("pageid", None)) page_id=change.get("pageid", None))
logger.debug(change) logger.debug(change)
context = Context(settings["appearance"]["mode"], settings["webhookURL"], client) context = Context(settings["appearance"]["mode"], "recentchanges", settings["webhookURL"], client)
run_hooks(pre_hooks, context, change)
if ("actionhidden" in change or "suppressed" in change) and "suppressed" not in settings["ignored"]: # if event is hidden using suppression if ("actionhidden" in change or "suppressed" in change) and "suppressed" not in settings["ignored"]: # if event is hidden using suppression
context.event = "suppressed" context.event = "suppressed"
run_hooks(pre_hooks, context, change)
try: try:
discord_message: Optional[DiscordMessage] = default_message("suppressed", formatter_hooks)(context, change) discord_message: Optional[DiscordMessage] = default_message("suppressed", formatter_hooks)(context, change)
except NoFormatter: except NoFormatter:
@ -242,6 +242,7 @@ def rc_processor(change, changed_categories):
if identification_string in settings["ignored"]: if identification_string in settings["ignored"]:
return return
context.event = identification_string context.event = identification_string
run_hooks(pre_hooks, context, change)
try: try:
discord_message: Optional[DiscordMessage] = default_message(identification_string, formatter_hooks)(context, change) discord_message: Optional[DiscordMessage] = default_message(identification_string, formatter_hooks)(context, change)
except: except:
@ -278,9 +279,9 @@ def abuselog_processing(entry):
action = "abuselog" action = "abuselog"
if action in settings["ignored"]: if action in settings["ignored"]:
return return
context = Context(settings["appearance"]["mode"], settings["webhookURL"], client) context = Context(settings["appearance"]["mode"], "abuselog", settings["webhookURL"], client)
run_hooks(pre_hooks, context, entry)
context.event = action context.event = action
run_hooks(pre_hooks, context, entry)
try: try:
discord_message: Optional[DiscordMessage] = default_message(action, formatter_hooks)(context, entry) discord_message: Optional[DiscordMessage] = default_message(action, formatter_hooks)(context, entry)
except NoFormatter: except NoFormatter: