anonymousland-synapse/synapse/server_notices/resource_limits_server_notices.py

137 lines
5.2 KiB
Python
Raw Normal View History

2018-08-09 21:16:00 +00:00
# -*- coding: utf-8 -*-
# Copyright 2018 New Vector Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from twisted.internet import defer
from synapse.api.constants import EventTypes
from synapse.api.errors import AuthError, SynapseError
2018-08-09 21:16:00 +00:00
logger = logging.getLogger(__name__)
class ResourceLimitsServerNotices(object):
"""
"""
def __init__(self, hs):
"""
Args:
hs (synapse.server.HomeServer):
"""
self._server_notices_manager = hs.get_server_notices_manager()
self._store = hs.get_datastore()
self.auth = hs.get_auth()
2018-08-09 21:16:00 +00:00
self._server_notice_content = hs.config.user_consent_server_notice_content
self._admin_uri = hs.config.admin_uri
2018-08-13 21:36:52 +00:00
self._limit_usage_by_mau = hs.config.limit_usage_by_mau
self._hs_disabled = hs.config.hs_disabled
2018-08-09 21:16:00 +00:00
self._resouce_limited = False
2018-08-16 10:10:53 +00:00
self._message_handler = hs.get_message_handler()
self._state = hs.get_state_handler()
2018-08-09 21:16:00 +00:00
# Config checks?
@defer.inlineCallbacks
def maybe_send_server_notice_to_user(self, user_id):
"""Check if we need to send a notice to this user, and does so if so
Args:
user_id (str): user to check
Returns:
Deferred
"""
if self._hs_disabled is True:
2018-08-09 21:16:00 +00:00
return
if self._limit_usage_by_mau is True:
timestamp = yield 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
2018-08-16 10:10:53 +00:00
room_id = yield self._server_notices_manager.get_notice_room_for_user(user_id)
2018-08-16 10:10:53 +00:00
2018-08-16 14:48:34 +00:00
# Need to set tag here because room may have been created prior to
# tagging being set on creation. Ideally would set something to read
# room tags first, and cache that aggressively/
yield self._store.add_tag_to_room(user_id, room_id, 'm.server_notice', None)
2018-08-16 10:10:53 +00:00
currently_blocked = False
pinned_state_event = None
try:
pinned_state_event = yield self._state.get_current_state(
room_id, event_type=EventTypes.Pinned
)
except AuthError as e:
# The user has yet to join the server notices room
pass
2018-08-16 10:10:53 +00:00
referenced_events = []
if pinned_state_event is not None:
referenced_events = pinned_state_event.content.get('pinned')
2018-08-16 10:10:53 +00:00
events = yield self._store.get_events(referenced_events)
for event_id, event in events.items():
if event.type == EventTypes.ServerNoticeLimitReached:
2018-08-16 10:10:53 +00:00
currently_blocked = True
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()
2018-08-09 21:16:00 +00:00
# Need to start removing notices
2018-08-16 10:10:53 +00:00
# if user_id in self._notified_of_blocking:
if currently_blocked:
# Send message to remove warning
# send state event here
# How do I do this? if drop the id, how to refer to it?
content = {
"pinned": []
}
yield self._server_notices_manager.send_notice(
user_id, content, EventTypes.Pinned, '',
)
2018-08-09 21:16:00 +00:00
except AuthError as e:
# Need to start notifying of blocking
try:
2018-08-16 10:10:53 +00:00
if not currently_blocked:
# TODO use admin email contained in error once PR lands
content = {
'body': e.msg,
'admin_uri': self._admin_uri,
}
event = yield self._server_notices_manager.send_notice(
user_id, content, EventTypes.ServerNoticeLimitReached
)
# send server notices state event here
# TODO Over writing pinned events
content = {
"pinned": [
event.event_id,
]
}
yield self._server_notices_manager.send_notice(
user_id, content, EventTypes.Pinned, '',
)
except SynapseError as e:
logger.error("Error sending resource limits server notice: %s", e)