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:
+3
-129
@@ -45,10 +45,7 @@ func (s *Server) contentIndex(w http.ResponseWriter, r *http.Request) {
|
||||
rows = append(rows, contentRow{Entry: e, Sources: fetcher.Sources(&e),
|
||||
Waiting: counts[strings.ToLower(e.Name)], LastChecked: last})
|
||||
}
|
||||
drafts, _ := content.CountDrafts(s.DB)
|
||||
s.render(w, r, "content", page{Title: "Content", Nav: "content", Data: map[string]any{
|
||||
"Rows": rows, "Drafts": drafts,
|
||||
}})
|
||||
s.render(w, r, "content", page{Title: "Content", Nav: "content", Data: map[string]any{"Rows": rows}})
|
||||
}
|
||||
|
||||
type channelChoice struct {
|
||||
@@ -65,7 +62,6 @@ type entryPage struct {
|
||||
Since time.Time
|
||||
Channels []channelChoice
|
||||
ProfileSet bool
|
||||
Drafts int
|
||||
// After a fetch
|
||||
Fetched bool
|
||||
NewItems int
|
||||
@@ -109,9 +105,8 @@ func (s *Server) entryData(e *inventory.Entry, showDismissed bool) (*entryPage,
|
||||
for _, c := range content.Channels {
|
||||
chans = append(chans, channelChoice{c, pubs[c.ID] != nil})
|
||||
}
|
||||
drafts, _ := content.CountDrafts(s.DB)
|
||||
return &entryPage{Entry: e, Sources: fetcher.Sources(e), Items: items, ShowDismissed: showDismissed,
|
||||
LastChecked: last, Since: since, Channels: chans, ProfileSet: p.IsSet(), Drafts: drafts}, nil
|
||||
LastChecked: last, Since: since, Channels: chans, ProfileSet: p.IsSet()}, nil
|
||||
}
|
||||
|
||||
func (s *Server) contentEntry(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -206,7 +201,7 @@ func (s *Server) contentItems(w http.ResponseWriter, r *http.Request) {
|
||||
showErr(err.Error())
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/drafts", "drafted")
|
||||
redirect(w, r, "/inbox", "drafted")
|
||||
default:
|
||||
http.Error(w, "unknown action", http.StatusBadRequest)
|
||||
}
|
||||
@@ -272,124 +267,3 @@ func (s *Server) writeDrafts(r *http.Request, e *inventory.Entry, ids []string)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- Drafts ---
|
||||
|
||||
type draftBatch struct {
|
||||
Batch string
|
||||
InventoryName string
|
||||
Kind string
|
||||
CreatedAt time.Time
|
||||
SourceURL string
|
||||
Drafts []content.Draft
|
||||
}
|
||||
|
||||
func (s *Server) draftsPage(w http.ResponseWriter, r *http.Request) {
|
||||
s.renderDrafts(w, r, "")
|
||||
}
|
||||
|
||||
func (s *Server) renderDrafts(w http.ResponseWriter, r *http.Request, errMsg string) {
|
||||
pending, err := content.ListDrafts(s.DB, 200, content.StatusDraft, content.StatusFailed)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
var batches []*draftBatch
|
||||
byID := map[string]*draftBatch{}
|
||||
for _, d := range pending {
|
||||
b := byID[d.Batch]
|
||||
if b == nil {
|
||||
b = &draftBatch{Batch: d.Batch, InventoryName: d.InventoryName, Kind: d.Kind, CreatedAt: d.CreatedAt, SourceURL: d.SourceURL}
|
||||
byID[d.Batch] = b
|
||||
batches = append(batches, b)
|
||||
}
|
||||
b.Drafts = append(b.Drafts, d)
|
||||
}
|
||||
for _, b := range batches {
|
||||
// X first, then LinkedIn, whatever order they were stored in.
|
||||
ordered := make([]content.Draft, 0, len(b.Drafts))
|
||||
for _, c := range content.Channels {
|
||||
for _, d := range b.Drafts {
|
||||
if d.Channel == c.ID {
|
||||
ordered = append(ordered, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
b.Drafts = ordered
|
||||
}
|
||||
published, err := content.ListDrafts(s.DB, 20, content.StatusPublished)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
pubs := s.publishers()
|
||||
connected := map[string]bool{}
|
||||
for id := range pubs {
|
||||
connected[id] = true
|
||||
}
|
||||
s.render(w, r, "drafts", page{Title: "Drafts", Nav: "content", Error: errMsg, Data: map[string]any{
|
||||
"Batches": batches, "Published": published, "Connected": connected,
|
||||
}})
|
||||
}
|
||||
|
||||
func (s *Server) draftID(w http.ResponseWriter, r *http.Request) (int64, bool) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
// draftAction saves, publishes or discards one draft.
|
||||
func (s *Server) draftAction(w http.ResponseWriter, r *http.Request) {
|
||||
id, ok := s.draftID(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fail := func(err error) {
|
||||
if errors.Is(err, content.ErrNoDraft) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
s.renderDrafts(w, r, err.Error())
|
||||
}
|
||||
action := r.PathValue("action")
|
||||
if action == "save" || action == "publish" {
|
||||
if text, sent := r.PostForm["content"]; sent {
|
||||
if err := content.UpdateDraft(s.DB, id, text[0]); err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
switch action {
|
||||
case "save":
|
||||
redirect(w, r, "/drafts", "saved")
|
||||
case "publish":
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
d, err := content.Publish(ctx, s.DB, s.publishers(), id)
|
||||
if err != nil {
|
||||
name := "the channel"
|
||||
if d != nil {
|
||||
name = d.ChannelName()
|
||||
}
|
||||
fail(fmt.Errorf("Couldn't publish to %s: %w", name, err))
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/drafts", "published")
|
||||
case "discard":
|
||||
if err := content.DiscardDraft(s.DB, id); err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
redirect(w, r, "/drafts", "discarded")
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user