Phase 10, part 7: posting accounts, and who may post to which
Social accounts become posting accounts: several per network, the company's and each person's own, with their tokens kept per account. The old sign-ins are adopted as the company's. Admins say who may post to which (Settings → Posting accounts); a draft can go out from one account or a copy to several, each recording who posted it and as what. Signing an account out, or removing its owner, holds the posts scheduled to it and tells whoever scheduled them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -132,13 +132,13 @@ func TestDraftsAndPublish(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
// LinkedIn isn't connected: refused, and nothing changes.
|
||||
pub := &fakePub{}
|
||||
if _, err := Publish(ctx, d, Publishers{ChannelX: pub}, nil, li.ID); err == nil || !strings.Contains(err.Error(), "isn't connected") {
|
||||
if _, err := Publish(ctx, d, ChannelPublishers{ChannelX: pub}, nil, li.ID); err == nil || !strings.Contains(err.Error(), "isn't connected") {
|
||||
t.Errorf("publishing without a connection: %v", err)
|
||||
}
|
||||
|
||||
// A failure is recorded; the draft can be fixed and published again.
|
||||
pub.err = errors.New("HTTP 503")
|
||||
if _, err := Publish(ctx, d, Publishers{ChannelX: pub}, nil, x.ID); err == nil {
|
||||
if _, err := Publish(ctx, d, ChannelPublishers{ChannelX: pub}, nil, x.ID); err == nil {
|
||||
t.Fatal("expected the failure")
|
||||
}
|
||||
got, _ := GetDraft(d, x.ID)
|
||||
@@ -149,7 +149,7 @@ func TestDraftsAndPublish(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pub.err = nil
|
||||
got, err = Publish(ctx, d, Publishers{ChannelX: pub}, nil, x.ID)
|
||||
got, err = Publish(ctx, d, ChannelPublishers{ChannelX: pub}, nil, x.ID)
|
||||
if err != nil || got.Status != StatusPublished || got.PostURL == "" || got.Error != "" {
|
||||
t.Fatalf("publish: %+v, %v", got, err)
|
||||
}
|
||||
@@ -157,7 +157,7 @@ func TestDraftsAndPublish(t *testing.T) {
|
||||
t.Errorf("published %q", last)
|
||||
}
|
||||
// Published once: a second click doesn't post again, and it can't be edited.
|
||||
if _, err := Publish(ctx, d, Publishers{ChannelX: pub}, nil, x.ID); !errors.Is(err, ErrBusy) {
|
||||
if _, err := Publish(ctx, d, ChannelPublishers{ChannelX: pub}, nil, x.ID); !errors.Is(err, ErrBusy) {
|
||||
t.Errorf("second publish: %v", err)
|
||||
}
|
||||
if err := UpdateDraft(d, x.ID, "again"); !errors.Is(err, ErrNotEditable) {
|
||||
@@ -170,7 +170,7 @@ func TestDraftsAndPublish(t *testing.T) {
|
||||
// Too long for X: refused before calling X.
|
||||
_, _ = CreateDrafts(d, []Draft{{Channel: ChannelX, Content: strings.Repeat("a", 270), SourceURL: "https://example.com"}})
|
||||
long, _ := ListDrafts(d, 1, StatusDraft)
|
||||
if _, err := Publish(ctx, d, Publishers{ChannelX: pub}, nil, long[0].ID); err == nil || !strings.Contains(err.Error(), "295 characters including the link") {
|
||||
if _, err := Publish(ctx, d, ChannelPublishers{ChannelX: pub}, nil, long[0].ID); err == nil || !strings.Contains(err.Error(), "295 characters including the link") {
|
||||
t.Errorf("over-long post: %v", err)
|
||||
}
|
||||
|
||||
@@ -314,7 +314,7 @@ func TestScheduleAndDue(t *testing.T) {
|
||||
// Publishing tags only the company's links, and stores the draft untagged.
|
||||
pub := &fakePub{}
|
||||
tags := utm.NewTagger([]string{"acme.example"})
|
||||
got, err := Publish(context.Background(), d, Publishers{ChannelLinkedIn: pub}, tags, soon.ID)
|
||||
got, err := Publish(context.Background(), d, ChannelPublishers{ChannelLinkedIn: pub}, tags, soon.ID)
|
||||
if err != nil || got.Status != StatusPublished {
|
||||
t.Fatalf("publish scheduled: %+v, %v", got, err)
|
||||
}
|
||||
@@ -405,7 +405,7 @@ func TestLinkedInAuthor(t *testing.T) {
|
||||
t.Error("an unknown author was taken")
|
||||
}
|
||||
pub := &fakePub{}
|
||||
if _, err := Publish(ctx, d, Publishers{ChannelLinkedIn: pub}, nil, li.ID); err != nil {
|
||||
if _, err := Publish(ctx, d, ChannelPublishers{ChannelLinkedIn: pub}, nil, li.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !pub.got.AsPage {
|
||||
@@ -419,7 +419,7 @@ func TestLinkedInAuthor(t *testing.T) {
|
||||
if err := SetLinkedInAs(d, dr.ID, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
Publish(ctx, d, Publishers{ChannelLinkedIn: pub}, nil, dr.ID)
|
||||
Publish(ctx, d, ChannelPublishers{ChannelLinkedIn: pub}, nil, dr.ID)
|
||||
}
|
||||
}
|
||||
if pub.got.AsPage {
|
||||
|
||||
@@ -80,7 +80,11 @@ type Draft struct {
|
||||
PublishAt time.Time // when a scheduled draft goes out
|
||||
SignalID int64 // the buying signal it was written for; 0: none
|
||||
SignalSlug string // that signal's campaign tag
|
||||
LinkedInAs string // LinkedIn: "" posts as you, AsPage as the company page
|
||||
LinkedInAs string // LinkedIn: "" posts as you, AsPage as the company page (before posting accounts)
|
||||
AccountID int64 // the posting account it goes out from; 0: the company's for its channel
|
||||
WrittenBy int64 // who wrote it (asked the AI for it); 0: unknown, or the local edition
|
||||
PostedBy int64 // who published or scheduled it
|
||||
PostedAs string // the account it went out as, once it did
|
||||
}
|
||||
|
||||
// AsPage is the LinkedIn author choice for the company page.
|
||||
@@ -193,9 +197,9 @@ func CreateDrafts(d *db.DB, drafts []Draft) (string, error) {
|
||||
if dr.Channel != ChannelLinkedIn {
|
||||
as = ""
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO drafts (batch, inventory_name, kind, channel, title, target, content, source_url, item_ids, signal_id, linkedin_as)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, batch, dr.InventoryName, dr.Kind, dr.Channel,
|
||||
strings.TrimSpace(dr.Title), dr.Target, strings.TrimSpace(dr.Content), dr.SourceURL, string(ids), sig, as); err != nil {
|
||||
if _, err := tx.Exec(`INSERT INTO drafts (batch, inventory_name, kind, channel, title, target, content, source_url, item_ids, signal_id, linkedin_as, account_id, written_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, batch, dr.InventoryName, dr.Kind, dr.Channel,
|
||||
strings.TrimSpace(dr.Title), dr.Target, strings.TrimSpace(dr.Content), dr.SourceURL, string(ids), sig, as, dr.AccountID, dr.WrittenBy); err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, id := range dr.ItemIDs {
|
||||
@@ -220,7 +224,8 @@ func nonNil(s []string) []string {
|
||||
|
||||
const draftColumns = `id, batch, inventory_name, kind, channel, content, source_url, item_ids, status, error,
|
||||
post_url, created_at, updated_at, COALESCE(published_at, ''), COALESCE(publish_at, ''), title, target,
|
||||
COALESCE(signal_id, 0), COALESCE((SELECT slug FROM signals WHERE signals.id = drafts.signal_id), ''), linkedin_as`
|
||||
COALESCE(signal_id, 0), COALESCE((SELECT slug FROM signals WHERE signals.id = drafts.signal_id), ''), linkedin_as,
|
||||
account_id, written_by, posted_by, posted_as`
|
||||
|
||||
func scanDrafts(rows *sql.Rows) ([]Draft, error) {
|
||||
defer rows.Close()
|
||||
@@ -230,7 +235,7 @@ func scanDrafts(rows *sql.Rows) ([]Draft, error) {
|
||||
var ids, created, updated, published, publishAt string
|
||||
if err := rows.Scan(&dr.ID, &dr.Batch, &dr.InventoryName, &dr.Kind, &dr.Channel, &dr.Content,
|
||||
&dr.SourceURL, &ids, &dr.Status, &dr.Error, &dr.PostURL, &created, &updated, &published, &publishAt, &dr.Title, &dr.Target,
|
||||
&dr.SignalID, &dr.SignalSlug, &dr.LinkedInAs); err != nil {
|
||||
&dr.SignalID, &dr.SignalSlug, &dr.LinkedInAs, &dr.AccountID, &dr.WrittenBy, &dr.PostedBy, &dr.PostedAs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = json.Unmarshal([]byte(ids), &dr.ItemIDs)
|
||||
@@ -417,3 +422,56 @@ func UpdateTitle(d *db.DB, id int64, title string) error {
|
||||
title, formatTime(time.Now()), id, ChannelReddit, StatusDraft, StatusFailed)
|
||||
return oneRow(res, err)
|
||||
}
|
||||
|
||||
// SetAccount chooses the posting account a draft goes out from (0: the
|
||||
// company's for its channel).
|
||||
func SetAccount(d *db.DB, id, account int64) error {
|
||||
res, err := d.Exec(`UPDATE drafts SET account_id = ?, updated_at = ? WHERE id = ? AND status IN (?, ?, ?)`,
|
||||
account, formatTime(time.Now()), id, StatusDraft, StatusFailed, StatusScheduled)
|
||||
return oneRow(res, err)
|
||||
}
|
||||
|
||||
// SetPostedBy records who published or scheduled a draft.
|
||||
func SetPostedBy(d *db.DB, id, user int64) error {
|
||||
_, err := d.Exec(`UPDATE drafts SET posted_by = ? WHERE id = ?`, user, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// Copy makes a copy of a draft for another posting account, in the same
|
||||
// batch, waiting in Posts; it returns the copy's ID.
|
||||
func Copy(d *db.DB, id, account int64) (int64, error) {
|
||||
dr, err := GetDraft(d, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
ids, _ := json.Marshal(nonNil(dr.ItemIDs))
|
||||
var sig any
|
||||
if dr.SignalID != 0 {
|
||||
sig = dr.SignalID
|
||||
}
|
||||
return d.Insert(`INSERT INTO drafts (batch, inventory_name, kind, channel, title, target, content, source_url, item_ids, signal_id, linkedin_as, account_id, written_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, dr.Batch, dr.InventoryName, dr.Kind, dr.Channel, dr.Title, dr.Target, dr.Content,
|
||||
dr.SourceURL, string(ids), sig, dr.LinkedInAs, account, dr.WrittenBy)
|
||||
}
|
||||
|
||||
// HoldForAccount sends the scheduled drafts going out from an account back
|
||||
// to Posts, with the reason: the account was signed out or removed. It
|
||||
// returns them, to tell their writers.
|
||||
func HoldForAccount(d *db.DB, account int64, reason string) ([]Draft, error) {
|
||||
rows, err := d.Query(`SELECT `+draftColumns+` FROM drafts WHERE account_id = ? AND status = ?`, account, StatusScheduled)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
held, err := scanDrafts(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = d.Exec(`UPDATE drafts SET status = ?, error = ?, account_id = 0, updated_at = ? WHERE account_id = ? AND status = ?`,
|
||||
StatusFailed, reason, formatTime(time.Now()), account, StatusScheduled)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Drafts waiting in Posts for it fall back to the company's account.
|
||||
_, err = d.Exec(`UPDATE drafts SET account_id = 0 WHERE account_id = ? AND status IN (?, ?)`, account, StatusDraft, StatusFailed)
|
||||
return held, err
|
||||
}
|
||||
|
||||
@@ -24,8 +24,24 @@ type Publisher interface {
|
||||
Publish(ctx context.Context, p Post) (string, error)
|
||||
}
|
||||
|
||||
// Publishers are the channels that can post now, by channel ID.
|
||||
type Publishers map[string]Publisher
|
||||
// Publishers find where a draft goes out: its publisher and the account's
|
||||
// name, or why it can't.
|
||||
type Publishers interface {
|
||||
For(dr *Draft) (Publisher, string, error)
|
||||
}
|
||||
|
||||
// ChannelPublishers is a publisher per channel: for tests, and anything
|
||||
// without posting accounts.
|
||||
type ChannelPublishers map[string]Publisher
|
||||
|
||||
// For is the draft's channel's publisher.
|
||||
func (c ChannelPublishers) For(dr *Draft) (Publisher, string, error) {
|
||||
p := c[dr.Channel]
|
||||
if p == nil {
|
||||
return nil, "", fmt.Errorf("%s isn't connected: sign in under Settings → Social accounts", dr.ChannelName())
|
||||
}
|
||||
return p, "", nil
|
||||
}
|
||||
|
||||
// ErrBusy means the draft is already being published.
|
||||
var ErrBusy = errors.New("this draft is already being published, or was published")
|
||||
@@ -44,9 +60,9 @@ func Publish(ctx context.Context, d *db.DB, pubs Publishers, tags *utm.Tagger, i
|
||||
if !dr.Editable() && dr.Status != StatusScheduled {
|
||||
return dr, ErrBusy
|
||||
}
|
||||
pub := pubs[dr.Channel]
|
||||
if pub == nil {
|
||||
return dr, fmt.Errorf("%s isn't connected: sign in under Settings → Social accounts", dr.ChannelName())
|
||||
pub, as, err := pubs.For(dr)
|
||||
if err != nil {
|
||||
return dr, err
|
||||
}
|
||||
if problem := dr.Problem(); problem != "" {
|
||||
return dr, fmt.Errorf("%s. Fix it first", problem)
|
||||
@@ -73,8 +89,8 @@ func Publish(ctx context.Context, d *db.DB, pubs Publishers, tags *utm.Tagger, i
|
||||
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 {
|
||||
if _, err := d.Exec(`UPDATE drafts SET status = ?, post_url = ?, published_at = ?, updated_at = ?, posted_as = ? WHERE id = ?`,
|
||||
StatusPublished, postURL, now, now, as, 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)
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ func TestRedditDraftsAndCache(t *testing.T) {
|
||||
}
|
||||
|
||||
pub := &fakePub{}
|
||||
if _, err := Publish(context.Background(), d, Publishers{ChannelReddit: pub}, utm.NewTagger([]string{"acme.example"}), dr.ID); err != nil {
|
||||
if _, err := Publish(context.Background(), d, ChannelPublishers{ChannelReddit: pub}, utm.NewTagger([]string{"acme.example"}), dr.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(pub.calls[0], "r/selfhosted|New title|Body|https://acme.example/a?utm_") ||
|
||||
|
||||
Reference in New Issue
Block a user