| 1 |
package gotify |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 8 |
) |
| 9 |
|
| 10 |
// DefaultPayloadBuilder provides the default implementation of PayloadBuilder. |
| 11 |
type DefaultPayloadBuilder struct{} |
| 12 |
|
| 13 |
// PayloadBuilder handles request payload construction and extras parsing. |
| 14 |
type PayloadBuilder interface { |
| 15 |
PrepareRequest( |
| 16 |
message string, |
| 17 |
config *Config, |
| 18 |
extras map[string]any, |
| 19 |
date string, |
| 20 |
) *MessageRequest |
| 21 |
ParseExtras(params *types.Params, config *Config) (map[string]any, error) |
| 22 |
} |
| 23 |
|
| 24 |
// ParseExtras handles extras parsing from params. |
| 25 |
// This function processes the 'extras' parameter which contains additional JSON data |
| 26 |
// to be sent with the notification. It attempts to parse JSON from parameters first, |
| 27 |
// falling back to configuration extras if parsing fails or no parameter extras exist. |
| 28 |
// Parameters: |
| 29 |
// - params: Request parameters that may contain 'extras' JSON string |
| 30 |
// - config: Configuration that may contain default extras |
| 31 |
// |
| 32 |
// Returns: map of extra data to include in the notification payload, or error if parsing fails. |
| 33 |
func (b *DefaultPayloadBuilder) ParseExtras( |
| 34 |
params *types.Params, |
| 35 |
config *Config, |
| 36 |
) (map[string]any, error) { |
| 37 |
// Initialize variable to hold parsed extras |
| 38 |
var requestExtras map[string]any |
| 39 |
|
| 40 |
// Check if parameters exist and contain extras |
| 41 |
if params != nil { |
| 42 |
// Look for 'extras' key in parameters |
| 43 |
if extrasStr, exists := (*params)["extras"]; exists && extrasStr != "" { |
| 44 |
// Attempt to parse the JSON string into a map |
| 45 |
if err := json.Unmarshal([]byte(extrasStr), &requestExtras); err != nil { |
| 46 |
// Return error for parsing failure |
| 47 |
return nil, fmt.Errorf("%w", ErrExtrasUnmarshalFailed) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// Fall back to configuration extras if no valid parameter extras were found |
| 53 |
if requestExtras == nil { |
| 54 |
requestExtras = config.Extras |
| 55 |
} |
| 56 |
|
| 57 |
// Return the resolved extras (either from params or config) |
| 58 |
return requestExtras, nil |
| 59 |
} |
| 60 |
|
| 61 |
// PrepareRequest builds the request payload. |
| 62 |
// This function constructs the JSON payload that will be sent to the Gotify API, |
| 63 |
// combining the message content with configuration settings and any additional extras. |
| 64 |
// Parameters: |
| 65 |
// - message: The main notification message text |
| 66 |
// - config: Configuration containing title, priority, and other settings |
| 67 |
// - extras: Additional key-value pairs to include in the notification |
| 68 |
// - date: Optional custom timestamp in ISO 8601 format |
| 69 |
// |
| 70 |
// Returns: *MessageRequest containing all data to be sent to the API. |
| 71 |
func (b *DefaultPayloadBuilder) PrepareRequest( |
| 72 |
message string, |
| 73 |
config *Config, |
| 74 |
extras map[string]any, |
| 75 |
date string, |
| 76 |
) *MessageRequest { |
| 77 |
var datePtr *string |
| 78 |
if date != "" { |
| 79 |
datePtr = &date |
| 80 |
} |
| 81 |
|
| 82 |
return &MessageRequest{ |
| 83 |
Message: message, // The notification message content |
| 84 |
Title: config.Title, // Notification title from configuration |
| 85 |
Priority: config.Priority, // Priority level for the notification |
| 86 |
Date: datePtr, // Optional custom timestamp |
| 87 |
Extras: extras, // Additional metadata or custom fields |
| 88 |
} |
| 89 |
} |