From 66cf54e4586a6db8ff660861935521ff47ce59d1 Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Tue, 14 Feb 2023 14:05:41 -0500 Subject: [PATCH] Fix delayed messages expiry, thanks to @karmanyaahm --- server/server.go | 2 +- server/server_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/server/server.go b/server/server.go index 169ccd72..d9102846 100644 --- a/server/server.go +++ b/server/server.go @@ -618,7 +618,7 @@ func (s *Server) handlePublishWithoutResponse(r *http.Request, v *visitor) (*mes } m.Sender = v.IP() m.User = v.MaybeUserID() - m.Expires = time.Now().Add(v.Limits().MessageExpiryDuration).Unix() + m.Expires = time.Unix(m.Time, 0).Add(v.Limits().MessageExpiryDuration).Unix() if err := s.handlePublishBody(r, v, m, body, unifiedpush); err != nil { return nil, err } diff --git a/server/server_test.go b/server/server_test.go index 375a5905..fb78523c 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -324,6 +324,18 @@ func TestServer_PublishAt(t *testing.T) { require.Equal(t, "9.9.9.9", messages[0].Sender.String()) // It's stored in the DB though! } +func TestServer_PublishAt_Expires(t *testing.T) { + s := newTestServer(t, newTestConfig(t)) + + response := request(t, s, "PUT", "/mytopic", "a message", map[string]string{ + "In": "2 days", + }) + require.Equal(t, 200, response.Code) + m := toMessage(t, response.Body.String()) + require.True(t, m.Expires > time.Now().Add(12*time.Hour+48*time.Hour-time.Minute).Unix()) + require.True(t, m.Expires < time.Now().Add(12*time.Hour+48*time.Hour+time.Minute).Unix()) +} + func TestServer_PublishAtWithCacheError(t *testing.T) { s := newTestServer(t, newTestConfig(t))