forked from mtusi2026/mtusi2026lab3
141 lines
4.0 KiB
Go
141 lines
4.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
"messenger/src/backend/repositories"
|
|
)
|
|
|
|
type ChatPage struct {
|
|
Items []repositories.Chat `json:"items"`
|
|
NextCursor *string `json:"next_cursor"`
|
|
}
|
|
|
|
type ChatService struct {
|
|
chats *repositories.ChatStore
|
|
}
|
|
|
|
func NewChatService(chats *repositories.ChatStore) *ChatService {
|
|
return &ChatService{chats: chats}
|
|
}
|
|
|
|
func ChatHasMember(chat repositories.Chat, userID string) bool {
|
|
for _, memberID := range chat.MemberIDs {
|
|
if memberID == userID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (s *ChatService) Create(ctx context.Context, currentUserID, title string, memberIDs []string) (repositories.Chat, error) {
|
|
if strings.TrimSpace(title) == "" {
|
|
return repositories.Chat{}, InvalidInput{"title must not be blank"}
|
|
}
|
|
if utf8.RuneCountInString(title) > 100 {
|
|
return repositories.Chat{}, InvalidInput{"title is too long"}
|
|
}
|
|
if len(memberIDs) == 0 || len(memberIDs) > 100 {
|
|
return repositories.Chat{}, InvalidInput{"member_ids must contain from 1 to 100 users"}
|
|
}
|
|
seen := make(map[string]struct{}, len(memberIDs))
|
|
for _, memberID := range memberIDs {
|
|
if _, err := uuid.Parse(memberID); err != nil {
|
|
return repositories.Chat{}, InvalidInput{"member_ids must contain valid UUIDs"}
|
|
}
|
|
if _, exists := seen[memberID]; exists {
|
|
return repositories.Chat{}, InvalidInput{"member_ids must not contain duplicates"}
|
|
}
|
|
seen[memberID] = struct{}{}
|
|
}
|
|
if _, exists := seen[currentUserID]; !exists {
|
|
return repositories.Chat{}, InvalidInput{"current user must be a chat member"}
|
|
}
|
|
chat, err := s.chats.Create(ctx, title, memberIDs)
|
|
if errors.Is(err, repositories.ErrUnknownMember) {
|
|
return repositories.Chat{}, InvalidInput{"all members must be existing users"}
|
|
}
|
|
return chat, err
|
|
}
|
|
|
|
func (s *ChatService) Get(ctx context.Context, currentUserID, chatID string) (repositories.Chat, error) {
|
|
chat, err := s.chats.GetByID(ctx, chatID)
|
|
if repositories.NotFound(err) {
|
|
return repositories.Chat{}, ErrChatNotFound
|
|
}
|
|
if err != nil {
|
|
return repositories.Chat{}, err
|
|
}
|
|
if !ChatHasMember(chat, currentUserID) {
|
|
return repositories.Chat{}, ErrChatNotFound
|
|
}
|
|
return chat, nil
|
|
}
|
|
|
|
func EncodeChatCursor(cursor repositories.ChatCursor) (string, error) {
|
|
data, err := json.Marshal(cursor)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return base64.RawURLEncoding.EncodeToString(data), nil
|
|
}
|
|
|
|
func DecodeChatCursor(value string) (repositories.ChatCursor, error) {
|
|
data, err := base64.RawURLEncoding.DecodeString(value)
|
|
if err != nil {
|
|
return repositories.ChatCursor{}, err
|
|
}
|
|
var cursor repositories.ChatCursor
|
|
if err := json.Unmarshal(data, &cursor); err != nil {
|
|
return repositories.ChatCursor{}, err
|
|
}
|
|
if cursor.UserID == "" || cursor.ChatID == "" || cursor.CreatedAt.IsZero() {
|
|
return repositories.ChatCursor{}, strconv.ErrSyntax
|
|
}
|
|
if _, err := uuid.Parse(cursor.UserID); err != nil {
|
|
return repositories.ChatCursor{}, err
|
|
}
|
|
if _, err := uuid.Parse(cursor.ChatID); err != nil {
|
|
return repositories.ChatCursor{}, err
|
|
}
|
|
return cursor, nil
|
|
}
|
|
|
|
func (s *ChatService) List(ctx context.Context, userID string, limit int, cursorToken string) (ChatPage, error) {
|
|
if limit < 1 || limit > 100 {
|
|
return ChatPage{}, InvalidInput{"limit must be between 1 and 100"}
|
|
}
|
|
var cursor *repositories.ChatCursor
|
|
if cursorToken != "" {
|
|
decoded, err := DecodeChatCursor(cursorToken)
|
|
if err != nil || decoded.UserID != userID {
|
|
return ChatPage{}, InvalidInput{"invalid cursor"}
|
|
}
|
|
cursor = &decoded
|
|
}
|
|
items, more, err := s.chats.ListPageForUser(ctx, userID, cursor, limit)
|
|
if err != nil {
|
|
return ChatPage{}, err
|
|
}
|
|
page := ChatPage{Items: items}
|
|
if page.Items == nil {
|
|
page.Items = []repositories.Chat{}
|
|
}
|
|
if more {
|
|
last := items[len(items)-1]
|
|
next, err := EncodeChatCursor(repositories.ChatCursor{UserID: userID, CreatedAt: last.CreatedAt, ChatID: last.ID})
|
|
if err != nil {
|
|
return ChatPage{}, err
|
|
}
|
|
page.NextCursor = &next
|
|
}
|
|
return page, nil
|
|
}
|