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:
@@ -46,7 +46,8 @@ func ChannelByID(id string) (Channel, bool) {
|
||||
|
||||
// Draft statuses.
|
||||
const (
|
||||
StatusDraft = "draft"
|
||||
StatusDraft = "draft" // waiting for review, in the Inbox
|
||||
StatusScheduled = "scheduled" // approved, goes out at PublishAt
|
||||
StatusPublishing = "publishing" // claimed by a publish in progress
|
||||
StatusPublished = "published"
|
||||
StatusFailed = "failed"
|
||||
@@ -69,6 +70,7 @@ type Draft struct {
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
PublishedAt time.Time
|
||||
PublishAt time.Time // when a scheduled draft goes out
|
||||
}
|
||||
|
||||
// Length is the draft's length as its channel counts it, including the
|
||||
@@ -155,20 +157,21 @@ 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, '')`
|
||||
post_url, created_at, updated_at, COALESCE(published_at, ''), COALESCE(publish_at, '')`
|
||||
|
||||
func scanDrafts(rows *sql.Rows) ([]Draft, error) {
|
||||
defer rows.Close()
|
||||
var out []Draft
|
||||
for rows.Next() {
|
||||
var dr Draft
|
||||
var ids, created, updated, published string
|
||||
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); err != nil {
|
||||
&dr.SourceURL, &ids, &dr.Status, &dr.Error, &dr.PostURL, &created, &updated, &published, &publishAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = json.Unmarshal([]byte(ids), &dr.ItemIDs)
|
||||
dr.CreatedAt, dr.UpdatedAt, dr.PublishedAt = parseTime(created), parseTime(updated), parseTime(published)
|
||||
dr.PublishAt = parseTime(publishAt)
|
||||
out = append(out, dr)
|
||||
}
|
||||
return out, rows.Err()
|
||||
@@ -187,6 +190,8 @@ func ListDrafts(d *db.DB, limit int, statuses ...string) ([]Draft, error) {
|
||||
order := "created_at DESC, id DESC"
|
||||
if len(statuses) == 1 && statuses[0] == StatusPublished {
|
||||
order = "published_at DESC, id DESC"
|
||||
} else if len(statuses) == 1 && statuses[0] == StatusScheduled {
|
||||
order = "publish_at, id"
|
||||
}
|
||||
rows, err := d.Query(`SELECT `+draftColumns+` FROM drafts WHERE status IN (?`+
|
||||
strings.Repeat(", ?", len(statuses)-1)+`) ORDER BY `+order+` LIMIT ?`, args...)
|
||||
@@ -266,3 +271,52 @@ func CountDrafts(d *db.DB) (int, error) {
|
||||
err := d.QueryRow(`SELECT count(*) FROM drafts WHERE status IN (?, ?)`, StatusDraft, StatusFailed).Scan(&n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Schedule approves a draft to go out at at. It must be one that can still
|
||||
// be published, and at most a year ahead.
|
||||
func Schedule(d *db.DB, id int64, at time.Time) error {
|
||||
if at.After(time.Now().AddDate(1, 0, 0)) {
|
||||
return errors.New("that's more than a year away")
|
||||
}
|
||||
res, err := d.Exec(`UPDATE drafts SET status = ?, publish_at = ?, error = '', updated_at = ?
|
||||
WHERE id = ? AND status IN (?, ?)`,
|
||||
StatusScheduled, formatTime(at), formatTime(time.Now()), id, StatusDraft, StatusFailed)
|
||||
return oneRow(res, err)
|
||||
}
|
||||
|
||||
// Unschedule sends a scheduled draft back to the Inbox, to edit or drop.
|
||||
func Unschedule(d *db.DB, id int64) error {
|
||||
res, err := d.Exec(`UPDATE drafts SET status = ?, publish_at = NULL, updated_at = ? WHERE id = ? AND status = ?`,
|
||||
StatusDraft, formatTime(time.Now()), id, StatusScheduled)
|
||||
return oneRow(res, err)
|
||||
}
|
||||
|
||||
// MaxLate is how late a scheduled post may still go out, if the app was
|
||||
// stopped when it was due. Later than that, news is stale: it goes back to
|
||||
// the Inbox instead.
|
||||
const MaxLate = 12 * time.Hour
|
||||
|
||||
// Due returns the scheduled drafts whose time has come, oldest first. Ones
|
||||
// more than MaxLate overdue are sent back to the Inbox with a note.
|
||||
func Due(d *db.DB, now time.Time) ([]Draft, error) {
|
||||
if _, err := d.Exec(`UPDATE drafts SET status = ?, error = ?, updated_at = ?
|
||||
WHERE status = ? AND publish_at < ?`,
|
||||
StatusFailed, "Not published: Supermarket wasn't running when this was due. Publish or reschedule it.",
|
||||
formatTime(now), StatusScheduled, formatTime(now.Add(-MaxLate))); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := d.Query(`SELECT `+draftColumns+` FROM drafts WHERE status = ? AND publish_at <= ?
|
||||
ORDER BY publish_at, id`, StatusScheduled, formatTime(now))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scanDrafts(rows)
|
||||
}
|
||||
|
||||
// FailScheduled sends a scheduled draft that couldn't go out (its channel
|
||||
// was signed out, say) back to the Inbox with the reason.
|
||||
func FailScheduled(d *db.DB, id int64, reason string) error {
|
||||
res, err := d.Exec(`UPDATE drafts SET status = ?, error = ?, updated_at = ? WHERE id = ? AND status = ?`,
|
||||
StatusFailed, reason, formatTime(time.Now()), id, StatusScheduled)
|
||||
return oneRow(res, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user