Files
supermarket/internal/web/search.go
davidandClaude Opus 5.5 b025a97e4a Phase 2 part 3: buying signals, the funnel and the scorecard
Schema 7 adds signals (hypothesis, how to spot it, angle, status, utm
slug), funnel_events (stage, channel, subject, campaign, source, deduped
by external ID) and outside_campaigns. Prospects -> Signals is the
scorecard: each signal's funnel from found to paid, people once per
stage, with revenue and outside spend, by period and channel. Each
signal's page shows its funnel by channel, its campaigns and its latest
steps. Prospects carry a signal (filter, change, "found" recorded) and
outcomes can be recorded by hand.

Settings -> Lead sources holds origami.chat and explee.com API keys. Every
6 hours (or Import now) Supermarket reads their campaigns, numbers and
spend, contacted people, replies and hot leads, read-only: interested
people and repliers become prospects, unsubscribes are suppressed, and
each campaign is assigned to a signal. The explee.com client follows its
OpenAPI schema; origami.chat's responses are undocumented, so its client
reads fields loosely and its Test shows the real response.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 18:06:58 -07:00

212 lines
6.3 KiB
Go

package web
import (
"net/http"
"strconv"
"strings"
"supermarket/internal/content"
"supermarket/internal/inventory"
"supermarket/internal/profile"
"supermarket/internal/prospects"
"supermarket/internal/signals"
)
// prospectBadge is a prospect's status as search shows it.
func prospectBadge(status string) string {
if status == prospects.StatusRejected {
return "set aside"
}
return status
}
// hit is one search result.
type hit struct {
Title, Detail, Link string
Badge string // e.g. a relationship or a draft's status
}
// hitGroup is a kind of result, in the order shown.
type hitGroup struct {
Name string
Hits []hit
}
const searchLimit = 25
// settingsPages are searched by name and by what they hold, so "anthropic
// key" or "reddit" find the right tab.
var settingsPages = []struct {
hit
Words string
}{
{hit{"Company profile", "Settings · your company, products, websites, voice and targets", "/settings/profile", ""},
"profile company products websites voice tone audience keywords feeds subreddits accounts"},
{hit{"Social accounts", "Settings · X, LinkedIn, Reddit and YouTube keys and sign-ins", "/settings/accounts", ""},
"social accounts credentials keys x twitter linkedin reddit youtube sign in connect oauth bearer token"},
{hit{"AI", "Settings · provider, model, API keys and the Hermes key", "/settings/ai", ""},
"ai provider model anthropic claude openai ollama lm studio api key hermes effort"},
{hit{"Core & Hermes", "Settings · the Federated Core, its services and the Hermes bridge", "/settings/core", ""},
"core hermes federated mcp hub bridge espocrm nextcloud plausible gitea"},
{hit{"Lead sources", "Settings · origami.chat and explee.com API keys", "/settings/sources", ""},
"lead sources origami explee api key import campaigns prospecting outreach"},
}
func (s *Server) searchPage(w http.ResponseWriter, r *http.Request) {
q := strings.TrimSpace(r.URL.Query().Get("q"))
var groups []hitGroup
total := 0
if q != "" {
var err error
groups, err = s.search(q)
if err != nil {
s.serverError(w, err)
return
}
for _, g := range groups {
total += len(g.Hits)
}
}
s.render(w, r, "search", page{Title: "Search", Nav: "search", Query: q, Data: map[string]any{
"Q": q,
"Groups": groups,
"Total": total,
"Limit": searchLimit,
}})
}
// search looks for q in the inventory, fetched items, posts and settings.
// Every word must appear (case-insensitively), in any order.
func (s *Server) search(q string) ([]hitGroup, error) {
words := strings.Fields(strings.ToLower(q))
matches := func(texts ...string) bool {
all := strings.ToLower(strings.Join(texts, "\n"))
for _, w := range words {
if !strings.Contains(all, w) {
return false
}
}
return true
}
entries, err := inventory.List(s.DB, "")
if err != nil {
return nil, err
}
ids := map[string]int64{} // inventory name (lower case) → ID, for links
var inv []hit
for _, e := range entries {
ids[strings.ToLower(e.Name)] = e.ID
if matches(e.Name, e.Description, e.TwitterHandle, e.TalkingPoints, strings.Join(e.Websites, " "),
strings.Join(e.Hashtags, " "), strings.Join(e.Keywords, " "), strings.Join(e.Subreddits, " ")) {
inv = append(inv, hit{Title: e.Name, Detail: e.Description,
Link: "/inventory/" + strconv.FormatInt(e.ID, 10), Badge: e.Relationship})
}
}
entryLink := func(name string) string {
if strings.EqualFold(name, content.OwnName) {
return "/content/own"
}
if id, ok := ids[strings.ToLower(name)]; ok {
return "/content/" + strconv.FormatInt(id, 10)
}
return "/content"
}
items, err := content.SearchItems(s.DB, words, searchLimit)
if err != nil {
return nil, err
}
var found []hit
for _, it := range items {
name := it.AppName
if strings.EqualFold(name, content.OwnName) {
name = "your own work"
}
title := it.Title
if title == "" {
title = it.Body
}
found = append(found, hit{Title: title, Detail: string(it.Source) + " · " + name + " · " + it.PublishedAt.Local().Format("2 Jan 2006"),
Link: entryLink(it.AppName), Badge: ifElse(it.Dismissed, "dismissed", "")})
}
drafts, err := content.SearchDrafts(s.DB, words, searchLimit)
if err != nil {
return nil, err
}
var posts []hit
for _, d := range drafts {
link := "/posts"
switch d.Status {
case content.StatusScheduled:
link = "/content/scheduled"
case content.StatusPublished:
link = "/content/published"
}
text := d.Content
if d.Title != "" {
text = d.Title
}
detail := d.Where()
if d.InventoryName != "" {
detail += " · " + d.InventoryName
}
posts = append(posts, hit{Title: text, Detail: detail, Link: link, Badge: d.Status})
}
companies, people, err := prospects.Search(s.DB, words, searchLimit)
if err != nil {
return nil, err
}
var prospectHits []hit
for _, c := range companies {
detail := "Company"
if c.Domain != "" {
detail += " · " + c.Domain
}
prospectHits = append(prospectHits, hit{Title: c.Name, Detail: detail, Link: "/prospects?status=" + c.Status, Badge: prospectBadge(c.Status)})
}
for _, p := range people {
detail := "Person"
if p.Title != "" {
detail += " · " + p.Title
}
if p.Email != "" {
detail += " · " + p.Email
}
prospectHits = append(prospectHits, hit{Title: p.Name(), Detail: detail, Link: "/prospects?status=" + p.Status, Badge: prospectBadge(p.Status)})
}
sigs, err := signals.Search(s.DB, words, searchLimit)
if err != nil {
return nil, err
}
for _, sg := range sigs {
prospectHits = append([]hit{{Title: sg.Name, Detail: "Buying signal · " + sg.Hypothesis,
Link: "/signals/" + strconv.FormatInt(sg.ID, 10), Badge: sg.Status}}, prospectHits...)
}
var settings []hit
if p, err := profile.Load(s.DB); err == nil && p.IsSet() {
for _, pr := range p.Products {
if matches(pr.Name, pr.Description) {
settings = append(settings, hit{Title: pr.Name, Detail: "Product, in the company profile", Link: "/settings/profile", Badge: "product"})
}
}
}
for _, sp := range settingsPages {
if matches(sp.Title, sp.Detail, sp.Words) {
settings = append(settings, sp.hit)
}
}
var groups []hitGroup
for _, g := range []hitGroup{{"Posts", posts}, {"Fetched items", found}, {"Inventory", inv}, {"Prospects", prospectHits}, {"Settings", settings}} {
if len(g.Hits) > 0 {
groups = append(groups, g)
}
}
return groups, nil
}