This commit is contained in:
parent
3b6c3e28f3
commit
b4ee5f77b8
@ -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)
|
||||
func (s *AddChatMemberState) curseAndLeave(chatID int64, err error) {
|
||||
s.LogError(util.SendInternalError(s.App.TG(), chatID, s.Localizer()))
|
||||
s.leaveChat(chatID)
|
||||
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,
|
||||
})
|
||||
s.LogError(err)
|
||||
pl.User = user
|
||||
pl.TGChat = tgChat
|
||||
pl.Loc = loc
|
||||
next = KeyboardChooserStateID
|
||||
}
|
||||
|
||||
func (s *AddChatMemberState) getRegisteredAdmin(chatID int64) (*model.User, error) {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user