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:
@@ -0,0 +1,126 @@
|
||||
// Package utm adds UTM tags to links that point at the company's own sites,
|
||||
// so its analytics (Plausible) can tell which posts brought visitors. Links
|
||||
// elsewhere are left alone: no one else's analytics will read the tags.
|
||||
package utm
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Params are the tags for one post.
|
||||
type Params struct {
|
||||
Source string // the channel: x, linkedin, reddit
|
||||
Medium string // "social"
|
||||
Campaign string // the kind of post: partner, competitor, watch, product
|
||||
Content string // which post: entry and draft
|
||||
}
|
||||
|
||||
// Tagger knows which hosts are the company's.
|
||||
type Tagger struct {
|
||||
hosts []string
|
||||
}
|
||||
|
||||
// NewTagger takes the company's site and product URLs (or bare hosts).
|
||||
func NewTagger(sites []string) *Tagger {
|
||||
t := &Tagger{}
|
||||
seen := map[string]bool{}
|
||||
for _, s := range sites {
|
||||
h := host(s)
|
||||
if h != "" && !seen[h] {
|
||||
seen[h] = true
|
||||
t.hosts = append(t.hosts, h)
|
||||
}
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func host(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
if !strings.Contains(s, "://") {
|
||||
s = "https://" + s
|
||||
}
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimPrefix(strings.ToLower(u.Hostname()), "www.")
|
||||
}
|
||||
|
||||
// Own reports whether rawURL is on one of the company's hosts or their
|
||||
// subdomains.
|
||||
func (t *Tagger) Own(rawURL string) bool {
|
||||
h := host(rawURL)
|
||||
if h == "" || !strings.Contains(rawURL, "://") {
|
||||
return false
|
||||
}
|
||||
for _, own := range t.hosts {
|
||||
if h == own || strings.HasSuffix(h, "."+own) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// URL tags rawURL if it's the company's. Tags already in the link win.
|
||||
func (t *Tagger) URL(rawURL string, p Params) string {
|
||||
if t == nil || !t.Own(rawURL) {
|
||||
return rawURL
|
||||
}
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return rawURL
|
||||
}
|
||||
q := u.Query()
|
||||
for k, v := range map[string]string{
|
||||
"utm_source": p.Source, "utm_medium": p.Medium, "utm_campaign": p.Campaign, "utm_content": p.Content,
|
||||
} {
|
||||
if v != "" && q.Get(k) == "" {
|
||||
q.Set(k, v)
|
||||
}
|
||||
}
|
||||
u.RawQuery = q.Encode()
|
||||
return u.String()
|
||||
}
|
||||
|
||||
// links finds http(s) URLs in text. Trailing punctuation that usually ends
|
||||
// a sentence isn't part of the link.
|
||||
var links = regexp.MustCompile(`https?://[^\s<>"]+`)
|
||||
|
||||
// Text tags the company's links inside a post.
|
||||
func (t *Tagger) Text(text string, p Params) string {
|
||||
if t == nil || len(t.hosts) == 0 {
|
||||
return text
|
||||
}
|
||||
return links.ReplaceAllStringFunc(text, func(m string) string {
|
||||
trail := ""
|
||||
for len(m) > 0 && strings.ContainsRune(".,;:!?)]}'", rune(m[len(m)-1])) {
|
||||
trail = m[len(m)-1:] + trail
|
||||
m = m[:len(m)-1]
|
||||
}
|
||||
return t.URL(m, p) + trail
|
||||
})
|
||||
}
|
||||
|
||||
// Slug turns a name into a lowercase tag value: "Jitsi Meet" → "jitsi-meet".
|
||||
func Slug(s string) string {
|
||||
var b strings.Builder
|
||||
dash := false
|
||||
for _, r := range strings.ToLower(s) {
|
||||
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
|
||||
b.WriteRune(r)
|
||||
dash = false
|
||||
} else if b.Len() > 0 && !dash {
|
||||
b.WriteByte('-')
|
||||
dash = true
|
||||
}
|
||||
}
|
||||
return strings.TrimSuffix(b.String(), "-")
|
||||
}
|
||||
|
||||
// Any reports whether the tagger knows any of the company's hosts.
|
||||
func (t *Tagger) Any() bool { return t != nil && len(t.hosts) > 0 }
|
||||
Reference in New Issue
Block a user