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