-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add endpoint to get an event at a given timestamp - MSC3030 #9445
Changes from 12 commits
91b1b36
8e5fa11
96c48ba
668aa4e
5b07487
f721899
af085ab
065273b
0e0ddda
e321ef7
e21e4b5
fa15989
ec2695d
22a93c3
b311853
5638123
612b51f
654d7ae
6280d36
8766b0a
86a2642
5a2c997
bc3ba38
edac953
ab800e3
9800a4b
8523bf3
f05c292
984a14b
dae7e0a
87ac1ed
2a5b622
0610fac
183e1bf
5362bd3
76ac526
58d67f2
63d61fc
70420e5
a8644b9
c38984c
ed1360a
13371a6
d137292
662366a
dd7e689
e7d2120
5660fde
c3c404b
1ff2db4
067d67a
5888eba
68704f4
3ee5d0c
2621e5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,37 @@ | |
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_domains_from_state(state: StateMap[EventBase]) -> List[Tuple[str, int]]: | ||
"""Get joined domains from state | ||
|
||
Args: | ||
state: State map from type/state key to event. | ||
|
||
Returns: | ||
Returns a list of servers with the lowest depth of their joins. | ||
Sorted by lowest depth first. | ||
""" | ||
joined_users = [ | ||
(state_key, int(event.depth)) | ||
for (e_type, state_key), event in state.items() | ||
if e_type == EventTypes.Member and event.membership == Membership.JOIN | ||
] | ||
|
||
joined_domains: Dict[str, int] = {} | ||
for u, d in joined_users: | ||
try: | ||
dom = get_domain_from_id(u) | ||
old_d = joined_domains.get(dom) | ||
if old_d: | ||
joined_domains[dom] = min(d, old_d) | ||
else: | ||
joined_domains[dom] = d | ||
except Exception: | ||
pass | ||
|
||
return sorted(joined_domains.items(), key=lambda d: d[1]) | ||
|
||
|
||
class FederationHandler: | ||
"""Handles general incoming federation requests | ||
|
||
|
@@ -268,36 +299,6 @@ async def _maybe_backfill_inner( | |
|
||
curr_state = await self.state_handler.get_current_state(room_id) | ||
|
||
def get_domains_from_state(state: StateMap[EventBase]) -> List[Tuple[str, int]]: | ||
"""Get joined domains from state | ||
|
||
Args: | ||
state: State map from type/state key to event. | ||
|
||
Returns: | ||
Returns a list of servers with the lowest depth of their joins. | ||
Sorted by lowest depth first. | ||
""" | ||
joined_users = [ | ||
(state_key, int(event.depth)) | ||
for (e_type, state_key), event in state.items() | ||
if e_type == EventTypes.Member and event.membership == Membership.JOIN | ||
] | ||
|
||
joined_domains: Dict[str, int] = {} | ||
for u, d in joined_users: | ||
try: | ||
dom = get_domain_from_id(u) | ||
old_d = joined_domains.get(dom) | ||
if old_d: | ||
joined_domains[dom] = min(d, old_d) | ||
else: | ||
joined_domains[dom] = d | ||
except Exception: | ||
pass | ||
|
||
return sorted(joined_domains.items(), key=lambda d: d[1]) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just de-nesting this function so we can re-use it |
||
curr_domains = get_domains_from_state(curr_state) | ||
|
||
likely_domains = [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1070,6 +1070,33 @@ def register_txn_path( | |
) | ||
|
||
|
||
class TimestampLookupRestServlet(RestServlet): | ||
PATTERNS = client_patterns("/rooms/(?P<room_id>[^/]*)/timestamp_to_event$") | ||
|
||
def __init__(self, hs: "HomeServer"): | ||
super().__init__() | ||
self._auth = hs.get_auth() | ||
self._store = hs.get_datastore() | ||
self.timestamp_lookup_handler = hs.get_timestamp_lookup_handler() | ||
|
||
async def on_GET( | ||
self, request: SynapseRequest, room_id: str | ||
) -> Tuple[int, JsonDict]: | ||
requester = await self._auth.get_user_by_req(request) | ||
await self._auth.check_user_in_room(room_id, requester.user.to_string()) | ||
|
||
timestamp = parse_integer(request, "ts") | ||
direction = parse_string(request, "dir", default="f", allowed_values=["f", "b"]) | ||
|
||
event_id = await self.timestamp_lookup_handler.get_event_for_timestamp( | ||
requester, room_id, timestamp, direction | ||
) | ||
|
||
return 200, { | ||
"event_id": event_id, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One problem I'm seeing with going with a We probably want to consider also returning a pagination stream token to use with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or just not worry about it, we can make a separate request to get one: // Get a pagination token for a single event
contextRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "context", someEventId}, client.WithContentType("application/json"), client.WithQueries(url.Values{
"limit": []string{"0"},
})) |
||
|
||
|
||
class RoomSpaceSummaryRestServlet(RestServlet): | ||
PATTERNS = ( | ||
re.compile( | ||
|
@@ -1239,6 +1266,7 @@ def register_servlets( | |
RoomAliasListServlet(hs).register(http_server) | ||
SearchRestServlet(hs).register(http_server) | ||
RoomCreateRestServlet(hs).register(http_server) | ||
TimestampLookupRestServlet(hs).register(http_server) | ||
MadLittleMods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Some servlets only get registered for the main process. | ||
if not is_worker: | ||
|
Uh oh!
There was an error while loading. Please reload this page.