forked from mtusi2026/mtusi2026lab4
143 lines
3.1 KiB
Go
143 lines
3.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"highload/internal/model"
|
|
"highload/internal/repository"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type MessageService struct {
|
|
repo repository.MessageRepository
|
|
userService *UserService
|
|
chatService *ChatService
|
|
}
|
|
|
|
func NewMessageService(repo repository.MessageRepository, chatService *ChatService, userService *UserService) *MessageService {
|
|
return &MessageService{repo: repo, chatService: chatService, userService: userService}
|
|
}
|
|
|
|
func (s *MessageService) CreateMessage(ctx context.Context, text, chatID, userID, key string, attachmentIDs []string) (*model.Message, error) {
|
|
|
|
_, err := s.chatService.GetChatByID(ctx, chatID, userID)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lenText := utf8.RuneCountInString(text)
|
|
|
|
if lenText > 4000 || (strings.TrimSpace(text) == "" && len(attachmentIDs) == 0) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
|
|
if len(attachmentIDs) > 10 {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
|
|
seen := make(map[uuid.UUID]struct{}, len(attachmentIDs))
|
|
for _, rawID := range attachmentIDs {
|
|
id, err := uuid.Parse(rawID)
|
|
if err != nil {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
if _, exists := seen[id]; exists {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
seen[id] = struct{}{}
|
|
|
|
}
|
|
|
|
message, err := s.repo.CreateMessage(ctx, chatID, userID, text, key, attachmentIDs)
|
|
|
|
if err != nil {
|
|
return nil, convertRepoError(err)
|
|
}
|
|
|
|
return message, nil
|
|
}
|
|
|
|
func (s *MessageService) GetMessagesByChatID(ctx context.Context, chatID, userID string, limit *int, cursor *string) ([]*model.Message, *string, error) {
|
|
|
|
actualLimit := 50
|
|
|
|
if limit != nil {
|
|
actualLimit = *limit
|
|
}
|
|
|
|
if actualLimit <= 0 || actualLimit > 100 {
|
|
|
|
return nil, nil, ErrInvalidInput
|
|
}
|
|
|
|
chat, err := s.chatService.GetChatByID(ctx, chatID, userID)
|
|
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var messagePosition *repository.MessagePosition
|
|
|
|
if cursor != nil {
|
|
mesCursor, err := decodeCursor[messageCursor](*cursor)
|
|
|
|
if err != nil {
|
|
return nil, nil, ErrInvalidInput
|
|
}
|
|
|
|
if _, err := uuid.Parse(mesCursor.ChatID); err != nil {
|
|
return nil, nil, ErrInvalidInput
|
|
}
|
|
|
|
if _, err := uuid.Parse(mesCursor.MessageID); err != nil {
|
|
return nil, nil, ErrInvalidInput
|
|
}
|
|
|
|
if mesCursor.CreatedAt.IsZero() {
|
|
return nil, nil, ErrInvalidInput
|
|
}
|
|
|
|
if mesCursor.ChatID != chat.ID {
|
|
return nil, nil, ErrInvalidInput
|
|
}
|
|
|
|
messagePosition = &repository.MessagePosition{
|
|
ID: mesCursor.MessageID,
|
|
CreatedAt: mesCursor.CreatedAt,
|
|
}
|
|
|
|
}
|
|
|
|
messages, err := s.repo.GetMessagesByChatID(ctx, chat.ID, actualLimit+1, messagePosition)
|
|
|
|
if err != nil {
|
|
return nil, nil, convertRepoError(err)
|
|
}
|
|
|
|
if len(messages) > actualLimit {
|
|
mesCursor := &messageCursor{
|
|
MessageID: messages[actualLimit-1].ID,
|
|
ChatID: chat.ID,
|
|
CreatedAt: messages[actualLimit-1].CreatedAt,
|
|
}
|
|
cursor, err = encodeCursor[messageCursor](*mesCursor)
|
|
if err != nil {
|
|
return nil, nil, ErrServerError
|
|
}
|
|
return messages[:actualLimit], cursor, nil
|
|
}
|
|
|
|
return messages, nil, nil
|
|
|
|
}
|
|
|
|
type messageCursor struct {
|
|
MessageID string `json:"message_id"`
|
|
ChatID string `json:"chat_id"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|