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
+98 -57
View File
@@ -1,3 +1,6 @@
// Package fetcher gathers recent content about an inventory entry from every
// source it lists and that has credentials: its X account, X posts with its
// hashtags, its YouTube channel and its RSS feeds.
package fetcher
import (
@@ -7,90 +10,128 @@ import (
"sync"
"time"
"supermarket/internal/api/rss"
"supermarket/internal/api/twitter"
"supermarket/internal/api/youtube"
"supermarket/internal/inventory"
"supermarket/internal/models"
)
// Fetcher holds the source clients; a nil client skips that source.
type Fetcher struct {
twitter *twitter.Client
youtube *youtube.Client
Twitter *twitter.Client
YouTube *youtube.Client
RSS *rss.Client
}
// Result is what a fetch found, and what failed or was skipped.
type Result struct {
Items []models.ContentItem
Errors []string
Items []models.ContentItem
Errors []string
Skipped []string // sources the entry lists but that have no credentials
}
func New(tw *twitter.Client, yt *youtube.Client) *Fetcher {
return &Fetcher{twitter: tw, youtube: yt}
}
// Fetch gets the entry's content published after since, from all its sources
// at once. One source failing doesn't stop the others.
func (f *Fetcher) Fetch(ctx context.Context, e *inventory.Entry, since time.Time) Result {
type job struct {
label string
run func() ([]models.ContentItem, error)
}
var jobs []job
var res Result
if e.TwitterHandle != "" {
if f.Twitter != nil {
jobs = append(jobs, job{"X @" + e.TwitterHandle, func() ([]models.ContentItem, error) {
return f.Twitter.GetUserTweets(ctx, e.TwitterHandle, since, e.Name)
}})
} else {
res.Skipped = append(res.Skipped, "X @"+e.TwitterHandle+" (no X bearer token)")
}
}
if len(e.Hashtags) > 0 {
if f.Twitter != nil {
q := twitter.BuildSearchQuery(e.Hashtags, e.TwitterHandle)
jobs = append(jobs, job{"X search", func() ([]models.ContentItem, error) {
return f.Twitter.SearchTweets(ctx, q, since, e.Name)
}})
} else {
res.Skipped = append(res.Skipped, "X hashtag search (no X bearer token)")
}
}
if e.YouTubeChannelID != "" {
if f.YouTube != nil {
jobs = append(jobs, job{"YouTube", func() ([]models.ContentItem, error) {
return f.YouTube.GetChannelVideos(ctx, e.YouTubeChannelID, since, e.Name)
}})
} else {
res.Skipped = append(res.Skipped, "YouTube (no YouTube API key)")
}
}
if f.RSS != nil {
for _, feed := range e.RSS {
feed := feed
jobs = append(jobs, job{"RSS " + feed, func() ([]models.ContentItem, error) {
return f.RSS.Fetch(ctx, feed, since, e.Name)
}})
}
}
// FetchForApp concurrently fetches content from all configured sources.
func (f *Fetcher) FetchForApp(ctx context.Context, app *models.Application, since time.Time) Result {
type batch struct {
label string
items []models.ContentItem
err error
label string
}
jobs := make(chan func() batch, 8)
results := make(chan batch, 8)
// Worker pool
results := make(chan batch, len(jobs))
sem := make(chan struct{}, 4)
var wg sync.WaitGroup
for i := 0; i < 4; i++ {
for _, j := range jobs {
wg.Add(1)
go func() {
go func(j job) {
defer wg.Done()
for job := range jobs {
results <- job()
}
}()
sem <- struct{}{}
defer func() { <-sem }()
items, err := j.run()
results <- batch{j.label, items, err}
}(j)
}
wg.Wait()
close(results)
// Enqueue fetches
enqueue := func(label string, fn func() ([]models.ContentItem, error)) {
jobs <- func() batch {
items, err := fn()
return batch{items: items, err: err, label: label}
}
}
if f.twitter != nil && app.TwitterHandle != "" {
enqueue("X (account @"+app.TwitterHandle+")", func() ([]models.ContentItem, error) {
return f.twitter.GetUserTweets(ctx, app.TwitterHandle, since, app.Name)
})
}
if f.youtube != nil && app.YouTubeChannelID != "" {
enqueue("YouTube (channel)", func() ([]models.ContentItem, error) {
return f.youtube.GetChannelVideos(ctx, app.YouTubeChannelID, since, app.Name)
})
}
close(jobs)
go func() {
wg.Wait()
close(results)
}()
var result Result
for b := range results {
if b.err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", b.label, b.err))
} else {
result.Items = append(result.Items, b.items...)
res.Errors = append(res.Errors, fmt.Sprintf("%s: %v", b.label, b.err))
continue
}
res.Items = append(res.Items, b.items...)
}
// Deduplicate by ID and sort newest first
result.Items = dedup(result.Items)
sort.Slice(result.Items, func(i, j int) bool {
return result.Items[i].PublishedAt.After(result.Items[j].PublishedAt)
sort.Strings(res.Errors)
res.Items = dedup(res.Items)
sort.Slice(res.Items, func(i, j int) bool {
return res.Items[i].PublishedAt.After(res.Items[j].PublishedAt)
})
return res
}
return result
// Sources says what Fetch would read for e, for the page.
func Sources(e *inventory.Entry) []string {
var out []string
if e.TwitterHandle != "" {
out = append(out, "X @"+e.TwitterHandle)
}
if len(e.Hashtags) > 0 {
out = append(out, "X hashtags")
}
if e.YouTubeChannelID != "" {
out = append(out, "YouTube")
}
if n := len(e.RSS); n == 1 {
out = append(out, "1 RSS feed")
} else if n > 1 {
out = append(out, fmt.Sprintf("%d RSS feeds", n))
}
return out
}
func dedup(items []models.ContentItem) []models.ContentItem {