Encode batch tokens better

erikj/paginate_sync
Erik Johnston 2016-06-27 15:21:12 +01:00
parent 4b7abedfd9
commit 6c137b321d
2 changed files with 12 additions and 6 deletions

View File

@ -62,13 +62,13 @@ class SyncPaginationConfig(collections.namedtuple("SyncPaginationConfig", [
super(SyncPaginationConfig, self).__init__(order, limit, tags)
SYNC_PAGINATION_TAGS_INCLUDE_ALL = "include_all"
SYNC_PAGINATION_TAGS_IGNORE = "ignore"
SYNC_PAGINATION_TAGS_INCLUDE_ALL = "m.include_all"
SYNC_PAGINATION_TAGS_IGNORE = "m.ignore"
SYNC_PAGINATION_VALID_TAGS_OPTIONS = (
SYNC_PAGINATION_TAGS_INCLUDE_ALL, SYNC_PAGINATION_TAGS_IGNORE,
)
SYNC_PAGINATION_ORDER_TS = "o"
SYNC_PAGINATION_ORDER_TS = "m.origin_server_ts"
SYNC_PAGINATION_VALID_ORDERS = (SYNC_PAGINATION_ORDER_TS,)

View File

@ -148,6 +148,12 @@ class SyncNextBatchToken(
return self._replace(**kwargs)
_ORDER_ENCODE = {"m.origin_server_ts": "o"}
_ORDER_DECODE = {v: k for k, v in _ORDER_ENCODE.items()}
_TAG_ENCODE = {"m.include_all": "i", "m.ignore": "x"}
_TAG_DECODE = {v: k for k, v in _TAG_ENCODE.items()}
class SyncPaginationState(
namedtuple("SyncPaginationState", (
"order",
@ -159,16 +165,16 @@ class SyncPaginationState(
@classmethod
def from_dict(cls, d):
try:
return cls(d["o"], d["v"], d["l"], d["t"])
return cls(_ORDER_DECODE[d["o"]], d["v"], d["l"], _TAG_DECODE[d["t"]])
except:
raise SynapseError(400, "Invalid Token")
def to_dict(self):
return {
"o": self.order,
"o": _ORDER_ENCODE[self.order],
"v": self.value,
"l": self.limit,
"t": self.tags,
"t": _TAG_ENCODE[self.tags],
}
def replace(self, **kwargs):