189 lines
3.8 KiB
Go
189 lines
3.8 KiB
Go
package service
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"highload/internal/model"
|
||
"highload/internal/repository"
|
||
"slices"
|
||
"strings"
|
||
"time"
|
||
"unicode/utf8"
|
||
|
||
"github.com/google/uuid"
|
||
)
|
||
|
||
type ChatService struct {
|
||
repo repository.ChatRepository
|
||
userRepo repository.UserRepository
|
||
userService *UserService
|
||
}
|
||
|
||
func NewChatService(repo repository.ChatRepository, userRepo repository.UserRepository, userService *UserService) *ChatService {
|
||
return &ChatService{repo: repo, userRepo: userRepo, userService: userService}
|
||
}
|
||
|
||
func (s *ChatService) Create(ctx context.Context, title string, memberIDs []string, userID string) (*model.Chat, error) {
|
||
|
||
trimTitle := strings.TrimSpace(title)
|
||
|
||
lenTitle := utf8.RuneCountInString(title)
|
||
if lenTitle < 1 || lenTitle > 100 {
|
||
return nil, ErrInvalidInput
|
||
}
|
||
|
||
if trimTitle == "" {
|
||
return nil, ErrInvalidInput
|
||
}
|
||
|
||
if memberIDs == nil || len(memberIDs) == 0 || len(memberIDs) > 100 {
|
||
return nil, ErrInvalidInput
|
||
}
|
||
|
||
seen := make(map[string]struct{})
|
||
for _, memberID := range memberIDs {
|
||
parsedID, err := uuid.Parse(memberID)
|
||
|
||
if err != nil {
|
||
return nil, ErrInvalidInput
|
||
}
|
||
|
||
normalizedID := parsedID.String()
|
||
|
||
if _, exist := seen[normalizedID]; exist {
|
||
return nil, ErrInvalidInput
|
||
}
|
||
|
||
seen[normalizedID] = struct{}{}
|
||
|
||
_, err = s.userRepo.GetUserByID(ctx, memberID)
|
||
if err != nil {
|
||
// NOTE: MemberNotFound –> InvalidInput
|
||
if errors.Is(err, repository.ErrNotFound) {
|
||
return nil, ErrInvalidInput
|
||
}
|
||
return nil, convertRepoError(err)
|
||
}
|
||
}
|
||
|
||
parsedUserID, err := uuid.Parse(userID)
|
||
if err != nil {
|
||
return nil, ErrServerError
|
||
}
|
||
normalizedUserID := parsedUserID.String()
|
||
|
||
_, creatorExists := seen[normalizedUserID]
|
||
if !creatorExists {
|
||
return nil, ErrInvalidInput
|
||
}
|
||
|
||
chat, err := s.repo.CreateChat(ctx, title, memberIDs)
|
||
|
||
if err != nil {
|
||
return nil, convertRepoError(err)
|
||
}
|
||
|
||
return chat, nil
|
||
}
|
||
|
||
func (s *ChatService) GetChatsByUserID(ctx context.Context, userID string, limit *int, cursor *string) ([]*model.Chat, *string, error) {
|
||
|
||
actualLimit := 50
|
||
|
||
if limit != nil {
|
||
actualLimit = *limit
|
||
}
|
||
|
||
if actualLimit <= 0 || actualLimit > 100 {
|
||
return nil, nil, ErrInvalidInput
|
||
}
|
||
|
||
var chatPosition *repository.ChatPosition
|
||
|
||
if cursor != nil {
|
||
chCursor, err := decodeCursor[chatCursor](*cursor)
|
||
|
||
if err != nil {
|
||
return nil, nil, ErrInvalidInput
|
||
}
|
||
|
||
_, err = uuid.Parse(chCursor.ChatID)
|
||
|
||
if err != nil {
|
||
return nil, nil, ErrInvalidInput
|
||
}
|
||
|
||
if chCursor.CreatedAt.IsZero() {
|
||
return nil, nil, ErrInvalidInput
|
||
}
|
||
|
||
_, err = uuid.Parse(chCursor.UserID)
|
||
if err != nil {
|
||
return nil, nil, ErrInvalidInput
|
||
}
|
||
|
||
if chCursor.UserID != userID {
|
||
return nil, nil, ErrInvalidInput
|
||
}
|
||
|
||
chatPosition = &repository.ChatPosition{
|
||
CreatedAt: chCursor.CreatedAt,
|
||
ID: chCursor.ChatID,
|
||
}
|
||
}
|
||
|
||
chats, err := s.repo.GetChatsByUserID(ctx, userID, actualLimit+1, chatPosition)
|
||
|
||
if err != nil {
|
||
return nil, nil, convertRepoError(err)
|
||
}
|
||
|
||
var chCursor *chatCursor
|
||
|
||
if len(chats) > actualLimit {
|
||
chCursor = &chatCursor{
|
||
ChatID: chats[actualLimit-1].ID,
|
||
UserID: userID,
|
||
CreatedAt: chats[actualLimit-1].CreatedAt,
|
||
}
|
||
cursor, err = encodeCursor[chatCursor](*chCursor)
|
||
if err != nil {
|
||
return nil, nil, ErrServerError
|
||
}
|
||
|
||
return chats[:actualLimit], cursor, nil
|
||
}
|
||
|
||
return chats, nil, nil
|
||
|
||
}
|
||
|
||
func (s *ChatService) GetChatByID(ctx context.Context, chatID, userID string) (*model.Chat, error) {
|
||
|
||
_, err := uuid.Parse(chatID)
|
||
|
||
if err != nil {
|
||
return nil, ErrInvalidInput
|
||
}
|
||
|
||
chat, err := s.repo.GetChatByID(ctx, chatID)
|
||
|
||
if err != nil {
|
||
return nil, convertRepoError(err)
|
||
}
|
||
|
||
index := slices.Index(chat.MemberIDs, userID)
|
||
|
||
if index == -1 {
|
||
return nil, ErrNotFound
|
||
}
|
||
|
||
return chat, nil
|
||
}
|
||
|
||
type chatCursor struct {
|
||
CreatedAt time.Time `json:"created_at"`
|
||
ChatID string `json:"chat_id"`
|
||
UserID string `json:"user_id"`
|
||
}
|