Neur0toxine
68f2975201
All checks were successful
continuous-integration/drone/push Build is passing
159 lines
4.0 KiB
Go
159 lines
4.0 KiB
Go
package wizard
|
|
|
|
import (
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/db/model"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/iface"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/internal/handler/util"
|
|
"gitea.neur0tx.site/Neur0toxine/vegapokerbot/pkg/fsm"
|
|
"github.com/mymmrac/telego"
|
|
tu "github.com/mymmrac/telego/telegoutil"
|
|
"golang.org/x/text/language"
|
|
)
|
|
|
|
const MaxChatMembers = 32
|
|
|
|
const AddChatMemberStateID fsm.StateID = "add_chat_member"
|
|
|
|
type AddChatMemberState struct {
|
|
State
|
|
}
|
|
|
|
func NewAddChatMemberState(app iface.App) fsm.IState[Wizard] {
|
|
return &AddChatMemberState{newBase(app)}
|
|
}
|
|
|
|
func (s *AddChatMemberState) Enter(pl *Wizard, mc fsm.MachineControls[*Wizard]) error {
|
|
if pl.Data.MyChatMember == nil {
|
|
_ = mc.Move(WaitingForMemberWebhookStateID, pl)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *AddChatMemberState) Handle(pl *Wizard, mc fsm.MachineControls[*Wizard]) {
|
|
next := WaitingForMemberWebhookStateID
|
|
defer func() {
|
|
_ = mc.Move(next, pl)
|
|
}()
|
|
cr := s.App.DB().ForChat()
|
|
tgChat := pl.Data.MyChatMember.Chat
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
if user == nil || user.ID == 0 {
|
|
_, err = s.App.TG().SendMessage(&telego.SendMessageParams{
|
|
ChatID: tu.ID(tgChat.ID),
|
|
Text: s.Localizer(language.English.String()).
|
|
Template("you_should_register_first", map[string]interface{}{"Name": s.App.TGProfile().Username}),
|
|
ParseMode: telego.ModeMarkdown,
|
|
})
|
|
s.leaveChat(tgChat.ID)
|
|
s.LogError(err)
|
|
return
|
|
}
|
|
|
|
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 {
|
|
_ = 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 {
|
|
_ = 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.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,
|
|
})
|
|
s.LogError(err)
|
|
pl.User = user
|
|
pl.TGChat = tgChat
|
|
pl.Loc = loc
|
|
next = KeyboardChooserStateID
|
|
}
|
|
|
|
func (s *AddChatMemberState) getRegisteredAdmin(chatID int64) (*model.User, error) {
|
|
admins, err := s.App.TG().GetChatAdministrators(&telego.GetChatAdministratorsParams{
|
|
ChatID: tu.ID(chatID),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
adminIDs := make([]int64, len(admins))
|
|
for i, admin := range admins {
|
|
adminIDs[i] = admin.MemberUser().ID
|
|
}
|
|
dbAdmins, err := s.App.DB().ForUser().ByTelegramIDs(adminIDs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(dbAdmins) > 0 {
|
|
return &dbAdmins[0], nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *AddChatMemberState) leaveChat(chatID int64) {
|
|
_ = s.App.TG().LeaveChat(&telego.LeaveChatParams{
|
|
ChatID: tu.ID(chatID),
|
|
})
|
|
}
|
|
|
|
func (s *AddChatMemberState) ID() fsm.StateID {
|
|
return AddChatMemberStateID
|
|
}
|