This commit is contained in:
Half-Shot 2024-11-29 13:05:30 +00:00
parent 62cb8cf060
commit c850e33cda
3 changed files with 13 additions and 15 deletions

View file

@ -175,7 +175,7 @@ class MessageHandler:
state_filter: Optional[StateFilter] = None, state_filter: Optional[StateFilter] = None,
at_token: Optional[StreamToken] = None, at_token: Optional[StreamToken] = None,
last_hash: Optional[str] = None, last_hash: Optional[str] = None,
) -> Tuple[List[dict], str]: ) -> Tuple[List[dict], Optional[str]]:
"""Retrieve all state events for a given room. If the user is """Retrieve all state events for a given room. If the user is
joined to the room then return the current state. If the user has joined to the room then return the current state. If the user has
left the room return the state events from when they left. If an explicit left the room return the state events from when they left. If an explicit
@ -192,7 +192,8 @@ class MessageHandler:
state based on the current_state_events table. state based on the current_state_events table.
Returns: Returns:
A list of dicts representing state events. [{}, {}, {}] A list of dicts representing state events. [{}, {}, {}]
A hash of the state IDs representing the state events. A hash of the state IDs representing the state events. This is only calculated if
no at_token is given and the user is joined to the room.
Raises: Raises:
NotFoundError (404) if the at token does not yield an event NotFoundError (404) if the at token does not yield an event
@ -250,7 +251,7 @@ class MessageHandler:
# If the requester's hash matches ours, their cache is up to date and we can skip # If the requester's hash matches ours, their cache is up to date and we can skip
# fetching events. # fetching events.
if last_hash == hash: if last_hash == hash:
return None, hash return [], hash
room_state = await self.store.get_events(state_ids.values()) room_state = await self.store.get_events(state_ids.values())
elif membership == Membership.LEAVE: elif membership == Membership.LEAVE:
# If the membership is not JOIN, then the event ID should exist. # If the membership is not JOIN, then the event ID should exist.

View file

@ -835,7 +835,7 @@ class RoomStateRestServlet(RestServlet):
@cancellable @cancellable
async def on_GET( async def on_GET(
self, request: SynapseRequest, room_id: str self, request: SynapseRequest, room_id: str
) -> Tuple[int, List[JsonDict]]: ) -> Tuple[int, Optional[List[JsonDict]]]:
requester = await self.auth.get_user_by_req(request, allow_guest=True) requester = await self.auth.get_user_by_req(request, allow_guest=True)
existing_hash = request.getHeader(b"If-None-Match") existing_hash = request.getHeader(b"If-None-Match")
@ -848,7 +848,7 @@ class RoomStateRestServlet(RestServlet):
) )
request.setHeader(b"ETag", f'"{hash}"'.encode("ascii")) request.setHeader(b"ETag", f'"{hash}"'.encode("ascii"))
if events is None: if len(events) == 0:
return 304, None return 304, None
return 200, events return 200, events

View file

@ -560,21 +560,18 @@ class RoomStateTestCase(RoomBase):
"/rooms/%s/state" % room_id, "/rooms/%s/state" % room_id,
) )
etag = channel.headers.getRawHeaders(b"ETag")[0] etagheader = channel.headers.getRawHeaders(b"ETag")
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.result["body"])
self.assertIsNotNone( assert etagheader
etag,
"has a ETag header",
)
channel2 = self.make_request( channel2 = self.make_request(
"GET", "GET",
"/rooms/%s/state" % room_id, "/rooms/%s/state" % room_id,
custom_headers=( custom_headers=[
( (
b"If-None-Match", b"If-None-Match",
etag, etagheader[0],
), ),
), ],
) )
self.assertEqual( self.assertEqual(
HTTPStatus.NOT_MODIFIED, HTTPStatus.NOT_MODIFIED,
@ -582,8 +579,8 @@ class RoomStateTestCase(RoomBase):
"Responds with not modified when provided with the correct ETag", "Responds with not modified when provided with the correct ETag",
) )
self.assertEqual( self.assertEqual(
etag, etagheader,
channel2.headers.getRawHeaders(b"ETag")[0], channel2.headers.getRawHeaders(b"ETag"),
"returns the same etag", "returns the same etag",
) )