77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type CommentFlowState struct {
|
|
ApprovalID uint64 `json:"approval_id"`
|
|
Stage string `json:"stage"` // "await_text"
|
|
}
|
|
|
|
type CommentReplyState struct {
|
|
ApprovalID uint64 `json:"approval_id"`
|
|
Stage string `json:"stage"` // "await_text"
|
|
}
|
|
|
|
func (s *StateManager) SetCommentFlow(ctx context.Context, telegramID int64, state *CommentFlowState) error {
|
|
key := fmt.Sprintf("comment_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) GetCommentFlow(ctx context.Context, telegramID int64) (*CommentFlowState, error) {
|
|
key := fmt.Sprintf("comment_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 CommentFlowState
|
|
if err := json.Unmarshal([]byte(data), &state); err != nil {
|
|
return nil, err
|
|
}
|
|
return &state, nil
|
|
}
|
|
|
|
func (s *StateManager) ClearCommentFlow(ctx context.Context, telegramID int64) error {
|
|
key := fmt.Sprintf("comment_flow:%d", telegramID)
|
|
return s.redis.Del(ctx, key)
|
|
}
|
|
|
|
func (s *StateManager) SetCommentReply(ctx context.Context, telegramID int64, state *CommentReplyState) error {
|
|
key := fmt.Sprintf("comment_reply:%d", telegramID)
|
|
data, _ := json.Marshal(state)
|
|
return s.redis.Set(ctx, key, data, 30*time.Minute)
|
|
}
|
|
|
|
func (s *StateManager) GetCommentReply(ctx context.Context, telegramID int64) (*CommentReplyState, error) {
|
|
key := fmt.Sprintf("comment_reply:%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 CommentReplyState
|
|
json.Unmarshal([]byte(data), &state)
|
|
return &state, nil
|
|
}
|
|
|
|
func (s *StateManager) ClearCommentReply(ctx context.Context, telegramID int64) error {
|
|
return s.redis.Del(ctx, fmt.Sprintf("comment_reply:%d", telegramID))
|
|
}
|