Debian Codegraph @

pkg/services/chat/discord/discord_json.go

raw
1 package discord
2
3 import (
4 "time"
5
6 "github.com/nicholas-fedor/shoutrrr/pkg/types"
7 "github.com/nicholas-fedor/shoutrrr/pkg/util"
8 )
9
10 // WebhookPayload is the webhook endpoint payload.
11 type WebhookPayload struct {
12 Content string `json:"content,omitempty"`
13 Embeds []embedItem `json:"embeds,omitempty"`
14 Username string `json:"username,omitempty"`
15 AvatarURL string `json:"avatar_url,omitempty"`
16 Attachments []attachment `json:"attachments,omitempty"`
17 }
18
19 // JSON is the actual notification payload.
20 type embedItem struct {
21 Title string `json:"title,omitempty"`
22 Content string `json:"description,omitempty"`
23 URL string `json:"url,omitempty"`
24 Timestamp string `json:"timestamp,omitempty"`
25 Color uint `json:"color,omitempty"`
26 Footer *embedFooter `json:"footer,omitempty"`
27 Author *embedAuthor `json:"author,omitempty"`
28 Image *embedImage `json:"image,omitempty"`
29 Thumbnail *embedThumbnail `json:"thumbnail,omitempty"`
30 Fields []embedField `json:"fields,omitempty"`
31 }
32
33 type embedFooter struct {
34 Text string `json:"text"`
35 IconURL string `json:"icon_url,omitempty"`
36 }
37
38 type embedAuthor struct {
39 Name string `json:"name,omitempty"`
40 URL string `json:"url,omitempty"`
41 IconURL string `json:"icon_url,omitempty"`
42 }
43
44 type embedImage struct {
45 URL string `json:"url,omitempty"`
46 }
47
48 type embedThumbnail struct {
49 URL string `json:"url,omitempty"`
50 }
51
52 type embedField struct {
53 Name string `json:"name"`
54 Value string `json:"value"`
55 Inline bool `json:"inline,omitempty"`
56 }
57
58 type attachment struct {
59 ID int `json:"id"`
60 Filename string `json:"filename"`
61 }
62
63 const (
64 MaxEmbeds = 10
65 )
66
67 // hasEmbedFields checks if the fields contain any fields that should trigger embed processing.
68 func hasEmbedFields(fields []types.Field) bool {
69 return len(fields) > 0
70 }
71
72 // processEmbedFields processes MessageItem fields into embed components.
73 func processEmbedFields(
74 fields []types.Field,
75 ) (*embedAuthor, *embedImage, *embedThumbnail, []embedField) {
76 var (
77 author *embedAuthor
78 image *embedImage
79 thumbnail *embedThumbnail
80 embedFields []embedField
81 )
82
83 for _, field := range fields {
84 switch field.Key {
85 case "embed_author_name":
86 if author == nil {
87 author = &embedAuthor{}
88 }
89
90 author.Name = field.Value
91 case "embed_author_url":
92 if author == nil {
93 author = &embedAuthor{}
94 }
95
96 author.URL = field.Value
97 case "embed_author_icon_url":
98 if author == nil {
99 author = &embedAuthor{}
100 }
101
102 author.IconURL = field.Value
103 case "embed_image_url":
104 image = &embedImage{URL: field.Value}
105 case "embed_thumbnail_url":
106 thumbnail = &embedThumbnail{URL: field.Value}
107 default:
108 // Regular fields become embed fields
109 //nolint:exhaustruct // Inline is optional and defaults to false
110 embedFields = append(embedFields, embedField{
111 Name: field.Key,
112 Value: field.Value,
113 })
114 }
115 }
116
117 return author, image, thumbnail, embedFields
118 }
119
120 // CreatePayloadFromItems creates a JSON payload to be sent to the discord webhook API.
121 func CreatePayloadFromItems(
122 items []types.MessageItem,
123 title string,
124 colors [types.MessageLevelCount]uint,
125 ) (WebhookPayload, error) {
126 if len(items) < 1 {
127 return WebhookPayload{}, ErrEmptyMessage
128 }
129
130 // Check if we can use content field for plain text messages
131 // Only if no special embed fields are present and no files
132 hasFiles := false
133
134 for _, item := range items {
135 if item.File != nil {
136 hasFiles = true
137
138 break
139 }
140 }
141
142 if len(items) == 1 && title == "" && items[0].Level == types.Unknown &&
143 items[0].Timestamp.IsZero() &&
144 !hasEmbedFields(items[0].Fields) &&
145 !hasFiles {
146 return WebhookPayload{
147 Content: items[0].Text,
148 }, nil
149 }
150
151 itemCount := util.Min(MaxEmbeds, len(items))
152
153 embeds := make([]embedItem, 0, itemCount)
154
155 var attachments []attachment
156
157 for i, item := range items {
158 if i >= itemCount {
159 break
160 }
161
162 color := uint(0)
163 if item.Level >= types.Unknown && int(item.Level) < len(colors) {
164 color = colors[item.Level]
165 }
166
167 author, image, thumbnail, embedFields := processEmbedFields(item.Fields)
168
169 //nolint:exhaustruct // Fields are conditionally set below
170 embeddedItem := embedItem{
171 Content: item.Text,
172 Color: color,
173 Author: author,
174 Image: image,
175 Thumbnail: thumbnail,
176 Fields: embedFields,
177 }
178
179 if item.Level != types.Unknown {
180 //nolint:exhaustruct // IconURL is optional
181 embeddedItem.Footer = &embedFooter{
182 Text: item.Level.String(),
183 }
184 }
185
186 if !item.Timestamp.IsZero() {
187 embeddedItem.Timestamp = item.Timestamp.UTC().Format(time.RFC3339)
188 }
189
190 embeds = append(embeds, embeddedItem)
191
192 // Add file attachment if present
193 if item.File != nil {
194 attachments = append(attachments, attachment{
195 ID: len(attachments),
196 Filename: item.File.Name,
197 })
198 }
199 }
200
201 // This should not happen, but it's better to leave the index check before dereferencing the array
202 if len(embeds) > 0 {
203 embeds[0].Title = title
204 }
205
206 return WebhookPayload{
207 Embeds: embeds,
208 Attachments: attachments,
209 }, nil
210 }