-
Notifications
You must be signed in to change notification settings - Fork 1
/
deletion_logic.py
95 lines (74 loc) · 3.16 KB
/
deletion_logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import asyncio
from telethon.sync import TelegramClient, events
from conf import *
from matchers_driver import MatchersDriver
class DeletionLogic:
def __init__(self, db: MatchersDriver):
self.db = db
self.last_event = ''
self.last_rule = ''
async def _delete_event(self, client: TelegramClient, event: events.NewMessage):
await asyncio.sleep(RETENTION_PERIOD)
await client.delete_messages(entity=event.message.chat_id, message_ids=event.message)
async def _delete_answer(self, client: TelegramClient, event: events.NewMessage):
current_nick = await event.get_sender()
current_nick = "@"+current_nick.username
last_rule_nick = self.last_rule.split(" ")[1]
if current_nick == last_rule_nick:
await self._delete_event(client, self.last_event)
await self._delete_event(client, event)
self.last_event = ''
self.last_rule = ''
return True
return False
async def _delete_squash(self, client: TelegramClient, event: events.NewMessage):
if event.message.sticker is not None:
matcher = await self.db.get_matcher_row(matcher="stickers")
if matcher != [] and self.last_event != '' and self.last_event.message.sticker is not None and self.last_rule == "squash":
if event.message.sticker.id == self.last_event.message.sticker.id:
await self._delete_event(client, event)
return True
elif event.message.message is not None:
matcher = await self.db.get_matcher_row(matcher=event.message.message)
if matcher != [] and self.last_event != '' and self.last_event.message.message is not None and self.last_rule == "squash":
if event.message.message == self.last_event.message.message:
await self._delete_event(client, event)
return True
return False
async def _process_rule(self, event: events.NewMessage, rule: str):
if 'answer' in rule:
self.last_event = event
self.last_rule = rule
elif 'squash' in rule:
self.last_event = event
self.last_rule = rule
async def _process_match(self, client: TelegramClient, event: events.NewMessage):
if await self._delete_squash(client, event):
return None
else:
if event.message.sticker is not None:
matcher = await self.db.get_matcher_row(matcher="stickers")
else:
matcher = await self.db.get_matcher_row(matcher=event.message.message)
if "answer" in self.last_rule and await self._delete_answer(client, event):
return None
current_nick = await event.get_sender()
current_nick = "@"+current_nick.username
all_rule = await self.db.get_all_rules(nick=current_nick)
if len(all_rule) != 0:
await self._delete_event(client, event)
if len(matcher) == 0:
self.last_event = ''
self.last_rule = ''
return None
rule = matcher[1]
matcher = matcher[0]
if rule is not None:
await self._process_rule(event, rule)
else:
await self._delete_event(client, event)
self.last_event = ''
self.last_rule = ''
async def add_to_queue(self, client: TelegramClient, event: events.NewMessage):
await self._process_match(client, event)
await asyncio.sleep(1)