One table maps every address to an area (a module, Settings, anything signed in, or public); each route is checked against it, and the app won't start with a route missing from it. Changing needs edit on the module. For members who may only view, forms that would change something are shown disabled and htmx buttons hidden. The menu, the Overview, Search and the SEO emails follow each person's modules; Settings is for admins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
213 lines
6.5 KiB
Go
213 lines
6.5 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, viewerOf(r))
|
|
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, v *viewer) ([]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: "/market/" + 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
|
|
area := map[string]string{"Posts": "content", "Fetched items": "content", "Market": "market", "Prospects": "prospects", "Settings": areaAdmin}
|
|
for _, g := range []hitGroup{{"Posts", posts}, {"Fetched items", found}, {"Market", inv}, {"Prospects", prospectHits}, {"Settings", settings}} {
|
|
if len(g.Hits) > 0 && v.can(area[g.Name], false) {
|
|
groups = append(groups, g)
|
|
}
|
|
}
|
|
return groups, nil
|
|
}
|