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// SearchHandler handles community search 12type SearchHandler struct { 13 service communities.Service 14} 15 16// NewSearchHandler creates a new search handler 17func NewSearchHandler(service communities.Service) *SearchHandler { 18 return &SearchHandler{ 19 service: service, 20 } 21} 22 23// HandleSearch searches communities by name/description 24// GET /xrpc/social.coves.community.search?q={query}&limit={n}&cursor={offset} 25func (h *SearchHandler) HandleSearch(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 searchQuery := query.Get("q") 35 if searchQuery == "" { 36 writeError(w, http.StatusBadRequest, "InvalidRequest", "q parameter is required") 37 return 38 } 39 40 limit := 50 41 if limitStr := query.Get("limit"); limitStr != "" { 42 if l, err := strconv.Atoi(limitStr); err == nil && l > 0 { 43 limit = l 44 } 45 } 46 47 offset := 0 48 if cursorStr := query.Get("cursor"); cursorStr != "" { 49 if o, err := strconv.Atoi(cursorStr); err == nil && o >= 0 { 50 offset = o 51 } 52 } 53 54 req := communities.SearchCommunitiesRequest{ 55 Query: searchQuery, 56 Limit: limit, 57 Offset: offset, 58 Visibility: query.Get("visibility"), 59 } 60 61 // Search communities in AppView DB 62 results, total, err := h.service.SearchCommunities(r.Context(), req) 63 if err != nil { 64 handleServiceError(w, err) 65 return 66 } 67 68 // Build response 69 response := map[string]interface{}{ 70 "communities": results, 71 "cursor": offset + len(results), 72 "total": total, 73 } 74 75 w.Header().Set("Content-Type", "application/json") 76 w.WriteHeader(http.StatusOK) 77 if err := json.NewEncoder(w).Encode(response); err != nil { 78 // Log encoding errors but don't return error response (headers already sent) 79 // This follows Go's standard practice for HTTP handlers 80 _ = err 81 } 82}