mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-02 06:12:45 +00:00
Refactor to allow passing actual filter
This commit is contained in:
parent
82afb32464
commit
4cf73a5dc2
4 changed files with 40 additions and 51 deletions
|
@ -33,21 +33,11 @@ const defaultSyncTimeout = time.Duration(0)
|
|||
const defaultIncludeLeave = false
|
||||
const DefaultTimelineLimit = 20
|
||||
|
||||
type filter struct {
|
||||
Room struct {
|
||||
IncludeLeave *bool `json:"include_leave"`
|
||||
Timeline struct {
|
||||
Limit *int `json:"limit"`
|
||||
} `json:"timeline"`
|
||||
} `json:"room"`
|
||||
}
|
||||
|
||||
// syncRequest represents a /sync request, with sensible defaults/sanity checks applied.
|
||||
type syncRequest struct {
|
||||
ctx context.Context
|
||||
device userapi.Device
|
||||
limit int
|
||||
includeLeave bool
|
||||
filter *gomatrixserverlib.Filter
|
||||
timeout time.Duration
|
||||
since types.StreamingToken // nil means that no since token was supplied
|
||||
wantFullState bool
|
||||
|
@ -66,21 +56,14 @@ func newSyncRequest(req *http.Request, device userapi.Device, syncDB storage.Dat
|
|||
return nil, err
|
||||
}
|
||||
}
|
||||
timelineLimit := DefaultTimelineLimit
|
||||
includeLeave := defaultIncludeLeave
|
||||
|
||||
var f *gomatrixserverlib.Filter
|
||||
// TODO: read from stored filters too
|
||||
filterQuery := req.URL.Query().Get("filter")
|
||||
if filterQuery != "" {
|
||||
if filterQuery[0] == '{' {
|
||||
// attempt to parse the timeline limit at least
|
||||
var f filter
|
||||
err := json.Unmarshal([]byte(filterQuery), &f)
|
||||
if err == nil && f.Room.Timeline.Limit != nil {
|
||||
timelineLimit = *f.Room.Timeline.Limit
|
||||
}
|
||||
if err == nil && f.Room.IncludeLeave != nil {
|
||||
includeLeave = *f.Room.IncludeLeave
|
||||
}
|
||||
json.Unmarshal([]byte(filterQuery), &f)
|
||||
} else {
|
||||
// attempt to load the filter ID
|
||||
localpart, _, err := gomatrixserverlib.SplitID('@', device.UserID)
|
||||
|
@ -88,10 +71,10 @@ func newSyncRequest(req *http.Request, device userapi.Device, syncDB storage.Dat
|
|||
util.GetLogger(req.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed")
|
||||
return nil, err
|
||||
}
|
||||
f, err := syncDB.GetFilter(req.Context(), localpart, filterQuery)
|
||||
if err == nil {
|
||||
timelineLimit = f.Room.Timeline.Limit
|
||||
includeLeave = f.Room.IncludeLeave
|
||||
f, err = syncDB.GetFilter(req.Context(), localpart, filterQuery)
|
||||
if err != nil {
|
||||
util.GetLogger(req.Context()).WithError(err).Error("syncDB.GetFilter failed")
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -102,8 +85,7 @@ func newSyncRequest(req *http.Request, device userapi.Device, syncDB storage.Dat
|
|||
timeout: timeout,
|
||||
since: since,
|
||||
wantFullState: wantFullState,
|
||||
limit: timelineLimit,
|
||||
includeLeave: includeLeave,
|
||||
filter: f,
|
||||
log: util.GetLogger(req.Context()),
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -140,12 +140,11 @@ func (rp *RequestPool) OnIncomingSyncRequest(req *http.Request, device *userapi.
|
|||
}
|
||||
|
||||
logger := util.GetLogger(req.Context()).WithFields(log.Fields{
|
||||
"user_id": device.UserID,
|
||||
"device_id": device.ID,
|
||||
"since": syncReq.since,
|
||||
"timeout": syncReq.timeout,
|
||||
"limit": syncReq.limit,
|
||||
"include_leave": syncReq.includeLeave,
|
||||
"user_id": device.UserID,
|
||||
"device_id": device.ID,
|
||||
"since": syncReq.since,
|
||||
"timeout": syncReq.timeout,
|
||||
"filter": syncReq.filter,
|
||||
})
|
||||
|
||||
activeSyncRequests.Inc()
|
||||
|
@ -248,9 +247,12 @@ func (rp *RequestPool) OnIncomingKeyChangeRequest(req *http.Request, device *use
|
|||
JSON: jsonerror.InvalidArgumentValue("bad 'to' value"),
|
||||
}
|
||||
}
|
||||
|
||||
// work out room joins/leaves
|
||||
var f gomatrixserverlib.Filter
|
||||
f.Room.Timeline.Limit = 10
|
||||
res, err := rp.db.IncrementalSync(
|
||||
req.Context(), types.NewResponse(), *device, fromToken, toToken, 10, false,
|
||||
req.Context(), types.NewResponse(), *device, fromToken, toToken, &f, false,
|
||||
)
|
||||
if err != nil {
|
||||
util.GetLogger(req.Context()).WithError(err).Error("Failed to IncrementalSync")
|
||||
|
@ -286,12 +288,12 @@ func (rp *RequestPool) currentSyncForUser(req syncRequest, latestPos types.Strea
|
|||
|
||||
// TODO: handle ignored users
|
||||
if req.since.IsEmpty() {
|
||||
res, err = rp.db.CompleteSync(req.ctx, res, req.device, req.limit, req.includeLeave)
|
||||
res, err = rp.db.CompleteSync(req.ctx, res, req.device, req.filter)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("rp.db.CompleteSync: %w", err)
|
||||
}
|
||||
} else {
|
||||
res, err = rp.db.IncrementalSync(req.ctx, res, req.device, req.since, latestPos, req.limit, req.wantFullState)
|
||||
res, err = rp.db.IncrementalSync(req.ctx, res, req.device, req.since, latestPos, req.filter, req.wantFullState)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("rp.db.IncrementalSync: %w", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue