mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-20 21:33:19 -06:00
Default to the default timeline limit if it's unset, also strip the extra event correctly
This commit is contained in:
parent
35602c00f3
commit
a4ecedc0c8
|
|
@ -15,14 +15,17 @@
|
||||||
package routing
|
package routing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
||||||
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
"github.com/matrix-org/dendrite/syncapi/storage"
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
||||||
|
"github.com/matrix-org/dendrite/syncapi/sync"
|
||||||
"github.com/matrix-org/dendrite/userapi/api"
|
"github.com/matrix-org/dendrite/userapi/api"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetFilter implements GET /_matrix/client/r0/user/{userId}/filter/{filterId}
|
// GetFilter implements GET /_matrix/client/r0/user/{userId}/filter/{filterId}
|
||||||
|
|
@ -81,8 +84,27 @@ func PutFilter(
|
||||||
|
|
||||||
var filter gomatrixserverlib.Filter
|
var filter gomatrixserverlib.Filter
|
||||||
|
|
||||||
if reqErr := httputil.UnmarshalJSONRequest(req, &filter); reqErr != nil {
|
defer req.Body.Close() // nolint:errcheck
|
||||||
return *reqErr
|
body, err := ioutil.ReadAll(req.Body)
|
||||||
|
if err != nil {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusBadRequest,
|
||||||
|
JSON: jsonerror.BadJSON("The request body could not be read. " + err.Error()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.Unmarshal(body, &filter); err != nil {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusBadRequest,
|
||||||
|
JSON: jsonerror.BadJSON("The request body could not be decoded into valid JSON. " + err.Error()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the filter `limit` is `int` which defaults to 0 if not set which is not what we want. We want to use the default
|
||||||
|
// limit if it is unset, which is what this does.
|
||||||
|
limitRes := gjson.GetBytes(body, "room.timeline.limit")
|
||||||
|
if !limitRes.Exists() {
|
||||||
|
util.GetLogger(req.Context()).Infof("missing timeline limit, using default")
|
||||||
|
filter.Room.Timeline.Limit = sync.DefaultTimelineLimit
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate generates a user-friendly error
|
// Validate generates a user-friendly error
|
||||||
|
|
|
||||||
|
|
@ -317,13 +317,6 @@ func (s *outputRoomEventsStatements) SelectRecentEvents(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
// we queried for 1 more than the limit, so if we returned one more mark limited=true
|
|
||||||
limited := false
|
|
||||||
if len(events) > limit {
|
|
||||||
limited = true
|
|
||||||
// re-slice the extra event out
|
|
||||||
events = events[:len(events)-1]
|
|
||||||
}
|
|
||||||
if chronologicalOrder {
|
if chronologicalOrder {
|
||||||
// The events need to be returned from oldest to latest, which isn't
|
// The events need to be returned from oldest to latest, which isn't
|
||||||
// necessary the way the SQL query returns them, so a sort is necessary to
|
// necessary the way the SQL query returns them, so a sort is necessary to
|
||||||
|
|
@ -332,6 +325,17 @@ func (s *outputRoomEventsStatements) SelectRecentEvents(
|
||||||
return events[i].StreamPosition < events[j].StreamPosition
|
return events[i].StreamPosition < events[j].StreamPosition
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
// we queried for 1 more than the limit, so if we returned one more mark limited=true
|
||||||
|
limited := false
|
||||||
|
if len(events) > limit {
|
||||||
|
limited = true
|
||||||
|
// re-slice the extra (oldest) event out: in chronological order this is the first entry, else the last.
|
||||||
|
if chronologicalOrder {
|
||||||
|
events = events[1:]
|
||||||
|
} else {
|
||||||
|
events = events[:len(events)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return events, limited, nil
|
return events, limited, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -328,13 +328,6 @@ func (s *outputRoomEventsStatements) SelectRecentEvents(
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
// we queried for 1 more than the limit, so if we returned one more mark limited=true
|
|
||||||
limited := false
|
|
||||||
if len(events) > limit {
|
|
||||||
limited = true
|
|
||||||
// re-slice the extra event out
|
|
||||||
events = events[:len(events)-1]
|
|
||||||
}
|
|
||||||
if chronologicalOrder {
|
if chronologicalOrder {
|
||||||
// The events need to be returned from oldest to latest, which isn't
|
// The events need to be returned from oldest to latest, which isn't
|
||||||
// necessary the way the SQL query returns them, so a sort is necessary to
|
// necessary the way the SQL query returns them, so a sort is necessary to
|
||||||
|
|
@ -343,6 +336,17 @@ func (s *outputRoomEventsStatements) SelectRecentEvents(
|
||||||
return events[i].StreamPosition < events[j].StreamPosition
|
return events[i].StreamPosition < events[j].StreamPosition
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
// we queried for 1 more than the limit, so if we returned one more mark limited=true
|
||||||
|
limited := false
|
||||||
|
if len(events) > limit {
|
||||||
|
limited = true
|
||||||
|
// re-slice the extra (oldest) event out: in chronological order this is the first entry, else the last.
|
||||||
|
if chronologicalOrder {
|
||||||
|
events = events[1:]
|
||||||
|
} else {
|
||||||
|
events = events[:len(events)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
return events, limited, nil
|
return events, limited, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -363,7 +363,7 @@ func newTestSyncRequest(userID, deviceID string, since types.StreamingToken) syn
|
||||||
timeout: 1 * time.Minute,
|
timeout: 1 * time.Minute,
|
||||||
since: &since,
|
since: &since,
|
||||||
wantFullState: false,
|
wantFullState: false,
|
||||||
limit: defaultTimelineLimit,
|
limit: DefaultTimelineLimit,
|
||||||
log: util.GetLogger(context.TODO()),
|
log: util.GetLogger(context.TODO()),
|
||||||
ctx: context.TODO(),
|
ctx: context.TODO(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultSyncTimeout = time.Duration(0)
|
const defaultSyncTimeout = time.Duration(0)
|
||||||
const defaultTimelineLimit = 20
|
const DefaultTimelineLimit = 20
|
||||||
|
|
||||||
type filter struct {
|
type filter struct {
|
||||||
Room struct {
|
Room struct {
|
||||||
|
|
@ -68,7 +68,7 @@ func newSyncRequest(req *http.Request, device userapi.Device, syncDB storage.Dat
|
||||||
tok := types.NewStreamToken(0, 0)
|
tok := types.NewStreamToken(0, 0)
|
||||||
since = &tok
|
since = &tok
|
||||||
}
|
}
|
||||||
timelineLimit := defaultTimelineLimit
|
timelineLimit := DefaultTimelineLimit
|
||||||
// TODO: read from stored filters too
|
// TODO: read from stored filters too
|
||||||
filterQuery := req.URL.Query().Get("filter")
|
filterQuery := req.URL.Query().Get("filter")
|
||||||
if filterQuery != "" {
|
if filterQuery != "" {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue