first
This commit is contained in:
91
internal/telegram/state.go
Normal file
91
internal/telegram/state.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package telegram
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
tele "gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
type PostStep int
|
||||
|
||||
const (
|
||||
StepAwaitForward PostStep = iota
|
||||
StepAwaitCategory
|
||||
StepAwaitConfirm
|
||||
)
|
||||
|
||||
type PostState struct {
|
||||
UserID int64
|
||||
ForwardedMsg *tele.Message
|
||||
SelectedCat string
|
||||
Step PostStep
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type StateManager struct {
|
||||
mu sync.RWMutex
|
||||
states map[int64]*PostState
|
||||
}
|
||||
|
||||
func NewStateManager() *StateManager {
|
||||
return &StateManager{
|
||||
states: make(map[int64]*PostState),
|
||||
}
|
||||
}
|
||||
|
||||
func (sm *StateManager) Get(userID int64) *PostState {
|
||||
sm.mu.RLock()
|
||||
defer sm.mu.RUnlock()
|
||||
return sm.states[userID]
|
||||
}
|
||||
|
||||
func (sm *StateManager) Set(state *PostState) {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
sm.states[state.UserID] = state
|
||||
}
|
||||
|
||||
func (sm *StateManager) Delete(userID int64) {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
delete(sm.states, userID)
|
||||
}
|
||||
|
||||
func (sm *StateManager) StartPost(userID int64) *PostState {
|
||||
state := &PostState{
|
||||
UserID: userID,
|
||||
Step: StepAwaitForward,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
sm.Set(state)
|
||||
return state
|
||||
}
|
||||
|
||||
func (sm *StateManager) SetForwarded(userID int64, msg *tele.Message) *PostState {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
|
||||
state := sm.states[userID]
|
||||
if state == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
state.ForwardedMsg = msg
|
||||
state.Step = StepAwaitCategory
|
||||
return state
|
||||
}
|
||||
|
||||
func (sm *StateManager) SetCategory(userID int64, category string) *PostState {
|
||||
sm.mu.Lock()
|
||||
defer sm.mu.Unlock()
|
||||
|
||||
state := sm.states[userID]
|
||||
if state == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
state.SelectedCat = category
|
||||
state.Step = StepAwaitConfirm
|
||||
return state
|
||||
}
|
||||
Reference in New Issue
Block a user