From b4ee5f77b81a27f300de8caa37b782df1a90dbc3 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Mon, 13 May 2024 19:46:55 +0300 Subject: [PATCH] little refactoring --- .../fsmwizard/add_chat_member_state.go | 149 ++++++++++-------- internal/handler/fsmwizard/base.go | 9 ++ internal/handler/fsmwizard/init.go | 6 +- .../fsmwizard/remove_chat_member_state.go | 3 +- 4 files changed, 94 insertions(+), 73 deletions(-) diff --git a/internal/handler/fsmwizard/add_chat_member_state.go b/internal/handler/fsmwizard/add_chat_member_state.go index 1886370..96672a7 100644 --- a/internal/handler/fsmwizard/add_chat_member_state.go +++ b/internal/handler/fsmwizard/add_chat_member_state.go @@ -34,22 +34,88 @@ func (s *AddChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) defer func() { s.Move(mc, next, pl) }() + cr := s.App.DB().ForChat() tgChat := pl.Data.MyChatMember.Chat + user, chat := s.getUserAndChat(tgChat) + if user == nil || chat == nil { + return + } + + s.Payload.Loc = s.Localizer(user.Language) + if s.handleTooManyMembers(tgChat.ID) { + return + } + + if chat.ID == 0 { + chat = &model.Chat{ + TelegramID: tgChat.ID, + UserID: user.ID, + } + err := cr.Save(chat) + if err != nil { + s.curseAndLeave(tgChat.ID, err) + return + } + } else { + chat.UserID = user.ID + err := s.App.DB().ForIntegration().DeleteForChat(chat.ID) + if err != nil { + s.curseAndLeave(tgChat.ID, err) + return + } + err = cr.Save(chat) + if err != nil { + s.curseAndLeave(tgChat.ID, err) + return + } + } + + _, err := s.App.TG().SendMessage(&telego.SendMessageParams{ + ChatID: tu.ID(user.ChatID), + Text: s.Payload.Loc.Template("bot_was_added", map[string]interface{}{"Name": tgChat.Title}), + ParseMode: telego.ModeMarkdown, + }) + s.LogError(err) + pl.User = user + pl.TGChat = tgChat + next = KeyboardChooserStateID +} + +func (s *AddChatMemberState) handleTooManyMembers(chatID int64) bool { + totalMembers, err := s.App.TG().GetChatMemberCount(&telego.GetChatMemberCountParams{ + ChatID: tu.ID(chatID), + }) + if err != nil { + s.curseAndLeave(chatID, err) + return true + } + if *totalMembers > MaxChatMembers { + _, err = s.App.TG().SendMessage(&telego.SendMessageParams{ + ChatID: tu.ID(chatID), + Text: s.Payload.Loc.Template( + "too_many_members_in_the_group", map[string]interface{}{"Limit": MaxChatMembers}), + ParseMode: telego.ModeMarkdown, + }) + s.leaveChat(chatID) + s.LogError(err) + return true + } + return false +} + +func (s *AddChatMemberState) getUserAndChat(tgChat telego.Chat) (*model.User, *model.Chat) { + cr := s.App.DB().ForChat() chat, err := cr.ByTelegramID(tgChat.ID) if err != nil { - s.LogError(util.SendInternalError(s.App.TG(), tgChat.ID, nil)) - s.leaveChat(tgChat.ID) - s.LogError(err) - return + s.curseAndLeave(tgChat.ID, err) + return nil, nil } user, err := s.getRegisteredAdmin(tgChat.ID) if err != nil { - s.LogError(util.SendInternalError(s.App.TG(), tgChat.ID, nil)) - s.leaveChat(tgChat.ID) - s.LogError(err) - return + s.curseAndLeave(tgChat.ID, err) + return nil, nil } if user == nil || user.ID == 0 { _, err = s.App.TG().SendMessage(&telego.SendMessageParams{ @@ -60,70 +126,15 @@ func (s *AddChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) }) s.leaveChat(tgChat.ID) s.LogError(err) - return + return nil, nil } + return user, chat +} - loc := s.Localizer(user.Language) - totalMembers, err := s.App.TG().GetChatMemberCount(&telego.GetChatMemberCountParams{ - ChatID: tu.ID(tgChat.ID), - }) - if err != nil { - s.LogError(util.SendInternalError(s.App.TG(), tgChat.ID, nil)) - s.leaveChat(tgChat.ID) - s.LogError(err) - return - } - if *totalMembers > MaxChatMembers { - _, err = s.App.TG().SendMessage(&telego.SendMessageParams{ - ChatID: tu.ID(tgChat.ID), - Text: loc.Template("too_many_members_in_the_group", map[string]interface{}{"Limit": MaxChatMembers}), - ParseMode: telego.ModeMarkdown, - }) - s.leaveChat(tgChat.ID) - s.LogError(err) - return - } - - if chat == nil || chat.ID == 0 { - chat = &model.Chat{ - TelegramID: tgChat.ID, - UserID: user.ID, - } - err := cr.Save(chat) - if err != nil { - s.LogError(util.SendInternalError(s.App.TG(), tgChat.ID, loc)) - s.leaveChat(tgChat.ID) - s.LogError(err) - return - } - } else { - chat.UserID = user.ID - err := s.App.DB().ForIntegration().DeleteForChat(chat.ID) - if err != nil { - s.LogError(util.SendInternalError(s.App.TG(), tgChat.ID, loc)) - s.leaveChat(tgChat.ID) - s.LogError(err) - return - } - err = cr.Save(chat) - if err != nil { - s.LogError(util.SendInternalError(s.App.TG(), tgChat.ID, loc)) - s.leaveChat(tgChat.ID) - s.LogError(err) - return - } - } - - _, err = s.App.TG().SendMessage(&telego.SendMessageParams{ - ChatID: tu.ID(user.ChatID), - Text: loc.Template("bot_was_added", map[string]interface{}{"Name": tgChat.Title}), - ParseMode: telego.ModeMarkdown, - }) +func (s *AddChatMemberState) curseAndLeave(chatID int64, err error) { + s.LogError(util.SendInternalError(s.App.TG(), chatID, s.Localizer())) + s.leaveChat(chatID) s.LogError(err) - pl.User = user - pl.TGChat = tgChat - pl.Loc = loc - next = KeyboardChooserStateID } func (s *AddChatMemberState) getRegisteredAdmin(chatID int64) (*model.User, error) { diff --git a/internal/handler/fsmwizard/base.go b/internal/handler/fsmwizard/base.go index 1dac845..1fea61e 100644 --- a/internal/handler/fsmwizard/base.go +++ b/internal/handler/fsmwizard/base.go @@ -32,6 +32,15 @@ func (s State) Localizer(langCode ...string) locale.Localizer { lang := language.English.String() if len(langCode) > 0 { lang = langCode[0] + } else { + if s.Payload.User != nil { + lang = s.Payload.User.Language + } else if s.Payload.UserID > 0 { + user, _ := s.App.DB().ForUser().ByTelegramID(s.Payload.UserID) + if user != nil && user.Language != "" { + lang = user.Language + } + } } if s.Payload.Loc != nil && (len(langCode) == 0 || s.Payload.Loc.Tag().String() == lang) { return s.Payload.Loc diff --git a/internal/handler/fsmwizard/init.go b/internal/handler/fsmwizard/init.go index b64d44e..5f28edb 100644 --- a/internal/handler/fsmwizard/init.go +++ b/internal/handler/fsmwizard/init.go @@ -26,12 +26,12 @@ func init() { wizards = storage } -func Get(chatID int64) fsm.IMachine[Wizard] { - if machine, ok := wizards.Get(chatID); ok { +func Get(userID int64) fsm.IMachine[Wizard] { + if machine, ok := wizards.Get(userID); ok { return machine } machine := newWizard() - wizards.Set(chatID, machine) + wizards.Set(userID, machine) return machine } diff --git a/internal/handler/fsmwizard/remove_chat_member_state.go b/internal/handler/fsmwizard/remove_chat_member_state.go index f461a6b..91f9ece 100644 --- a/internal/handler/fsmwizard/remove_chat_member_state.go +++ b/internal/handler/fsmwizard/remove_chat_member_state.go @@ -19,7 +19,7 @@ func NewRemoveChatMemberState(app iface.App) fsm.IState[Wizard] { func (s *RemoveChatMemberState) Enter(pl *Wizard, mc fsm.MachineControls[*Wizard]) error { if pl.Data.MyChatMember == nil { - s.LogError(mc.Move(WaitingForMemberWebhookStateID, pl)) + s.Move(mc, WaitingForMemberWebhookStateID, pl) } return nil } @@ -44,6 +44,7 @@ func (s *RemoveChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizar } s.LogError(cr.Delete(chat)) } + s.Move(mc, WaitingForMemberWebhookStateID, pl) } func (s *RemoveChatMemberState) ID() fsm.StateID {