summary refs log tree commit diff
path: root/synapse/streams
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-08-27 15:25:21 +0100
committerErik Johnston <erik@matrix.org>2014-08-27 15:25:27 +0100
commit05672a6a8ca66bd2165217c06c5478a06b0cd952 (patch)
tree161acf6cc738bc98ebd5c2a6f6b9d86a1bb34e01 /synapse/streams
parentPEP8 tweaks. (diff)
downloadsynapse-05672a6a8ca66bd2165217c06c5478a06b0cd952.tar.xz
Convert get_paginat_rows to use PaginationConfig. This allows people to supply directions.
Diffstat (limited to 'synapse/streams')
-rw-r--r--synapse/streams/config.py67
-rw-r--r--synapse/streams/events.py20
2 files changed, 54 insertions, 33 deletions
diff --git a/synapse/streams/config.py b/synapse/streams/config.py
index 69c7145a36..2434844d80 100644
--- a/synapse/streams/config.py
+++ b/synapse/streams/config.py
@@ -26,40 +26,53 @@ class PaginationConfig(object):
 
     """A configuration object which stores pagination parameters."""
 
-    def __init__(self, from_tok=None, to_tok=None, direction='f', limit=0):
-        self.from_token = (
-            StreamToken.from_string(from_tok) if from_tok else None
-        )
-        self.to_token = StreamToken.from_string(to_tok) if to_tok else None
+    def __init__(self, from_token=None, to_token=None, direction='f',
+                 limit=0):
+        self.from_token = from_token
+        self.to_token = to_token
         self.direction = 'f' if direction == 'f' else 'b'
         self.limit = int(limit)
 
     @classmethod
     def from_request(cls, request, raise_invalid_params=True):
-        params = {
-            "direction": 'f',
-        }
-
-        query_param_mappings = [  # 3-tuple of qp_key, attribute, rules
-            ("from", "from_tok", lambda x: type(x) == str),
-            ("to", "to_tok", lambda x: type(x) == str),
-            ("limit", "limit", lambda x: x.isdigit()),
-            ("dir", "direction", lambda x: x == 'f' or x == 'b'),
-        ]
-
-        for qp, attr, is_valid in query_param_mappings:
-            if qp in request.args:
-                if is_valid(request.args[qp][0]):
-                    params[attr] = request.args[qp][0]
-                elif raise_invalid_params:
-                    raise SynapseError(400, "%s parameter is invalid." % qp)
-
-        if "from_tok" in params and params["from_tok"] == "END":
-            # TODO (erikj): This is for compatibility only.
-            del params["from_tok"]
+        def get_param(name, default=None):
+            lst = request.args.get(name, [])
+            if len(lst) > 1:
+                raise SynapseError(
+                    400, "%s must be specified only once" % (name,)
+                )
+            elif len(lst) == 1:
+                return lst[0]
+            else:
+                return default
+
+        direction = get_param("dir", 'f')
+        if direction not in ['f', 'b']:
+            raise SynapseError(400, "'dir' parameter is invalid.")
+
+        from_tok = get_param("from")
+        to_tok = get_param("to")
+
+        try:
+            if from_tok == "END":
+                from_tok = None  # For backwards compat.
+            elif from_tok:
+                from_tok = StreamToken.from_string(from_tok)
+        except:
+            raise SynapseError(400, "'from' paramater is invalid")
+
+        try:
+            if to_tok:
+                to_tok = StreamToken.from_string(to_tok)
+        except:
+            raise SynapseError(400, "'to' paramater is invalid")
+
+        limit = get_param("limit", "0")
+        if not limit.isdigit():
+            raise SynapseError(400, "'limit' parameter must be an integer.")
 
         try:
-            return PaginationConfig(**params)
+            return PaginationConfig(from_tok, to_tok, direction, limit)
         except:
             logger.exception("Failed to create pagination config")
             raise SynapseError(400, "Invalid request.")
diff --git a/synapse/streams/events.py b/synapse/streams/events.py
index bf48df5b79..8a84a9d392 100644
--- a/synapse/streams/events.py
+++ b/synapse/streams/events.py
@@ -47,14 +47,19 @@ class RoomEventSource(object):
         return self.store.get_room_events_max_id()
 
     @defer.inlineCallbacks
-    def get_pagination_rows(self, user, from_token, to_token, limit, key):
+    def get_pagination_rows(self, user, pagination_config, key):
+        from_token = pagination_config.from_token
+        to_token = pagination_config.to_token
+        limit = pagination_config.limit
+        direction = pagination_config.direction
+
         to_key = to_token.events_key if to_token else None
 
         events, next_key = yield self.store.paginate_room_events(
             room_id=key,
             from_key=from_token.events_key,
             to_key=to_key,
-            direction='b',
+            direction=direction,
             limit=limit,
             with_feedback=True
         )
@@ -101,7 +106,12 @@ class PresenceSource(object):
         presence = self.hs.get_handlers().presence_handler
         return presence._user_cachemap_latest_serial
 
-    def get_pagination_rows(self, user, from_token, to_token, limit, key):
+    def get_pagination_rows(self, user, pagination_config, key):
+        # TODO (erikj): Does this make sense? Ordering?
+
+        from_token = pagination_config.from_token
+        to_token = pagination_config.to_token
+
         from_key = int(from_token.presence_key)
 
         if to_token:
@@ -167,7 +177,5 @@ class StreamSource(object):
     def get_current_token_part(self):
         raise NotImplementedError("get_current_token_part")
 
-
-class PaginationSource(object):
-    def get_pagination_rows(self, user, from_token, to_token, limit, key):
+    def get_pagination_rows(self, user, pagination_config, key):
         raise NotImplementedError("get_rows")