1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-11-29 11:50:22 +01:00

Self-review

This commit is contained in:
binwiederhier 2025-07-19 21:32:05 +02:00
parent dde07adbdc
commit f0d5392e9e
5 changed files with 17 additions and 15 deletions

View file

@ -1456,7 +1456,8 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release
**Features:**
* You can now use a subset of [Sprig](https://github.com/Masterminds/sprig) functions in message/title templates ([#1121](https://github.com/binwiederhier/ntfy/issues/1121), thanks to [@davidatkinsondoyle](https://github.com/davidatkinsondoyle) for reporting and to [@wunter8](https://github.com/wunter8) for implementing)
* Enhanced JSON webhook support via [pre-defined](publish.md#pre-defined-templates) and [custom templates](publish.md#custom-templates) ([#1390](https://github.com/binwiederhier/ntfy/pull/1390))
* Support of advanced [template functions](publish.md#template-functions) based on the [Sprig](https://github.com/Masterminds/sprig) library ([#1121](https://github.com/binwiederhier/ntfy/issues/1121), thanks to [@davidatkinsondoyle](https://github.com/davidatkinsondoyle) for reporting, to [@wunter8](https://github.com/wunter8) for implementing, and to the Sprig team for their work)
### ntfy Android app v1.16.1 (UNRELEASED)

View file

@ -57,7 +57,7 @@ type Server struct {
userManager *user.Manager // Might be nil!
messageCache *messageCache // Database that stores the messages
webPush *webPushStore // Database that stores web push subscriptions
fileCache *fileCache // Name system based cache that stores attachments
fileCache *fileCache // File system based cache that stores attachments
stripe stripeAPI // Stripe API, can be replaced with a mock
priceCache *util.LookupCache[map[string]int64] // Stripe price ID -> price as cents (USD implied!)
metricsHandler http.Handler // Handles /metrics if enable-metrics set, and listen-metrics-http not set
@ -1120,11 +1120,11 @@ func (s *Server) handleBodyAsTemplatedTextMessage(m *message, template templateM
}
peekedBody := strings.TrimSpace(string(body.PeekedBytes))
if templateName := template.Name(); templateName != "" {
if err := s.replaceTemplateFromFile(m, templateName, peekedBody); err != nil {
if err := s.renderTemplateFromFile(m, templateName, peekedBody); err != nil {
return err
}
} else {
if err := s.replaceTemplateFromParams(m, peekedBody); err != nil {
if err := s.renderTemplateFromParams(m, peekedBody); err != nil {
return err
}
}
@ -1134,7 +1134,9 @@ func (s *Server) handleBodyAsTemplatedTextMessage(m *message, template templateM
return nil
}
func (s *Server) replaceTemplateFromFile(m *message, templateName, peekedBody string) error {
// renderTemplateFromFile transforms the JSON message body according to a template from the filesystem.
// The template file must be in the templates directory, or in the configured template directory.
func (s *Server) renderTemplateFromFile(m *message, templateName, peekedBody string) error {
if !templateNameRegex.MatchString(templateName) {
return errHTTPBadRequestTemplateFileNotFound
}
@ -1153,30 +1155,33 @@ func (s *Server) replaceTemplateFromFile(m *message, templateName, peekedBody st
}
var err error
if tpl.Message != nil {
if m.Message, err = s.replaceTemplate(*tpl.Message, peekedBody); err != nil {
if m.Message, err = s.renderTemplate(*tpl.Message, peekedBody); err != nil {
return err
}
}
if tpl.Title != nil {
if m.Title, err = s.replaceTemplate(*tpl.Title, peekedBody); err != nil {
if m.Title, err = s.renderTemplate(*tpl.Title, peekedBody); err != nil {
return err
}
}
return nil
}
func (s *Server) replaceTemplateFromParams(m *message, peekedBody string) error {
// renderTemplateFromParams transforms the JSON message body according to the inline template in the
// message and title parameters.
func (s *Server) renderTemplateFromParams(m *message, peekedBody string) error {
var err error
if m.Message, err = s.replaceTemplate(m.Message, peekedBody); err != nil {
if m.Message, err = s.renderTemplate(m.Message, peekedBody); err != nil {
return err
}
if m.Title, err = s.replaceTemplate(m.Title, peekedBody); err != nil {
if m.Title, err = s.renderTemplate(m.Title, peekedBody); err != nil {
return err
}
return nil
}
func (s *Server) replaceTemplate(tpl string, source string) (string, error) {
// renderTemplate renders a template with the given JSON source data.
func (s *Server) renderTemplate(tpl string, source string) (string, error) {
if templateDisallowedRegex.MatchString(tpl) {
return "", errHTTPBadRequestTemplateDisallowedFunctionCalls
}

View file

@ -25,5 +25,3 @@ message: |
Source: {{ .generatorURL }}
{{ end }}

View file

@ -55,4 +55,3 @@ message: |
{{- else }}
{{ fail "Unsupported GitHub event type or action." }}
{{- end }}

View file

@ -8,4 +8,3 @@ title: |
{{- end }}
message: |
{{ .message | trunc 2000 }}