Factor out `_send_dummy_event_for_room` (#8370)

this makes it possible to use from the manhole, and seems cleaner anyway.
pull/8401/head
Richard van der Hoff 2020-09-23 18:18:43 +01:00 committed by GitHub
parent 91c60f3042
commit 2983049a77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 48 deletions

1
changelog.d/8370.misc Normal file
View File

@ -0,0 +1 @@
Factor out a `_send_dummy_event_for_room` method.

View File

@ -1182,15 +1182,36 @@ class EventCreationHandler:
) )
for room_id in room_ids: for room_id in room_ids:
dummy_event_sent = await self._send_dummy_event_for_room(room_id)
if not dummy_event_sent:
# Did not find a valid user in the room, so remove from future attempts
# Exclusion is time limited, so the room will be rechecked in the future
# dependent on _DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY
logger.info(
"Failed to send dummy event into room %s. Will exclude it from "
"future attempts until cache expires" % (room_id,)
)
now = self.clock.time_msec()
self._rooms_to_exclude_from_dummy_event_insertion[room_id] = now
async def _send_dummy_event_for_room(self, room_id: str) -> bool:
"""Attempt to send a dummy event for the given room.
Args:
room_id: room to try to send an event from
Returns:
True if a dummy event was successfully sent. False if no user was able
to send an event.
"""
# For each room we need to find a joined member we can use to send # For each room we need to find a joined member we can use to send
# the dummy event with. # the dummy event with.
latest_event_ids = await self.store.get_prev_events_for_room(room_id) latest_event_ids = await self.store.get_prev_events_for_room(room_id)
members = await self.state.get_current_users_in_room( members = await self.state.get_current_users_in_room(
room_id, latest_event_ids=latest_event_ids room_id, latest_event_ids=latest_event_ids
) )
dummy_event_sent = False
for user_id in members: for user_id in members:
if not self.hs.is_mine_id(user_id): if not self.hs.is_mine_id(user_id):
continue continue
@ -1212,14 +1233,9 @@ class EventCreationHandler:
# Since this is a dummy-event it is OK if it is sent by a # Since this is a dummy-event it is OK if it is sent by a
# shadow-banned user. # shadow-banned user.
await self.send_nonmember_event( await self.send_nonmember_event(
requester, requester, event, context, ratelimit=False, ignore_shadow_ban=True,
event,
context,
ratelimit=False,
ignore_shadow_ban=True,
) )
dummy_event_sent = True return True
break
except ConsentNotGivenError: except ConsentNotGivenError:
logger.info( logger.info(
"Failed to send dummy event into room %s for user %s due to " "Failed to send dummy event into room %s for user %s due to "
@ -1230,17 +1246,7 @@ class EventCreationHandler:
"Failed to send dummy event into room %s for user %s due to " "Failed to send dummy event into room %s for user %s due to "
"lack of power. Will try another user" % (room_id, user_id) "lack of power. Will try another user" % (room_id, user_id)
) )
return False
if not dummy_event_sent:
# Did not find a valid user in the room, so remove from future attempts
# Exclusion is time limited, so the room will be rechecked in the future
# dependent on _DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY
logger.info(
"Failed to send dummy event into room %s. Will exclude it from "
"future attempts until cache expires" % (room_id,)
)
now = self.clock.time_msec()
self._rooms_to_exclude_from_dummy_event_insertion[room_id] = now
def _expire_rooms_to_exclude_from_dummy_event_insertion(self): def _expire_rooms_to_exclude_from_dummy_event_insertion(self):
expire_before = self.clock.time_msec() - _DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY expire_before = self.clock.time_msec() - _DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY