mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-04 19:02:33 -06:00
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package telegram
|
|
|
|
import "sync"
|
|
|
|
// Состояния пользователя
|
|
type State int
|
|
|
|
const (
|
|
StateNone State = iota
|
|
StateAddServerURL
|
|
StateAddServerLogin
|
|
StateAddServerPassword
|
|
)
|
|
|
|
// UserContext хранит временные данные в процессе диалога
|
|
type UserContext struct {
|
|
State State
|
|
TempURL string
|
|
TempLogin string
|
|
TempPassword string
|
|
}
|
|
|
|
// StateManager управляет состояниями
|
|
type StateManager struct {
|
|
mu sync.RWMutex
|
|
states map[int64]*UserContext
|
|
}
|
|
|
|
func NewStateManager() *StateManager {
|
|
return &StateManager{
|
|
states: make(map[int64]*UserContext),
|
|
}
|
|
}
|
|
|
|
func (sm *StateManager) GetState(userID int64) State {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
if ctx, ok := sm.states[userID]; ok {
|
|
return ctx.State
|
|
}
|
|
return StateNone
|
|
}
|
|
|
|
func (sm *StateManager) SetState(userID int64, state State) {
|
|
sm.mu.Lock()
|
|
defer sm.mu.Unlock()
|
|
|
|
if _, ok := sm.states[userID]; !ok {
|
|
sm.states[userID] = &UserContext{}
|
|
}
|
|
sm.states[userID].State = state
|
|
}
|
|
|
|
func (sm *StateManager) GetContext(userID int64) *UserContext {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
if ctx, ok := sm.states[userID]; ok {
|
|
return ctx
|
|
}
|
|
return &UserContext{} // Return empty safe struct
|
|
}
|
|
|
|
func (sm *StateManager) UpdateContext(userID int64, updater func(*UserContext)) {
|
|
sm.mu.Lock()
|
|
defer sm.mu.Unlock()
|
|
|
|
if _, ok := sm.states[userID]; !ok {
|
|
sm.states[userID] = &UserContext{}
|
|
}
|
|
updater(sm.states[userID])
|
|
}
|
|
|
|
func (sm *StateManager) Reset(userID int64) {
|
|
sm.mu.Lock()
|
|
defer sm.mu.Unlock()
|
|
delete(sm.states, userID)
|
|
}
|