diff --git a/synapse/server_notices/resource_limits_server_notices.py b/synapse/server_notices/resource_limits_server_notices.py
index ce4a828894..d97166351e 100644
--- a/synapse/server_notices/resource_limits_server_notices.py
+++ b/synapse/server_notices/resource_limits_server_notices.py
@@ -16,8 +16,6 @@ import logging
from six import iteritems
-from twisted.internet import defer
-
from synapse.api.constants import (
EventTypes,
LimitBlockingTypes,
@@ -50,8 +48,7 @@ class ResourceLimitsServerNotices(object):
self._notifier = hs.get_notifier()
- @defer.inlineCallbacks
- def maybe_send_server_notice_to_user(self, user_id):
+ async def maybe_send_server_notice_to_user(self, user_id):
"""Check if we need to send a notice to this user, this will be true in
two cases.
1. The server has reached its limit does not reflect this
@@ -74,13 +71,13 @@ class ResourceLimitsServerNotices(object):
# Don't try and send server notices unless they've been enabled
return
- timestamp = yield self._store.user_last_seen_monthly_active(user_id)
+ timestamp = await self._store.user_last_seen_monthly_active(user_id)
if timestamp is None:
# This user will be blocked from receiving the notice anyway.
# In practice, not sure we can ever get here
return
- room_id = yield self._server_notices_manager.get_or_create_notice_room_for_user(
+ room_id = await self._server_notices_manager.get_or_create_notice_room_for_user(
user_id
)
@@ -88,10 +85,10 @@ class ResourceLimitsServerNotices(object):
logger.warning("Failed to get server notices room")
return
- yield self._check_and_set_tags(user_id, room_id)
+ await self._check_and_set_tags(user_id, room_id)
# Determine current state of room
- currently_blocked, ref_events = yield self._is_room_currently_blocked(room_id)
+ currently_blocked, ref_events = await self._is_room_currently_blocked(room_id)
limit_msg = None
limit_type = None
@@ -99,7 +96,7 @@ class ResourceLimitsServerNotices(object):
# Normally should always pass in user_id to check_auth_blocking
# if you have it, but in this case are checking what would happen
# to other users if they were to arrive.
- yield self._auth.check_auth_blocking()
+ await self._auth.check_auth_blocking()
except ResourceLimitError as e:
limit_msg = e.msg
limit_type = e.limit_type
@@ -112,22 +109,21 @@ class ResourceLimitsServerNotices(object):
# We have hit the MAU limit, but MAU alerting is disabled:
# reset room if necessary and return
if currently_blocked:
- self._remove_limit_block_notification(user_id, ref_events)
+ await self._remove_limit_block_notification(user_id, ref_events)
return
if currently_blocked and not limit_msg:
# Room is notifying of a block, when it ought not to be.
- yield self._remove_limit_block_notification(user_id, ref_events)
+ await self._remove_limit_block_notification(user_id, ref_events)
elif not currently_blocked and limit_msg:
# Room is not notifying of a block, when it ought to be.
- yield self._apply_limit_block_notification(
+ await self._apply_limit_block_notification(
user_id, limit_msg, limit_type
)
except SynapseError as e:
logger.error("Error sending resource limits server notice: %s", e)
- @defer.inlineCallbacks
- def _remove_limit_block_notification(self, user_id, ref_events):
+ async def _remove_limit_block_notification(self, user_id, ref_events):
"""Utility method to remove limit block notifications from the server
notices room.
@@ -137,12 +133,13 @@ class ResourceLimitsServerNotices(object):
limit blocking and need to be preserved.
"""
content = {"pinned": ref_events}
- yield self._server_notices_manager.send_notice(
+ await self._server_notices_manager.send_notice(
user_id, content, EventTypes.Pinned, ""
)
- @defer.inlineCallbacks
- def _apply_limit_block_notification(self, user_id, event_body, event_limit_type):
+ async def _apply_limit_block_notification(
+ self, user_id, event_body, event_limit_type
+ ):
"""Utility method to apply limit block notifications in the server
notices room.
@@ -159,17 +156,16 @@ class ResourceLimitsServerNotices(object):
"admin_contact": self._config.admin_contact,
"limit_type": event_limit_type,
}
- event = yield self._server_notices_manager.send_notice(
+ event = await self._server_notices_manager.send_notice(
user_id, content, EventTypes.Message
)
content = {"pinned": [event.event_id]}
- yield self._server_notices_manager.send_notice(
+ await self._server_notices_manager.send_notice(
user_id, content, EventTypes.Pinned, ""
)
- @defer.inlineCallbacks
- def _check_and_set_tags(self, user_id, room_id):
+ async def _check_and_set_tags(self, user_id, room_id):
"""
Since server notices rooms were originally not with tags,
important to check that tags have been set correctly
@@ -177,20 +173,19 @@ class ResourceLimitsServerNotices(object):
user_id(str): the user in question
room_id(str): the server notices room for that user
"""
- tags = yield self._store.get_tags_for_room(user_id, room_id)
+ tags = await self._store.get_tags_for_room(user_id, room_id)
need_to_set_tag = True
if tags:
if SERVER_NOTICE_ROOM_TAG in tags:
# tag already present, nothing to do here
need_to_set_tag = False
if need_to_set_tag:
- max_id = yield self._store.add_tag_to_room(
+ max_id = await self._store.add_tag_to_room(
user_id, room_id, SERVER_NOTICE_ROOM_TAG, {}
)
self._notifier.on_new_event("account_data_key", max_id, users=[user_id])
- @defer.inlineCallbacks
- def _is_room_currently_blocked(self, room_id):
+ async def _is_room_currently_blocked(self, room_id):
"""
Determines if the room is currently blocked
@@ -198,7 +193,7 @@ class ResourceLimitsServerNotices(object):
room_id(str): The room id of the server notices room
Returns:
-
+ Deferred[Tuple[bool, List]]:
bool: Is the room currently blocked
list: The list of pinned events that are unrelated to limit blocking
This list can be used as a convenience in the case where the block
@@ -208,7 +203,7 @@ class ResourceLimitsServerNotices(object):
currently_blocked = False
pinned_state_event = None
try:
- pinned_state_event = yield self._state.get_current_state(
+ pinned_state_event = await self._state.get_current_state(
room_id, event_type=EventTypes.Pinned
)
except AuthError:
@@ -219,7 +214,7 @@ class ResourceLimitsServerNotices(object):
if pinned_state_event is not None:
referenced_events = list(pinned_state_event.content.get("pinned", []))
- events = yield self._store.get_events(referenced_events)
+ events = await self._store.get_events(referenced_events)
for event_id, event in iteritems(events):
if event.type != EventTypes.Message:
continue
|