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 "strconv"
8)
9
10// ListHandler handles listing communities
11type ListHandler struct {
12 service communities.Service
13}
14
15// NewListHandler creates a new list handler
16func NewListHandler(service communities.Service) *ListHandler {
17 return &ListHandler{
18 service: service,
19 }
20}
21
22// HandleList lists communities with filters
23// GET /xrpc/social.coves.community.list?limit={n}&cursor={offset}&visibility={public|unlisted}&sortBy={created_at|member_count}
24func (h *ListHandler) HandleList(w http.ResponseWriter, r *http.Request) {
25 if r.Method != http.MethodGet {
26 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
27 return
28 }
29
30 // Parse query parameters
31 query := r.URL.Query()
32
33 limit := 50
34 if limitStr := query.Get("limit"); limitStr != "" {
35 if l, err := strconv.Atoi(limitStr); err == nil && l > 0 {
36 limit = l
37 }
38 }
39
40 offset := 0
41 if cursorStr := query.Get("cursor"); cursorStr != "" {
42 if o, err := strconv.Atoi(cursorStr); err == nil && o >= 0 {
43 offset = o
44 }
45 }
46
47 req := communities.ListCommunitiesRequest{
48 Limit: limit,
49 Offset: offset,
50 Visibility: query.Get("visibility"),
51 HostedBy: query.Get("hostedBy"),
52 SortBy: query.Get("sortBy"),
53 SortOrder: query.Get("sortOrder"),
54 }
55
56 // Get communities from AppView DB
57 results, total, err := h.service.ListCommunities(r.Context(), req)
58 if err != nil {
59 handleServiceError(w, err)
60 return
61 }
62
63 // Build response
64 response := map[string]interface{}{
65 "communities": results,
66 "cursor": offset + len(results),
67 "total": total,
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}