48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type RevisionFlowState struct {
|
|
ApprovalID uint64 `json:"approval_id"`
|
|
Stage string `json:"stage"` // "await_action" | "await_content" | "await_files"
|
|
}
|
|
|
|
func (s *StateManager) SetRevisionFlow(ctx context.Context, telegramID int64, state *RevisionFlowState) error {
|
|
key := fmt.Sprintf("revision_flow:%d", telegramID)
|
|
data, err := json.Marshal(state)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.redis.Set(ctx, key, data, 30*time.Minute)
|
|
}
|
|
|
|
func (s *StateManager) GetRevisionFlow(ctx context.Context, telegramID int64) (*RevisionFlowState, error) {
|
|
key := fmt.Sprintf("revision_flow:%d", telegramID)
|
|
data, err := s.redis.Get(ctx, key)
|
|
if errors.Is(err, redis.Nil) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var state RevisionFlowState
|
|
if err := json.Unmarshal([]byte(data), &state); err != nil {
|
|
return nil, err
|
|
}
|
|
return &state, nil
|
|
}
|
|
|
|
func (s *StateManager) ClearRevisionFlow(ctx context.Context, telegramID int64) error {
|
|
key := fmt.Sprintf("revision_flow:%d", telegramID)
|
|
return s.redis.Del(ctx, key)
|
|
}
|