Added mention filter
This commit is contained in:
parent
4d7a9be45f
commit
d19a3d2005
|
@ -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
|
||||
|
|
|
@ -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
|
||||
)
|
||||
)
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue