ruspet_portal_back/service/telegram_service/state/attachment.go

47 lines
1.2 KiB
Go

package state
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/redis/go-redis/v9"
"time"
)
type AttachmentSession struct {
ApprovalID uint64 `json:"approval_id"`
TaskID uint64 `json:"task_id,omitempty"`
Files []FileAttachment `json:"files"` // накапливаем
Stage string `json:"stage"` // "collecting"
}
func (s *StateManager) SetAttachmentSession(ctx context.Context, telegramID int64, session *AttachmentSession) error {
key := fmt.Sprintf("attach_session:%d", telegramID)
data, err := json.Marshal(session)
if err != nil {
return err
}
return s.redis.Set(ctx, key, data, 30*time.Minute)
}
func (s *StateManager) GetAttachmentSession(ctx context.Context, telegramID int64) (*AttachmentSession, error) {
key := fmt.Sprintf("attach_session:%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 session AttachmentSession
if err := json.Unmarshal([]byte(data), &session); err != nil {
return nil, err
}
return &session, nil
}
func (s *StateManager) ClearAttachmentSession(ctx context.Context, telegramID int64) error {
return s.redis.Del(ctx, fmt.Sprintf("attach_session:%d", telegramID))
}