fix(tgbot): scope the add-client wizard to the admin, not the chat (#6604)
Two admins in one group chat shared one draft and one wizard step: clientDrafts and userStateStore were keyed by chat id alone, so the second admin's wizard opened on the first one's email and limits, and whichever of them tapped a control last decided what the other created. Key both stores by (chat, user) instead. A private chat is unaffected: its two ids are equal, so the key matches what the chat alone used to be. A message with no sender (a channel post) keys to user 0, which no admin holds. Fixes #6593.
This commit is contained in:
1 parent
3b9ca47a4e
commit
ee2ff48c81
7 files changed
+179
-53
No files matched your search
@@ -82,30 +82,52 @@ type clientDraft struct {
|
||||
reset int
|
||||
}
|
||||
|
||||
// clientDrafts keys a draft by chat: the steps arrive on the worker pool, so a
|
||||
// single draft let two admins fill in one client between them.
|
||||
type clientDrafts struct {
|
||||
mu sync.Mutex
|
||||
drafts map[int64]*clientDraft
|
||||
// chatUser names the admin a wizard belongs to. A private chat's ids are equal;
|
||||
// in a group they are not, and each admin at its keyboard fills in their own.
|
||||
type chatUser struct {
|
||||
chatID int64
|
||||
userID int64
|
||||
}
|
||||
|
||||
var addClientDrafts = &clientDrafts{drafts: make(map[int64]*clientDraft)}
|
||||
// messageActor reads the sender off a message. A post without one (a channel)
|
||||
// keys to user 0, an id no admin can hold.
|
||||
func messageActor(message telego.Message) chatUser {
|
||||
if message.From == nil {
|
||||
return chatUser{chatID: message.Chat.ID}
|
||||
}
|
||||
return chatUser{chatID: message.Chat.ID, userID: message.From.ID}
|
||||
}
|
||||
|
||||
func (s *clientDrafts) forChat(chatID int64) *clientDraft {
|
||||
// callbackActor reads the admin who tapped the button, not the chat the keyboard
|
||||
// sits in: every admin in a group sees the same keyboard.
|
||||
func callbackActor(callbackQuery *telego.CallbackQuery) chatUser {
|
||||
return chatUser{chatID: callbackQuery.Message.GetChat().ID, userID: callbackQuery.From.ID}
|
||||
}
|
||||
|
||||
// clientDrafts keys a draft by the admin filling it in: the steps arrive on the
|
||||
// worker pool, so one draft let two admins fill in one client between them.
|
||||
type clientDrafts struct {
|
||||
mu sync.Mutex
|
||||
drafts map[chatUser]*clientDraft
|
||||
}
|
||||
|
||||
var addClientDrafts = &clientDrafts{drafts: make(map[chatUser]*clientDraft)}
|
||||
|
||||
func (s *clientDrafts) forActor(actor chatUser) *clientDraft {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
draft, ok := s.drafts[chatID]
|
||||
draft, ok := s.drafts[actor]
|
||||
if !ok {
|
||||
draft = &clientDraft{}
|
||||
s.drafts[chatID] = draft
|
||||
s.drafts[actor] = draft
|
||||
}
|
||||
return draft
|
||||
}
|
||||
|
||||
func (s *clientDrafts) reset(chatID int64) {
|
||||
func (s *clientDrafts) reset(actor chatUser) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
delete(s.drafts, chatID)
|
||||
delete(s.drafts, actor)
|
||||
}
|
||||
|
||||
// isAddClientStep reports whether callback data belongs to the add-client
|
||||
@@ -117,17 +139,17 @@ func isAddClientStep(data string) bool {
|
||||
func (s *clientDrafts) resetAll() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.drafts = make(map[int64]*clientDraft)
|
||||
s.drafts = make(map[chatUser]*clientDraft)
|
||||
}
|
||||
|
||||
// userStateStore guards the per-chat conversation states. The Telegram command
|
||||
// userStateStore guards the per-admin conversation states. The Telegram command
|
||||
// and callback handlers run on a worker-pool goroutine while the message handler
|
||||
// runs on the dispatch goroutine, so a bare map would be a concurrent-map-write
|
||||
// crash. It also expires abandoned conversations so a user who starts a flow and
|
||||
// goes silent doesn't leave an entry forever.
|
||||
type userStateStore struct {
|
||||
mu sync.Mutex
|
||||
states map[int64]userStateEntry
|
||||
states map[chatUser]userStateEntry
|
||||
lastPrune time.Time
|
||||
}
|
||||
|
||||
@@ -136,30 +158,30 @@ type userStateEntry struct {
|
||||
at time.Time
|
||||
}
|
||||
|
||||
var userStateMgr = &userStateStore{states: make(map[int64]userStateEntry)}
|
||||
var userStateMgr = &userStateStore{states: make(map[chatUser]userStateEntry)}
|
||||
|
||||
func (s *userStateStore) set(chatID int64, state string) {
|
||||
func (s *userStateStore) set(actor chatUser, state string) {
|
||||
s.mu.Lock()
|
||||
s.states[chatID] = userStateEntry{state: state, at: time.Now()}
|
||||
s.states[actor] = userStateEntry{state: state, at: time.Now()}
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *userStateStore) get(chatID int64) (string, bool) {
|
||||
func (s *userStateStore) get(actor chatUser) (string, bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
e, ok := s.states[chatID]
|
||||
e, ok := s.states[actor]
|
||||
return e.state, ok
|
||||
}
|
||||
|
||||
func (s *userStateStore) clear(chatID int64) {
|
||||
func (s *userStateStore) clear(actor chatUser) {
|
||||
s.mu.Lock()
|
||||
delete(s.states, chatID)
|
||||
delete(s.states, actor)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *userStateStore) reset() {
|
||||
s.mu.Lock()
|
||||
s.states = make(map[int64]userStateEntry)
|
||||
s.states = make(map[chatUser]userStateEntry)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +20,10 @@ func TestAddClientExpiryPresetReplacesTheTerm(t *testing.T) {
|
||||
swapTestBot(t, url)
|
||||
|
||||
// A fresh draft attaches no inbound, so the card never looks one up by remark.
|
||||
draft := addClientDrafts.forChat(chatID)
|
||||
draft := addClientDrafts.forActor(chatUser{chatID: chatID, userID: 1})
|
||||
origRunning := isRunning
|
||||
t.Cleanup(func() {
|
||||
addClientDrafts.reset(chatID)
|
||||
addClientDrafts.reset(chatUser{chatID: chatID, userID: 1})
|
||||
isRunning = origRunning
|
||||
})
|
||||
isRunning = true
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
|
||||
"github.com/mymmrac/telego"
|
||||
)
|
||||
|
||||
// Regression test: keying the add-client wizard by chat alone left the two
|
||||
// admins of a group chat filling in one client between them.
|
||||
func TestAddClientDraftIsPerAdminInGroupChat(t *testing.T) {
|
||||
if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
|
||||
t.Fatalf("InitDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = database.CloseDB() })
|
||||
|
||||
const (
|
||||
groupChat = int64(-1001234567890)
|
||||
adminA = int64(8101)
|
||||
adminB = int64(8202)
|
||||
)
|
||||
url, textsFor := draftTexts(t)
|
||||
swapTestBot(t, url)
|
||||
origRunning := isRunning
|
||||
t.Cleanup(func() {
|
||||
isRunning = origRunning
|
||||
userStateMgr.reset()
|
||||
})
|
||||
isRunning = true
|
||||
|
||||
// Both admins tap in the same chat; only the sender tells them apart.
|
||||
tap := func(userID int64, data string) {
|
||||
t.Helper()
|
||||
(&Tgbot{}).answerCallback(&telego.CallbackQuery{
|
||||
ID: "q1",
|
||||
From: telego.User{ID: userID},
|
||||
Data: data,
|
||||
Message: &telego.Message{MessageID: 7, Chat: telego.Chat{ID: groupChat}},
|
||||
}, true)
|
||||
}
|
||||
|
||||
tap(adminA, "add_client_to 1")
|
||||
emailA := cardEmail(t, lastDraftCard(t, textsFor(groupChat)))
|
||||
tap(adminB, "add_client_to 2")
|
||||
emailB := cardEmail(t, lastDraftCard(t, textsFor(groupChat)))
|
||||
if emailA == "" || emailA == emailB {
|
||||
t.Fatalf("both admins start with email %q, want one draft per admin", emailA)
|
||||
}
|
||||
|
||||
// Admin A renders again, with admin B's wizard already past its start.
|
||||
tap(adminA, "add_client_default_traffic_exp")
|
||||
if got := cardEmail(t, lastDraftCard(t, textsFor(groupChat))); got != emailA {
|
||||
t.Errorf("admin A's card shows email %q, want its own %q from admin B's draft", got, emailA)
|
||||
}
|
||||
|
||||
// The step they are each on is their own too: A's prompt must not put B into
|
||||
// the same step, or B's next message lands in A's wizard.
|
||||
tap(adminA, "add_client_ch_default_email")
|
||||
if st, ok := userStateMgr.get(chatUser{chatID: groupChat, userID: adminA}); !ok || st != "awaiting_email" {
|
||||
t.Errorf("admin A's step = %q (set: %v), want awaiting_email", st, ok)
|
||||
}
|
||||
if st, ok := userStateMgr.get(chatUser{chatID: groupChat, userID: adminB}); ok {
|
||||
t.Errorf("admin B's step = %q, want none: only the tapper's step may change", st)
|
||||
}
|
||||
}
|
||||
|
||||
// The wizard's typed steps arrive as messages, and that handler is a closure no
|
||||
// test can drive, so pin the key it takes there: sender, not chat.
|
||||
func TestMessageActorSeparatesAdminsInOneChat(t *testing.T) {
|
||||
const (
|
||||
groupChat = int64(-1001234567890)
|
||||
adminA = int64(8101)
|
||||
adminB = int64(8202)
|
||||
)
|
||||
t.Cleanup(userStateMgr.reset)
|
||||
|
||||
fromA := messageActor(telego.Message{Chat: telego.Chat{ID: groupChat}, From: &telego.User{ID: adminA}})
|
||||
fromB := messageActor(telego.Message{Chat: telego.Chat{ID: groupChat}, From: &telego.User{ID: adminB}})
|
||||
// A channel post carries no sender and must not land on an admin's step.
|
||||
fromChannel := messageActor(telego.Message{Chat: telego.Chat{ID: groupChat}})
|
||||
|
||||
if want := (chatUser{chatID: groupChat, userID: adminA}); fromA != want {
|
||||
t.Errorf("message from admin A keyed as %+v, want %+v", fromA, want)
|
||||
}
|
||||
if fromA == fromB || fromA == fromChannel || fromB == fromChannel {
|
||||
t.Fatalf("keys collide: %+v, %+v, %+v", fromA, fromB, fromChannel)
|
||||
}
|
||||
|
||||
userStateMgr.set(fromA, "awaiting_email")
|
||||
if st, ok := userStateMgr.get(fromB); ok {
|
||||
t.Errorf("admin B sees step %q: admin A's answer would land in B's wizard", st)
|
||||
}
|
||||
if st, ok := userStateMgr.get(fromChannel); ok {
|
||||
t.Errorf("a senderless post sees step %q", st)
|
||||
}
|
||||
if st, ok := userStateMgr.get(fromA); !ok || st != "awaiting_email" {
|
||||
t.Errorf("admin A's step = %q (set: %v), want awaiting_email", st, ok)
|
||||
}
|
||||
}
|
||||
Loaded 3 of 7 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user