Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To Issue #13 #14

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,33 @@
from helpers import DB_ENABLED


class HasNoValidPreviousMessages(MessageFilter):
MIN_PREVIOUS_MESSAGES_COUNT = 3
class IsNewfag(MessageFilter):

def filter(self, message: Message) -> bool:
if not DB_ENABLED() or message.from_user is None:
da-maltsev marked this conversation as resolved.
Show resolved Hide resolved
return True
return self.has_no_valid_previous_messages(user_id=message.from_user.id, chat_id=message.chat_id)

@classmethod
def has_no_valid_previous_messages(cls, user_id: int, chat_id: int) -> bool:
return not self.message_is_from_oldfag(message)

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 All @@ -36,7 +45,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,7 @@ def user():
class FakeUser:
def __init__(self, id: int):
self.id = id

return FakeUser(4815162342)
return FakeUser(10**9 + 1)


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

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


@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 Expand Up @@ -88,3 +87,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