From b67d9fc85d06d12276c570a89d78864f07e5a3a0 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 29 Mar 2022 15:40:26 -0400 Subject: [PATCH] Added missing 'delay' and 'email' params to publish as json --- docs/publish.md | 6 ++++-- docs/releases.md | 1 + server/server.go | 6 ++++++ server/server_test.go | 19 ++++++++++++++++++- server/types.go | 2 ++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/docs/publish.md b/docs/publish.md index 4b1e4e43..94a9a9f6 100644 --- a/docs/publish.md +++ b/docs/publish.md @@ -661,7 +661,8 @@ the example. To publish as JSON, you must **PUT/POST to the ntfy root URL**, not to the topic URL. Be sure to check that you're POST-ing to `https://ntfy.sh/` (correct), and not to `https://ntfy.sh/mytopic` (incorrect). -Here's an example using all supported parameters. The `topic` parameter is the only required one: +Here's an example using most supported parameters. Check the table below for a complete list. The `topic` parameter +is the only required one: === "Command line (curl)" ``` @@ -798,7 +799,8 @@ all the supported fields: | `click` | - | *URL* | `https://example.com` | Website opened when notification is [clicked](#click-action) | | `attach` | - | *URL* | `https://example.com/file.jpg` | URL of an attachment, see [attach via URL](#attach-file-from-url) | | `filename` | - | *string* | `file.jpg` | File name of the attachment | - +| `delay` | - | *string* | `30min`, `9am` | Timestamp or duration for delayed delivery | +| `email` | - | *e-mail address* | `phil@example.com` | E-mail address for e-mail notifications | ## Click action You can define which URL to open when a notification is clicked. This may be useful if your notification is related diff --git a/docs/releases.md b/docs/releases.md index 0b365edc..ffa4456d 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -45,6 +45,7 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release * Do not allow comma in topic name in publish via GET endpoint (no ticket) * Add "Access-Control-Allow-Origin: *" for attachments (no ticket, thanks to @FrameXX) * Make pruning run again in web app ([#186](https://github.com/binwiederhier/ntfy/issues/186)) +* Added missing params `delay` and `email` to publish as JSON body (no ticket) **Documentation:** diff --git a/server/server.go b/server/server.go index c59d1ca0..726d5a4d 100644 --- a/server/server.go +++ b/server/server.go @@ -1135,6 +1135,12 @@ func (s *Server) transformBodyJSON(next handleFunc) handleFunc { if m.Click != "" { r.Header.Set("X-Click", m.Click) } + if m.Email != "" { + r.Header.Set("X-Email", m.Email) + } + if m.Delay != "" { + r.Header.Set("X-Delay", m.Delay) + } return next(w, r, v) } } diff --git a/server/server_test.go b/server/server_test.go index 0827cc90..062821d8 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -873,7 +873,8 @@ func TestServer_PublishUnifiedPushText(t *testing.T) { func TestServer_PublishAsJSON(t *testing.T) { s := newTestServer(t, newTestConfig(t)) body := `{"topic":"mytopic","message":"A message","title":"a title\nwith lines","tags":["tag1","tag 2"],` + - `"not-a-thing":"ok", "attach":"http://google.com","filename":"google.pdf", "click":"http://ntfy.sh","priority":4}` + `"not-a-thing":"ok", "attach":"http://google.com","filename":"google.pdf", "click":"http://ntfy.sh","priority":4,` + + `"delay":"30min"}` response := request(t, s, "PUT", "/", body, nil) require.Equal(t, 200, response.Code) @@ -886,6 +887,22 @@ func TestServer_PublishAsJSON(t *testing.T) { require.Equal(t, "google.pdf", m.Attachment.Name) require.Equal(t, "http://ntfy.sh", m.Click) require.Equal(t, 4, m.Priority) + require.True(t, m.Time > time.Now().Unix()+29*60) + require.True(t, m.Time < time.Now().Unix()+31*60) +} + +func TestServer_PublishAsJSON_WithEmail(t *testing.T) { + mailer := &testMailer{} + s := newTestServer(t, newTestConfig(t)) + s.mailer = mailer + body := `{"topic":"mytopic","message":"A message","email":"phil@example.com"}` + response := request(t, s, "PUT", "/", body, nil) + require.Equal(t, 200, response.Code) + + m := toMessage(t, response.Body.String()) + require.Equal(t, "mytopic", m.Topic) + require.Equal(t, "A message", m.Message) + require.Equal(t, 1, mailer.count) } func TestServer_PublishAsJSON_Invalid(t *testing.T) { diff --git a/server/types.go b/server/types.go index 6594f050..049e7029 100644 --- a/server/types.go +++ b/server/types.go @@ -52,6 +52,8 @@ type publishMessage struct { Click string `json:"click"` Attach string `json:"attach"` Filename string `json:"filename"` + Email string `json:"email"` + Delay string `json:"delay"` } // messageEncoder is a function that knows how to encode a message