|
| 1 | +# MSC3715: Add a pagination direction parameter to `/relations` |
| 2 | + |
| 3 | +[MSC2675](https://github.com/matrix-org/matrix-doc/pull/2675) introduced the |
| 4 | +`/relations` API as an endpoint to paginate over the relations of an event. Unfortunately, |
| 5 | +it is missing a `dir` parameter which is necessary to properly load thread timelines |
| 6 | +at an arbitrary location. |
| 7 | + |
| 8 | +Consider a long thread (e.g. consisting of 300 events), if a user receives a link |
| 9 | +to an event somewhere in the middle it is desirable for the client to only display |
| 10 | +a few dozen messages: the event itself and some context before and after it. As |
| 11 | +the user scrolls additional content from the thread should be fetched. |
| 12 | + |
| 13 | +Currently clients are forced to fetch *the entire thread* until the target |
| 14 | +message (plus any surround context) is reached in order to implement this |
| 15 | +functionality. A simpler implementation would allow the client to: |
| 16 | + |
| 17 | +1. Load events in a thread backwards from a pagination token, |
| 18 | +2. Load events in a thread forwards from a pagination token, |
| 19 | +3. Load context around an event. |
| 20 | + |
| 21 | +1 & 3 are currently possible; this MSC attempts to solve condition 2. |
| 22 | + |
| 23 | +With the `dir` paraemter on `/relations, it now becomes possible for a client to: |
| 24 | + |
| 25 | +1. Call `/context` on the target event to get a pagination token. |
| 26 | +2. Call `/relations` twice (once with `dir=b` and once with `dir=f`) on the same |
| 27 | + pagination token to collect any contextual events. |
| 28 | +3. Store the resulting pagination tokens to paginate further, if necessary. |
| 29 | + |
| 30 | +This allows the thread to be fetched in arbitrary chunks: |
| 31 | + |
| 32 | +```mermaid |
| 33 | +flowchart RL |
| 34 | + subgraph /relations?dir=f |
| 35 | + $2-->$1 |
| 36 | + end |
| 37 | + $1-->$0 |
| 38 | + subgraph /context/$0 |
| 39 | + $0 |
| 40 | + end |
| 41 | + $0-->$-1 |
| 42 | + subgraph /relations?dir=b |
| 43 | + $-1-->$-2 |
| 44 | + end |
| 45 | +``` |
| 46 | + |
| 47 | +## Proposal |
| 48 | + |
| 49 | +It is proposed to add the `dir` parameter to the `/relations` API for parity with `/messages`. |
| 50 | +It will [have the same as definition as for `/messages`](https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientv3roomsroomidmessages), |
| 51 | +which is copied below: |
| 52 | + |
| 53 | +> The direction to return events from. If this is set to `f`, events will |
| 54 | +> be returned in chronological order starting at `from`. If it is set to `b`, |
| 55 | +> events will be returned in *reverse* chronological order, again starting at `from`. |
| 56 | +> |
| 57 | +> One of: `[b f]`. |
| 58 | +
|
| 59 | +In order to be backwards compatible with MSC2675 (and Synapse's legacy |
| 60 | +implementation), the `dir` parameter must be optional (defaulting to `b`). This |
| 61 | +differs from the specification of `/messages` where the `dir` parameter is |
| 62 | +required.[^1] |
| 63 | + |
| 64 | +Including the `dir` parameter will make it easier to request missing relation |
| 65 | +information without needed to paginate through known information -- this is |
| 66 | +particularly needed for mobile or low-bandwidth devices where it is desired to |
| 67 | +keep the round-trips to the server to a minimum. |
| 68 | + |
| 69 | +It is additionally useful to unify similar endpoints as much as possible to avoid |
| 70 | +surprises for developers. |
| 71 | + |
| 72 | +Since this endpoint can now be used to paginate backwards over children events, |
| 73 | +it is also useful for the `from` parameter to accept `prev_batch` values from |
| 74 | +previous calls (as well as `next_batch` values, as is currently specified). The |
| 75 | +[definition of the `from` parameter](https://spec.matrix.org/unstable/client-server-api/#get_matrixclientv1roomsroomidrelationseventid) |
| 76 | +is updated: |
| 77 | + |
| 78 | +> Can be a `from_batch` token **or `next_batch`** token from a previous call, or a |
| 79 | +> returned `start` token from `/messages`, or a `next_batch` token from `/sync`. |
| 80 | +
|
| 81 | +(Bold indicates new text.) |
| 82 | +## Potential issues |
| 83 | + |
| 84 | +`/messages` does have one additional parameter (`filter`) which would still not |
| 85 | +be implemented for `/relations`. It is unclear if this parameter is useful here. |
| 86 | + |
| 87 | + |
| 88 | +## Alternatives |
| 89 | + |
| 90 | +The endpoint could be replaced with the `/event_relationships` API proposed in |
| 91 | +[MSC2836](https://github.com/matrix-org/matrix-doc/pull/2836). That API would |
| 92 | +add significant complexity over the current `/relations` API (e.g. arbitrary |
| 93 | +of relations) and is not necessary to simply iterate events in the reverse ordering. |
| 94 | + |
| 95 | +Instead of having a different default direction from `/messages`, a `v2` version |
| 96 | +could be added which accepts the `dir` parameter with the same default directionality |
| 97 | +as `/messages`, but the opposite of the current (`v1`) version of the endpoint. |
| 98 | + |
| 99 | + |
| 100 | +## Security considerations |
| 101 | + |
| 102 | +None. |
| 103 | + |
| 104 | +## Unstable prefix |
| 105 | + |
| 106 | +Before standardization, `org.matrix.msc3715.dir` should be used in place of `dir`. |
| 107 | + |
| 108 | +[^1]: Note that [Synapse's implementation](https://github.com/matrix-org/synapse/blob/10a88ba91cb16ccf757984f0a7d41ddf8b4dc07f/synapse/streams/config.py#L48) |
| 109 | +does not require a `dir` parameter for the `/messages` API and defaults to `f` |
| 110 | +if it is not provided. |
0 commit comments