This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
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) == ""
|
||||
}
|
||||
Reference in New Issue
Block a user