Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Handle empty from in /messages as per MSC3567 (#3298)
Browse files Browse the repository at this point in the history
  • Loading branch information
devonh authored Jan 9, 2024
1 parent 9510fa0 commit 57646d5
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions syncapi/routing/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ func OnIncomingMessagesRequest(
var fromStream *types.StreamingToken
fromQuery := req.URL.Query().Get("from")
toQuery := req.URL.Query().Get("to")
emptyFromSupplied := fromQuery == ""
if emptyFromSupplied {
// NOTSPEC: We will pretend they used the latest sync token if no ?from= was provided.
// We do this to allow clients to get messages without having to call `/sync` e.g Cerulean
currPos := srp.Notifier.CurrentPosition()
fromQuery = currPos.String()
}

// Direction to return events from.
dir := req.URL.Query().Get("dir")
Expand All @@ -155,6 +148,23 @@ func OnIncomingMessagesRequest(
// to have one of the two accepted values (so dir == "f" <=> !backwardOrdering).
backwardOrdering := (dir == "b")

emptyFromSupplied := fromQuery == ""
if emptyFromSupplied {
// If "from" isn't provided, it defaults to either the earliest stream
// position (if we're going forward) or to the latest one (if we're
// going backward).

var from types.TopologyToken
if backwardOrdering {
from = types.TopologyToken{Depth: math.MaxInt64, PDUPosition: math.MaxInt64}
} else {
// go 1 earlier than the first event so we correctly fetch the earliest event
// this is because Database.GetEventsInTopologicalRange is exclusive of the lower-bound.
from = types.TopologyToken{}
}
fromQuery = from.String()
}

from, err := types.NewTopologyTokenFromString(fromQuery)
if err != nil {
var streamToken types.StreamingToken
Expand Down

0 comments on commit 57646d5

Please sign in to comment.