forked from mtusi2026/mtusi2026lab4
лаба 4 на оценку 5
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const MaxMessageAttachments = 10
|
||||
|
||||
var usernameRegex = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
func ValidateUser(username, displayName string) error {
|
||||
@@ -65,13 +67,35 @@ func ValidateChatInput(title string, memberIDs []string, callerID string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateMessageText(text string) error {
|
||||
func ValidateMessageInput(text string, attachmentIDs []string) error {
|
||||
textRunes := utf8.RuneCountInString(text)
|
||||
if textRunes < 1 || textRunes > 4000 {
|
||||
return fmt.Errorf("message text length must be between 1 and 4000 code points")
|
||||
if textRunes > 4000 {
|
||||
return fmt.Errorf("message text length must be at most 4000 code points")
|
||||
}
|
||||
if strings.TrimSpace(text) == "" {
|
||||
return fmt.Errorf("message text cannot be only whitespace")
|
||||
if len(attachmentIDs) > MaxMessageAttachments {
|
||||
return fmt.Errorf("attachment_ids must contain at most %d items", MaxMessageAttachments)
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{}, len(attachmentIDs))
|
||||
for _, attachmentID := range attachmentIDs {
|
||||
parsedID, err := uuid.Parse(attachmentID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid attachment uuid: %s", attachmentID)
|
||||
}
|
||||
canonicalID := parsedID.String()
|
||||
if _, exists := seen[canonicalID]; exists {
|
||||
return fmt.Errorf("duplicate attachment_id: %s", attachmentID)
|
||||
}
|
||||
seen[canonicalID] = struct{}{}
|
||||
}
|
||||
|
||||
if strings.TrimSpace(text) == "" && len(attachmentIDs) == 0 {
|
||||
return fmt.Errorf("message must contain non-blank text or at least one attachment")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateMessageText remains for callers that create text-only messages.
|
||||
func ValidateMessageText(text string) error {
|
||||
return ValidateMessageInput(text, nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user