Merged Sites pages are checked on the live site until they answer with their own words and title; removed ones until they're gone. The SEO loop, the page, the signal's page and Sites → Publishing check (or Check now); two hours without the page points at the deploy hook. Pages going live or away go to IndexNow. Sites → Publishing lists what's waiting, open pull requests and recent merges. Schema 24. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
298 lines
11 KiB
Go
298 lines
11 KiB
Go
package sites
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
// Page statuses.
|
|
const (
|
|
PageDraft = "draft" // composed; the user reviews it
|
|
PageWorking = "working" // being pushed
|
|
PagePushed = "pushed" // on its branch, with a preview; no pull request yet
|
|
PagePR = "pr" // a pull request is open
|
|
PageMerged = "merged" // merged: published
|
|
PageClosed = "closed" // the pull request was closed without merging
|
|
PageFailed = "failed" // the last push failed; Error says why
|
|
PageRemoving = "removing" // a pull request taking it off the site is open
|
|
PageRemoved = "removed" // that pull request was merged: it's off the site
|
|
)
|
|
|
|
// Kinds of page, with what each is for (shown to the user and the AI).
|
|
var Kinds = []struct{ ID, Label, Guide string }{
|
|
{"landing", "Landing page",
|
|
"A landing page for one audience or campaign. One clear promise in the hero; the problem in the reader's words; how we solve it; proof we actually have; the objections answered (an FAQ); one call to action, repeated at the end. Short and focused: no tour of everything we do."},
|
|
{"product", "Product page",
|
|
"A page for one product: what it is and who it's for, its benefits, how it works, what it costs or where to see prices, an FAQ, and a call to action."},
|
|
{"alternative", "Alternative to a competitor",
|
|
"A \"<competitor> alternative\" page, for people searching for one. Fair and factual: why people look for an alternative, how we differ, a comparison (a table where the site has one), switching over, an FAQ, a call to action. Say only what you're given about the competitor; where a fact isn't known, leave it out."},
|
|
{"comparison", "Comparison with a competitor",
|
|
"A side-by-side \"<us> vs <competitor>\" page: a short verdict, a comparison table, who each one is best for (honestly, including when the competitor fits better), an FAQ, a call to action. Say only what you're given about the competitor."},
|
|
{"blog", "Blog post",
|
|
"A blog post that teaches something useful to our customers, mostly text sections, ending with one call to action."},
|
|
}
|
|
|
|
// KindLabel is a kind's name for people.
|
|
func KindLabel(id string) string {
|
|
for _, k := range Kinds {
|
|
if k.ID == id {
|
|
return k.Label
|
|
}
|
|
}
|
|
return id
|
|
}
|
|
|
|
func kindGuide(id string) (string, bool) {
|
|
for _, k := range Kinds {
|
|
if k.ID == id {
|
|
return k.Guide, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// Source is what a page was built from.
|
|
type Source struct {
|
|
Product string `json:"product,omitempty"` // a product's name, from the profile
|
|
Competitor int64 `json:"competitor,omitempty"` // an Inventory entry
|
|
Signal int64 `json:"signal,omitempty"` // a buying signal
|
|
Brief int64 `json:"brief,omitempty"` // an SEO brief
|
|
VariantOf int64 `json:"variant_of,omitempty"` // the page this one is a variant of
|
|
Variant string `json:"variant,omitempty"` // "B", "C"…; the first page is "A"
|
|
}
|
|
|
|
// SitePage is a page built from the site's sections.
|
|
type SitePage struct {
|
|
ID int64
|
|
Site string
|
|
Repo string
|
|
Kind string
|
|
Source Source
|
|
Notes string
|
|
Path string
|
|
Title string
|
|
Description string
|
|
Sections string // YAML: the front matter's sections list
|
|
Needs []string
|
|
Warnings []string
|
|
Claims []string
|
|
Status string
|
|
Branch string
|
|
File string
|
|
PRNumber int64
|
|
PRURL string
|
|
Note string
|
|
Error string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
MergedAt time.Time
|
|
|
|
RemovalBranch string
|
|
RemovalPRNumber int64
|
|
RemovalPRURL string
|
|
|
|
LiveAt time.Time // seen on the live site after merging
|
|
GoneAt time.Time // seen gone after its removal was merged
|
|
CheckedAt time.Time // the live site was last checked
|
|
LiveNote string // what that check found
|
|
}
|
|
|
|
// Live says whether the page is on the published site.
|
|
func (p *SitePage) Live() bool { return p.Status == PageMerged || p.Status == PageRemoving }
|
|
|
|
// Root is the page variants are made from: itself, or the page it's a
|
|
// variant of.
|
|
func (p *SitePage) Root() int64 {
|
|
if p.Source.VariantOf != 0 {
|
|
return p.Source.VariantOf
|
|
}
|
|
return p.ID
|
|
}
|
|
|
|
// Editable says whether the user can still change the page here.
|
|
func (p *SitePage) Editable() bool {
|
|
return p.Status == PageDraft || p.Status == PagePushed || p.Status == PagePR || p.Status == PageFailed
|
|
}
|
|
|
|
const pageCols = `id, site, repo, kind, source, notes, path, title, description, sections, needs, warnings, claims,
|
|
status, branch, file, pr_number, pr_url, note, error, created_at, updated_at, merged_at,
|
|
removal_branch, removal_pr_number, removal_pr_url, live_at, gone_at, checked_at, live_note`
|
|
|
|
func scanPages(rows *sql.Rows) ([]SitePage, error) {
|
|
defer rows.Close()
|
|
var out []SitePage
|
|
for rows.Next() {
|
|
var p SitePage
|
|
var src, needs, warns, claims, created, updated string
|
|
var merged, live, gone, checked sql.NullString
|
|
if err := rows.Scan(&p.ID, &p.Site, &p.Repo, &p.Kind, &src, &p.Notes, &p.Path, &p.Title, &p.Description, &p.Sections,
|
|
&needs, &warns, &claims, &p.Status, &p.Branch, &p.File, &p.PRNumber, &p.PRURL, &p.Note, &p.Error,
|
|
&created, &updated, &merged, &p.RemovalBranch, &p.RemovalPRNumber, &p.RemovalPRURL,
|
|
&live, &gone, &checked, &p.LiveNote); err != nil {
|
|
return nil, err
|
|
}
|
|
_ = json.Unmarshal([]byte(src), &p.Source)
|
|
_ = json.Unmarshal([]byte(needs), &p.Needs)
|
|
_ = json.Unmarshal([]byte(warns), &p.Warnings)
|
|
_ = json.Unmarshal([]byte(claims), &p.Claims)
|
|
p.CreatedAt, _ = time.Parse(time.RFC3339, created)
|
|
p.UpdatedAt, _ = time.Parse(time.RFC3339, updated)
|
|
for _, t := range []struct {
|
|
v sql.NullString
|
|
to *time.Time
|
|
}{{merged, &p.MergedAt}, {live, &p.LiveAt}, {gone, &p.GoneAt}, {checked, &p.CheckedAt}} {
|
|
if t.v.Valid {
|
|
*t.to, _ = time.Parse(time.RFC3339, t.v.String)
|
|
}
|
|
}
|
|
out = append(out, p)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func jsonText(v any) string {
|
|
raw, _ := json.Marshal(v)
|
|
if string(raw) == "null" {
|
|
return "[]"
|
|
}
|
|
return string(raw)
|
|
}
|
|
|
|
// GetPage reads one page.
|
|
func GetPage(d *db.DB, id int64) (*SitePage, error) {
|
|
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE id = ?`, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ps, err := scanPages(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(ps) == 0 {
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
return &ps[0], nil
|
|
}
|
|
|
|
// Pages lists a website's pages, newest first.
|
|
func Pages(d *db.DB, site string) ([]SitePage, error) {
|
|
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE site = ? ORDER BY created_at DESC, id DESC`, site)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return scanPages(rows)
|
|
}
|
|
|
|
// SignalPages lists a buying signal's pages, on every site, oldest first
|
|
// (so variants follow the page they're made from).
|
|
func SignalPages(d *db.DB, signal int64) ([]SitePage, error) {
|
|
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE json_extract(source, '$.signal') = ? ORDER BY id`, signal)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return scanPages(rows)
|
|
}
|
|
|
|
// LivePageCounts is how many pages each signal has on the published sites.
|
|
func LivePageCounts(d *db.DB) (map[int64]int, error) {
|
|
rows, err := d.Query(`SELECT json_extract(source, '$.signal'), count(*) FROM site_pages
|
|
WHERE status IN (?, ?) AND json_extract(source, '$.signal') IS NOT NULL GROUP BY 1`, PageMerged, PageRemoving)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := map[int64]int{}
|
|
for rows.Next() {
|
|
var sig int64
|
|
var n int
|
|
if err := rows.Scan(&sig, &n); err != nil {
|
|
return nil, err
|
|
}
|
|
out[sig] = n
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// PagesIn lists pages in the given statuses, for every site.
|
|
func PagesIn(d *db.DB, statuses ...string) ([]SitePage, error) {
|
|
var all []SitePage
|
|
for _, st := range statuses {
|
|
rows, err := d.Query(`SELECT `+pageCols+` FROM site_pages WHERE status = ? ORDER BY id`, st)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ps, err := scanPages(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
all = append(all, ps...)
|
|
}
|
|
return all, nil
|
|
}
|
|
|
|
// SavePage stores a page, new (ID 0) or not.
|
|
func SavePage(d *db.DB, p *SitePage) error {
|
|
now := time.Now().UTC()
|
|
p.UpdatedAt = now
|
|
ts := func(t time.Time) any {
|
|
if t.IsZero() {
|
|
return nil
|
|
}
|
|
return t.UTC().Format(time.RFC3339)
|
|
}
|
|
merged := ts(p.MergedAt)
|
|
src, _ := json.Marshal(p.Source)
|
|
if p.ID == 0 {
|
|
if p.CreatedAt.IsZero() {
|
|
p.CreatedAt = now
|
|
}
|
|
if p.Status == "" {
|
|
p.Status = PageDraft
|
|
}
|
|
res, err := d.Exec(`INSERT INTO site_pages (site, repo, kind, source, notes, path, title, description, sections, needs, warnings,
|
|
claims, status, branch, file, pr_number, pr_url, note, error, created_at, updated_at, merged_at,
|
|
removal_branch, removal_pr_number, removal_pr_url, live_at, gone_at, checked_at, live_note)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
p.Site, p.Repo, p.Kind, string(src), p.Notes, p.Path, p.Title, p.Description, p.Sections, jsonText(p.Needs), jsonText(p.Warnings),
|
|
jsonText(p.Claims), p.Status, p.Branch, p.File, p.PRNumber, p.PRURL, p.Note, p.Error,
|
|
p.CreatedAt.UTC().Format(time.RFC3339), now.Format(time.RFC3339), merged,
|
|
p.RemovalBranch, p.RemovalPRNumber, p.RemovalPRURL, ts(p.LiveAt), ts(p.GoneAt), ts(p.CheckedAt), p.LiveNote)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.ID, err = res.LastInsertId()
|
|
return err
|
|
}
|
|
res, err := d.Exec(`UPDATE site_pages SET site = ?, repo = ?, kind = ?, source = ?, notes = ?, path = ?, title = ?, description = ?,
|
|
sections = ?, needs = ?, warnings = ?, claims = ?, status = ?, branch = ?, file = ?, pr_number = ?, pr_url = ?, note = ?, error = ?,
|
|
updated_at = ?, merged_at = ?, removal_branch = ?, removal_pr_number = ?, removal_pr_url = ?,
|
|
live_at = ?, gone_at = ?, checked_at = ?, live_note = ? WHERE id = ?`,
|
|
p.Site, p.Repo, p.Kind, string(src), p.Notes, p.Path, p.Title, p.Description, p.Sections, jsonText(p.Needs), jsonText(p.Warnings),
|
|
jsonText(p.Claims), p.Status, p.Branch, p.File, p.PRNumber, p.PRURL, p.Note, p.Error, now.Format(time.RFC3339), merged,
|
|
p.RemovalBranch, p.RemovalPRNumber, p.RemovalPRURL, ts(p.LiveAt), ts(p.GoneAt), ts(p.CheckedAt), p.LiveNote, p.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n, _ := res.RowsAffected(); n == 0 {
|
|
return sql.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeletePage forgets a page. Its branch and pull request, if any, stay on
|
|
// Gitea for the user to close.
|
|
func DeletePage(d *db.DB, id int64) error {
|
|
_, err := d.Exec(`DELETE FROM site_pages WHERE id = ?`, id)
|
|
return err
|
|
}
|
|
|
|
// RecoverPages puts pages left half-pushed (the app stopped) back to failed.
|
|
func RecoverPages(d *db.DB) error {
|
|
_, err := d.Exec(`UPDATE site_pages SET status = ?, error = ? WHERE status = ?`, PageFailed,
|
|
"Supermarket stopped while pushing this page; push it again.", PageWorking)
|
|
return err
|
|
}
|