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>
137 lines
4.6 KiB
Go
137 lines
4.6 KiB
Go
package content
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
"supermarket/internal/utm"
|
|
)
|
|
|
|
// Post is what a publisher sends.
|
|
type Post struct {
|
|
Text string
|
|
SourceURL string // tagged, if it's the company's
|
|
Title string // Reddit
|
|
Target string // Reddit: the subreddit
|
|
AsPage bool // LinkedIn: post as the company page
|
|
}
|
|
|
|
// Publisher posts to one channel and returns the post's URL.
|
|
type Publisher interface {
|
|
Publish(ctx context.Context, p Post) (string, error)
|
|
}
|
|
|
|
// 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")
|
|
|
|
// 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.
|
|
//
|
|
// Links to the company's own sites get UTM tags (tags may be nil); the draft
|
|
// itself is kept without them.
|
|
func Publish(ctx context.Context, d *db.DB, pubs Publishers, tags *utm.Tagger, id int64) (*Draft, error) {
|
|
dr, err := GetDraft(d, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !dr.Editable() && dr.Status != StatusScheduled {
|
|
return dr, ErrBusy
|
|
}
|
|
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)
|
|
}
|
|
|
|
res, err := d.Exec(`UPDATE drafts SET status = ?, error = '', updated_at = ? WHERE id = ? AND status IN (?, ?, ?)`,
|
|
StatusPublishing, formatTime(time.Now()), id, StatusDraft, StatusFailed, StatusScheduled)
|
|
if err := oneRow(res, err); err != nil {
|
|
if errors.Is(err, ErrNotEditable) {
|
|
return dr, ErrBusy
|
|
}
|
|
return dr, err
|
|
}
|
|
|
|
text, src := Manual(dr, tags)
|
|
postURL, pubErr := pub.Publish(ctx, Post{Text: text, SourceURL: src, Title: dr.Title, Target: dr.Target,
|
|
AsPage: dr.Channel == ChannelLinkedIn && dr.LinkedInAs == AsPage})
|
|
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 = ?, 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)
|
|
}
|
|
return GetDraft(d, id)
|
|
}
|
|
|
|
// UTM is the tags for the draft's links: the channel, the kind of post,
|
|
// and which entry and draft it was.
|
|
func (d *Draft) UTM() utm.Params {
|
|
content := fmt.Sprintf("draft-%d", d.ID)
|
|
if slug := utm.Slug(d.InventoryName); slug != "" {
|
|
content = slug + "-" + fmt.Sprint(d.ID)
|
|
}
|
|
campaign := d.Kind
|
|
if d.SignalSlug != "" {
|
|
// Written for a buying signal: its visits count for the signal.
|
|
campaign = d.SignalSlug
|
|
}
|
|
return utm.Params{Source: d.Channel, Medium: "social", Campaign: campaign, Content: content}
|
|
}
|
|
|
|
// MarkPosted records a draft the user posted by hand (on a network
|
|
// Supermarket can't post to for them), with the post's address if they
|
|
// gave one.
|
|
func MarkPosted(d *db.DB, id int64, postURL string) error {
|
|
now := formatTime(time.Now())
|
|
res, err := d.Exec(`UPDATE drafts SET status = ?, error = '', post_url = ?, published_at = ?, updated_at = ?
|
|
WHERE id = ? AND status IN (?, ?, ?)`,
|
|
StatusPublished, postURL, now, now, id, StatusDraft, StatusFailed, StatusScheduled)
|
|
return oneRow(res, err)
|
|
}
|
|
|
|
// Manual is a draft as it would be published, for posting by hand: links
|
|
// to the company's sites tagged, and the source link added the way the
|
|
// channel's publisher would add it.
|
|
func Manual(dr *Draft, tags *utm.Tagger) (text, sourceURL string) {
|
|
text, sourceURL = dr.Content, dr.SourceURL
|
|
if tags != nil {
|
|
p := dr.UTM()
|
|
text, sourceURL = tags.Text(text, p), tags.URL(sourceURL, p)
|
|
}
|
|
return text, sourceURL
|
|
}
|