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