forked from mtusi2026/mtusi2026lab4
feat: add S3 attachments and reconciliation
This commit is contained in:
@@ -38,6 +38,9 @@ func (s *Server) readiness(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if err := s.sessions.Ping(ctx); err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
if err := s.objects.Ping(ctx); err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return nil
|
||||
}
|
||||
@@ -207,16 +210,17 @@ func (s *Server) messages(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
return invalidRequest()
|
||||
}
|
||||
var input struct {
|
||||
Text string `json:"text"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
if err := readJSON(w, r, &input); err != nil {
|
||||
return err
|
||||
}
|
||||
if !validText(input.Text, 4000) {
|
||||
if !validMessage(input.Text, input.AttachmentIDs) || !canonicalAttachmentIDs(input.AttachmentIDs) {
|
||||
return invalidRequest()
|
||||
}
|
||||
message, replay, err := s.messengerStore.CreateMessage(
|
||||
r.Context(), chatID, user.ID, input.Text, key, s.idempotencyTTL,
|
||||
r.Context(), chatID, user.ID, input.Text, input.AttachmentIDs, key, s.idempotencyTTL,
|
||||
)
|
||||
if errors.Is(err, postgres.ErrNotFound) {
|
||||
return notFound()
|
||||
@@ -234,6 +238,32 @@ func (s *Server) messages(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validMessage(text string, attachmentIDs []string) bool {
|
||||
if !utf8.ValidString(text) || utf8.RuneCountInString(text) > 4000 {
|
||||
return false
|
||||
}
|
||||
return len(attachmentIDs) > 0 || strings.TrimSpace(text) != ""
|
||||
}
|
||||
|
||||
func canonicalAttachmentIDs(attachmentIDs []string) bool {
|
||||
if len(attachmentIDs) > 10 {
|
||||
return false
|
||||
}
|
||||
seen := make(map[string]struct{}, len(attachmentIDs))
|
||||
for index, attachmentID := range attachmentIDs {
|
||||
canonical, valid := canonicalID(attachmentID)
|
||||
if !valid {
|
||||
return false
|
||||
}
|
||||
if _, duplicate := seen[canonical]; duplicate {
|
||||
return false
|
||||
}
|
||||
seen[canonical] = struct{}{}
|
||||
attachmentIDs[index] = canonical
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) listMessages(w http.ResponseWriter, r *http.Request, user User, chatID string) *apiError {
|
||||
limit, cursor, err := pageParams(r, "message", chatID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user