Phase 1, part 1: fetch, write and publish posts from the web app

Brings back posting, which Phase 0 removed with the terminal interface.

- Content page: per inventory entry, fetch its X account, X hashtag
  search, YouTube channel and RSS/Atom feeds (new), then select, dismiss
  or restore the fetched items, which are kept in the database.
- Writing: one AI call returns a draft per channel (X and LinkedIn),
  prompted from the company profile, the entry's relationship, talking
  points and mention policy, and each channel's rules. Fetched text is
  quoted as untrusted. An over-long X draft gets one request to shorten.
- Drafts page: edit with a live length counter that counts like X, then
  publish each draft on its own. A draft is claimed before posting so it
  can't go out twice; failures are kept on the draft for a retry; X
  tokens are refreshed and saved.
- Migration 2 adds content_items and drafts, and turns queued posts into
  drafts for the platforms they hadn't reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 12:58:24 -07:00
co-authored by Claude Opus 5.5
parent abf410d097
commit 13bf18facc
29 changed files with 2484 additions and 95 deletions
+27 -8
View File
@@ -262,18 +262,37 @@ func importPosts(d *db.DB, path string) (int, error) {
if ok, err := readJSON(path, &ps); !ok || err != nil {
return 0, err
}
// Each queued post becomes a draft for every platform it hasn't reached.
n := 0
for _, p := range ps.Posts {
posted, _ := json.Marshal(p.PostedTo)
if p.PostedTo == nil {
posted = []byte("[]")
posted := map[string]bool{}
for _, ch := range p.PostedTo {
posted[ch] = true
}
if _, err := d.Exec(`INSERT OR IGNORE INTO posts (id, inventory_name, content, source_url, generated_at, posted_to)
VALUES (?, ?, ?, ?, ?, ?)`, p.ID, p.AppName, p.Content, p.SourceURL,
p.GeneratedAt.UTC().Format(time.RFC3339), string(posted)); err != nil {
return n, err
at := p.GeneratedAt.UTC().Format(time.RFC3339)
added := false
for _, ch := range []string{"x", "linkedin"} {
if posted[ch] {
continue
}
// The batch is the old post's ID, so a re-run adds nothing twice.
var exists int
if err := d.QueryRow(`SELECT count(*) FROM drafts WHERE batch = ? AND channel = ?`, p.ID, ch).Scan(&exists); err != nil {
return n, err
}
if exists > 0 {
continue
}
if _, err := d.Exec(`INSERT INTO drafts (batch, inventory_name, kind, channel, content, source_url, created_at, updated_at)
VALUES (?, ?, COALESCE((SELECT relationship FROM inventory WHERE name = ?), 'partner'), ?, ?, ?, ?, ?)`,
p.ID, p.AppName, p.AppName, ch, p.Content, p.SourceURL, at, at); err != nil {
return n, err
}
added = true
}
if added {
n++
}
n++
}
return n, nil
}
+4 -2
View File
@@ -101,8 +101,10 @@ func TestImport(t *testing.T) {
d.QueryRow(q).Scan(&n)
return n
}
if count(`SELECT count(*) FROM posts WHERE posted_to = '["x"]'`) != 1 {
t.Error("posts not imported")
// Posted to X already, so only the LinkedIn draft is left.
if count(`SELECT count(*) FROM drafts WHERE batch = '1' AND channel = 'linkedin'`) != 1 ||
count(`SELECT count(*) FROM drafts WHERE batch = '1' AND channel = 'x'`) != 0 {
t.Error("posts not imported as drafts")
}
if count(`SELECT count(*) FROM dismissed`) != 2 {
t.Error("dismissed not imported")