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
+51
View File
@@ -61,6 +61,57 @@ CREATE TABLE bridge_checkins (
mcphub_tools TEXT NOT NULL DEFAULT '[]',
note TEXT NOT NULL DEFAULT ''
);
`,
// 2: the Content module. Fetched items are kept so browsing doesn't fetch
// again, and posts become per-channel drafts. Statuses and channels are
// checked in Go, not by CHECK constraints, so later phases can add some
// without rebuilding the table.
`
CREATE TABLE content_items (
id TEXT PRIMARY KEY, -- the source's own ID (tweet, video), or rss:<hash>
inventory_name TEXT NOT NULL COLLATE NOCASE,
source TEXT NOT NULL,
author TEXT NOT NULL DEFAULT '',
author_handle TEXT NOT NULL DEFAULT '',
title TEXT NOT NULL DEFAULT '',
body TEXT NOT NULL DEFAULT '',
url TEXT NOT NULL DEFAULT '',
published_at TEXT NOT NULL,
fetched_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
used_at TEXT -- when a draft was written from it
);
CREATE INDEX content_items_by_entry ON content_items (inventory_name, published_at);
-- One row per channel: a generation that targets X and LinkedIn makes two
-- drafts with the same batch. Each is edited, published and retried on its
-- own, so a failure on one channel never re-posts to the other.
CREATE TABLE drafts (
id INTEGER PRIMARY KEY,
batch TEXT NOT NULL,
inventory_name TEXT NOT NULL DEFAULT '',
kind TEXT NOT NULL, -- partner, competitor, watch (later: product)
channel TEXT NOT NULL, -- x, linkedin (later: reddit)
content TEXT NOT NULL,
source_url TEXT NOT NULL DEFAULT '',
item_ids TEXT NOT NULL DEFAULT '[]',
status TEXT NOT NULL DEFAULT 'draft', -- draft, publishing, published, failed, discarded
error TEXT NOT NULL DEFAULT '',
post_url TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
published_at TEXT
);
CREATE INDEX drafts_by_status ON drafts (status, created_at);
-- Queued posts become drafts for the platforms they haven't reached yet.
INSERT INTO drafts (batch, inventory_name, kind, channel, content, source_url, created_at, updated_at)
SELECT p.id, p.inventory_name,
COALESCE((SELECT relationship FROM inventory WHERE name = p.inventory_name), 'partner'),
c.channel, p.content, p.source_url, p.generated_at, p.generated_at
FROM posts p, (SELECT 'x' AS channel UNION ALL SELECT 'linkedin') c
WHERE NOT EXISTS (SELECT 1 FROM json_each(p.posted_to) WHERE value = c.channel)
ORDER BY p.generated_at, c.channel;
DROP TABLE posts;
`,
}
+68
View File
@@ -0,0 +1,68 @@
package db
import (
"database/sql"
"path/filepath"
"testing"
)
// A database from Phase 0 (schema 1) keeps its queued posts as drafts for
// the platforms they hadn't reached.
func TestMigratePostsToDrafts(t *testing.T) {
path := filepath.Join(t.TempDir(), "app.db")
old, err := sql.Open("sqlite", "file:"+path)
if err != nil {
t.Fatal(err)
}
for _, q := range []string{
migrations[0],
`PRAGMA user_version = 1`,
`INSERT INTO inventory (name, relationship, data) VALUES ('Jitsi', 'competitor', '{}')`,
`INSERT INTO posts (id, inventory_name, content, source_url, generated_at, posted_to) VALUES
('a', 'Jitsi', 'Posted to X only', 'https://x.com/1', '2026-11-01T10:00:00Z', '["x"]'),
('b', 'Nextcloud', 'Not posted', '', '2026-11-02T10:00:00Z', '[]'),
('c', 'Nextcloud', 'Posted everywhere', '', '2026-11-03T10:00:00Z', '["x","linkedin"]')`,
} {
if _, err := old.Exec(q); err != nil {
t.Fatal(err)
}
}
old.Close()
d, err := Open(path)
if err != nil {
t.Fatal(err)
}
defer d.Close()
rows, err := d.Query(`SELECT batch, inventory_name, kind, channel, content, status FROM drafts ORDER BY batch, channel`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
var got []string
for rows.Next() {
var batch, name, kind, channel, content, status string
if err := rows.Scan(&batch, &name, &kind, &channel, &content, &status); err != nil {
t.Fatal(err)
}
got = append(got, batch+" "+name+" "+kind+" "+channel+" "+status)
}
want := []string{
"a Jitsi competitor linkedin draft",
"b Nextcloud partner linkedin draft",
"b Nextcloud partner x draft",
}
if len(got) != len(want) {
t.Fatalf("drafts = %q, want %q", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Errorf("draft %d = %q, want %q", i, got[i], want[i])
}
}
var n int
if err := d.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name = 'posts'`).Scan(&n); err != nil || n != 0 {
t.Errorf("posts table still there (%d, %v)", n, err)
}
}