forked from carrydela/mygoTgChanBot
108 lines
1.8 KiB
Go
108 lines
1.8 KiB
Go
package telegram
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
tele "gopkg.in/telebot.v3"
|
|
)
|
|
|
|
type PostStep int
|
|
|
|
const (
|
|
StepAwaitForward PostStep = iota
|
|
StepAwaitCategory
|
|
StepAwaitTitle
|
|
StepAwaitConfirm
|
|
)
|
|
|
|
type PostState struct {
|
|
UserID int64
|
|
ForwardedMsg *tele.Message
|
|
SelectedCat string
|
|
Title 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 = StepAwaitTitle
|
|
return state
|
|
}
|
|
|
|
func (sm *StateManager) SetTitle(userID int64, title string) *PostState {
|
|
sm.mu.Lock()
|
|
defer sm.mu.Unlock()
|
|
|
|
state := sm.states[userID]
|
|
if state == nil {
|
|
return nil
|
|
}
|
|
|
|
state.Title = title
|
|
state.Step = StepAwaitConfirm
|
|
return state
|
|
}
|