Files
plumber/internal/discord/format.go
T
codegirl007 06fcc16c02
CI / test (pull_request) Successful in 6m20s
Add Discord outbound posting.
2026-08-29 01:13:44 -07:00

76 lines
1.5 KiB
Go

package discord
import (
"strings"
"plumber/internal/events"
)
const (
embedTitleLimit = 256
embedDescriptionLimit = 4096
threadNameLimit = 100
embedColor = 0xe96a26
)
// Message is a Discord-ready snapshot of a site post event.
type Message struct {
Title string
URL string
Description string
City string
Author string
ImageURLs []string
ThreadName string
}
func formatMessage(ev events.PostEvent) Message {
title := strings.TrimSpace(ev.Title)
if title == "" {
title = "Reply"
}
author := strings.TrimSpace(ev.AuthorName)
if author == "" {
author = "Someone"
}
msg := Message{
Title: truncateRunes(title, embedTitleLimit),
URL: strings.TrimSpace(ev.Permalink),
Description: truncateRunes(strings.TrimSpace(ev.Body), embedDescriptionLimit),
City: strings.TrimSpace(ev.City),
Author: author,
ThreadName: threadName(ev.Title),
}
for _, img := range ev.Images {
url := strings.TrimSpace(img.URL)
if url == "" {
continue
}
msg.ImageURLs = append(msg.ImageURLs, url)
}
return msg
}
func threadName(title string) string {
title = strings.TrimSpace(title)
if title == "" {
return "Question"
}
return truncateRunes(title, threadNameLimit)
}
func truncateRunes(s string, max int) string {
if max <= 0 {
return ""
}
runes := []rune(s)
if len(runes) <= max {
return s
}
return string(runes[:max])
}
func isRoot(ev events.PostEvent) bool {
return strings.TrimSpace(ev.ParentID) == ""
}