A community based topic aggregation platform built on atproto
at main 2.3 kB view raw
1package comments 2 3import ( 4 "Coves/internal/api/middleware" 5 "Coves/internal/core/comments" 6 "encoding/json" 7 "log" 8 "net/http" 9) 10 11// DeleteCommentHandler handles comment deletion requests 12type DeleteCommentHandler struct { 13 service comments.Service 14} 15 16// NewDeleteCommentHandler creates a new handler for deleting comments 17func NewDeleteCommentHandler(service comments.Service) *DeleteCommentHandler { 18 return &DeleteCommentHandler{ 19 service: service, 20 } 21} 22 23// DeleteCommentInput matches the lexicon input schema for social.coves.community.comment.delete 24type DeleteCommentInput struct { 25 URI string `json:"uri"` 26} 27 28// DeleteCommentOutput is empty per lexicon specification 29type DeleteCommentOutput struct{} 30 31// HandleDelete handles comment deletion requests 32// POST /xrpc/social.coves.community.comment.delete 33// 34// Request body: { "uri": "at://..." } 35// Response: {} 36func (h *DeleteCommentHandler) HandleDelete(w http.ResponseWriter, r *http.Request) { 37 // 1. Check method is POST 38 if r.Method != http.MethodPost { 39 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) 40 return 41 } 42 43 // 2. Limit request body size to prevent DoS attacks (100KB should be plenty for comments) 44 r.Body = http.MaxBytesReader(w, r.Body, 100*1024) 45 46 // 3. Parse JSON body into DeleteCommentInput 47 var input DeleteCommentInput 48 if err := json.NewDecoder(r.Body).Decode(&input); err != nil { 49 writeError(w, http.StatusBadRequest, "InvalidRequest", "Invalid request body") 50 return 51 } 52 53 // 4. Get OAuth session from context (injected by auth middleware) 54 session := middleware.GetOAuthSession(r) 55 if session == nil { 56 writeError(w, http.StatusUnauthorized, "AuthRequired", "Authentication required") 57 return 58 } 59 60 // 5. Convert input to DeleteCommentRequest 61 req := comments.DeleteCommentRequest{ 62 URI: input.URI, 63 } 64 65 // 6. Call service to delete comment 66 err := h.service.DeleteComment(r.Context(), session, req) 67 if err != nil { 68 handleServiceError(w, err) 69 return 70 } 71 72 // 7. Return empty JSON object per lexicon specification 73 output := DeleteCommentOutput{} 74 75 w.Header().Set("Content-Type", "application/json") 76 w.WriteHeader(http.StatusOK) 77 if err := json.NewEncoder(w).Encode(output); err != nil { 78 log.Printf("Failed to encode response: %v", err) 79 } 80}