Skip to content

Commit 5e3ea11

Browse files
authored
Replace "Pagination" section in C-S API with text in appendices (#3366)
The Pagination section in the C-S API was, basically, full of rubbish. I think that anything of any value it contained was repeated either directly on the API definitions or in the text specific to syncing at https://spec.matrix.org/unstable/client-server-api/#syncing. The conventions I've added to the Appendices are based on the discussions in #1898. They are there because I don't want to have to go through it all again next time we add a paginated API. Fixes: #1898 Fixes: #2268
1 parent da51f7f commit 5e3ea11

File tree

4 files changed

+29
-84
lines changed

4 files changed

+29
-84
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove the inaccurate 'Pagination' section.

content/appendices.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,8 @@ The event signing algorithm should emit the following signed event:
10641064
## Conventions for Matrix APIs
10651065

10661066
This section is intended primarily to guide API designers when adding to Matrix,
1067-
setting guidelines to follow for how those APIs should work. This is important to
1068-
maintain consistency with the Matrix protocol, and thus improve developer
1067+
setting guidelines to follow for how those APIs should work. This is important to
1068+
maintain consistency with the Matrix protocol, and thus improve developer
10691069
experience.
10701070

10711071
### HTTP endpoint and JSON property naming
@@ -1079,3 +1079,29 @@ The key names in JSON objects passed over the API also follow this convention.
10791079
There are a few historical exceptions to this rule, such as `/createRoom`.
10801080
These inconsistencies may be addressed in future versions of this specification.
10811081
{{% /boxes/note %}}
1082+
1083+
### Pagination
1084+
1085+
REST API endpoints which can return multiple "pages" of results should adopt the
1086+
following conventions.
1087+
1088+
* If more results are available, the endpoint should return a property named
1089+
`next_batch`. The value should be a string token which can be passed into
1090+
a subsequent call to the endpoint to retrieve the next page of results.
1091+
1092+
If no more results are available, this is indicated by *omitting* the
1093+
`next_batch` property from the results.
1094+
1095+
* The endpoint should accept a query-parameter named `from` which the client
1096+
is expected to set to the value of a previous `next_batch`.
1097+
1098+
* Some endpoints might support pagination in two directions (example:
1099+
`/messages`, which can be used to move forward or backwards in the timeline
1100+
from a known point). In this case, the endpoint should return a `prev_batch`
1101+
property which can be passed into `from` to receive the previous page of
1102+
results.
1103+
1104+
Avoid having a separate "direction" parameter, which is generally redundant:
1105+
the tokens returned by `next_batch` and `prev_batch` should contain enough
1106+
information for subsequent calls to the API to know which page of results
1107+
they should return.

content/client-server-api/_index.md

-82
Original file line numberDiff line numberDiff line change
@@ -1214,88 +1214,6 @@ using an `unstable` version.
12141214
When this capability is not listed, clients should use `"1"` as the
12151215
default and only stable `available` room version.
12161216

1217-
## Pagination
1218-
1219-
{{% boxes/note %}}
1220-
The paths referred to in this section are not actual endpoints. They
1221-
only serve as examples to explain how pagination functions.
1222-
{{% /boxes/note %}}
1223-
1224-
Pagination is the process of dividing a dataset into multiple discrete
1225-
pages. Matrix makes use of pagination to allow clients to view extremely
1226-
large datasets. These datasets are not limited to events in a room (for
1227-
example clients may want to paginate a list of rooms in addition to
1228-
events within those rooms). Regardless of what is being paginated, there
1229-
is a common approach which is used to give clients an easy way of
1230-
selecting subsets of a potentially changing dataset. Each endpoint that
1231-
uses pagination may use different parameters. However the theme among
1232-
them is that they take a `from` and `to` token, and occasionally a
1233-
`limit` and `dir`. Together, these parameters describe the position in a
1234-
data set, where `from` and `to` are known as "stream tokens" matching
1235-
the regular expression `[a-zA-Z0-9.=_-]+`. If supported, the `dir`
1236-
defines the direction of events to return: either forwards (`f`) or
1237-
backwards (`b`). The response may contain tokens that can be used for
1238-
retrieving results before or after the returned set. These tokens may be
1239-
called <span class="title-ref">start</span> or <span
1240-
class="title-ref">prev\_batch</span> for retrieving the previous result
1241-
set, or <span class="title-ref">end</span>, <span
1242-
class="title-ref">next\_batch</span> or <span
1243-
class="title-ref">next\_token</span> for retrieving the next result set.
1244-
1245-
In the following examples, 'START' and 'END' are placeholders to signify
1246-
the start and end of the data sets respectively.
1247-
1248-
For example, if an endpoint had events E1 -&gt; E15. The client wants
1249-
the last 5 events and doesn't know any previous events:
1250-
1251-
```
1252-
S E
1253-
|-E1-E2-E3-E4-E5-E6-E7-E8-E9-E10-E11-E12-E13-E14-E15-|
1254-
| | |
1255-
| _____| <--backwards-- |
1256-
|__________________ | | ________|
1257-
| | | |
1258-
GET /somepath?to=START&limit=5&dir=b&from=END
1259-
Returns:
1260-
E15,E14,E13,E12,E11
1261-
```
1262-
1263-
Another example: a public room list has rooms R1 -&gt; R17. The client
1264-
is showing 5 rooms at a time on screen, and is on page 2. They want to
1265-
now show page 3 (rooms R11 -&gt; 15):
1266-
1267-
```
1268-
S E
1269-
| 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | stream token
1270-
|-R1-R2-R3-R4-R5-R6-R7-R8-R9-R10-R11-R12-R13-R14-R15-R16-R17| room
1271-
|____________| |________________|
1272-
| |
1273-
Currently |
1274-
viewing |
1275-
|
1276-
GET /roomslist?from=9&to=END&limit=5
1277-
Returns: R11,R12,R13,R14,R15
1278-
```
1279-
1280-
Note that tokens are treated in an *exclusive*, not inclusive, manner.
1281-
The end token from the initial request was '9' which corresponded to
1282-
R10. When the 2nd request was made, R10 did not appear again, even
1283-
though from=9 was specified. If you know the token, you already have the
1284-
data.
1285-
1286-
Responses for pagination-capable endpoints SHOULD have a `chunk` array
1287-
alongside the applicable stream tokens to represent the result set.
1288-
1289-
In general, when the end of a result set is reached the applicable
1290-
stream token will be excluded from the response. For example, if a user
1291-
was backwards-paginating events in a room they'd eventually reach the
1292-
first event in the room. In this scenario, the `prev_batch` token would
1293-
be excluded from the response. Some paginated endpoints are open-ended
1294-
in one direction, such as endpoints which expose an event stream for an
1295-
active room. In this case, it is not possible for the client to reach
1296-
the true "end" of the data set and therefore should always be presented
1297-
with a token to keep moving forwards.
1298-
12991217
## Filtering
13001218

13011219
Filters can be created on the server and can be passed as a parameter to

0 commit comments

Comments
 (0)