| 1 | package mattermostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 7 | |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 9 | ) |
| 10 | |
| 11 | // JSON represents the payload structure for Mattermost notifications. |
| 12 | type JSONscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON# struct { |
| 13 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#Text. string `json:"text"` |
| 14 | UserNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#UserName. string `json:"username,omitempty"` |
| 15 | Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#Channel. string `json:"channel,omitempty"` |
| 16 | IconEmojiscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#IconEmoji. string `json:"icon_emoji,omitempty"` |
| 17 | IconURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#IconURL. string `json:"icon_url,omitempty"` |
| 18 | } |
| 19 | |
| 20 | // SetIcon sets the appropriate icon field in the payload based on whether the input is a URL or not. |
| 21 | func (j *JSONscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#) SetIconscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#SetIcon().(icon string) { |
| 22 | j.IconURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#IconURL. = "" |
| 23 | j.IconEmojiscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#IconEmoji. = "" |
| 24 | |
| 25 | if icon != "" { |
| 26 | lower := stringsscip-go gomod github.com/golang/go/src . strings/.ToLowerscip-go gomod github.com/golang/go/src . strings/ToLower().(icon) |
| 27 | if stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "http://") || stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(lower, "https://") { |
| 28 | j.IconURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#IconURL. = icon |
| 29 | } else { |
| 30 | j.IconEmojiscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#IconEmoji. = icon |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // CreateJSONPayload generates a JSON payload for the Mattermost service. |
| 36 | func CreateJSONPayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/CreateJSONPayload().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#, message string, params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#) ([]byte, error) { |
| 37 | // Extract title from params if provided, before building the text |
| 38 | title := config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#Title. |
| 39 | |
| 40 | if params != nil { |
| 41 | if value, found := (*params)["title"]; found { |
| 42 | title = value |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Prepend title to message when present |
| 47 | text := message |
| 48 | if title != "" { |
| 49 | text = title + "\n" + message |
| 50 | } |
| 51 | |
| 52 | payload := JSONscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#{ |
| 53 | Textscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#Text.: text, |
| 54 | UserNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#UserName.: config.UserNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#UserName., |
| 55 | Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#Channel.: config.Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#Channel., |
| 56 | IconEmojiscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#IconEmoji.: "", |
| 57 | IconURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#IconURL.: "", |
| 58 | } |
| 59 | |
| 60 | icon := config.Iconscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/Config#Icon. |
| 61 | |
| 62 | if params != nil { |
| 63 | if value, found := (*params)["username"]; found { |
| 64 | payload.UserNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#UserName. = value |
| 65 | } |
| 66 | |
| 67 | if value, found := (*params)["channel"]; found { |
| 68 | payload.Channelscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#Channel. = value |
| 69 | } |
| 70 | |
| 71 | if value, found := (*params)["icon"]; found { |
| 72 | icon = value |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | payload.SetIconscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/mattermost`/JSON#SetIcon().(icon) |
| 77 | |
| 78 | payloadBytes, err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src . `encoding/json`/Marshal().(payload) |
| 79 | if err != nil { |
| 80 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("marshaling Mattermost payload to JSON: %w", err) |
| 81 | } |
| 82 | |
| 83 | return payloadBytes, nil |
| 84 | } |