summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
Diffstat (limited to 'synapse')
-rw-r--r--synapse/handlers/room.py9
-rw-r--r--synapse/storage/stream.py25
2 files changed, 28 insertions, 6 deletions
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index cdc98d2b08..9e10235fa0 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -269,11 +269,16 @@ class MessageHandler(BaseHandler):
             if event.membership != Membership.JOIN:
                 continue
             try:
-                messages = yield self.store.get_recent_events_for_room(
+                messages, token = yield self.store.get_recent_events_for_room(
                     event.room_id,
                     limit=50,
                 )
-                d["messages"] = [m.get_dict() for m in messages]
+
+                d["messages"] = {
+                    "chunk": [m.get_dict() for m in messages],
+                    "start": token[0],
+                    "end": token[1],
+                }
             except:
                 pass
 
diff --git a/synapse/storage/stream.py b/synapse/storage/stream.py
index f7968f576f..6728a4b5ea 100644
--- a/synapse/storage/stream.py
+++ b/synapse/storage/stream.py
@@ -104,19 +104,36 @@ class StreamStore(SQLBaseStore):
     def get_recent_events_for_room(self, room_id, limit, with_feedback=False):
         # TODO (erikj): Handle compressed feedback
 
+        end_token = yield self.get_room_events_max_id()
+
         sql = (
-            "SELECT * FROM events WHERE room_id = ? "
-            "ORDER BY token_ordering, rowid DESC LIMIT ? "
+            "SELECT * FROM events WHERE "
+            "WHERE room_id = ? AND token_ordering <= ? "
+            "ORDER BY topological_ordering, rowid DESC LIMIT ? "
         )
 
         rows = yield self._execute_and_decode(
             sql,
-            room_id, limit
+            room_id, end_token, limit
         )
 
         rows.reverse()  # As we selected with reverse ordering
 
-        defer.returnValue([self._parse_event_from_row(r) for r in rows])
+        if rows:
+            topo = rows[0]["topological_ordering"]
+            row_id = rows[0]["rowid"]
+            start_token = "p%s-%s" % (topo, row_id)
+
+            token = (start_token, end_token)
+        else:
+            token = ("START", end_token)
+
+        defer.returnValue(
+            (
+                [self._parse_event_from_row(r) for r in rows],
+                token
+            )
+        )
 
     @defer.inlineCallbacks
     def get_room_events_max_id(self):