Skip to content

Commit

Permalink
refactor filter to use one complex check is_from_oldfag
Browse files Browse the repository at this point in the history
  • Loading branch information
da-maltsev committed Jul 18, 2023
1 parent 5ef0f49 commit d06ea48
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
24 changes: 14 additions & 10 deletions filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@


class IsNewfag(MessageFilter):
MIN_PREVIOUS_MESSAGES_COUNT = 3
OLDFAG_ID_BORDER = 10**9

def filter(self, message: Message) -> bool:
if not DB_ENABLED() or message.from_user is None:
return True
return self.is_not_true_oldfag(message.from_user.id) and self.has_no_valid_previous_messages(user_id=message.from_user.id, chat_id=message.chat_id)

@classmethod
def is_not_true_oldfag(cls, user_id: int) -> bool:
return int(user_id) >= cls.OLDFAG_ID_BORDER
return not self.message_is_from_oldfag(message)

@classmethod
def has_no_valid_previous_messages(cls, user_id: int, chat_id: int) -> bool:
def message_is_from_oldfag(self, message: Message) -> bool:
if int(message.from_user.id) < 10**9: # type: ignore
return True

messages_count = self.messages_count(user_id=message.from_user.id, chat_id=message.chat_id) # type: ignore
if messages_count >= 3:
return True

return False

@staticmethod
def messages_count(user_id: int, chat_id: int) -> int:
from models import LogEntry

messages_count = LogEntry.select().where(
return LogEntry.select().where(
(LogEntry.user_id == user_id),
(LogEntry.chat_id == chat_id),
(LogEntry.action != 'delete'),
).count()
return messages_count < cls.MIN_PREVIOUS_MESSAGES_COUNT


class ChatMessageOnly(MessageFilter):
Expand Down
5 changes: 2 additions & 3 deletions tests/tests_filters/test_is_newfag.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def user():
class FakeUser:
def __init__(self, id: int):
self.id = id
newfag_id = IsNewfag.OLDFAG_ID_BORDER
return FakeUser(newfag_id)
return FakeUser(10**9 + 1)


@pytest.fixture
Expand All @@ -43,7 +42,7 @@ def filter_obj():
@pytest.fixture
def valid_messages(user, filter_obj):
message_id = 1
for _ in range(filter_obj.MIN_PREVIOUS_MESSAGES_COUNT):
for _ in range(3):
create_log_message(user_id=user.id, message_id=message_id)
message_id += 1

Expand Down

0 comments on commit d06ea48

Please sign in to comment.