|
| 1 | +# Improving the way membership lists are queried |
| 2 | + |
| 3 | +## Problem scope |
| 4 | + |
| 5 | +A common operation for bots, bridges, scripts, and clients is to determine what rooms the user is a |
| 6 | +member of and who the members of those rooms are. Although possible with the current specification, |
| 7 | +the API can be improved to provide more granular and simplified access to this information. |
| 8 | + |
| 9 | +The affected routes are: |
| 10 | + |
| 11 | +* `GET /_matrix/client/r0/rooms/{roomId}/members` |
| 12 | +* `GET /_matrix/client/r0/rooms/{roomId}/joined_members` |
| 13 | +* `GET /_matrix/client/r0/joined_rooms` |
| 14 | + |
| 15 | +This proposal aims to resolve [matrix-doc#1123](https://github.com/matrix-org/matrix-doc/issues/1123). |
| 16 | + |
| 17 | +## Background |
| 18 | + |
| 19 | +The `/joined_members` and `/joined_rooms` endpoints were originally added in [synapse#1680](https://github.com/matrix-org/synapse/pull/1680) |
| 20 | +during a time when the IRC bridge on matrix.org was under extreme load. The endpoints were fully |
| 21 | +intended to alleviate load by having the bridge do less work and have the server doing more. |
| 22 | + |
| 23 | +## Proposal |
| 24 | + |
| 25 | +This proposal calls for both `/joined_members` and `/joined_rooms` to be deprecated. The |
| 26 | +deprecation is to be coupled with improving how `/members` works and introducing a new `/rooms` |
| 27 | +endpoint, which will work in a very similar way to the updated `/members` endpoint. Both endpoints |
| 28 | +are proposed to get some way to filter based upon membership, as outlined in the options below. |
| 29 | + |
| 30 | +### Option 1: Query string |
| 31 | + |
| 32 | +A new query parameter, `membership`, should be added to the `/members` endpoint. The parameter |
| 33 | +filters the membership list of the room such that only members with a matching `membership` are |
| 34 | +returned. The parameter can be supplied multiple times to filter on multiple membership states. For |
| 35 | +example, the request could be `/members?membership=join&membership=invite` to get all invited and |
| 36 | +joined members for the room. If no `membership` parameter is specified, the default is to return |
| 37 | +all members of the room regardless of membership state. |
| 38 | + |
| 39 | +To compliment the `/members` endpoint, a new endpoint should be added to query the rooms for the |
| 40 | +user. This uses the same style of using a membership query parameter to filter the rooms. |
| 41 | + |
| 42 | +Some examples of using this endpoint are below. The `rooms` field is an object where the key is a |
| 43 | +room ID and the value is information about that room, currently storing a single `membership` |
| 44 | +field. The value is an object to support future expansion of this API. |
| 45 | + |
| 46 | +```json5 |
| 47 | +// GET /_matrix/client/r0/rooms?membership=join&membership=invite |
| 48 | +{ |
| 49 | + "rooms": { |
| 50 | + "!somewhere:domain.com": { |
| 51 | + "membership": "join" |
| 52 | + }, |
| 53 | + "!elsewhere:matrix.org": { |
| 54 | + "membership": "invite" |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +```json5 |
| 61 | +// GET /_matrix/client/r0/rooms?membership=ban |
| 62 | +{ |
| 63 | + "rooms": { |
| 64 | + "!plzno:domain.com": { |
| 65 | + "membership": "ban" |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +```json5 |
| 72 | +// GET /_matrix/client/r0/rooms |
| 73 | +{ |
| 74 | + "rooms": { |
| 75 | + "!somewhere:domain.com": { |
| 76 | + "membership": "join" |
| 77 | + }, |
| 78 | + "!elsewhere:matrix.org": { |
| 79 | + "membership": "invite" |
| 80 | + }, |
| 81 | + "!plzno:domain.com": { |
| 82 | + "membership": "ban" |
| 83 | + }, |
| 84 | + "!curbaf:domain.com": { |
| 85 | + "membership": "leave" |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Option 2: Filter |
| 92 | + |
| 93 | +As with Option 1, a new endpoint would be added to handle getting the list of rooms. However, instead of both `/members` and `/rooms` taking a query parameter for membership they would instead take a filter (re-using existing matrix concepts). Similar to how `/messages` works, this filter would be a `RoomEventFilter` instead of having all the available options. Additionally, the filter would support a `membership` field to filter based upon membership. |
| 94 | + |
| 95 | +An example filter for getting members/rooms of membership `invite` or `join` would be: |
| 96 | + |
| 97 | +```json5 |
| 98 | +{ |
| 99 | + "limit": 5, // The maximum number of items to return. Defaults to no limit. |
| 100 | + |
| 101 | + // These only apply when fetching members in a room |
| 102 | + "senders": ["*"], |
| 103 | + "not_senders": [], |
| 104 | + |
| 105 | + // These only apply when fetching rooms |
| 106 | + "rooms": ["*"], |
| 107 | + "not_rooms": [], |
| 108 | + |
| 109 | + // NEW! Filter based upon the given membership values. |
| 110 | + "membership": ["join", "invite"], |
| 111 | + |
| 112 | + // These are copied from the RoomEventFilter schema, but are ignored |
| 113 | + "types": [], |
| 114 | + "not_types": [], |
| 115 | + "contains_url": true, |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Option 3: Even more filters |
| 120 | + |
| 121 | +Expanding on Option 2, we give `/state` the option of a filter (also from Option 2). This would |
| 122 | +require the `types` to be useful, and we could potentially deprecate the `/members` endpoint |
| 123 | +entirely with this approach. |
| 124 | + |
| 125 | +Likewise, `/context` should take a similar filter so clients can get members at a given point in |
| 126 | +history. |
| 127 | + |
| 128 | +## Alternative solutions |
| 129 | + |
| 130 | +### Using ?membership=join,invite or ?membership=join+invite instead |
| 131 | + |
| 132 | +The arguments in favour of this approach are: |
| 133 | + |
| 134 | +* It doesn’t rely on undefined behaviour in RFC3986 |
| 135 | +* Using multiple keys in the query string hasn’t been done before in the matrix spec |
| 136 | + |
| 137 | +The arguments against this approach are: |
| 138 | + |
| 139 | +* It’s not as pretty and may require hex encoding |
| 140 | +* It adds unnecessary complexity given most query string parsers are capable of handling multiple |
| 141 | + keys in the query string. It is additional complexity because implementations would now need to |
| 142 | + do string splitting instead of relying on their already-in-use parsing libraries |
| 143 | + |
| 144 | +### Encoding ?membership as a JSON value |
| 145 | + |
| 146 | +The arguments in favour of this approach are: |
| 147 | + |
| 148 | +* The filtering API already does this |
| 149 | +* It doesn’t rely on undefined behaviour in RFC3986 |
| 150 | + |
| 151 | +The arguments against this approach are: |
| 152 | + |
| 153 | +* It’s not as pretty and requires hex encoding |
| 154 | +* Implementations would be forced to perform decoding, adding additional complexity (see the con |
| 155 | + for comma-separated values) |
0 commit comments