Skip to content

Commit

Permalink
default filter for previous messages
Browse files Browse the repository at this point in the history
  • Loading branch information
da-maltsev committed Jul 5, 2023
1 parent 80c886c commit fb93f35
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[settings]
line_length = 119
line_length = 160
skip=migrations/*.py, node_modules, venv
known_standard_library=typing
multi_line_output=4
4 changes: 1 addition & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

import rekognition
import text
from filters import (
ContainsLink, ContainsTelegramContact, ContainsThreeOrMoreEmojies, IsMedia, IsMessageOnBehalfOfChat,
with_default_filters)
from filters import ContainsLink, ContainsTelegramContact, ContainsThreeOrMoreEmojies, IsMedia, IsMessageOnBehalfOfChat, with_default_filters
from helpers import DB_ENABLED, enable_logging, in_production, init_sentry


Expand Down
30 changes: 19 additions & 11 deletions filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@
import text
from helpers import DB_ENABLED

MIN_PREVIOUS_MESSAGES_COUNT = 3

class HasValidPreviousMessages(MessageFilter):
MIN_PREVIOUS_MESSAGES_COUNT = 3

def is_fake_user(user_id: int) -> bool:
from models import LogEntry
def filter(self, message: Message) -> bool:
if not DB_ENABLED():
return False

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:
from models import LogEntry

messages_count = LogEntry.select().where((LogEntry.user_id == user_id) & (LogEntry.action != 'delete')).count()
return messages_count < MIN_PREVIOUS_MESSAGES_COUNT
messages_count = 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 @@ -25,6 +37,7 @@ def with_default_filters(*filters: BaseFilter) -> BaseFilter:
"""Apply default filters to the given filter classes"""
default_filters = [
ChatMessageOnly(),
HasValidPreviousMessages(),
]
return reduce(operator.and_, [*default_filters, *filters]) # МАМА Я УМЕЮ ФУНКЦИОНАЛЬНО ПРОГРАММИРОВАТЬ

Expand All @@ -49,12 +62,7 @@ def filter(self, message: Message) -> bool:
return False # type: ignore

entities_types = set([entity.type for entity in message.entities])
has_links = len(entities_types.intersection({'url', 'text_link'})) != 0

if DB_ENABLED() and has_links:
return is_fake_user(message.from_user.id)

return has_links
return len(entities_types.intersection({'url', 'text_link'})) != 0


class ContainsThreeOrMoreEmojies(MessageFilter):
Expand Down

0 comments on commit fb93f35

Please sign in to comment.