A community based topic aggregation platform built on atproto
1package community
2
3import (
4 "Coves/internal/core/communities"
5 "encoding/json"
6 "net/http"
7)
8
9// UpdateHandler handles community updates
10type UpdateHandler struct {
11 service communities.Service
12}
13
14// NewUpdateHandler creates a new update handler
15func NewUpdateHandler(service communities.Service) *UpdateHandler {
16 return &UpdateHandler{
17 service: service,
18 }
19}
20
21// HandleUpdate updates an existing community
22// POST /xrpc/social.coves.community.update
23// Body matches UpdateCommunityRequest
24func (h *UpdateHandler) HandleUpdate(w http.ResponseWriter, r *http.Request) {
25 if r.Method != http.MethodPost {
26 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
27 return
28 }
29
30 // Parse request body
31 var req communities.UpdateCommunityRequest
32 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
33 writeError(w, http.StatusBadRequest, "InvalidRequest", "Invalid request body")
34 return
35 }
36
37 // Validate required fields
38 if req.CommunityDID == "" {
39 writeError(w, http.StatusBadRequest, "InvalidRequest", "communityDid is required")
40 return
41 }
42
43 // TODO(Communities-OAuth): Extract authenticated user DID from request context
44 // This MUST be replaced with OAuth middleware before production deployment
45 // Expected implementation:
46 // userDID := r.Context().Value("authenticated_user_did").(string)
47 // req.UpdatedByDID = userDID
48 // For now, we require client to send it (INSECURE - allows impersonation)
49 if req.UpdatedByDID == "" {
50 writeError(w, http.StatusUnauthorized, "AuthRequired", "Authentication required")
51 return
52 }
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}