forked from mtusi2026/mtusi2026lab4
fix: validate message request fields
This commit is contained in:
@@ -208,18 +208,16 @@ func (s *Server) messages(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if !valid {
|
||||
return invalidRequest()
|
||||
}
|
||||
var input struct {
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
var input createMessageRequest
|
||||
if err := readJSON(w, r, &input); err != nil {
|
||||
return err
|
||||
}
|
||||
if !validMessage(input.Text, input.AttachmentIDs) || !canonicalAttachmentIDs(input.AttachmentIDs) {
|
||||
text, attachmentIDs, valid := input.values()
|
||||
if !valid || !validMessage(text, attachmentIDs) || !canonicalAttachmentIDs(attachmentIDs) {
|
||||
return invalidRequest()
|
||||
}
|
||||
message, replay, err := s.messageRepository.CreateMessage(
|
||||
r.Context(), chatID, user.ID, input.Text, input.AttachmentIDs, key, s.idempotencyTTL,
|
||||
r.Context(), chatID, user.ID, text, attachmentIDs, key, s.idempotencyTTL,
|
||||
)
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return notFound()
|
||||
|
||||
Reference in New Issue
Block a user