diff --git a/internal/next/next.go b/internal/next/next.go index 3fdee5c..d817ddf 100644 --- a/internal/next/next.go +++ b/internal/next/next.go @@ -87,8 +87,14 @@ func SignalsWithoutPages(d *db.DB, now time.Time) ([]Suggestion, error) { continue } id := strconv.FormatInt(r.Signal.ID, 10) - why := fmt.Sprintf("In 90 days, %s and %s, from outreach on this signal, and no landing page to send them to.", - count(replied, "reply", "replies"), count(interested, "interested person", "interested people")) + got := count(replied, "reply", "replies") + switch { + case replied == 0: + got = count(interested, "interested person", "interested people") + case interested > 0: + got += " and " + count(interested, "interested person", "interested people") + } + why := fmt.Sprintf("In 90 days, %s from outreach on this signal, and no landing page to send them to.", got) out = append(out, Suggestion{ Key: "signal-page:" + id, Kind: "Landing page", Title: r.Signal.Name, Why: why, Score: perReply * float64(replied+interested), @@ -225,11 +231,26 @@ func replyTexts(d *db.DB) ([]string, error) { if _, after, ok := strings.Cut(t, "”: "); ok { t = after } - out = append(out, t) + out = append(out, ownWords(t)) } return out, rows.Err() } +// quoteStart marks where a reply stops being the person's own words: the +// message they're answering, quoted below, in the usual languages. +var quoteStart = regexp.MustCompile(`(?im)(^\s*>|^\s*-{2,}\s*(original message|ursprüngliche nachricht|message d'origine|ursprungligt meddelande)|(^|\s)(from|från|fra|de|von|van|da|od)\s*:\s|\bon .{3,80} wrote:|\bam .{3,80} schrieb|\ble .{3,80} a écrit)`) + +// signature drops "Sent from…" lines a phone or mail app adds. +var signature = regexp.MustCompile(`(?i)\b(sent|skickat|sendt|envoyé|gesendet|enviado|inviato|verzonden|wysłane)\s+(from|från|fra|de|depuis|von|desde|da|met|z)\s+\S+(\s+\S+){0,4}`) + +// ownWords is the part of a reply the person wrote. +func ownWords(t string) string { + if loc := quoteStart.FindStringIndex(t); loc != nil { + t = t[:loc[0]] + } + return signature.ReplaceAllString(t, " ") +} + // wellKnown are apps people often name: the ones a business leaves, and // open-source ones it might run instead. var wellKnown = []string{ diff --git a/internal/next/next_test.go b/internal/next/next_test.go index fa2ba47..602122b 100644 --- a/internal/next/next_test.go +++ b/internal/next/next_test.go @@ -77,3 +77,14 @@ func TestAppsInReplies(t *testing.T) { t.Error("hasPage") } } + +func TestOwnWords(t *testing.T) { + in := "Hej, vi använder Planning Center. Mvh Roland Skickat från Outlook för Android Från: Janae Skickat: tisdag We host Nextcloud for you" + got := ownWords(in) + if strings.Contains(got, "Outlook") || strings.Contains(got, "Nextcloud") || !strings.Contains(got, "Planning Center") { + t.Errorf("own words: %q", got) + } + if got := ownWords("Sounds good.\n\nOn Mon, 3 Nov 2026 at 10:00, David wrote:\n> We run FreeScout"); strings.Contains(got, "FreeScout") { + t.Errorf("quoted: %q", got) + } +} diff --git a/internal/web/next.go b/internal/web/next.go new file mode 100644 index 0000000..9b51703 --- /dev/null +++ b/internal/web/next.go @@ -0,0 +1,154 @@ +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") +} diff --git a/internal/web/seo_test.go b/internal/web/seo_test.go index e743286..8bb50d6 100644 --- a/internal/web/seo_test.go +++ b/internal/web/seo_test.go @@ -5,12 +5,15 @@ import ( "net/http" "net/http/httptest" "net/url" + "strconv" "strings" "testing" + "time" "supermarket/internal/oauth" "supermarket/internal/profile" "supermarket/internal/secrets" + "supermarket/internal/signals" ) func TestSearchConsole(t *testing.T) { @@ -140,3 +143,31 @@ func TestSignInHostsAllowedAsFormTargets(t *testing.T) { } } } + +func TestWhatToWriteNext(t *testing.T) { + s, h := newTestServer(t, nil) + p := &profile.Profile{Name: "Acme", Websites: []profile.Website{{URL: "https://acme.example", SearchConsoleProperty: "sc-domain:acme.example"}}} + if err := profile.Save(s.DB, p); err != nil { + t.Fatal(err) + } + sig := &signals.Signal{Name: "Leaving M365", Status: "testing"} + signals.Save(s.DB, sig) + signals.Record(s.DB, signals.Event{SignalID: sig.ID, Stage: signals.StageReplied, Channel: "email", Source: "manual", Subject: "contact:1", ExternalID: "r1", At: time.Now().AddDate(0, 0, -2)}) + + body := do(h, http.MethodGet, "/seo/next", nil, nil).Body.String() + if !strings.Contains(body, "Leaving M365") || !strings.Contains(body, "kind=landing&signal=") || !strings.Contains(body, "Ranked by impressions for now") { + t.Fatalf("next: %s", body) + } + rec := postForm(h, "/seo/next/dismiss?site=0", url.Values{"key": {"signal-page:" + strconv.FormatInt(sig.ID, 10)}}) + if rec.Code != http.StatusSeeOther { + t.Fatalf("dismiss: %d", rec.Code) + } + if body := do(h, http.MethodGet, "/seo/next", nil, nil).Body.String(); strings.Contains(body, "Leaving M365") { + t.Error("a dismissed suggestion still shows") + } + // The New pages form takes the suggestion's kind and signal. + if body := do(h, http.MethodGet, "/sites/pages?kind=landing&signal="+strconv.FormatInt(sig.ID, 10), nil, nil).Body.String(); strings.Contains(body, " +{{else}} +

Nothing to suggest yet: sync Search Console, run an audit, and let the funnel bring replies in.

+{{end}} +{{with .More}}

And {{.}} more, below these. Not now hides one for 90 days.

{{end}} +{{end}} +{{end}}{{end}} diff --git a/internal/web/templates/seo-report.html b/internal/web/templates/seo-report.html index 9d45ed9..c296337 100644 --- a/internal/web/templates/seo-report.html +++ b/internal/web/templates/seo-report.html @@ -37,6 +37,7 @@

{{pagePath .Page}}
{{.Why}}

{{else}}

Nothing is waiting: no change has been proposed.

{{end}} {{if .Waiting}}

Say yes or no to each on Changes: a yes opens a pull request.

{{end}} +

Everything worth writing, ranked, is on What to write next.

{{with .Candidates}}

Worth a proposal

{{range .}}

{{pagePath .Page}}
{{.Why}}

{{end}}