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

Commit

Permalink
Add endpoint to get an event at a given timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston authored and MadLittleMods committed Jul 15, 2021
1 parent 0ae95b3 commit 91b1b36
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
25 changes: 25 additions & 0 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,30 @@ def register_txn_path(servlet, regex_string, http_server, with_get=False):
servlet.__class__.__name__,
)

class TimestampLookupRestServlet(ClientV1RestServlet):
PATTERNS = client_path_patterns("/rooms/(?P<room_id>[^/]*)/timestamp_to_event$")

def __init__(self, hs):
super(TimestampLookupRestServlet, self).__init__(hs)
self.store = hs.get_datastore()

@defer.inlineCallbacks
def on_GET(self, request, room_id):
requester = yield self.auth.get_user_by_req(request)
yield self.auth.check_joined_room(room_id, requester..to_string())

timestamp = parse_integer(request, "ts")
thread_id = parse_integer(request, "thread_id", 0)

event_id = yield self.store.get_event_for_timestamp(
room_id, thread_id, timestamp,
)

.returnValue((200, {
"event_id": event_id,
}))



class RoomSpaceSummaryRestServlet(RestServlet):
PATTERNS = (
Expand Down Expand Up @@ -1458,6 +1482,7 @@ def register_servlets(hs: "HomeServer", http_server, is_worker=False):
JoinedRoomsRestServlet(hs).register(http_server)
RoomAliasListServlet(hs).register(http_server)
SearchRestServlet(hs).register(http_server)
TimestampLookupRestServlet(hs).register(http_server)

# Some servlets only get registered for the main process.
if not is_worker:
Expand Down
40 changes: 40 additions & 0 deletions synapse/storage/databases/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,3 +1511,43 @@ def _cleanup_old_transaction_ids_txn(txn):
"_cleanup_old_transaction_ids",
_cleanup_old_transaction_ids_txn,
)

def get_event_for_timestamp(self, room_id, thread_id, timestamp):
sql_template = """
SELECT event_id, origin_server_ts FROM
WHERE
origin_server_ts %s ?
AND room_id = ?
AND thread_id = ?
ORDER BY origin_server_ts
LIMIT 1;
"""

def f(txn):
txn.execute(sql_template % ("<=",), (timestamp, room_id, thread_id))
row = txn.fetchone()
if row:
event_id_before, ts_before = row
else:
event_id_before, ts_before = None, None

txn.execute(sql_template % (">=",), (timestamp, room_id, thread_id))
row = txn.fetchone()
if row:
event_id_after, ts_after = row
else:
event_id_after, ts_after = None, None

if event_id_before and event_id_after:
# Return the closest
if (timestamp - ts_before) < (ts_after - timestamp):
return event_id_before
else:
return event_id_after

if event_id_before:
return event_id_before

return event_id_after

return self.runInteraction("get_event_for_timestamp", f)

0 comments on commit 91b1b36

Please sign in to comment.