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