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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user