File: gotify_payload.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,796 kB
  • sloc: sh: 74; makefile: 62
file content (89 lines) | stat: -rw-r--r-- 3,047 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package gotify

import (
	"encoding/json"
	"fmt"

	"github.com/nicholas-fedor/shoutrrr/pkg/types"
)

// PayloadBuilder handles request payload construction and extras parsing.
type PayloadBuilder interface {
	PrepareRequest(
		message string,
		config *Config,
		extras map[string]any,
		date string,
	) *MessageRequest
	ParseExtras(params *types.Params, config *Config) (map[string]any, error)
}

// DefaultPayloadBuilder provides the default implementation of PayloadBuilder.
type DefaultPayloadBuilder struct{}

// PrepareRequest builds the request payload.
// This function constructs the JSON payload that will be sent to the Gotify API,
// combining the message content with configuration settings and any additional extras.
// Parameters:
//   - message: The main notification message text
//   - config: Configuration containing title, priority, and other settings
//   - extras: Additional key-value pairs to include in the notification
//   - date: Optional custom timestamp in ISO 8601 format
//
// Returns: *MessageRequest containing all data to be sent to the API.
func (b *DefaultPayloadBuilder) PrepareRequest(
	message string,
	config *Config,
	extras map[string]any,
	date string,
) *MessageRequest {
	var datePtr *string
	if date != "" {
		datePtr = &date
	}

	return &MessageRequest{
		Message:  message,         // The notification message content
		Title:    config.Title,    // Notification title from configuration
		Priority: config.Priority, // Priority level for the notification
		Date:     datePtr,         // Optional custom timestamp
		Extras:   extras,          // Additional metadata or custom fields
	}
}

// ParseExtras handles extras parsing from params.
// This function processes the 'extras' parameter which contains additional JSON data
// to be sent with the notification. It attempts to parse JSON from parameters first,
// falling back to configuration extras if parsing fails or no parameter extras exist.
// Parameters:
//   - params: Request parameters that may contain 'extras' JSON string
//   - config: Configuration that may contain default extras
//
// Returns: map of extra data to include in the notification payload, or error if parsing fails.
func (b *DefaultPayloadBuilder) ParseExtras(
	params *types.Params,
	config *Config,
) (map[string]any, error) {
	// Initialize variable to hold parsed extras
	var requestExtras map[string]any

	// Check if parameters exist and contain extras
	if params != nil {
		// Look for 'extras' key in parameters
		if extrasStr, exists := (*params)["extras"]; exists && extrasStr != "" {
			// Attempt to parse the JSON string into a map
			if err := json.Unmarshal([]byte(extrasStr), &requestExtras); err != nil {
				// Return error for parsing failure
				return nil, fmt.Errorf("%w", ErrExtrasUnmarshalFailed)
			}
		}
	}

	// Fall back to configuration extras if no valid parameter extras were found
	if requestExtras == nil {
		requestExtras = config.Extras
	}

	// Return the resolved extras (either from params or config)
	return requestExtras, nil
}