1
0
Fork 0
discord-tweeter/cmd/tweeter/embed.go
Manuel 21d580d1a6
feat: Major refactor, implement web, caching, better tests and build files
* Update golang version to 1.24
* Update multiarch Dockerfile to be more ISA agnostic
* Refactor existing code and properly structure project into modules
* Get rid of global variables except where necessary (go:embed)
* Add default values to Config
* Add webserver with templates to finally correctly serve videos and gifs
* Add tiny caching library to decrease api load and improve latency
* Improve Webhook data preparation by filtering out redundant links
  from the tweet text and properly attaching videos and gifs in separate
  webhook request by utilising new webserver
* Improve tests for filter function
* Improve bake definition for easier CI integration
2025-03-18 19:22:00 +01:00

116 lines
2.5 KiB
Go

package tweeter
import (
"errors"
"strconv"
"strings"
"time"
)
type Embed struct {
Author Author `json:"author,omitempty"`
Title string `json:"title,omitempty"`
URL string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Color int64 `json:"color,omitempty"`
Fields []Field `json:"fields,omitempty"`
Thumbnail Image `json:"thumbnail,omitempty"`
Image Image `json:"image,omitempty"`
Video Video `json:"video,omitempty"`
Footer Footer `json:"footer,omitempty"`
}
type Author struct {
Name string `json:"name"`
URL string `json:"url"`
IconURL string `json:"icon_url"`
}
type Field struct {
Name string `json:"name"`
Value string `json:"value"`
Inline bool `json:"inline,omitempty"`
}
type Footer struct {
Text string `json:"text"`
IconURL string `json:"icon_url,omitempty"`
}
type Image struct {
URL string `json:"url"`
}
type Video struct {
URL string `json:"url"`
}
func (w *Webhook) NewEmbedWithURL(URL string) *Embed {
emb := Embed{URL: URL}
w.Embeds = append(w.Embeds, &emb)
return &emb
}
func (w *Webhook) NewEmbed() *Embed {
emb := Embed{}
w.Embeds = append(w.Embeds, &emb)
return &emb
}
func (e *Embed) SetTitle(Title string) *Embed {
e.Title = Title
return e
}
func (e *Embed) SetText(Description string) *Embed {
e.Description = Description
return e
}
func (e *Embed) SetAuthor(Name, URL, IconURL string) *Embed {
e.Author = Author{Name, URL, IconURL}
return e
}
func (e *Embed) SetColor(color string) (*Embed, error) {
color = strings.Replace(color, "0x", "", -1)
color = strings.Replace(color, "0X", "", -1)
color = strings.Replace(color, "#", "", -1)
colorInt, err := strconv.ParseInt(color, 16, 64)
if err != nil {
return nil, errors.New("invalid hex code passed")
}
e.Color = colorInt
return e, nil
}
func (e *Embed) SetThumbnail(URL string) *Embed {
e.Thumbnail = Image{URL}
return e
}
func (e *Embed) SetImage(URL string) *Embed {
e.Image = Image{URL}
return e
}
func (e *Embed) SetVideo(URL string) *Embed {
e.Video = Video{URL}
return e
}
func (e *Embed) SetFooter(Text, IconURL string) *Embed {
e.Footer = Footer{Text, IconURL}
return e
}
func (e *Embed) SetTimestamp(timestamp time.Time) *Embed {
e.Timestamp = timestamp.Format(time.RFC3339)
return e
}
func (e *Embed) AddField(Name, Value string, Inline bool) *Embed {
e.Fields = append(e.Fields, Field{Name, Value, Inline})
return e
}