package application import ( "context" "fmt" "mtusi2026lab3/internal/domain" ) func (s *Service) SendMessage(ctx context.Context, command SendMessageCommand) (domain.Message, error) { if err := domain.ValidateMessageText(command.Text); err != nil { return domain.Message{}, fmt.Errorf("%w: %v", ErrInvalidInput, err) } return s.repository.CreateMessage(ctx, command.ChatID, command.ActorID, command.Text) } func (s *Service) ListMessages( ctx context.Context, actorID string, chatID string, limit int, cursorToken string, ) (domain.MessagePage, error) { var cursor *domain.MessageCursor if cursorToken != "" { decoded, err := s.cursors.DecodeMessage(cursorToken, chatID) if err != nil { return domain.MessagePage{}, ErrInvalidCursor } cursor = decoded } items, hasMore, err := s.repository.GetMessages(ctx, chatID, actorID, limit, cursor) if err != nil { return domain.MessagePage{}, err } page := domain.MessagePage{Items: items} if page.Items == nil { page.Items = make([]domain.Message, 0) } if hasMore { last := page.Items[len(page.Items)-1] next, encodeErr := s.cursors.EncodeMessage(domain.MessageCursor{ CreatedAt: last.CreatedAt, MessageID: last.ID, ChatID: chatID, }) if encodeErr != nil { return domain.MessagePage{}, encodeErr } page.NextCursor = &next } return page, nil }