move fsm wizard to its own package, little refactor
Some checks are pending
continuous-integration/drone/push Build is running
Some checks are pending
continuous-integration/drone/push Build is running
This commit is contained in:
parent
68f2975201
commit
1a28ab584a
@ -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),
|
||||
|
@ -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 {
|
@ -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))
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package wizard
|
||||
package fsmwizard
|
||||
|
||||
import (
|
||||
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm"
|
@ -1,4 +1,4 @@
|
||||
package wizard
|
||||
package fsmwizard
|
||||
|
||||
import (
|
||||
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
|
@ -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),
|
@ -1,4 +1,4 @@
|
||||
package wizard
|
||||
package fsmwizard
|
||||
|
||||
import (
|
||||
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
|
@ -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) {
|
51
internal/handler/fsmwizard/remove_chat_member_state.go
Normal file
51
internal/handler/fsmwizard/remove_chat_member_state.go
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user