2018-08-09 23:16:00 +02: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
|
|
|
|
|
2018-08-10 16:12:59 +02:00
|
|
|
from synapse.api.errors import AuthError, SynapseError
|
2018-08-09 23:16:00 +02: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()
|
2018-08-10 16:12:59 +02:00
|
|
|
self.auth = hs.get_auth()
|
2018-08-09 23:16:00 +02:00
|
|
|
self._server_notice_content = hs.config.user_consent_server_notice_content
|
2018-08-10 16:12:59 +02:00
|
|
|
self._limit_usage_by_mau = hs.config.limit_usage_by_mau = False
|
|
|
|
self._hs_disabled = hs.config.hs_disabled
|
2018-08-09 23:16:00 +02:00
|
|
|
|
2018-08-10 16:12:59 +02:00
|
|
|
self._notified_of_blocking = set()
|
2018-08-09 23:16:00 +02:00
|
|
|
self._resouce_limited = False
|
|
|
|
# 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
|
|
|
|
"""
|
2018-08-10 16:12:59 +02:00
|
|
|
if self._hs_disabled is True:
|
2018-08-09 23:16:00 +02:00
|
|
|
return
|
|
|
|
|
2018-08-10 16:12:59 +02:00
|
|
|
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
|
|
|
|
try:
|
|
|
|
yield self.auth.check_auth_blocking()
|
|
|
|
self._resouce_limited = False
|
2018-08-09 23:16:00 +02:00
|
|
|
# Need to start removing notices
|
2018-08-10 16:12:59 +02:00
|
|
|
if user_id in self._notified_of_blocking:
|
|
|
|
# Send message to remove warning - needs updating
|
|
|
|
content = "remove warning"
|
|
|
|
self._send_server_notice(user_id, content)
|
|
|
|
self._notified_of_blocking.remove(user_id)
|
2018-08-09 23:16:00 +02:00
|
|
|
|
2018-08-10 16:12:59 +02:00
|
|
|
except AuthError:
|
|
|
|
# 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"
|
|
|
|
self._send_server_notice(user_id, content)
|
|
|
|
self._notified_of_blocking.add(user_id)
|
2018-08-09 23:16:00 +02:00
|
|
|
|
2018-08-10 16:12:59 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _send_server_notice(self, user_id, content):
|
|
|
|
"""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)
|