Random Coffee alternative - random meetings for Telegram chats
https://t.me/ranteabot
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.6 KiB
53 lines
1.6 KiB
from __future__ import annotations |
|
from dataclasses import dataclass, field, asdict |
|
import typing |
|
import time |
|
|
|
if typing.TYPE_CHECKING: |
|
from bot import Bot |
|
from community import Community |
|
|
|
|
|
@dataclass |
|
class User: |
|
user_id: int |
|
chat_id: int = 0 |
|
locale: str = '' |
|
start_timestamp: int = field(default_factory=lambda: int(time.time())) |
|
last_action_timestamp: int = field(default_factory=lambda: int(time.time())) |
|
|
|
def dict(self) -> dict: |
|
data = asdict(self) |
|
data['last_action_timestamp'] = int(time.time()) |
|
return data |
|
|
|
@classmethod |
|
def from_dict(cls, data: dict) -> User: |
|
data = getattr(data, '__dict__', data) |
|
data_ = {key: data[key] for key in data.keys() if |
|
key in ['user_id', 'chat_id', 'locale', 'start_timestamp', 'last_action_timestamp']} |
|
self = cls(**data_) |
|
return self |
|
|
|
@classmethod |
|
def by_id(cls, user_id: int, bot) -> typing.Optional[User]: |
|
data = bot.get_user_from_db({'user_id': user_id}) |
|
if data is None: |
|
return None |
|
return data |
|
|
|
@property |
|
def id(self): |
|
return self._id |
|
|
|
def is_admin(self) -> bool: |
|
return self.user_id in [218952152] |
|
|
|
def get_communities(self, bot: Bot) -> typing.Iterable[Community]: |
|
""" |
|
Returns a list of community ids for which the user is a member |
|
|
|
Perform a MongoDB query to get the list of communities for which the user is a member |
|
""" |
|
for community in bot.db.communities.find({'members': self.user_id}): |
|
yield Community.from_dict(community)
|
|
|