wip cut at sending resource server notices

This commit is contained in:
Neil Johnson 2018-08-15 15:04:52 +01:00
parent c24fc9797b
commit eabc5f8271
2 changed files with 53 additions and 42 deletions

View file

@ -40,6 +40,7 @@ class ResourceLimitsServerNotices(object):
self._notified_of_blocking = set() self._notified_of_blocking = set()
self._resouce_limited = False self._resouce_limited = False
# Config checks? # Config checks?
@defer.inlineCallbacks @defer.inlineCallbacks
@ -69,43 +70,43 @@ class ResourceLimitsServerNotices(object):
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
# send state event here
# How do I do this? if drop the id, how to refer to it?
content = { content = {
'body': '', "pinned":[]
'admin_email': '',
} }
self._send_server_notice(user_id, content) yield self._server_notices_manager.send_notice(
user_id, content, EventTypes.Pinned, '',
)
self._notified_of_blocking.remove(user_id) self._notified_of_blocking.remove(user_id)
except AuthError as e: except AuthError as e:
# Need to start notifying of blocking # Need to start notifying of blocking
try:
self._resouce_limited = True
if user_id not in self._notified_of_blocking:
# TODO use admin email contained in error once PR lands
content = {
'body': e.msg,
'admin_email': 'stunt@adminemail.com',
}
event = yield self._server_notices_manager.send_notice(
user_id, content, EventTypes.ServerNoticeLimitReached
)
self._resouce_limited = True # send server notices state event here
if user_id not in self._notified_of_blocking: # TODO Over writing pinned events
# TODO use admin email contained in error once PR lands content = {
content = { "pinned":[
'body': e.msg, event.event_id,
'admin_email': 'stunt@adminemail.com', ]
'msgtype': 'm.text' }
} yield self._server_notices_manager.send_notice(
self._send_server_notice(user_id, content) user_id, content, EventTypes.Pinned, '',
self._notified_of_blocking.add(user_id) )
self._notified_of_blocking.add(user_id)
@defer.inlineCallbacks except SynapseError as e:
def _send_server_notice(self, user_id, content): logger.error("Error sending server notice about resource limits: %s", e)
"""Sends Server notice
Args:
user_id(str): The user to send to
content(str): The content of the message
Returns:
Deferred[]
"""
try:
yield self._server_notices_manager.send_notice(
user_id, content
)
except SynapseError as e:
logger.error("Error sending server notice about resource limits: %s", e)

View file

@ -46,7 +46,10 @@ 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=EventTypes.Message, state_key=None
):
"""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.
@ -54,9 +57,11 @@ class ServerNoticesManager(object):
Args: Args:
user_id (str): mxid of user to send event to. user_id (str): mxid of user to send event to.
event_content (dict): content of event to send event_content (dict): content of event to send
type(EventTypes): type of event
is_state_event(bool): Is the event a state event
Returns: Returns:
Deferred[None] Deferred[FrozenEvent]
""" """
room_id = yield self.get_notice_room_for_user(user_id) room_id = yield self.get_notice_room_for_user(user_id)
@ -65,15 +70,19 @@ class ServerNoticesManager(object):
logger.info("Sending server notice to %s", user_id) logger.info("Sending server notice to %s", user_id)
yield self._event_creation_handler.create_and_send_nonmember_event( event_dict = {
requester, { "type": type,
"type": EventTypes.Message, "room_id": room_id,
"room_id": room_id, "sender": system_mxid,
"sender": system_mxid, "content": event_content,
"content": event_content, }
}, if state_key:
ratelimit=False, event_dict['state_key'] = state_key
res = yield self._event_creation_handler.create_and_send_nonmember_event(
requester, event_dict, ratelimit=False,
) )
defer.returnValue(res)
@cachedInlineCallbacks() @cachedInlineCallbacks()
def get_notice_room_for_user(self, user_id): def get_notice_room_for_user(self, user_id):
@ -141,6 +150,7 @@ class ServerNoticesManager(object):
creator_join_profile=join_profile, creator_join_profile=join_profile,
) )
room_id = info['room_id'] room_id = info['room_id']
yield self._store.add_tag_to_room(user_id, room_id, 'm.server_notice', None)
logger.info("Created server notices room %s for %s", room_id, user_id) logger.info("Created server notices room %s for %s", room_id, user_id)
defer.returnValue(room_id) defer.returnValue(room_id)