A community based topic aggregation platform built on atproto
1package community
2
3import (
4 "encoding/json"
5 "net/http"
6
7 "Coves/internal/api/middleware"
8 "Coves/internal/core/communities"
9)
10
11// UpdateHandler handles community updates
12type UpdateHandler struct {
13 service communities.Service
14}
15
16// NewUpdateHandler creates a new update handler
17func NewUpdateHandler(service communities.Service) *UpdateHandler {
18 return &UpdateHandler{
19 service: service,
20 }
21}
22
23// HandleUpdate updates an existing community
24// POST /xrpc/social.coves.community.update
25// Body matches UpdateCommunityRequest
26func (h *UpdateHandler) HandleUpdate(w http.ResponseWriter, r *http.Request) {
27 if r.Method != http.MethodPost {
28 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
29 return
30 }
31
32 // Parse request body
33 var req communities.UpdateCommunityRequest
34 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
35 writeError(w, http.StatusBadRequest, "InvalidRequest", "Invalid request body")
36 return
37 }
38
39 // Validate required fields
40 if req.CommunityDID == "" {
41 writeError(w, http.StatusBadRequest, "InvalidRequest", "communityDid is required")
42 return
43 }
44
45 // Extract authenticated user DID from request context (injected by auth middleware)
46 userDID := middleware.GetUserDID(r)
47 if userDID == "" {
48 writeError(w, http.StatusUnauthorized, "AuthRequired", "Authentication required")
49 return
50 }
51
52 // Set the authenticated user as the updater
53 req.UpdatedByDID = userDID
54
55 // Update community via service (write-forward to PDS)
56 community, err := h.service.UpdateCommunity(r.Context(), req)
57 if err != nil {
58 handleServiceError(w, err)
59 return
60 }
61
62 // Return success response matching lexicon output
63 response := map[string]interface{}{
64 "uri": community.RecordURI,
65 "cid": community.RecordCID,
66 "did": community.DID,
67 "handle": community.Handle,
68 }
69
70 w.Header().Set("Content-Type", "application/json")
71 w.WriteHeader(http.StatusOK)
72 if err := json.NewEncoder(w).Encode(response); err != nil {
73 // Log encoding errors but don't return error response (headers already sent)
74 // This follows Go's standard practice for HTTP handlers
75 _ = err
76 }
77}