|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "github.com/h0tak88r/AutoAR/internal/bbcatalog" |
| 10 | + "github.com/h0tak88r/AutoAR/internal/db" |
| 11 | +) |
| 12 | + |
| 13 | +// lookupResult is one row in a program-lookup response. |
| 14 | +type lookupResult struct { |
| 15 | + Source string `json:"source"` // h1, bc, it, ywh, as93 |
| 16 | + Company string `json:"company"` |
| 17 | + Handle string `json:"handle"` |
| 18 | + URL string `json:"url"` |
| 19 | + Rewards string `json:"rewards"` |
| 20 | + SafeHarbor string `json:"safe_harbor"` |
| 21 | + OffersBounty bool `json:"offers_bounty"` |
| 22 | + MatchType string `json:"match_type"` // "keyword" or "domain" |
| 23 | + MatchedDomain string `json:"matched_domain,omitempty"` |
| 24 | + InScope *bool `json:"in_scope,omitempty"` // set for domain matches |
| 25 | +} |
| 26 | + |
| 27 | +// looksLikeDomain heuristically decides whether a query is a domain vs a keyword. |
| 28 | +func looksLikeDomain(q string) bool { |
| 29 | + q = strings.TrimSpace(q) |
| 30 | + if q == "" || strings.ContainsAny(q, " \t") { |
| 31 | + return false |
| 32 | + } |
| 33 | + q = strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(q, "http://"), "https://"), "/") |
| 34 | + return strings.Contains(q, ".") && !strings.Contains(q, " ") |
| 35 | +} |
| 36 | + |
| 37 | +// GET /api/assets/program-lookup?q=<keyword-or-domain>&mode=auto|keyword|domain |
| 38 | +func apiProgramLookup(c *gin.Context) { |
| 39 | + q := strings.TrimSpace(c.Query("q")) |
| 40 | + if q == "" { |
| 41 | + c.JSON(http.StatusBadRequest, gin.H{"error": "q is required"}) |
| 42 | + return |
| 43 | + } |
| 44 | + mode := strings.ToLower(c.DefaultQuery("mode", "auto")) |
| 45 | + const limit = 100 |
| 46 | + |
| 47 | + var out []lookupResult |
| 48 | + seen := map[string]bool{} // dedupe by source|handle|matchType |
| 49 | + |
| 50 | + doKeyword := mode == "keyword" || mode == "auto" |
| 51 | + doDomain := mode == "domain" || (mode == "auto" && looksLikeDomain(q)) |
| 52 | + |
| 53 | + if doDomain { |
| 54 | + // Strip scheme/path so a URL paste still matches. |
| 55 | + dq := strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(q, "http://"), "https://"), "/") |
| 56 | + if i := strings.IndexByte(dq, '/'); i >= 0 { |
| 57 | + dq = dq[:i] |
| 58 | + } |
| 59 | + matches, err := db.SearchCatalogByDomain(dq, limit) |
| 60 | + if err == nil { |
| 61 | + for _, m := range matches { |
| 62 | + k := "d|" + m.Source + "|" + m.Handle |
| 63 | + if seen[k] { |
| 64 | + continue |
| 65 | + } |
| 66 | + seen[k] = true |
| 67 | + in := m.InScope |
| 68 | + out = append(out, lookupResult{ |
| 69 | + Source: m.Source, Company: m.Company, Handle: m.Handle, URL: m.URL, |
| 70 | + Rewards: m.Rewards, SafeHarbor: m.SafeHarbor, OffersBounty: m.OffersBounty, |
| 71 | + MatchType: "domain", MatchedDomain: m.MatchedDomain, InScope: &in, |
| 72 | + }) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if doKeyword { |
| 78 | + progs, err := db.SearchCatalogByKeyword(q, limit) |
| 79 | + if err == nil { |
| 80 | + for _, p := range progs { |
| 81 | + k := "k|" + p.Source + "|" + p.Handle |
| 82 | + if seen[k] { |
| 83 | + continue |
| 84 | + } |
| 85 | + seen[k] = true |
| 86 | + out = append(out, lookupResult{ |
| 87 | + Source: p.Source, Company: p.Company, Handle: p.Handle, URL: p.URL, |
| 88 | + Rewards: p.Rewards, SafeHarbor: p.SafeHarbor, OffersBounty: p.OffersBounty, |
| 89 | + MatchType: "keyword", |
| 90 | + }) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + c.JSON(http.StatusOK, gin.H{ |
| 96 | + "query": q, |
| 97 | + "is_domain": looksLikeDomain(q), |
| 98 | + "results": out, |
| 99 | + "total": len(out), |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +// POST /api/assets/program-sync — rebuild the catalog in the background. |
| 104 | +func apiProgramSync(c *gin.Context) { |
| 105 | + if bbcatalog.SyncAsync() { |
| 106 | + c.JSON(http.StatusAccepted, gin.H{"status": "started", "message": "Catalog sync started in the background"}) |
| 107 | + return |
| 108 | + } |
| 109 | + c.JSON(http.StatusConflict, gin.H{"status": "running", "message": "A sync is already in progress"}) |
| 110 | +} |
| 111 | + |
| 112 | +// GET /api/assets/catalog-status — counts + last sync summary. |
| 113 | +func apiCatalogStatus(c *gin.Context) { |
| 114 | + progs, doms, _ := db.CatalogCounts() |
| 115 | + running, last, at := bbcatalog.SyncStatus() |
| 116 | + var lastAt string |
| 117 | + if !at.IsZero() { |
| 118 | + lastAt = at.Format(time.RFC3339) |
| 119 | + } |
| 120 | + c.JSON(http.StatusOK, gin.H{ |
| 121 | + "programs": progs, |
| 122 | + "domains": doms, |
| 123 | + "sync_running": running, |
| 124 | + "last_sync_at": lastAt, |
| 125 | + "last_sync": last, |
| 126 | + }) |
| 127 | +} |
0 commit comments