mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2024-12-25 20:59:23 -05:00
Avoid extra db lookups
Since we're about to look up the events themselves anyway, we can skip the extra db queries here.
This commit is contained in:
parent
0a65450d04
commit
14fa9d4d92
@ -1350,7 +1350,9 @@ class FederationHandler(BaseHandler):
|
|||||||
"""Returns the state at the event. i.e. not including said event.
|
"""Returns the state at the event. i.e. not including said event.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
yield self._verify_events_in_room([event_id], room_id)
|
event = yield self.store.get_event(
|
||||||
|
event_id, allow_none=False, check_room_id=room_id,
|
||||||
|
)
|
||||||
|
|
||||||
state_groups = yield self.store.get_state_groups(
|
state_groups = yield self.store.get_state_groups(
|
||||||
room_id, [event_id]
|
room_id, [event_id]
|
||||||
@ -1362,8 +1364,7 @@ class FederationHandler(BaseHandler):
|
|||||||
(e.type, e.state_key): e for e in state
|
(e.type, e.state_key): e for e in state
|
||||||
}
|
}
|
||||||
|
|
||||||
event = yield self.store.get_event(event_id)
|
if event.is_state():
|
||||||
if event and event.is_state():
|
|
||||||
# Get previous state
|
# Get previous state
|
||||||
if "replaces_state" in event.unsigned:
|
if "replaces_state" in event.unsigned:
|
||||||
prev_id = event.unsigned["replaces_state"]
|
prev_id = event.unsigned["replaces_state"]
|
||||||
@ -1394,8 +1395,9 @@ class FederationHandler(BaseHandler):
|
|||||||
def get_state_ids_for_pdu(self, room_id, event_id):
|
def get_state_ids_for_pdu(self, room_id, event_id):
|
||||||
"""Returns the state at the event. i.e. not including said event.
|
"""Returns the state at the event. i.e. not including said event.
|
||||||
"""
|
"""
|
||||||
|
event = yield self.store.get_event(
|
||||||
yield self._verify_events_in_room([event_id], room_id)
|
event_id, allow_none=False, check_room_id=room_id,
|
||||||
|
)
|
||||||
|
|
||||||
state_groups = yield self.store.get_state_groups_ids(
|
state_groups = yield self.store.get_state_groups_ids(
|
||||||
room_id, [event_id]
|
room_id, [event_id]
|
||||||
@ -1405,8 +1407,7 @@ class FederationHandler(BaseHandler):
|
|||||||
_, state = state_groups.items().pop()
|
_, state = state_groups.items().pop()
|
||||||
results = state
|
results = state
|
||||||
|
|
||||||
event = yield self.store.get_event(event_id)
|
if event.is_state():
|
||||||
if event and event.is_state():
|
|
||||||
# Get previous state
|
# Get previous state
|
||||||
if "replaces_state" in event.unsigned:
|
if "replaces_state" in event.unsigned:
|
||||||
prev_id = event.unsigned["replaces_state"]
|
prev_id = event.unsigned["replaces_state"]
|
||||||
@ -1426,8 +1427,6 @@ class FederationHandler(BaseHandler):
|
|||||||
if not in_room:
|
if not in_room:
|
||||||
raise AuthError(403, "Host not in room.")
|
raise AuthError(403, "Host not in room.")
|
||||||
|
|
||||||
yield self._verify_events_in_room(pdu_list, room_id)
|
|
||||||
|
|
||||||
events = yield self.store.get_backfill_events(
|
events = yield self.store.get_backfill_events(
|
||||||
room_id,
|
room_id,
|
||||||
pdu_list,
|
pdu_list,
|
||||||
@ -1723,7 +1722,9 @@ class FederationHandler(BaseHandler):
|
|||||||
if not in_room:
|
if not in_room:
|
||||||
raise AuthError(403, "Host not in room.")
|
raise AuthError(403, "Host not in room.")
|
||||||
|
|
||||||
yield self._verify_events_in_room([event_id], room_id)
|
event = yield self.store.get_event(
|
||||||
|
event_id, allow_none=False, check_room_id=room_id
|
||||||
|
)
|
||||||
|
|
||||||
# Just go through and process each event in `remote_auth_chain`. We
|
# Just go through and process each event in `remote_auth_chain`. We
|
||||||
# don't want to fall into the trap of `missing` being wrong.
|
# don't want to fall into the trap of `missing` being wrong.
|
||||||
@ -1734,7 +1735,6 @@ class FederationHandler(BaseHandler):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
# Now get the current auth_chain for the event.
|
# Now get the current auth_chain for the event.
|
||||||
event = yield self.store.get_event(event_id)
|
|
||||||
local_auth_chain = yield self.store.get_auth_chain(
|
local_auth_chain = yield self.store.get_auth_chain(
|
||||||
[auth_id for auth_id, _ in event.auth_events],
|
[auth_id for auth_id, _ in event.auth_events],
|
||||||
include_given=True
|
include_given=True
|
||||||
@ -2385,19 +2385,3 @@ class FederationHandler(BaseHandler):
|
|||||||
)
|
)
|
||||||
if "valid" not in response or not response["valid"]:
|
if "valid" not in response or not response["valid"]:
|
||||||
raise AuthError(403, "Third party certificate was invalid")
|
raise AuthError(403, "Third party certificate was invalid")
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
|
||||||
def _verify_events_in_room(self, pdu_ids, room_id):
|
|
||||||
"""Checks whether the given PDU IDs are in the given room or not.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
pdu_ids (list): list of PDU IDs
|
|
||||||
room_id (str): the room ID that the PDUs should be in
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
AuthError: if one or more of the PDUs does not belong to the
|
|
||||||
given room.
|
|
||||||
"""
|
|
||||||
room_ids = yield self.store.get_room_ids_for_events(pdu_ids)
|
|
||||||
if len(room_ids) != 1 or room_ids[0] != room_id:
|
|
||||||
raise AuthError(403, "Events must belong to the given room")
|
|
||||||
|
@ -295,35 +295,6 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore,
|
|||||||
get_forward_extremeties_for_room_txn
|
get_forward_extremeties_for_room_txn
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_room_ids_for_events(self, event_ids):
|
|
||||||
"""Get a list of room IDs for which the given events belong.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
event_ids (list): the events to look up the room of
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
list, the room IDs for the events
|
|
||||||
"""
|
|
||||||
return self.runInteraction(
|
|
||||||
"get_room_ids_for_events",
|
|
||||||
self._get_room_ids_for_events, event_ids
|
|
||||||
)
|
|
||||||
|
|
||||||
def _get_room_ids_for_events(self, txn, event_ids):
|
|
||||||
logger.debug("_get_room_ids_for_events: %s", repr(event_ids))
|
|
||||||
|
|
||||||
base_sql = (
|
|
||||||
"SELECT DISTINCT room_id FROM events"
|
|
||||||
" WHERE event_id IN (%s)"
|
|
||||||
)
|
|
||||||
|
|
||||||
txn.execute(
|
|
||||||
base_sql % (",".join(["?"] * len(event_ids)),),
|
|
||||||
event_ids
|
|
||||||
)
|
|
||||||
|
|
||||||
return [r[0] for r in txn]
|
|
||||||
|
|
||||||
def get_backfill_events(self, room_id, event_list, limit):
|
def get_backfill_events(self, room_id, event_list, limit):
|
||||||
"""Get a list of Events for a given topic that occurred before (and
|
"""Get a list of Events for a given topic that occurred before (and
|
||||||
including) the events in event_list. Return a list of max size `limit`
|
including) the events in event_list. Return a list of max size `limit`
|
||||||
@ -372,6 +343,7 @@ class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore,
|
|||||||
table="events",
|
table="events",
|
||||||
keyvalues={
|
keyvalues={
|
||||||
"event_id": event_id,
|
"event_id": event_id,
|
||||||
|
"room_id": room_id,
|
||||||
},
|
},
|
||||||
retcol="depth",
|
retcol="depth",
|
||||||
allow_none=True,
|
allow_none=True,
|
||||||
|
@ -19,7 +19,7 @@ from canonicaljson import json
|
|||||||
|
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import NotFoundError
|
||||||
# these are only included to make the type annotations work
|
# these are only included to make the type annotations work
|
||||||
from synapse.events import EventBase # noqa: F401
|
from synapse.events import EventBase # noqa: F401
|
||||||
from synapse.events import FrozenEvent
|
from synapse.events import FrozenEvent
|
||||||
@ -76,7 +76,7 @@ class EventsWorkerStore(SQLBaseStore):
|
|||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_event(self, event_id, check_redacted=True,
|
def get_event(self, event_id, check_redacted=True,
|
||||||
get_prev_content=False, allow_rejected=False,
|
get_prev_content=False, allow_rejected=False,
|
||||||
allow_none=False):
|
allow_none=False, check_room_id=None):
|
||||||
"""Get an event from the database by event_id.
|
"""Get an event from the database by event_id.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -87,7 +87,9 @@ class EventsWorkerStore(SQLBaseStore):
|
|||||||
include the previous states content in the unsigned field.
|
include the previous states content in the unsigned field.
|
||||||
allow_rejected (bool): If True return rejected events.
|
allow_rejected (bool): If True return rejected events.
|
||||||
allow_none (bool): If True, return None if no event found, if
|
allow_none (bool): If True, return None if no event found, if
|
||||||
False throw an exception.
|
False throw a NotFoundError
|
||||||
|
check_room_id (str|None): if not None, check the room of the found event.
|
||||||
|
If there is a mismatch, behave as per allow_none.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Deferred : A FrozenEvent.
|
Deferred : A FrozenEvent.
|
||||||
@ -99,10 +101,16 @@ class EventsWorkerStore(SQLBaseStore):
|
|||||||
allow_rejected=allow_rejected,
|
allow_rejected=allow_rejected,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not events and not allow_none:
|
event = events[0] if events else None
|
||||||
raise SynapseError(404, "Could not find event %s" % (event_id,))
|
|
||||||
|
|
||||||
defer.returnValue(events[0] if events else None)
|
if event is not None and check_room_id is not None:
|
||||||
|
if event.room_id != check_room_id:
|
||||||
|
event = None
|
||||||
|
|
||||||
|
if event is None and not allow_none:
|
||||||
|
raise NotFoundError("Could not find event %s" % (event_id,))
|
||||||
|
|
||||||
|
defer.returnValue(event)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_events(self, event_ids, check_redacted=True,
|
def get_events(self, event_ids, check_redacted=True,
|
||||||
|
Loading…
Reference in New Issue
Block a user