Schema 6 adds prospect_companies, prospect_contacts (merged by domain and email, each with its source, detail and date) and suppressions. Prospects -> Review lists New, Accepted and Set aside: accepting a company queues an EspoCRM Account, a person a Lead at their company, with provenance and why they fit in the description; the page warns about records already in the CRM as of the last sync, and shows each accepted prospect's CRM progress. Prospects can be added by hand. The suppression list (emails and domains) is checked on add and again before queueing, and suppressing sets matching new prospects aside. Search finds prospects too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
5.8 KiB
Go
200 lines
5.8 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"supermarket/internal/content"
|
|
"supermarket/internal/inventory"
|
|
"supermarket/internal/profile"
|
|
"supermarket/internal/prospects"
|
|
)
|
|
|
|
// 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"},
|
|
}
|
|
|
|
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)})
|
|
}
|
|
|
|
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
|
|
}
|