From 7917c79495887790b19d306fe34ee1b17ff7624f Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Sat, 5 Feb 2022 17:38:38 +1100 Subject: [PATCH] add back evaluate to newpost, close #4 pass buttons as an arg for notify, ref #3 add buttons to report notifications, close #2 --- components/evaluators.py | 4 +--- components/notifiers.py | 21 ++++++++++++--------- components/watchers.py | 27 +++++++++++++++++++++------ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/components/evaluators.py b/components/evaluators.py index 20e4fd7..d5a8619 100755 --- a/components/evaluators.py +++ b/components/evaluators.py @@ -25,8 +25,6 @@ class PostEvaluator(Evaluator): trigger_urls = [ *filter(self.url_blacklist_re.match, self._url_extractor.find_urls(text, only_unique=True)) ] if self.url_blacklist_re and text else [] - trigger_entries = [ - _format_match(entry) for entry in re.finditer(self.blacklist_re, text) - ] if self.blacklist_re and text else [] + trigger_entries = re.findall(self.blacklist_re, text) if self.blacklist_re and text else [] logging.debug(f'Evaluated text:{text}\ntrigger urls:{trigger_urls}\ntrigger entries:{trigger_entries}') return trigger_urls, trigger_entries diff --git a/components/notifiers.py b/components/notifiers.py index 96264b5..d1bf365 100644 --- a/components/notifiers.py +++ b/components/notifiers.py @@ -2,7 +2,6 @@ import subprocess from os import getcwd from abc import ABC, abstractmethod - class Notifier(ABC): @abstractmethod def notify(self, title, content, *args, **kwargs): @@ -12,14 +11,18 @@ class TermuxNotifier(Notifier): def notify(self, title, content, *args, **kwargs): args = ['termux-notification', '--title', title, '--content', content or 'No Message'] - if 'url' in kwargs: - args = args + ['--action', f'termux-open-url {kwargs["url"]}', - '--button1', 'Delete', - '--button1-action', f'python3 {getcwd()}/notification_button.py -b {kwargs["board"]} -p {kwargs["postId"]} -a delete', - '--button2', 'Delete+Ban', - '--button2-action', f'python3 {getcwd()}/notification_button.py -b {kwargs["board"]} -p {kwargs["postId"]} -a delete,ban', - '--button3', 'Delete+Global Ban', - '--button3-action', f'python3 {getcwd()}/notification_button.py -b {kwargs["board"]} -p {kwargs["postId"]} -a delete,global_ban'] + + #open link when clicking on notification + if 'link' in kwargs: + args = args + ['--action', f'termux-open-url {kwargs["link"]}'] + + #add buttons to the notification + if 'buttons' in kwargs: + post = kwargs["post"] + for i, button in enumerate(kwargs["buttons"], start=1): + args = args + [f'--button{i}', button["text"], + f'--button{i}-action', f'python3 {getcwd()}/notification_button.py -b {post["board"]} -p {post["postId"]} -a {button["actions"]}'] + subprocess.call(args) class NotifySendNotifier(Notifier): diff --git a/components/watchers.py b/components/watchers.py index 38756be..a5cde87 100644 --- a/components/watchers.py +++ b/components/watchers.py @@ -5,7 +5,9 @@ from threading import Thread, Event import socketio from requests import RequestException -def get_path(post): return f'>>>/{post["board"]}/{post["thread"] or post["postId"]} ({post["postId"]})' +def get_quote(post): return f'>>>/{post["board"]}/{post["thread"] or post["postId"]} ({post["postId"]})' + +def get_manage_path(post): return f'/{post["board"]}/manage/thread/{post["thread"] or post["postId"]}.html#{post["postId"]}' class Watcher(ABC, Thread): def __init__(self, session): @@ -40,8 +42,15 @@ class RecentWatcher(Watcher): @client.on('newPost') def on_new_post(post): - post_url=f'{session.imageboard_url}/{post["board"]}/manage/thread/{post["thread"] or post["postId"]}.html#{post["postId"]}' - notify(f'Alert! {get_path(post)}\n', post['nomarkup'], url=post_url, board=post["board"], postId=post["postId"]) + urls, entries = evaluate(post["nomarkup"]) + if urls or entries: + post_url=f'{session.imageboard_url}{get_manage_path(post)}' + buttons=[{"text":"Delete","actions":"delete"}, + {"text":"Delete+Ban" if board else "Delete+Global Ban","actions":"delete,ban" if board else "delete,global_ban"}] + #todo: add this last button even if a board recents, because global staff can still global ban. but need a way to + #check if the account is global staff, which we dont have a json endpoint for in jschan yet. + #{"text":"Delete+Global Ban","actions":"dismiss" if board else "global_dismiss"}] + notify(f'Alert! {get_quote(post)}\n', post['nomarkup'], link=post_url, post=post, buttons=buttons) self.client = client self.start() @@ -61,6 +70,7 @@ class ReportsWatcher(Watcher): self.notify = notify self.fetch_interval = fetch_interval + self.board = board self._endpoint = f'{session.imageboard_url}/{f"{board}/manage" if board else "globalmanage"}/reports.json' self.known_reports = 0 @@ -75,9 +85,14 @@ class ReportsWatcher(Watcher): try: reported_posts, num_reported_posts = self.fetch_reports() if 0 < num_reported_posts != self.known_reports: - self.notify(f'New reports!', "\n".join([ - f'{get_path(p)} {[r["reason"] for r in (p["globalreports"] if "globalreports" in p else p["reports"])]}' - for p in reported_posts])) + for p in reported_posts: + post_url=f'{self.session.imageboard_url}{get_manage_path(p)}' + #todo: allow to customise these buttons somewhere + buttons=[{"text":"Delete","actions":"delete"}, + {"text":"Delete+Ban" if self.board else "Delete+Global Ban","actions":"delete,ban" if self.board else "delete,global_ban"}, + {"text":"Dismiss","actions":"dismiss" if self.board else "global_dismiss"}] + self.notify(f'New reports!', "\n".join([f'{get_quote(p)} {[r["reason"] for r in (p["globalreports"] if "globalreports" in p else p["reports"])]}']), + link=post_url, post=p, buttons=buttons) self.known_reports = num_reported_posts