Phase 1, part 2: Inbox, scheduled publishing and UTM tags

- 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>
This commit is contained in:
2026-11-20 13:17:38 -07:00
co-authored by Claude Opus 5.5
parent 13bf18facc
commit f275f039f9
24 changed files with 873 additions and 184 deletions
+24 -5
View File
@@ -7,6 +7,7 @@ import (
"time"
"supermarket/internal/db"
"supermarket/internal/utm"
)
// Publisher posts to one channel and returns the post's URL.
@@ -23,12 +24,15 @@ var ErrBusy = errors.New("this draft is already being published, or was publishe
// 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) {
//
// 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() {
if !dr.Editable() && dr.Status != StatusScheduled {
return dr, ErrBusy
}
pub := pubs[dr.Channel]
@@ -44,8 +48,8 @@ func Publish(ctx context.Context, d *db.DB, pubs Publishers, id int64) (*Draft,
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)
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
@@ -53,7 +57,12 @@ func Publish(ctx context.Context, d *db.DB, pubs Publishers, id int64) (*Draft,
return dr, err
}
postURL, pubErr := pub.Publish(ctx, dr.Content, dr.SourceURL)
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 = ?`,
@@ -70,3 +79,13 @@ func Publish(ctx context.Context, d *db.DB, pubs Publishers, id int64) (*Draft,
}
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}
}