mirror of
https://github.com/element-hq/synapse
synced 2024-09-01 21:30:21 +00:00
support server notice state events for resource limits
This commit is contained in:
parent
63417c31e9
commit
9b75c78b4d
4 changed files with 22 additions and 8 deletions
|
@ -79,6 +79,8 @@ class EventTypes(object):
|
||||||
|
|
||||||
ServerACL = "m.room.server_acl"
|
ServerACL = "m.room.server_acl"
|
||||||
|
|
||||||
|
ServerNoticeLimitReached = "m.server_notice.usage_limit_reached"
|
||||||
|
|
||||||
|
|
||||||
class RejectedReason(object):
|
class RejectedReason(object):
|
||||||
AUTH_ERROR = "auth_error"
|
AUTH_ERROR = "auth_error"
|
||||||
|
|
|
@ -22,6 +22,7 @@ from synapse.api.errors import SynapseError
|
||||||
from synapse.api.urls import ConsentURIBuilder
|
from synapse.api.urls import ConsentURIBuilder
|
||||||
from synapse.config import ConfigError
|
from synapse.config import ConfigError
|
||||||
from synapse.types import get_localpart_from_id
|
from synapse.types import get_localpart_from_id
|
||||||
|
from synapse.api.constants import EventTypes
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -103,7 +104,7 @@ class ConsentServerNotices(object):
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
yield self._server_notices_manager.send_notice(
|
yield self._server_notices_manager.send_notice(
|
||||||
user_id, content,
|
user_id, content, EventTypes.Message
|
||||||
)
|
)
|
||||||
yield self._store.user_set_consent_server_notice_sent(
|
yield self._store.user_set_consent_server_notice_sent(
|
||||||
user_id, self._current_consent_version,
|
user_id, self._current_consent_version,
|
||||||
|
|
|
@ -17,6 +17,7 @@ import logging
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
from synapse.api.errors import AuthError, SynapseError
|
from synapse.api.errors import AuthError, SynapseError
|
||||||
|
from synapse.api.constants import EventTypes
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -61,22 +62,32 @@ class ResourceLimitsServerNotices(object):
|
||||||
# In practice, not sure we can ever get here
|
# In practice, not sure we can ever get here
|
||||||
return
|
return
|
||||||
try:
|
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()
|
yield self.auth.check_auth_blocking()
|
||||||
self._resouce_limited = False
|
self._resouce_limited = False
|
||||||
# Need to start removing notices
|
# Need to start removing notices
|
||||||
if user_id in self._notified_of_blocking:
|
if user_id in self._notified_of_blocking:
|
||||||
# Send message to remove warning - needs updating
|
# Send message to remove warning - needs updating
|
||||||
content = "remove warning"
|
content = {
|
||||||
|
'body': '',
|
||||||
|
'admin_email': '',
|
||||||
|
}
|
||||||
self._send_server_notice(user_id, content)
|
self._send_server_notice(user_id, content)
|
||||||
self._notified_of_blocking.remove(user_id)
|
self._notified_of_blocking.remove(user_id)
|
||||||
|
|
||||||
except AuthError:
|
except AuthError as e:
|
||||||
# Need to start notifying of blocking
|
# Need to start notifying of blocking
|
||||||
|
|
||||||
self._resouce_limited = True
|
self._resouce_limited = True
|
||||||
if user_id not in self._notified_of_blocking:
|
if user_id not in self._notified_of_blocking:
|
||||||
# Send message to add warning - needs updating
|
# TODO use admin email contained in error once PR lands
|
||||||
content = "add warning"
|
content = {
|
||||||
|
'body': e.msg,
|
||||||
|
'admin_email': 'stunt@adminemail.com',
|
||||||
|
'msgtype': 'm.text'
|
||||||
|
}
|
||||||
self._send_server_notice(user_id, content)
|
self._send_server_notice(user_id, content)
|
||||||
self._notified_of_blocking.add(user_id)
|
self._notified_of_blocking.add(user_id)
|
||||||
|
|
||||||
|
@ -93,7 +104,7 @@ class ResourceLimitsServerNotices(object):
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
yield self._server_notices_manager.send_notice(
|
yield self._server_notices_manager.send_notice(
|
||||||
user_id, content,
|
user_id, content, EventTypes.ServerNoticeLimitReached
|
||||||
)
|
)
|
||||||
except SynapseError as e:
|
except SynapseError as e:
|
||||||
logger.error("Error sending server notice about resource limits: %s", e)
|
logger.error("Error sending server notice about resource limits: %s", e)
|
||||||
|
|
|
@ -46,7 +46,7 @@ class ServerNoticesManager(object):
|
||||||
return self._config.server_notices_mxid is not None
|
return self._config.server_notices_mxid is not None
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@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
|
"""Send a notice to the given user
|
||||||
|
|
||||||
Creates the server notices room, if none exists.
|
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(
|
yield self._event_creation_handler.create_and_send_nonmember_event(
|
||||||
requester, {
|
requester, {
|
||||||
"type": EventTypes.Message,
|
"type": type,
|
||||||
"room_id": room_id,
|
"room_id": room_id,
|
||||||
"sender": system_mxid,
|
"sender": system_mxid,
|
||||||
"content": event_content,
|
"content": event_content,
|
||||||
|
|
Loading…
Reference in a new issue