diff --git a/internal/app/app.go b/internal/app/app.go index f08c9d6..07095b6 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -4,8 +4,8 @@ import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/config" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler" + "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/fsmwizard" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" - "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/wizard" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/locale" "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/logger" "github.com/mymmrac/telego" @@ -114,7 +114,7 @@ func (a *App) initTelegram() error { } func (a *App) initHandlers() { - wizard.PopulateStates(a) + fsmwizard.PopulateStates(a) a.Handlers = map[handler.Type]handler.Handler{ handler.Noop: handler.NewNoopHandler(a.Logger, a.Config.Debug), handler.Message: handler.NewMessageHandler(a), diff --git a/internal/handler/wizard/add_chat_member_state.go b/internal/handler/fsmwizard/add_chat_member_state.go similarity index 91% rename from internal/handler/wizard/add_chat_member_state.go rename to internal/handler/fsmwizard/add_chat_member_state.go index 7110291..1886370 100644 --- a/internal/handler/wizard/add_chat_member_state.go +++ b/internal/handler/fsmwizard/add_chat_member_state.go @@ -1,4 +1,4 @@ -package wizard +package fsmwizard import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model" @@ -24,7 +24,7 @@ func NewAddChatMemberState(app iface.App) fsm.IState[Wizard] { func (s *AddChatMemberState) Enter(pl *Wizard, mc fsm.MachineControls[*Wizard]) error { if pl.Data.MyChatMember == nil { - _ = mc.Move(WaitingForMemberWebhookStateID, pl) + s.Move(mc, WaitingForMemberWebhookStateID, pl) } return nil } @@ -32,7 +32,7 @@ func (s *AddChatMemberState) Enter(pl *Wizard, mc fsm.MachineControls[*Wizard]) func (s *AddChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) { next := WaitingForMemberWebhookStateID defer func() { - _ = mc.Move(next, pl) + s.Move(mc, next, pl) }() cr := s.App.DB().ForChat() tgChat := pl.Data.MyChatMember.Chat @@ -91,7 +91,7 @@ func (s *AddChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) } err := cr.Save(chat) if err != nil { - _ = util.SendInternalError(s.App.TG(), tgChat.ID, loc) + s.LogError(util.SendInternalError(s.App.TG(), tgChat.ID, loc)) s.leaveChat(tgChat.ID) s.LogError(err) return @@ -100,14 +100,14 @@ func (s *AddChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) chat.UserID = user.ID err := s.App.DB().ForIntegration().DeleteForChat(chat.ID) if err != nil { - _ = util.SendInternalError(s.App.TG(), tgChat.ID, loc) + 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 { - _ = util.SendInternalError(s.App.TG(), tgChat.ID, loc) + s.LogError(util.SendInternalError(s.App.TG(), tgChat.ID, loc)) s.leaveChat(tgChat.ID) s.LogError(err) return @@ -148,9 +148,9 @@ func (s *AddChatMemberState) getRegisteredAdmin(chatID int64) (*model.User, erro } func (s *AddChatMemberState) leaveChat(chatID int64) { - _ = s.App.TG().LeaveChat(&telego.LeaveChatParams{ + s.LogError(s.App.TG().LeaveChat(&telego.LeaveChatParams{ ChatID: tu.ID(chatID), - }) + })) } func (s *AddChatMemberState) ID() fsm.StateID { diff --git a/internal/handler/wizard/base.go b/internal/handler/fsmwizard/base.go similarity index 90% rename from internal/handler/wizard/base.go rename to internal/handler/fsmwizard/base.go index 2002d6a..1dac845 100644 --- a/internal/handler/wizard/base.go +++ b/internal/handler/fsmwizard/base.go @@ -1,4 +1,4 @@ -package wizard +package fsmwizard import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model" @@ -58,3 +58,7 @@ func (s State) LogError(err error) { } s.App.Log().Errorf("handler error: %s, user id: %d, chat id: %d", err, s.Payload.UserID, s.Payload.ChatID) } + +func (s State) Move(mc fsm.MachineControls[*Wizard], stateID fsm.StateID, pl *Wizard) { + s.LogError(mc.Move(stateID, pl)) +} diff --git a/internal/handler/wizard/error_state.go b/internal/handler/fsmwizard/error_state.go similarity index 96% rename from internal/handler/wizard/error_state.go rename to internal/handler/fsmwizard/error_state.go index f69e529..63bd336 100644 --- a/internal/handler/wizard/error_state.go +++ b/internal/handler/fsmwizard/error_state.go @@ -1,4 +1,4 @@ -package wizard +package fsmwizard import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm" diff --git a/internal/handler/wizard/help_state.go b/internal/handler/fsmwizard/help_state.go similarity index 97% rename from internal/handler/wizard/help_state.go rename to internal/handler/fsmwizard/help_state.go index bf2770f..c6ebd2f 100644 --- a/internal/handler/wizard/help_state.go +++ b/internal/handler/fsmwizard/help_state.go @@ -1,4 +1,4 @@ -package wizard +package fsmwizard import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" diff --git a/internal/handler/wizard/init.go b/internal/handler/fsmwizard/init.go similarity index 93% rename from internal/handler/wizard/init.go rename to internal/handler/fsmwizard/init.go index 1dc1a35..b64d44e 100644 --- a/internal/handler/wizard/init.go +++ b/internal/handler/fsmwizard/init.go @@ -1,4 +1,4 @@ -package wizard +package fsmwizard import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" @@ -39,6 +39,7 @@ func newWizard() fsm.IMachine[Wizard] { return fsm.New[Wizard](states[0].ID(), Wizard{}, states, errorState) } +// PopulateStates will init all state handlers for future use. func PopulateStates(app iface.App) { states = []fsm.IState[Wizard]{ NewRegisterState(app), diff --git a/internal/handler/wizard/keyboard_chooser_state.go b/internal/handler/fsmwizard/keyboard_chooser_state.go similarity index 98% rename from internal/handler/wizard/keyboard_chooser_state.go rename to internal/handler/fsmwizard/keyboard_chooser_state.go index e20500d..3c87528 100644 --- a/internal/handler/wizard/keyboard_chooser_state.go +++ b/internal/handler/fsmwizard/keyboard_chooser_state.go @@ -1,4 +1,4 @@ -package wizard +package fsmwizard import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model" diff --git a/internal/handler/wizard/register_state.go b/internal/handler/fsmwizard/register_state.go similarity index 92% rename from internal/handler/wizard/register_state.go rename to internal/handler/fsmwizard/register_state.go index 6270e15..e36a58e 100644 --- a/internal/handler/wizard/register_state.go +++ b/internal/handler/fsmwizard/register_state.go @@ -1,4 +1,4 @@ -package wizard +package fsmwizard import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model" @@ -37,10 +37,10 @@ func (s *RegisterState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) { shouldUpdate = true } if shouldUpdate { - _ = userRepo.Save(user) + s.LogError(userRepo.Save(user)) } pl.User = user - _ = mc.Move(WaitingForMemberWebhookStateID, pl) + s.Move(mc, WaitingForMemberWebhookStateID, pl) return } @@ -59,7 +59,7 @@ func (s *RegisterState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) { s.LogError(err) } pl.User = user - _ = mc.Move(WaitingForMemberWebhookStateID, pl) + s.Move(mc, WaitingForMemberWebhookStateID, pl) } func (s *RegisterState) Exit(pl *Wizard) { diff --git a/internal/handler/fsmwizard/remove_chat_member_state.go b/internal/handler/fsmwizard/remove_chat_member_state.go new file mode 100644 index 0000000..f461a6b --- /dev/null +++ b/internal/handler/fsmwizard/remove_chat_member_state.go @@ -0,0 +1,51 @@ +package fsmwizard + +import ( + "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" + "gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm" + "github.com/mymmrac/telego" + tu "github.com/mymmrac/telego/telegoutil" +) + +const RemoveChatMemberStateID fsm.StateID = "remove_chat_member" + +type RemoveChatMemberState struct { + State +} + +func NewRemoveChatMemberState(app iface.App) fsm.IState[Wizard] { + return &RemoveChatMemberState{newBase(app)} +} + +func (s *RemoveChatMemberState) Enter(pl *Wizard, mc fsm.MachineControls[*Wizard]) error { + if pl.Data.MyChatMember == nil { + s.LogError(mc.Move(WaitingForMemberWebhookStateID, pl)) + } + return nil +} + +func (s *RemoveChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) { + cr := s.App.DB().ForChat() + chat, err := cr.ByTelegramID(pl.Data.MyChatMember.Chat.ID) + if err != nil { + s.LogError(err) + return + } + if chat != nil && chat.ID > 0 { + user, _ := s.App.DB().ForUser().ByID(chat.UserID) + if user != nil && user.ID > 0 && user.ChatID > 0 { + _, err := s.App.TG().SendMessage(&telego.SendMessageParams{ + ChatID: tu.ID(user.ChatID), + Text: s.Localizer(user.Language).Template( + "bot_was_removed_from_group", map[string]interface{}{"Name": pl.Data.MyChatMember.Chat.Title}), + ParseMode: telego.ModeMarkdown, + }) + s.LogError(err) + } + s.LogError(cr.Delete(chat)) + } +} + +func (s *RemoveChatMemberState) ID() fsm.StateID { + return RemoveChatMemberStateID +} diff --git a/internal/handler/wizard/waiting_for_member_webhook_state.go b/internal/handler/fsmwizard/waiting_for_member_webhook_state.go similarity index 92% rename from internal/handler/wizard/waiting_for_member_webhook_state.go rename to internal/handler/fsmwizard/waiting_for_member_webhook_state.go index 982aa3e..f151f70 100644 --- a/internal/handler/wizard/waiting_for_member_webhook_state.go +++ b/internal/handler/fsmwizard/waiting_for_member_webhook_state.go @@ -1,4 +1,4 @@ -package wizard +package fsmwizard import ( "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" @@ -21,12 +21,12 @@ func (s *WaitingForMemberWebhookState) Handle(pl *Wizard, mc fsm.MachineControls cm := pl.Data.MyChatMember if !cm.OldChatMember.MemberIsMember() && cm.OldChatMember.MemberUser().ID == s.App.TGProfile().ID && cm.NewChatMember.MemberIsMember() && cm.NewChatMember.MemberUser().ID == s.App.TGProfile().ID { - _ = mc.Move(AddChatMemberStateID, pl) + s.Move(mc, AddChatMemberStateID, pl) return } if cm.OldChatMember.MemberIsMember() && cm.OldChatMember.MemberUser().ID == s.App.TGProfile().ID && !cm.NewChatMember.MemberIsMember() && cm.NewChatMember.MemberUser().ID == s.App.TGProfile().ID { - _ = mc.Move(RemoveChatMemberStateID, pl) + s.Move(mc, RemoveChatMemberStateID, pl) return } diff --git a/internal/handler/wizard/remove_chat_member_state.go b/internal/handler/wizard/remove_chat_member_state.go deleted file mode 100644 index fab7cc7..0000000 --- a/internal/handler/wizard/remove_chat_member_state.go +++ /dev/null @@ -1,31 +0,0 @@ -package wizard - -import ( - "gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface" - "gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm" -) - -const RemoveChatMemberStateID fsm.StateID = "remove_chat_member" - -type RemoveChatMemberState struct { - State -} - -func NewRemoveChatMemberState(app iface.App) fsm.IState[Wizard] { - return &RemoveChatMemberState{newBase(app)} -} - -func (s *RemoveChatMemberState) Enter(pl *Wizard, mc fsm.MachineControls[*Wizard]) error { - if pl.Data.MyChatMember == nil { - _ = mc.Move(WaitingForMemberWebhookStateID, pl) - } - return nil -} - -func (s *RemoveChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) { - // todo: copy implementation from func (h *ChatMemberUpdatedHandler) handleRemoveFromChat(tgChat telego.Chat) error -} - -func (s *RemoveChatMemberState) ID() fsm.StateID { - return RemoveChatMemberStateID -}