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:
@@ -0,0 +1,72 @@
|
||||
package content
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/db"
|
||||
)
|
||||
|
||||
// Publisher posts to one channel and returns the post's URL.
|
||||
type Publisher interface {
|
||||
Publish(ctx context.Context, text, sourceURL string) (string, error)
|
||||
}
|
||||
|
||||
// Publishers are the channels that can post now, by channel ID.
|
||||
type Publishers map[string]Publisher
|
||||
|
||||
// ErrBusy means the draft is already being published.
|
||||
var ErrBusy = errors.New("this draft is already being published, or was published")
|
||||
|
||||
// Publish posts one draft. It claims the draft first, so a second click or a
|
||||
// second tab can't post it twice. A failure is recorded on the draft, which
|
||||
// can then be edited and published again.
|
||||
func Publish(ctx context.Context, d *db.DB, pubs Publishers, id int64) (*Draft, error) {
|
||||
dr, err := GetDraft(d, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !dr.Editable() {
|
||||
return dr, ErrBusy
|
||||
}
|
||||
pub := pubs[dr.Channel]
|
||||
if pub == nil {
|
||||
return dr, fmt.Errorf("%s isn't connected: sign in under Settings → Credentials", dr.ChannelName())
|
||||
}
|
||||
if dr.TooLong() {
|
||||
withLink := ""
|
||||
if dr.Channel == ChannelX && dr.SourceURL != "" {
|
||||
withLink = " including the link"
|
||||
}
|
||||
return dr, fmt.Errorf("the post is %d characters%s; %s allows %d. Shorten it first",
|
||||
dr.Length(), withLink, dr.ChannelName(), dr.Limit())
|
||||
}
|
||||
|
||||
res, err := d.Exec(`UPDATE drafts SET status = ?, error = '', updated_at = ? WHERE id = ? AND status IN (?, ?)`,
|
||||
StatusPublishing, formatTime(time.Now()), id, StatusDraft, StatusFailed)
|
||||
if err := oneRow(res, err); err != nil {
|
||||
if errors.Is(err, ErrNotEditable) {
|
||||
return dr, ErrBusy
|
||||
}
|
||||
return dr, err
|
||||
}
|
||||
|
||||
postURL, pubErr := pub.Publish(ctx, dr.Content, dr.SourceURL)
|
||||
now := formatTime(time.Now())
|
||||
if pubErr != nil {
|
||||
if _, err := d.Exec(`UPDATE drafts SET status = ?, error = ?, updated_at = ? WHERE id = ?`,
|
||||
StatusFailed, pubErr.Error(), now, id); err != nil {
|
||||
return dr, fmt.Errorf("%v (and couldn't record it: %v)", pubErr, err)
|
||||
}
|
||||
dr.Status, dr.Error = StatusFailed, pubErr.Error()
|
||||
return dr, pubErr
|
||||
}
|
||||
if _, err := d.Exec(`UPDATE drafts SET status = ?, post_url = ?, published_at = ?, updated_at = ? WHERE id = ?`,
|
||||
StatusPublished, postURL, now, now, id); err != nil {
|
||||
// It went out: say so, so the user doesn't post it again.
|
||||
return dr, fmt.Errorf("published to %s (%s), but couldn't record it: %w", dr.ChannelName(), postURL, err)
|
||||
}
|
||||
return GetDraft(d, id)
|
||||
}
|
||||
Reference in New Issue
Block a user