2021-10-08 15:25:47 +02:00
|
|
|
from telegram.ext.callbackcontext import CallbackContext
|
|
|
|
from telegram.ext.commandhandler import CommandHandler
|
|
|
|
from telegram.update import Update
|
|
|
|
|
2021-10-06 19:44:03 +02:00
|
|
|
from bot.handler.abstractHandler import AbstractHandler
|
2021-10-08 15:25:47 +02:00
|
|
|
from bot.message.replier import Replier
|
2021-10-06 19:44:03 +02:00
|
|
|
from config.contents import left, not_left
|
2021-10-14 20:24:58 +02:00
|
|
|
from exception.invalidActionException import InvalidActionException
|
2021-09-25 16:49:11 +02:00
|
|
|
from repository.userRepository import UserRepository
|
2021-10-14 20:24:58 +02:00
|
|
|
from repository.chatRepository import ChatRepository
|
2021-09-25 16:49:11 +02:00
|
|
|
|
|
|
|
|
2021-10-06 19:44:03 +02:00
|
|
|
class LeaveHandler(AbstractHandler):
|
2021-09-28 17:03:11 +02:00
|
|
|
bot_handler: CommandHandler
|
|
|
|
user_repository: UserRepository
|
2021-10-14 20:24:58 +02:00
|
|
|
chat_repository: ChatRepository
|
2021-10-08 15:25:47 +02:00
|
|
|
action: str = 'leave'
|
2021-09-25 16:49:11 +02:00
|
|
|
|
|
|
|
def __init__(self) -> None:
|
2021-10-11 17:20:39 +02:00
|
|
|
self.bot_handler = CommandHandler(self.action, self.wrap)
|
2021-09-28 17:03:11 +02:00
|
|
|
self.user_repository = UserRepository()
|
2021-10-14 20:24:58 +02:00
|
|
|
self.chat_repository = ChatRepository()
|
2021-09-25 16:49:11 +02:00
|
|
|
|
|
|
|
def handle(self, update: Update, context: CallbackContext) -> None:
|
2021-10-14 20:24:58 +02:00
|
|
|
user = self.user_repository.provide(self.inbound)
|
|
|
|
chat = self.chat_repository.provide(self.inbound)
|
|
|
|
group = chat.groups.get(self.inbound.group_name)
|
|
|
|
|
|
|
|
if user.user_id not in group:
|
|
|
|
raise InvalidActionException(Replier.interpolate(not_left, self.inbound))
|
|
|
|
|
|
|
|
group.remove(user.user_id)
|
|
|
|
if not group:
|
|
|
|
chat.groups.pop(self.inbound.group_name)
|
|
|
|
|
|
|
|
self.chat_repository.save(chat)
|
|
|
|
|
|
|
|
Replier.markdown(update, Replier.interpolate(left, self.inbound))
|