48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type SuggestionFlowState struct {
|
|
SuggestionID uint64 `json:"suggestion_id,omitempty"`
|
|
Stage string `json:"stage"` // "await_subject", "await_message", "await_file"
|
|
}
|
|
|
|
func (s *StateManager) SetSuggestionFlow(ctx context.Context, telegramID int64, state *SuggestionFlowState) error {
|
|
key := fmt.Sprintf("suggestion_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) GetSuggestionFlow(ctx context.Context, telegramID int64) (*SuggestionFlowState, error) {
|
|
key := fmt.Sprintf("suggestion_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 SuggestionFlowState
|
|
if err := json.Unmarshal([]byte(data), &state); err != nil {
|
|
return nil, err
|
|
}
|
|
return &state, nil
|
|
}
|
|
|
|
func (s *StateManager) ClearSuggestionFlow(ctx context.Context, telegramID int64) error {
|
|
key := fmt.Sprintf("suggestion_flow:%d", telegramID)
|
|
return s.redis.Del(ctx, key)
|
|
}
|