A community based topic aggregation platform built on atproto
1package community
2
3import (
4 "encoding/json"
5 "net/http"
6 "strconv"
7
8 "Coves/internal/core/communities"
9)
10
11// ListHandler handles listing communities
12type ListHandler struct {
13 service communities.Service
14}
15
16// NewListHandler creates a new list handler
17func NewListHandler(service communities.Service) *ListHandler {
18 return &ListHandler{
19 service: service,
20 }
21}
22
23// HandleList lists communities with filters
24// GET /xrpc/social.coves.community.list?limit={n}&cursor={offset}&visibility={public|unlisted}&sortBy={created_at|member_count}
25func (h *ListHandler) HandleList(w http.ResponseWriter, r *http.Request) {
26 if r.Method != http.MethodGet {
27 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
28 return
29 }
30
31 // Parse query parameters
32 query := r.URL.Query()
33
34 limit := 50
35 if limitStr := query.Get("limit"); limitStr != "" {
36 if l, err := strconv.Atoi(limitStr); err == nil && l > 0 {
37 limit = l
38 }
39 }
40
41 offset := 0
42 if cursorStr := query.Get("cursor"); cursorStr != "" {
43 if o, err := strconv.Atoi(cursorStr); err == nil && o >= 0 {
44 offset = o
45 }
46 }
47
48 req := communities.ListCommunitiesRequest{
49 Limit: limit,
50 Offset: offset,
51 Visibility: query.Get("visibility"),
52 HostedBy: query.Get("hostedBy"),
53 SortBy: query.Get("sortBy"),
54 SortOrder: query.Get("sortOrder"),
55 }
56
57 // Get communities from AppView DB
58 results, total, err := h.service.ListCommunities(r.Context(), req)
59 if err != nil {
60 handleServiceError(w, err)
61 return
62 }
63
64 // Build response
65 response := map[string]interface{}{
66 "communities": results,
67 "cursor": offset + len(results),
68 "total": total,
69 }
70
71 w.Header().Set("Content-Type", "application/json")
72 w.WriteHeader(http.StatusOK)
73 if err := json.NewEncoder(w).Encode(response); err != nil {
74 // Log encoding errors but don't return error response (headers already sent)
75 // This follows Go's standard practice for HTTP handlers
76 _ = err
77 }
78}