128 lines
3.6 KiB
Go
128 lines
3.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
"messenger/src/backend/services"
|
|
)
|
|
|
|
type createMessageRequest struct {
|
|
Text *string `json:"text"`
|
|
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
|
}
|
|
|
|
func messageChatID(path string) (string, bool) {
|
|
const prefix = "/api/v1/chats/"
|
|
const suffix = "/messages"
|
|
if !strings.HasPrefix(path, prefix) || !strings.HasSuffix(path, suffix) {
|
|
return "", false
|
|
}
|
|
chatID := strings.TrimSuffix(strings.TrimPrefix(path, prefix), suffix)
|
|
if chatID == "" || strings.Contains(chatID, "/") {
|
|
return "", false
|
|
}
|
|
return chatID, true
|
|
}
|
|
|
|
func createMessageHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
requestID := newRequestID()
|
|
w.Header().Set("X-Request-Id", requestID)
|
|
if r.Method != http.MethodPost {
|
|
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
|
return
|
|
}
|
|
userID, ok := requestUserID(w, r, requestID)
|
|
if !ok {
|
|
return
|
|
}
|
|
chatID, ok := messageChatID(r.URL.Path)
|
|
if !ok {
|
|
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
|
return
|
|
}
|
|
if _, err := uuid.Parse(chatID); err != nil {
|
|
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
|
return
|
|
}
|
|
chat, err := chats.Get(r.Context(), userID, chatID)
|
|
if err != nil {
|
|
writeServiceError(w, err, requestID)
|
|
return
|
|
}
|
|
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
|
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
|
return
|
|
}
|
|
var request createMessageRequest
|
|
if err := decodeJSONBody(r, &request); err != nil {
|
|
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
|
return
|
|
}
|
|
ids := []string{}
|
|
if request.Text == nil {
|
|
writeError(w, 400, "invalid_request", "text must be a string", requestID)
|
|
return
|
|
}
|
|
if len(request.AttachmentIDs) > 0 {
|
|
if err := json.Unmarshal(request.AttachmentIDs, &ids); err != nil || ids == nil {
|
|
writeError(w, 400, "invalid_request", "attachment_ids must be an array of UUIDs", requestID)
|
|
return
|
|
}
|
|
}
|
|
keys := r.Header.Values("Idempotency-Key")
|
|
if len(keys) != 1 {
|
|
writeError(w, 400, "invalid_request", "exactly one Idempotency-Key is required", requestID)
|
|
return
|
|
}
|
|
message, err := messages.CreateIdempotent(r.Context(), userID, chat, *request.Text, keys[0], ids)
|
|
if err != nil {
|
|
writeServiceError(w, err, requestID)
|
|
return
|
|
}
|
|
writeJSON(w, 201, message)
|
|
}
|
|
}
|
|
|
|
func listMessagesHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
requestID := newRequestID()
|
|
w.Header().Set("X-Request-Id", requestID)
|
|
if r.Method != http.MethodGet {
|
|
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
|
return
|
|
}
|
|
userID, ok := requestUserID(w, r, requestID)
|
|
if !ok {
|
|
return
|
|
}
|
|
chatID, ok := messageChatID(r.URL.Path)
|
|
if !ok {
|
|
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
|
return
|
|
}
|
|
if _, err := uuid.Parse(chatID); err != nil {
|
|
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
|
return
|
|
}
|
|
chat, err := chats.Get(r.Context(), userID, chatID)
|
|
if err != nil {
|
|
writeServiceError(w, err, requestID)
|
|
return
|
|
}
|
|
limit, cursor, ok := pageParams(w, r, requestID)
|
|
if !ok {
|
|
return
|
|
}
|
|
page, err := messages.List(r.Context(), userID, chat, limit, cursor)
|
|
if err != nil {
|
|
writeServiceError(w, err, requestID)
|
|
return
|
|
}
|
|
writeJSON(w, 200, page)
|
|
}
|
|
}
|