Various filter fixes

This commit is contained in:
Casey 2022-08-29 17:17:57 +03:00
parent 8b7c381870
commit 0274bbe186
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
5 changed files with 8 additions and 7 deletions

View File

@ -1,7 +1,7 @@
from typing import List
from mastoposter.types import Status
from .base import FilterInstance # NOQA
from .base import FilterInstance
from mastoposter.filters.boost import BoostFilter # NOQA
from mastoposter.filters.combined import CombinedFilter # NOQA
from mastoposter.filters.mention import MentionFilter # NOQA

View File

@ -10,7 +10,7 @@ class BoostFilter(BaseFilter, filter_name="boost"):
self.list = section.get("list", "").split()
@classmethod
def check_account(cls, acct: str, mask: str):
def check_account(cls, acct: str, mask: str) -> bool:
return fnmatch(acct, mask)
def __call__(self, status: Status) -> bool:
@ -20,7 +20,7 @@ class BoostFilter(BaseFilter, filter_name="boost"):
return True
return any(
[
self.check_account(status.reblog.account.acct, mask)
self.check_account("@" + status.reblog.account.acct, mask)
for mask in self.list
]
)

View File

@ -14,8 +14,8 @@ class MentionFilter(BaseFilter, filter_name="mention"):
self.list = section.get("list", "").split()
@classmethod
def check_account(cls, acct: str, mask: str):
return fnmatch(acct, mask)
def check_account(cls, acct: str, mask: str) -> bool:
return fnmatch("@" + acct, mask)
def __call__(self, status: Status) -> bool:
if not self.list and status.mentions:

View File

@ -7,7 +7,7 @@ from mastoposter.types import Status
class SpoilerFilter(BaseFilter, filter_name="spoiler"):
def __init__(self, section: SectionProxy):
super().__init__(section)
self.regexp: Pattern = regexp(section["regexp"])
self.regexp: Pattern = regexp(section.get("regexp", "^.*$"))
def __call__(self, status: Status) -> bool:
return self.regexp.match(status.spoiler_text) is not None

View File

@ -16,7 +16,7 @@ class TextFilter(BaseFilter, filter_name="content"):
if self.mode == "regexp":
self.regexp = regexp(section["regexp"])
elif self.mode == "hashtag":
elif self.mode in ("hashtag", "tag"):
self.tags = set(section["tags"].split())
else:
raise ValueError(f"Invalid filter mode {self.mode}")
@ -46,6 +46,7 @@ class TextFilter(BaseFilter, filter_name="content"):
is not None
)
elif self.tags:
print(f"{self.tags=} {source.tags=}")
return len(self.tags & {t.name for t in source.tags}) > 0
else:
raise ValueError("Neither regexp or tags were set. Why?")