Skip to content

Commit

Permalink
refactor filter to IsNewfag
Browse files Browse the repository at this point in the history
  • Loading branch information
da-maltsev committed Jul 18, 2023
1 parent 54427b6 commit 5ef0f49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 8 additions & 3 deletions filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
from helpers import DB_ENABLED


class HasNoValidPreviousMessages(MessageFilter):
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.has_no_valid_previous_messages(user_id=message.from_user.id, chat_id=message.chat_id)
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

@classmethod
def has_no_valid_previous_messages(cls, user_id: int, chat_id: int) -> bool:
Expand All @@ -36,7 +41,7 @@ def with_default_filters(*filters: BaseFilter) -> BaseFilter:
"""Apply default filters to the given filter classes"""
default_filters = [
ChatMessageOnly(),
HasNoValidPreviousMessages(),
IsNewfag(),
]
return reduce(operator.and_, [*default_filters, *filters]) # МАМА Я УМЕЮ ФУНКЦИОНАЛЬНО ПРОГРАММИРОВАТЬ

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import random

from filters import HasNoValidPreviousMessages
from filters import IsNewfag
from models import LogEntry

CHAT_ID = 1
Expand All @@ -24,8 +24,8 @@ def user():
class FakeUser:
def __init__(self, id: int):
self.id = id

return FakeUser(4815162342)
newfag_id = IsNewfag.OLDFAG_ID_BORDER
return FakeUser(newfag_id)


@pytest.fixture
Expand All @@ -37,7 +37,7 @@ def message(mock_message, user):

@pytest.fixture(scope="session")
def filter_obj():
return HasNoValidPreviousMessages()
return IsNewfag()


@pytest.fixture
Expand Down Expand Up @@ -88,3 +88,9 @@ def test_true_if_user_has_not_enough_valid_messages(do_filter, message, valid_me

def test_false_if_has_valid_messages(do_filter, message, valid_messages):
assert do_filter(message) is False


def test_false_if_true_oldfag(do_filter, message):
message.from_user.id = 987654321 # still less than 10^9

assert do_filter(message) is False

0 comments on commit 5ef0f49

Please sign in to comment.