mirror of
https://github.com/miloszowi/everyone-mention-telegram-bot.git
synced 2025-05-20 17:24:06 +00:00
56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
|
from typing import Iterable
|
||
|
|
||
|
from bot.message.inboundMessage import InboundMessage
|
||
|
from entity.chat import Chat
|
||
|
from entity.user import User
|
||
|
from exception.notFoundException import NotFoundException
|
||
|
from repository.abstractRepository import AbstractRepository
|
||
|
from repository.userRepository import UserRepository
|
||
|
|
||
|
|
||
|
class ChatRepository(AbstractRepository):
|
||
|
collection_name: str = 'chats'
|
||
|
user_repository: UserRepository
|
||
|
|
||
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
self.user_repository = UserRepository()
|
||
|
|
||
|
def provide(self, inbound: InboundMessage) -> Chat:
|
||
|
try:
|
||
|
chat = self.get(inbound.chat_id)
|
||
|
if not chat.groups.get(inbound.group_name):
|
||
|
chat.groups[inbound.group_name] = []
|
||
|
except NotFoundException:
|
||
|
chat = Chat.from_inbound_message(inbound)
|
||
|
|
||
|
return chat
|
||
|
|
||
|
def get(self, chat_id: str) -> Chat:
|
||
|
chat = self.database_client.find_one(
|
||
|
self.collection_name,
|
||
|
{
|
||
|
Chat.mongo_chat_id_index: chat_id
|
||
|
}
|
||
|
)
|
||
|
|
||
|
if not chat:
|
||
|
raise NotFoundException
|
||
|
|
||
|
return Chat.from_mongo_document(chat)
|
||
|
|
||
|
def get_users_for_group(self, chat_id: str, group: str) -> Iterable[User]:
|
||
|
chat = self.get(chat_id)
|
||
|
if not chat.groups.get(group):
|
||
|
raise NotFoundException
|
||
|
|
||
|
return [self.user_repository.get(user_id) for user_id in chat.groups.get(group)]
|
||
|
|
||
|
def save(self, chat: Chat) -> None:
|
||
|
self.database_client.save(
|
||
|
self.collection_name,
|
||
|
{Chat.mongo_chat_id_index: chat.chat_id},
|
||
|
chat.to_mongo_document()
|
||
|
)
|
||
|
|