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

Commit

Permalink
Don't assume RoomStreamTokens can be compared
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Sep 29, 2020
1 parent d68a91d commit 10755ab
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions synapse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,18 @@ def parse_stream_token(cls, string: str) -> "RoomStreamToken":
pass
raise SynapseError(400, "Invalid token %r" % (string,))

def copy_and_advance(self, other: "RoomStreamToken") -> "RoomStreamToken":
"""Return a new token such that if an event is after both this token and
the other token, then its after the returned token too.
"""

if self.topological or other.topological:
raise Exception("Can't advance topological tokens")

max_stream = max(self.stream, other.stream)

return RoomStreamToken(None, max_stream)

def as_tuple(self) -> Tuple[Optional[int], int]:
return (self.topological, self.stream)

Expand Down Expand Up @@ -462,13 +474,16 @@ def copy_and_advance(self, key, new_value) -> "StreamToken":
"""Advance the given key in the token to a new value if and only if the
new value is after the old value.
"""
new_token = self.copy_and_replace(key, new_value)
if key == "room_key":
new_id = new_token.room_stream_id
old_id = self.room_stream_id
else:
new_id = int(getattr(new_token, key))
old_id = int(getattr(self, key))
new_token = self.copy_and_replace(
"room_key", self.room_key.copy_and_advance(new_value)
)
return new_token

new_token = self.copy_and_replace(key, new_value)
new_id = int(getattr(new_token, key))
old_id = int(getattr(self, key))

if old_id < new_id:
return new_token
else:
Expand Down

0 comments on commit 10755ab

Please sign in to comment.