mirror of
https://github.com/element-hq/synapse.git
synced 2024-12-19 17:56:19 +03:00
Add timestamp lookup API
This commit is contained in:
parent
ca0e0892ca
commit
93db2124ec
3 changed files with 81 additions and 0 deletions
|
@ -830,6 +830,30 @@ class JoinedRoomsRestServlet(ClientV1RestServlet):
|
|||
defer.returnValue((200, {"joined_rooms": list(room_ids)}))
|
||||
|
||||
|
||||
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.user.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,
|
||||
)
|
||||
|
||||
defer.returnValue((200, {
|
||||
"event_id": event_id,
|
||||
}))
|
||||
|
||||
|
||||
def register_txn_path(servlet, regex_string, http_server, with_get=False):
|
||||
"""Registers a transaction-based path.
|
||||
|
||||
|
@ -879,6 +903,7 @@ def register_servlets(hs, http_server):
|
|||
JoinedRoomsRestServlet(hs).register(http_server)
|
||||
RoomEventServlet(hs).register(http_server)
|
||||
RoomEventContextServlet(hs).register(http_server)
|
||||
TimestampLookupRestServlet(hs).register(http_server)
|
||||
|
||||
|
||||
def register_deprecated_servlets(hs, http_server):
|
||||
|
|
|
@ -528,3 +528,43 @@ class EventsWorkerStore(SQLBaseStore):
|
|||
return res
|
||||
|
||||
return self.runInteraction("get_rejection_reasons", f)
|
||||
|
||||
def get_event_for_timestamp(self, room_id, thread_id, timestamp):
|
||||
sql_template = """
|
||||
SELECT event_id, origin_server_ts FROM events
|
||||
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_before:
|
||||
# Return the closest one
|
||||
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)
|
||||
|
|
16
synapse/storage/schema/delta/52/add_threa_index.sql
Normal file
16
synapse/storage/schema/delta/52/add_threa_index.sql
Normal file
|
@ -0,0 +1,16 @@
|
|||
/* Copyright 2018 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
CREATE INDEX event_room_thread_ts ON events (room_id, thread_id, origin_server_ts);
|
Loading…
Reference in a new issue