From d19a3d2005f854b1a7d23fe58dd9d221f455abc4 Mon Sep 17 00:00:00 2001 From: hkc Date: Sat, 27 Aug 2022 16:16:36 +0300 Subject: [PATCH] Added mention filter --- mastoposter/filters/__init__.py | 4 ++- .../filters/{boost_filter.py => boost.py} | 0 .../{combined_filter.py => combined.py} | 0 mastoposter/filters/mention.py | 29 +++++++++++++++++++ mastoposter/types.py | 2 ++ 5 files changed, 34 insertions(+), 1 deletion(-) rename mastoposter/filters/{boost_filter.py => boost.py} (100%) rename mastoposter/filters/{combined_filter.py => combined.py} (100%) create mode 100644 mastoposter/filters/mention.py diff --git a/mastoposter/filters/__init__.py b/mastoposter/filters/__init__.py index dfb4c2b..3229002 100644 --- a/mastoposter/filters/__init__.py +++ b/mastoposter/filters/__init__.py @@ -1,2 +1,4 @@ from .base import BaseFilter # NOQA -from mastoposter.filters.boost_filter import BoostFilter # NOQA +from mastoposter.filters.boost import BoostFilter # NOQA +from mastoposter.filters.combined import CombinedFilter # NOQA +from mastoposter.filters.mention import MentionFilter # NOQA diff --git a/mastoposter/filters/boost_filter.py b/mastoposter/filters/boost.py similarity index 100% rename from mastoposter/filters/boost_filter.py rename to mastoposter/filters/boost.py diff --git a/mastoposter/filters/combined_filter.py b/mastoposter/filters/combined.py similarity index 100% rename from mastoposter/filters/combined_filter.py rename to mastoposter/filters/combined.py diff --git a/mastoposter/filters/mention.py b/mastoposter/filters/mention.py new file mode 100644 index 0000000..66c142e --- /dev/null +++ b/mastoposter/filters/mention.py @@ -0,0 +1,29 @@ +from configparser import SectionProxy +from re import Pattern, compile as regexp +from typing import ClassVar +from fnmatch import fnmatch +from mastoposter.filters.base import BaseFilter +from mastoposter.types import Status + + +class MentionFilter(BaseFilter, filter_name="mention"): + MENTION_REGEX: ClassVar[Pattern] = regexp(r"@([^@]+)(@([^@]+))?") + + def __init__(self, section: SectionProxy): + super().__init__(section) + self.list = section.get("list", "").split() + + @classmethod + def check_account(cls, acct: str, mask: str): + return fnmatch(acct, mask) + + def __call__(self, status: Status) -> bool: + return any( + ( + any( + self.check_account(mention.acct, mask) + for mask in self.list + ) + for mention in status.mentions + ) + ) diff --git a/mastoposter/types.py b/mastoposter/types.py index 8d16b12..7815deb 100644 --- a/mastoposter/types.py +++ b/mastoposter/types.py @@ -262,6 +262,7 @@ class Status: reblogs_count: int favourites_count: int replies_count: int + mentions: List[Mention] application: Optional[Application] = None url: Optional[str] = None in_reply_to_id: Optional[str] = None @@ -298,6 +299,7 @@ class Status: card=data.get("card"), language=data.get("language"), text=data.get("text"), + mentions=[Mention.from_dict(m) for m in data.get("mentions", [])], ) @property