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

Commit

Permalink
Factor out _send_dummy_event_for_room (#8370)
Browse files Browse the repository at this point in the history
this makes it possible to use from the manhole, and seems cleaner anyway.
  • Loading branch information
richvdh authored Sep 23, 2020
1 parent 91c60f3 commit 2983049
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
1 change: 1 addition & 0 deletions changelog.d/8370.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Factor out a `_send_dummy_event_for_room` method.
102 changes: 54 additions & 48 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,54 +1182,7 @@ async def _send_dummy_events_to_fill_extremities(self):
)

for room_id in room_ids:
# For each room we need to find a joined member we can use to send
# the dummy event with.

latest_event_ids = await self.store.get_prev_events_for_room(room_id)

members = await self.state.get_current_users_in_room(
room_id, latest_event_ids=latest_event_ids
)
dummy_event_sent = False
for user_id in members:
if not self.hs.is_mine_id(user_id):
continue
requester = create_requester(user_id)
try:
event, context = await self.create_event(
requester,
{
"type": "org.matrix.dummy_event",
"content": {},
"room_id": room_id,
"sender": user_id,
},
prev_event_ids=latest_event_ids,
)

event.internal_metadata.proactively_send = False

# Since this is a dummy-event it is OK if it is sent by a
# shadow-banned user.
await self.send_nonmember_event(
requester,
event,
context,
ratelimit=False,
ignore_shadow_ban=True,
)
dummy_event_sent = True
break
except ConsentNotGivenError:
logger.info(
"Failed to send dummy event into room %s for user %s due to "
"lack of consent. Will try another user" % (room_id, user_id)
)
except AuthError:
logger.info(
"Failed to send dummy event into room %s for user %s due to "
"lack of power. Will try another user" % (room_id, user_id)
)
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
Expand All @@ -1242,6 +1195,59 @@ async def _send_dummy_events_to_fill_extremities(self):
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
# the dummy event with.
latest_event_ids = await self.store.get_prev_events_for_room(room_id)
members = await self.state.get_current_users_in_room(
room_id, latest_event_ids=latest_event_ids
)
for user_id in members:
if not self.hs.is_mine_id(user_id):
continue
requester = create_requester(user_id)
try:
event, context = await self.create_event(
requester,
{
"type": "org.matrix.dummy_event",
"content": {},
"room_id": room_id,
"sender": user_id,
},
prev_event_ids=latest_event_ids,
)

event.internal_metadata.proactively_send = False

# Since this is a dummy-event it is OK if it is sent by a
# shadow-banned user.
await self.send_nonmember_event(
requester, event, context, ratelimit=False, ignore_shadow_ban=True,
)
return True
except ConsentNotGivenError:
logger.info(
"Failed to send dummy event into room %s for user %s due to "
"lack of consent. Will try another user" % (room_id, user_id)
)
except AuthError:
logger.info(
"Failed to send dummy event into room %s for user %s due to "
"lack of power. Will try another user" % (room_id, user_id)
)
return False

def _expire_rooms_to_exclude_from_dummy_event_insertion(self):
expire_before = self.clock.time_msec() - _DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY
to_expire = set()
Expand Down

0 comments on commit 2983049

Please sign in to comment.