support server notice state events for resource limits

This commit is contained in:
Neil Johnson 2018-08-14 11:20:41 +01:00
parent 63417c31e9
commit 9b75c78b4d
4 changed files with 22 additions and 8 deletions

View file

@ -79,6 +79,8 @@ class EventTypes(object):
ServerACL = "m.room.server_acl"
ServerNoticeLimitReached = "m.server_notice.usage_limit_reached"
class RejectedReason(object):
AUTH_ERROR = "auth_error"

View file

@ -22,6 +22,7 @@ from synapse.api.errors import SynapseError
from synapse.api.urls import ConsentURIBuilder
from synapse.config import ConfigError
from synapse.types import get_localpart_from_id
from synapse.api.constants import EventTypes
logger = logging.getLogger(__name__)
@ -103,7 +104,7 @@ class ConsentServerNotices(object):
},
)
yield self._server_notices_manager.send_notice(
user_id, content,
user_id, content, EventTypes.Message
)
yield self._store.user_set_consent_server_notice_sent(
user_id, self._current_consent_version,

View file

@ -17,6 +17,7 @@ import logging
from twisted.internet import defer
from synapse.api.errors import AuthError, SynapseError
from synapse.api.constants import EventTypes
logger = logging.getLogger(__name__)
@ -61,22 +62,32 @@ class ResourceLimitsServerNotices(object):
# In practice, not sure we can ever get here
return
try:
# Normally should always pass in user_id 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()
self._resouce_limited = False
# Need to start removing notices
if user_id in self._notified_of_blocking:
# Send message to remove warning - needs updating
content = "remove warning"
content = {
'body': '',
'admin_email': '',
}
self._send_server_notice(user_id, content)
self._notified_of_blocking.remove(user_id)
except AuthError:
except AuthError as e:
# Need to start notifying of blocking
self._resouce_limited = True
if user_id not in self._notified_of_blocking:
# Send message to add warning - needs updating
content = "add warning"
# TODO use admin email contained in error once PR lands
content = {
'body': e.msg,
'admin_email': 'stunt@adminemail.com',
'msgtype': 'm.text'
}
self._send_server_notice(user_id, content)
self._notified_of_blocking.add(user_id)
@ -93,7 +104,7 @@ class ResourceLimitsServerNotices(object):
"""
try:
yield self._server_notices_manager.send_notice(
user_id, content,
user_id, content, EventTypes.ServerNoticeLimitReached
)
except SynapseError as e:
logger.error("Error sending server notice about resource limits: %s", e)

View file

@ -46,7 +46,7 @@ class ServerNoticesManager(object):
return self._config.server_notices_mxid is not None
@defer.inlineCallbacks
def send_notice(self, user_id, event_content):
def send_notice(self, user_id, event_content, type):
"""Send a notice to the given user
Creates the server notices room, if none exists.
@ -67,7 +67,7 @@ class ServerNoticesManager(object):
yield self._event_creation_handler.create_and_send_nonmember_event(
requester, {
"type": EventTypes.Message,
"type": type,
"room_id": room_id,
"sender": system_mxid,
"content": event_content,