Files
supermarket/internal/web/search.go
T
davidandClaude Opus 5.5 4b2d791909 LinkedIn publishing works again; rename Inbox to Posts
LinkedIn:
- The API version was fixed at 202501, retired by LinkedIn (426
  NONEXISTENT_VERSION). It's now the month before last, stepping back a
  month (up to four) if LinkedIn says a version isn't active.
- The versioned Posts API needs a title for a link's article card (422).
  Supermarket reads the linked page's title and description and sends
  them, falling back to the page's address as the title.
Tested for real: a post with a link went out to the member's profile.

Inbox is now Posts: the menu, page, messages and docs; the page moves to
/posts, and /inbox redirects there.

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

168 lines
4.8 KiB
Go

package web
import (
"net/http"
"strconv"
"strings"
"supermarket/internal/content"
"supermarket/internal/inventory"
"supermarket/internal/profile"
)
// 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})
}
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}, {"Settings", settings}} {
if len(g.Hits) > 0 {
groups = append(groups, g)
}
}
return groups, nil
}