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
+80 -5
View File
@@ -3,6 +3,7 @@ package content
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"testing"
@@ -13,6 +14,7 @@ import (
"supermarket/internal/inventory"
"supermarket/internal/models"
"supermarket/internal/profile"
"supermarket/internal/utm"
)
func openDB(t *testing.T) *db.DB {
@@ -123,13 +125,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}, li.ID); err == nil || !strings.Contains(err.Error(), "isn't connected") {
if _, err := Publish(ctx, d, Publishers{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}, x.ID); err == nil {
if _, err := Publish(ctx, d, Publishers{ChannelX: pub}, nil, x.ID); err == nil {
t.Fatal("expected the failure")
}
got, _ := GetDraft(d, x.ID)
@@ -140,7 +142,7 @@ func TestDraftsAndPublish(t *testing.T) {
t.Fatal(err)
}
pub.err = nil
got, err = Publish(ctx, d, Publishers{ChannelX: pub}, x.ID)
got, err = Publish(ctx, d, Publishers{ChannelX: pub}, nil, x.ID)
if err != nil || got.Status != StatusPublished || got.PostURL == "" || got.Error != "" {
t.Fatalf("publish: %+v, %v", got, err)
}
@@ -148,7 +150,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}, x.ID); !errors.Is(err, ErrBusy) {
if _, err := Publish(ctx, d, Publishers{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) {
@@ -161,7 +163,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}, long[0].ID); err == nil || !strings.Contains(err.Error(), "295 characters including the link") {
if _, err := Publish(ctx, d, Publishers{ChannelX: pub}, nil, long[0].ID); err == nil || !strings.Contains(err.Error(), "295 characters including the link") {
t.Errorf("over-long post: %v", err)
}
@@ -256,3 +258,76 @@ func TestGenerate(t *testing.T) {
t.Error("expected an error without a profile")
}
}
func TestScheduleAndDue(t *testing.T) {
d := openDB(t)
_, _ = CreateDrafts(d, []Draft{
{InventoryName: "Jitsi Meet", Kind: "partner", Channel: ChannelLinkedIn, Content: "See https://acme.example/blog.", SourceURL: "https://acme.example/news"},
})
_, _ = CreateDrafts(d, []Draft{{InventoryName: "Old", Kind: "watch", Channel: ChannelX, Content: "Stale"}})
all, _ := ListDrafts(d, 10, StatusDraft)
var soon, stale Draft
for _, dr := range all {
if dr.InventoryName == "Old" {
stale = dr
} else {
soon = dr
}
}
now := time.Now().UTC().Truncate(time.Second)
if err := Schedule(d, soon.ID, now.Add(time.Hour)); err != nil {
t.Fatal(err)
}
if err := Schedule(d, stale.ID, now.Add(-13*time.Hour)); err != nil {
t.Fatal(err)
}
if err := UpdateDraft(d, soon.ID, "edit"); !errors.Is(err, ErrNotEditable) {
t.Errorf("a scheduled draft must be unscheduled before editing: %v", err)
}
if err := Schedule(d, soon.ID, now.AddDate(2, 0, 0)); err == nil {
t.Error("scheduling twice or two years out should fail")
}
if n(CountDrafts(d)) != 0 {
t.Errorf("scheduled drafts still counted for review")
}
// Not due yet; the stale one goes back to the Inbox with a note.
due, err := Due(d, now)
if err != nil || len(due) != 0 {
t.Fatalf("due now = %+v, %v", due, err)
}
if got, _ := GetDraft(d, stale.ID); got.Status != StatusFailed || !strings.Contains(got.Error, "wasn't running") {
t.Errorf("stale draft: %+v", got)
}
due, _ = Due(d, now.Add(2*time.Hour))
if len(due) != 1 || due[0].ID != soon.ID || !due[0].PublishAt.Equal(now.Add(time.Hour)) {
t.Fatalf("due later = %+v", due)
}
// 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)
if err != nil || got.Status != StatusPublished {
t.Fatalf("publish scheduled: %+v, %v", got, err)
}
q := fmt.Sprintf("utm_campaign=partner&utm_content=jitsi-meet-%d&utm_medium=social&utm_source=linkedin", soon.ID)
want := "See https://acme.example/blog?" + q + ".|https://acme.example/news?" + q
if pub.calls[0] != want {
t.Errorf("published\n%s\nwant\n%s", pub.calls[0], want)
}
if got.Content != "See https://acme.example/blog." {
t.Errorf("stored content changed: %q", got.Content)
}
// Unschedule sends it back for editing.
_, _ = CreateDrafts(d, []Draft{{Channel: ChannelX, Content: "Later"}})
later, _ := ListDrafts(d, 1, StatusDraft)
_ = Schedule(d, later[0].ID, now.Add(time.Hour))
if err := Unschedule(d, later[0].ID); err != nil {
t.Fatal(err)
}
if got, _ := GetDraft(d, later[0].ID); got.Status != StatusDraft || !got.PublishAt.IsZero() {
t.Errorf("unscheduled: %+v", got)
}
}