- Sign in with a Reddit web app; access tokens refresh automatically, and an app-only token reads rules before sign-in. - Reddit drafts: a title and text for one subreddit, chosen when writing. The subreddit's guidelines and rules go into the prompt, which asks for a community member's post that discloses the affiliation and avoids marketing language. - The Inbox shows the subreddit's rules (cached for a day, with Refresh) and warns, without blocking, about promotion rules, restricted subreddits, link-only subreddits, posting in the same subreddit within a week, more than 1 in 10 recent posts linking to the company's sites, and new accounts. - Publishing makes a text post with the source link at the end, or a link post where the subreddit takes only links. - Migration 4 adds the draft's title and subreddit, and the rules cache. Publishers now take a Post, which carries the title and subreddit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
140 lines
3.4 KiB
Go
140 lines
3.4 KiB
Go
// 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 }
|
|
|
|
// HasOwnLink reports whether text contains a link to the company's sites.
|
|
func (t *Tagger) HasOwnLink(text string) bool {
|
|
if !t.Any() {
|
|
return false
|
|
}
|
|
for _, m := range links.FindAllString(text, -1) {
|
|
if t.Own(strings.TrimRight(m, ".,;:!?)]}'")) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|