- Inbox: a top-level page, with a count in the menu, for everything waiting for approval; Content's drafts are reviewed there. /drafts redirects to it. - Scheduling: publish a draft now or at a time (migration 3 adds publish_at). A scheduler in the app publishes due drafts every 30 seconds. Posts more than 12 hours overdue, or refused before posting, go back to the Inbox with the reason. Content gets Scheduled and Published tabs. - UTM tags on links to the company's own websites and product pages, added when publishing; drafts stay untagged. - The container runs in the computer's time zone, so times are shown and read correctly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
92 lines
3.0 KiB
Go
92 lines
3.0 KiB
Go
package content
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
"supermarket/internal/utm"
|
|
)
|
|
|
|
// 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.
|
|
//
|
|
// 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 := 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, StatusScheduled)
|
|
if err := oneRow(res, err); err != nil {
|
|
if errors.Is(err, ErrNotEditable) {
|
|
return dr, ErrBusy
|
|
}
|
|
return dr, err
|
|
}
|
|
|
|
text, src := dr.Content, dr.SourceURL
|
|
if tags != nil {
|
|
p := dr.UTM()
|
|
text, src = tags.Text(text, p), tags.URL(src, p)
|
|
}
|
|
postURL, pubErr := pub.Publish(ctx, text, src)
|
|
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)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
return utm.Params{Source: d.Channel, Medium: "social", Campaign: d.Kind, Content: content}
|
|
}
|