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// CreateHandler handles community creation
10type CreateHandler struct {
11 service communities.Service
12}
13
14// NewCreateHandler creates a new create handler
15func NewCreateHandler(service communities.Service) *CreateHandler {
16 return &CreateHandler{
17 service: service,
18 }
19}
20
21// HandleCreate creates a new community
22// POST /xrpc/social.coves.community.create
23// Body matches CreateCommunityRequest
24func (h *CreateHandler) HandleCreate(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.CreateCommunityRequest
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 // TODO(Communities-OAuth): Extract authenticated user DID from request context
38 // This MUST be replaced with OAuth middleware before production deployment
39 // Expected implementation:
40 // userDID := r.Context().Value("authenticated_user_did").(string)
41 // req.CreatedByDID = userDID
42 // For now, we require client to send it (INSECURE - allows impersonation)
43 if req.CreatedByDID == "" {
44 writeError(w, http.StatusUnauthorized, "AuthRequired", "Authentication required")
45 return
46 }
47
48 if req.HostedByDID == "" {
49 writeError(w, http.StatusBadRequest, "InvalidRequest", "hostedByDid is required")
50 return
51 }
52
53 // Create community via service (write-forward to PDS)
54 community, err := h.service.CreateCommunity(r.Context(), req)
55 if err != nil {
56 handleServiceError(w, err)
57 return
58 }
59
60 // Return success response matching lexicon output
61 response := map[string]interface{}{
62 "uri": community.RecordURI,
63 "cid": community.RecordCID,
64 "did": community.DID,
65 "handle": community.Handle,
66 }
67
68 w.Header().Set("Content-Type", "application/json")
69 w.WriteHeader(http.StatusOK)
70 if err := json.NewEncoder(w).Encode(response); err != nil {
71 // Log encoding errors but don't return error response (headers already sent)
72 // This follows Go's standard practice for HTTP handlers
73 _ = err
74 }
75}