Skip to content

Commit

Permalink
add filter for test links
Browse files Browse the repository at this point in the history
  • Loading branch information
da-maltsev committed Jul 5, 2023
1 parent 641d6ce commit 444a380
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
from functools import reduce
from telegram import Message
from telegram.ext import BaseFilter, MessageFilter
from urlextract import URLExtract

import text
from helpers import DB_ENABLED

MIN_PREVIOUS_MESSAGES_COUNT = 3


def is_fake_user(user_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


class ChatMessageOnly(MessageFilter):
Expand Down Expand Up @@ -34,14 +43,18 @@ def filter(self, message: Message) -> bool:


class ContainsLink(MessageFilter):
def __init__(self) -> None:
self.extractor = URLExtract()

def filter(self, message: Message) -> bool:
if message.text is None:
return False # type: ignore

return len(self.extractor.find_urls(message.text)) >= 1
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


class ContainsThreeOrMoreEmojies(MessageFilter):
Expand Down

0 comments on commit 444a380

Please sign in to comment.