Files
supermarket/internal/web/next.go
T
davidandClaude Opus 5.5 5705a9b58b Phase 7 part 4: SEO → What to write next
One list per website of everything worth writing: new wording for pages
(Push pages and the audit's title and description issues), landing
pages for signals that get replies, pages for apps people name in their
replies (their own words, without quoted mail or signatures), and
keywords or briefs that stalled. Ranked by the sign-ups each is expected
to bring, at most 10 shown, each with its reason in numbers and the
button that starts its flow; Not now hides one for 90 days. The SEO
module still opens on the Morning check; the Monday report links here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 07:17:42 -07:00

155 lines
4.5 KiB
Go

package web
import (
"net/http"
"slices"
"strconv"
"strings"
"time"
"supermarket/internal/next"
"supermarket/internal/profile"
"supermarket/internal/seo"
)
// What to write next (SEO → What to write next): SEO's suggestions and the
// funnel's in one list, ranked by the sign-ups they're expected to bring.
const (
maxNext = 10
nextDismissed = "next:dismissed" // suggestion key → when it was dismissed
dismissFor = 90 * 24 * time.Hour
)
// nextFor works out a website's list, without the dismissed suggestions;
// more is how many fell below the first maxNext.
func (s *Server) nextFor(cur seoSite, now time.Time) (list []next.Suggestion, more int, err error) {
var all []next.Suggestion
site := strconv.Itoa(cur.Index)
// SEO: the pages that would gain most from new wording (Push pages and
// the audit's title and description issues), by expected sign-ups.
if cur.Property != "" {
conv := s.seoConv(cur.URL)
cands, err := seo.Candidates(s.DB, auditKey(cur), cur.Property, conv, now)
if err != nil {
return nil, 0, err
}
for _, c := range cands {
what, kind := "", "New title and description"
switch {
case c.Title && !c.Desc:
what, kind = "title", "New title"
case c.Desc && !c.Title:
what, kind = "description", "New description"
}
score := c.Expected
if conv == nil || score == 0 {
// Without conversion data, by how often Google shows it.
score = float64(c.Traffic.Impressions) / 20000
}
all = append(all, next.Suggestion{
Key: "change:" + seo.PathKey(c.Page), Kind: kind, Title: pagePath(c.Page), Why: c.Why, Score: score,
Action: next.Action{Label: "Propose new wording", Method: "POST", URL: "/seo/changes/page?site=" + site, Busy: "Writing…",
Fields: map[string]string{"page": c.Page, "why": c.Why, "what": what}},
})
}
}
// The funnel: signals without a landing page, apps people name.
sigs, err := next.SignalsWithoutPages(s.DB, now)
if err != nil {
return nil, 0, err
}
all = append(all, sigs...)
if p, _ := profile.Load(s.DB); p != nil {
var paths, titles []string
if a, _ := seo.LatestAudit(s.DB, cur.URL); a != nil {
for _, pg := range a.Pages {
paths = append(paths, seo.PathKey(pg.URL))
titles = append(titles, pg.Title)
}
}
if len(paths) > 0 { // without the site's pages, any app would look missing
ms, err := next.AppsInReplies(s.DB, p, paths, titles)
if err != nil {
return nil, 0, err
}
all = append(all, next.MentionSuggestions(ms, p)...)
}
}
stalled, err := next.Stalled(s.DB, cur.URL, cur.Index, now)
if err != nil {
return nil, 0, err
}
all = append(all, stalled...)
// Not the dismissed ones; the most promising first.
var dismissed map[string]time.Time
_, _ = s.DB.GetJSON(nextDismissed, &dismissed)
seen := map[string]bool{}
for _, sg := range all {
if at, ok := dismissed[sg.Key]; (ok && now.Sub(at) < dismissFor) || seen[sg.Key] {
continue
}
seen[sg.Key] = true
list = append(list, sg)
}
slices.SortStableFunc(list, func(a, b next.Suggestion) int {
switch {
case a.Score > b.Score:
return -1
case a.Score < b.Score:
return 1
}
return strings.Compare(a.Key, b.Key)
})
if len(list) > maxNext {
more = len(list) - maxNext
list = list[:maxNext]
}
return list, more, nil
}
func (s *Server) seoNextPage(w http.ResponseWriter, r *http.Request) {
data, cur := s.seoBase(r)
data["Path"] = "/seo/next"
if cur != nil {
list, more, err := s.nextFor(*cur, time.Now())
if err != nil {
s.serverError(w, err)
return
}
data["Next"], data["More"] = list, more
data["HasConv"] = s.seoConv(cur.URL) != nil
}
s.render(w, r, "seo-next", page{Title: "What to write next", Nav: "seo/next", Data: data})
}
// seoNextDismiss hides a suggestion for 90 days.
func (s *Server) seoNextDismiss(w http.ResponseWriter, r *http.Request) {
key := strings.TrimSpace(r.PostFormValue("key"))
if key == "" || len(key) > 300 {
http.Error(w, "no suggestion", http.StatusBadRequest)
return
}
var dismissed map[string]time.Time
_, _ = s.DB.GetJSON(nextDismissed, &dismissed)
if dismissed == nil {
dismissed = map[string]time.Time{}
}
now := time.Now()
for k, at := range dismissed { // forget the ones that came back anyway
if now.Sub(at) > dismissFor {
delete(dismissed, k)
}
}
dismissed[key] = now
if err := s.DB.PutJSON(nextDismissed, dismissed); err != nil {
s.serverError(w, err)
return
}
idx, _ := strconv.Atoi(r.URL.Query().Get("site"))
redirect(w, r, "/seo/next?site="+strconv.Itoa(idx), "next-dismissed")
}