Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions api/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package api
import (
"encoding/json"
"errors"
"net/url"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/gotify/location"
"github.com/gotify/server/v2/auth"
"github.com/gotify/server/v2/model"
)
Expand Down Expand Up @@ -104,13 +104,10 @@ func buildWithPaging(ctx *gin.Context, paging *pagingParams, messages []*model.M
if len(messages) > paging.Limit {
useMessages = messages[:len(messages)-1]
since = useMessages[len(useMessages)-1].ID
url := location.Get(ctx)
url.Path = ctx.Request.URL.Path
query := url.Query()
query := url.Values{}
query.Add("limit", strconv.Itoa(paging.Limit))
query.Add("since", strconv.FormatUint(uint64(since), 10))
url.RawQuery = query.Encode()
next = url.String()
next = ctx.Request.URL.Path + "?" + query.Encode()
}
return &model.PagedMessages{
Paging: model.Paging{Size: len(useMessages), Limit: paging.Limit, Next: next, Since: since},
Expand Down
12 changes: 6 additions & 6 deletions api/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ func (s *MessageSuite) Test_ensureCorrectJsonRepresentation() {
t, _ := time.Parse("2006/01/02", "2017/01/02")

actual := &model.PagedMessages{
Paging: model.Paging{Limit: 5, Since: 122, Size: 5, Next: "http://example.com/message?limit=5&since=122"},
Paging: model.Paging{Limit: 5, Since: 122, Size: 5, Next: "/message?limit=5&since=122"},
Messages: []*model.MessageExternal{{ID: 55, ApplicationID: 2, Message: "hi", Title: "hi", Date: t, Priority: intPtr(4), Extras: map[string]any{
"test::string": "string",
"test::array": []any{1, 2, 3},
"test::int": 1,
"test::float": 0.5,
}}},
}
test.JSONEquals(s.T(), actual, `{"paging": {"limit":5, "since": 122, "size": 5, "next": "http://example.com/message?limit=5&since=122"},
test.JSONEquals(s.T(), actual, `{"paging": {"limit":5, "since": 122, "size": 5, "next": "/message?limit=5&since=122"},
"messages": [{"id":55,"appid":2,"message":"hi","title":"hi","priority":4,"date":"2017-01-02T00:00:00Z","extras":{"test::string":"string","test::array":[1,2,3],"test::int":1,"test::float":0.5}}]}`)
}

Expand Down Expand Up @@ -99,7 +99,7 @@ func (s *MessageSuite) Test_GetMessages_WithLimit_ReturnsNext() {

// Since: entries with ids from 100 - 96 will be returned (5 entries)
expected := &model.PagedMessages{
Paging: model.Paging{Limit: 5, Size: 5, Since: 96, Next: "http://example.com/messages?limit=5&since=96"},
Paging: model.Paging{Limit: 5, Size: 5, Since: 96, Next: "/messages?limit=5&since=96"},
Messages: toExternalMessages(messages[:5]),
}

Expand All @@ -123,7 +123,7 @@ func (s *MessageSuite) Test_GetMessages_WithLimit_WithSince_ReturnsNext() {

// Since: entries with ids from 54 - 42 will be returned (13 entries)
expected := &model.PagedMessages{
Paging: model.Paging{Limit: 13, Size: 13, Since: 42, Next: "http://example.com/messages?limit=13&since=42"},
Paging: model.Paging{Limit: 13, Size: 13, Since: 42, Next: "/messages?limit=13&since=42"},
Messages: toExternalMessages(messages[46 : 46+13]),
}
test.BodyEquals(s.T(), expected, s.recorder)
Expand Down Expand Up @@ -189,7 +189,7 @@ func (s *MessageSuite) Test_GetMessagesWithToken_WithLimit_ReturnsNext() {

// Since: entries with ids from 100 - 92 will be returned (9 entries)
expected := &model.PagedMessages{
Paging: model.Paging{Limit: 9, Size: 9, Since: 92, Next: "http://example.com/app/2/message?limit=9&since=92"},
Paging: model.Paging{Limit: 9, Size: 9, Since: 92, Next: "/app/2/message?limit=9&since=92"},
Messages: toExternalMessages(messages[:9]),
}

Expand All @@ -212,7 +212,7 @@ func (s *MessageSuite) Test_GetMessagesWithToken_WithLimit_WithSince_ReturnsNext

// Since: entries with ids from 54 - 42 will be returned (13 entries)
expected := &model.PagedMessages{
Paging: model.Paging{Limit: 13, Size: 13, Since: 42, Next: "http://example.com/app/2/message?limit=13&since=42"},
Paging: model.Paging{Limit: 13, Size: 13, Since: 42, Next: "/app/2/message?limit=13&since=42"},
Messages: toExternalMessages(messages[46 : 46+13]),
}
test.BodyEquals(s.T(), expected, s.recorder)
Expand Down
4 changes: 2 additions & 2 deletions docs/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3211,11 +3211,11 @@
"example": 123
},
"next": {
"description": "The request url for the next page. Empty/Null when no next page is available.",
"description": "The relative path for the next page. Empty/Null when no next page is available. Should be combined with the gotify base url.",
"type": "string",
"x-go-name": "Next",
"readOnly": true,
"example": "http://example.com/message?limit=50\u0026since=123456"
"example": "/message?limit=50\u0026since=123456"
},
"since": {
"description": "The ID of the last message returned in the current request. Use this as alternative to the next link.",
Expand Down
4 changes: 2 additions & 2 deletions model/paging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package model
//
// swagger:model Paging
type Paging struct {
// The request url for the next page. Empty/Null when no next page is available.
// The relative path for the next page. Empty/Null when no next page is available. Should be combined with the gotify base url.
//
// read only: true
// required: false
// example: http://example.com/message?limit=50&since=123456
// example: /message?limit=50&since=123456
Next string `json:"next,omitempty"`
// The amount of messages that got returned in the current request.
//
Expand Down
Loading