Files
mtusi2026lab4/internal/dto/message.go
T

65 lines
1.5 KiB
Go

package dto
import (
"mtusi2026-messenger/internal/model"
"strings"
"time"
"unicode/utf8"
"github.com/google/uuid"
)
type CreateMessageRequest struct {
Text string `json:"text"`
AttachmentIDs []string `json:"attachment_ids"`
}
type MessageResponse struct {
ID uuid.UUID `json:"id"`
ChatID uuid.UUID `json:"chat_id"`
SenderID uuid.UUID `json:"sender_id"`
Text string `json:"text"`
AttachmentIDs []uuid.UUID `json:"attachment_ids"`
CreatedAt time.Time `json:"created_at"`
}
type MessagePageResponse struct {
Items []MessageResponse `json:"items"`
NextCursor *string `json:"next_cursor"`
}
func (r CreateMessageRequest) Validate() bool {
if strings.ContainsRune(r.Text, 0) || utf8.RuneCountInString(r.Text) > 4000 {
return false
}
if len(r.AttachmentIDs) > 10 {
return false
}
seen := make(map[string]struct{}, len(r.AttachmentIDs))
for _, id := range r.AttachmentIDs {
if _, ok := seen[id]; ok {
return false
}
seen[id] = struct{}{}
}
hasText := strings.TrimSpace(r.Text) != ""
hasAttachment := len(r.AttachmentIDs) > 0
return hasText || hasAttachment
}
func ToMessageResponse(m model.Message) MessageResponse {
ids := make([]uuid.UUID, len(m.AttachmentIDs))
copy(ids, m.AttachmentIDs)
return MessageResponse{
ID: m.ID,
ChatID: m.ChatID,
SenderID: m.SenderID,
Text: m.Text,
AttachmentIDs: ids,
CreatedAt: m.CreatedAt,
}
}