68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// PendingCommentItem — одна отложенная заявка, ожидающая комментария.
|
|
type PendingCommentItem struct {
|
|
ApprovalID uint64 `json:"approval_id"`
|
|
Action string `json:"action"` // "reject" | "revoke"
|
|
}
|
|
|
|
func pendingCommentsKey(telegramID int64) string {
|
|
return fmt.Sprintf("appr_pending_comments:%d", telegramID)
|
|
}
|
|
|
|
func (s *StateManager) GetPendingComments(ctx context.Context, telegramID int64) ([]PendingCommentItem, error) {
|
|
data, err := s.redis.Get(ctx, pendingCommentsKey(telegramID))
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
var items []PendingCommentItem
|
|
if err := json.Unmarshal([]byte(data), &items); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (s *StateManager) AddPendingComment(ctx context.Context, telegramID int64, item PendingCommentItem) error {
|
|
items, _ := s.GetPendingComments(ctx, telegramID)
|
|
for _, existing := range items {
|
|
if existing.ApprovalID == item.ApprovalID {
|
|
return nil // уже есть — не дублируем
|
|
}
|
|
}
|
|
items = append(items, item)
|
|
data, err := json.Marshal(items)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.redis.Set(ctx, pendingCommentsKey(telegramID), string(data), 24*time.Hour)
|
|
}
|
|
|
|
func (s *StateManager) RemovePendingComment(ctx context.Context, telegramID int64, approvalID uint64) error {
|
|
items, _ := s.GetPendingComments(ctx, telegramID)
|
|
var filtered []PendingCommentItem
|
|
for _, item := range items {
|
|
if item.ApprovalID != approvalID {
|
|
filtered = append(filtered, item)
|
|
}
|
|
}
|
|
if len(filtered) == 0 {
|
|
return s.redis.Del(ctx, pendingCommentsKey(telegramID))
|
|
}
|
|
data, err := json.Marshal(filtered)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.redis.Set(ctx, pendingCommentsKey(telegramID), string(data), 24*time.Hour)
|
|
}
|
|
|
|
func (s *StateManager) ClearPendingComments(ctx context.Context, telegramID int64) error {
|
|
return s.redis.Del(ctx, pendingCommentsKey(telegramID))
|
|
}
|