-
Notifications
You must be signed in to change notification settings - Fork 7
/
santa.py
262 lines (204 loc) · 7.96 KB
/
santa.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import datetime
from functools import wraps
from typing import Optional, Union
from telegram import User
import utilities
from config import config
NAME_MAX_LENGTH = 100
def update_time(func):
@wraps(func)
def wrapped(instance, *args, **kwargs):
result = func(*args, **kwargs)
instance.updated()
return result
return wrapped
class SecretSanta:
def __init__(
self,
origin_message_id: int,
user_id: int,
user_name: str,
chat_id: int,
chat_title: str,
santa_message_id: Optional[int] = None,
participants: Optional[dict] = None,
created_on: Optional[datetime.datetime] = None,
updated_on: Optional[datetime.datetime] = None,
started: bool = False,
started_on: Optional[datetime.datetime] = None,
):
now = utilities.now()
self._santa_dict = {
"origin_message_id": origin_message_id, # message received from the user in the group
"santa_message_id": santa_message_id, # message we send in the group
"participants": participants or {},
"created_on": created_on or now,
"updated_on": updated_on or now,
"user_id": user_id,
"user_name": user_name,
"chat_id": chat_id,
"chat_title": chat_title,
"started": started,
"started_on": started_on,
}
@classmethod
def from_dict(cls, santa_dict: dict):
return cls(
origin_message_id=santa_dict["origin_message_id"],
user_id=santa_dict["user_id"],
user_name=santa_dict["user_name"],
chat_id=santa_dict["chat_id"],
chat_title=santa_dict["chat_title"],
santa_message_id=santa_dict["santa_message_id"],
participants=santa_dict["participants"],
created_on=santa_dict["created_on"],
updated_on=santa_dict["updated_on"],
started=santa_dict["started"],
started_on=santa_dict.get("started_on", None),
)
def dict(self):
return self._santa_dict
@property
def creator_id(self):
return self._santa_dict["user_id"]
@property
def creator_name(self):
return self._santa_dict["user_name"]
@property
def creator_name_escaped(self):
return utilities.html_escape(self.creator_name)
@property
def chat_id(self):
return self._santa_dict["chat_id"]
@property
def chat_title(self):
return self._santa_dict["chat_title"]
@property
def chat_title_escaped(self):
return utilities.html_escape(self.chat_title)
@property
def origin_message_id(self):
return self._santa_dict["origin_message_id"]
@property
def santa_message_id(self):
return self._santa_dict["santa_message_id"]
@santa_message_id.setter
def santa_message_id(self, santa_message_id):
self._santa_dict["santa_message_id"] = santa_message_id
@property
def started(self):
return self._santa_dict["started"]
@started.setter
def started(self, new_value):
self._santa_dict["started"] = new_value
@property
def started_on(self):
return self._santa_dict["started_on"]
@started_on.setter
def started_on(self, new_value):
self._santa_dict["started_on"] = new_value
@property
def message_id(self):
return self.santa_message_id
@property
def id(self):
return self.santa_message_id
@property
def participants(self) -> dict:
return self._santa_dict["participants"]
@property
def updated_on(self):
return self._santa_dict["updated_on"]
@staticmethod
def user_id(user_id: Union[int, User]):
if isinstance(user_id, User):
return user_id.id
return user_id
@property
def created_on(self):
return self._santa_dict["created_on"]
def get_participants_count(self):
return len(self.participants)
def get_missing_count(self):
return config.santa.min_participants - self.get_participants_count()
# @update_time
def add(
self,
user: User,
match_message_id: Optional[int] = None,
join_message_id: Optional[int] = None,
) -> bool:
already_a_participant = user.id in self.participants
self._santa_dict["participants"][user.id] = {
"name": user.first_name[:NAME_MAX_LENGTH],
"match_message_id": match_message_id,
"last_join_message_id": join_message_id
}
return already_a_participant
# @update_time
def update_user_name(self, user: Union[User, str]):
name = user
if isinstance(user, User):
name = user.first_name
self._santa_dict["participants"][user.id]["name"] = name[:NAME_MAX_LENGTH]
def is_duplicate_name(self, name):
name_lower = name.lower()[:NAME_MAX_LENGTH]
for user_id, user_data in self.participants.items():
if user_data["name"].lower() == name_lower:
return name[:NAME_MAX_LENGTH] # we return the saved name (that is, shortened), for clarity
return False
# @update_time
def remove(self, user: Union[int, User]) -> bool:
user_id = self.user_id(user)
result = bool(self._santa_dict["participants"].pop(user_id, None))
return result
def updated(self):
self._santa_dict["updated_on"] = utilities.now()
def start(self):
self.started = True
self.started_on = utilities.now()
def is_participant(self, user: Union[int, User]) -> bool:
user_id = self.user_id(user)
return user_id in self.participants
def is_creator(self, user: Union[int, User]) -> bool:
user_id = self.user_id(user)
return self.creator_id == user_id
def get_user_match_message_id(self, user: Union[int, User]) -> int:
user_id = self.user_id(user)
# noinspection PyTypeChecker
return self._santa_dict["participants"][user_id]["match_message_id"]
def set_user_match_message_id(self, user: Union[int, User], message_id: int):
user_id = self.user_id(user)
self._santa_dict["participants"][user_id]["match_message_id"] = message_id
def get_user_join_message_id(self, user: Union[int, User]) -> int:
user_id = self.user_id(user)
# noinspection PyTypeChecker
return self._santa_dict["participants"][user_id]["last_join_message_id"]
def set_user_join_message_id(self, user: Union[int, User], message_id: int):
user_id = self.user_id(user)
self._santa_dict["participants"][user_id]["last_join_message_id"] = message_id
def get_user_name(self, user: Union[int, User]) -> str:
user_id = self.user_id(user)
# noinspection PyTypeChecker
return self._santa_dict["participants"][user_id]["name"]
def set_user_name(self, user: Union[int, User], name: str):
user_id = self.user_id(user)
self._santa_dict["participants"][user_id]["name"] = name
def user_mention_escaped(self, user: Union[int, User]) -> str:
user_id = self.user_id(user)
# noinspection PyTypeChecker
name = self._santa_dict["participants"][user_id]["name"]
return utilities.mention_escaped_by_id(user_id, name)
def link(self):
link = ""
if utilities.is_supergroup(self.chat_id):
link = utilities.message_link(self.chat_id, self.santa_message_id, force_private=True)
return link
def inline_link(self, text: str, escape=False):
if not utilities.is_supergroup(self.chat_id):
return text
link = self.link()
text = text if not escape else utilities.html_escape(text)
return f"<a href=\"{link}\">{text}</a>"
def __str__(self):
return f"{type(self).__name__}(id={self.origin_message_id}, participants={self.get_participants_count()}, updated_on={self.updated_on})"