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
- `webhook_url` - string - webhook url for given formatter
- `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
- `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
### Util
@ -148,4 +149,4 @@ RcGcDw implements i18n with gettext and already exposes Translations instance wi
**Path**: `src.api.hook`
There are two decorator functions available in the module: `pre_hook` and `post_hook`. They don't take arguments and simply register the function as a hook.
Pre-hook functions take the following arguments: `context` ([Context object](#Context)) and `change` (dict object with change).
Post-hook functions take the following arguments: `message` ([Discord message object](#DiscordMessage)), `metadata` ([Discord message metadata](#DiscordMessageMetadata)), `context` ([Context object](#Context)) and `change` (dictionary of main change body)
Post-hook functions take the following arguments: `message` ([Discord message object](#DiscordMessage)), `metadata` ([Discord message metadata](#DiscordMessageMetadata)), `context` ([Context object](#Context)) and `change` (dictionary of main change body)

View file

@ -22,10 +22,11 @@ if TYPE_CHECKING:
class Context:
"""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"""
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.webhook_url = webhook_url
self.message_type = message_type
self.feed_type = feed_type
self.categories = None
self.parsedcomment = None
self.event = None
@ -41,4 +42,4 @@ class Context:
self.comment_page = page
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"""
global client
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
if post_type == "FORUM" and settings["fandom_discussions"].get("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),
page_id=change.get("pageid", None))
logger.debug(change)
context = Context(settings["appearance"]["mode"], settings["webhookURL"], client)
run_hooks(pre_hooks, context, change)
context = Context(settings["appearance"]["mode"], "recentchanges", settings["webhookURL"], client)
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)
try:
discord_message: Optional[DiscordMessage] = default_message("suppressed", formatter_hooks)(context, change)
except NoFormatter:
@ -242,6 +242,7 @@ def rc_processor(change, changed_categories):
if identification_string in settings["ignored"]:
return
context.event = identification_string
run_hooks(pre_hooks, context, change)
try:
discord_message: Optional[DiscordMessage] = default_message(identification_string, formatter_hooks)(context, change)
except:
@ -278,9 +279,9 @@ def abuselog_processing(entry):
action = "abuselog"
if action in settings["ignored"]:
return
context = Context(settings["appearance"]["mode"], settings["webhookURL"], client)
run_hooks(pre_hooks, context, entry)
context = Context(settings["appearance"]["mode"], "abuselog", settings["webhookURL"], client)
context.event = action
run_hooks(pre_hooks, context, entry)
try:
discord_message: Optional[DiscordMessage] = default_message(action, formatter_hooks)(context, entry)
except NoFormatter: