Added mention filter

This commit is contained in:
Casey 2022-08-27 16:16:36 +03:00
parent 4d7a9be45f
commit d19a3d2005
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
5 changed files with 34 additions and 1 deletions

View File

@ -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

View File

@ -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
)
)

View File

@ -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