2018-08-09 17:16:00 -04: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
|
|
|
|
|
2018-08-17 10:21:34 -04:00
|
|
|
from six import iteritems
|
|
|
|
|
2018-08-09 17:16:00 -04:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-08-22 12:00:29 -04:00
|
|
|
from synapse.api.constants import (
|
|
|
|
EventTypes,
|
2019-10-24 06:48:46 -04:00
|
|
|
LimitBlockingTypes,
|
2018-08-22 12:00:29 -04:00
|
|
|
ServerNoticeLimitReached,
|
|
|
|
ServerNoticeMsgType,
|
|
|
|
)
|
2018-08-17 10:21:34 -04:00
|
|
|
from synapse.api.errors import AuthError, ResourceLimitError, SynapseError
|
2018-08-16 12:02:04 -04:00
|
|
|
from synapse.server_notices.server_notices_manager import SERVER_NOTICE_ROOM_TAG
|
2018-08-09 17:16:00 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceLimitsServerNotices(object):
|
2018-08-17 10:21:34 -04:00
|
|
|
""" Keeps track of whether the server has reached it's resource limit and
|
|
|
|
ensures that the client is kept up to date.
|
2018-08-09 17:16:00 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2018-08-09 17:16:00 -04:00
|
|
|
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-17 10:21:34 -04:00
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._config = hs.config
|
2018-08-09 17:16:00 -04:00
|
|
|
self._resouce_limited = False
|
2018-08-16 06:10:53 -04:00
|
|
|
self._message_handler = hs.get_message_handler()
|
|
|
|
self._state = hs.get_state_handler()
|
2018-08-09 17:16:00 -04:00
|
|
|
|
2018-08-24 09:50:03 -04:00
|
|
|
self._notifier = hs.get_notifier()
|
|
|
|
|
2018-08-09 17:16:00 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def maybe_send_server_notice_to_user(self, user_id):
|
2018-08-17 10:21:34 -04:00
|
|
|
"""Check if we need to send a notice to this user, this will be true in
|
|
|
|
two cases.
|
|
|
|
1. The server has reached its limit does not reflect this
|
|
|
|
2. The room state indicates that the server has reached its limit when
|
|
|
|
actually the server is fine
|
2018-08-09 17:16:00 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): user to check
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred
|
|
|
|
"""
|
2018-08-17 10:21:34 -04:00
|
|
|
if self._config.hs_disabled is True:
|
2018-08-09 17:16:00 -04:00
|
|
|
return
|
|
|
|
|
2018-08-17 10:21:34 -04:00
|
|
|
if self._config.limit_usage_by_mau is False:
|
2018-08-16 12:02:04 -04:00
|
|
|
return
|
2018-08-16 06:10:53 -04:00
|
|
|
|
2018-08-24 06:31:31 -04:00
|
|
|
if not self._server_notices_manager.is_enabled():
|
2019-10-24 06:48:46 -04:00
|
|
|
# Don't try and send server notices unless they've been enabled
|
2018-08-24 06:31:31 -04:00
|
|
|
return
|
|
|
|
|
2018-08-16 12:02:04 -04:00
|
|
|
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:48:34 -04:00
|
|
|
|
2018-08-16 12:02:04 -04:00
|
|
|
room_id = yield self._server_notices_manager.get_notice_room_for_user(user_id)
|
|
|
|
|
2018-08-23 11:20:51 -04:00
|
|
|
if not room_id:
|
2019-10-31 06:23:24 -04:00
|
|
|
logger.warning("Failed to get server notices room")
|
2018-08-23 11:20:51 -04:00
|
|
|
return
|
|
|
|
|
2018-08-16 12:02:04 -04:00
|
|
|
yield self._check_and_set_tags(user_id, room_id)
|
2019-10-24 06:48:46 -04:00
|
|
|
|
|
|
|
# Determine current state of room
|
2018-08-16 12:02:04 -04:00
|
|
|
currently_blocked, ref_events = yield self._is_room_currently_blocked(room_id)
|
|
|
|
|
2019-10-24 06:48:46 -04:00
|
|
|
limit_msg = None
|
|
|
|
limit_type = None
|
2018-08-16 12:02:04 -04:00
|
|
|
try:
|
2019-10-24 06:48:46 -04:00
|
|
|
# Normally should always pass in user_id to check_auth_blocking
|
|
|
|
# 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()
|
|
|
|
except ResourceLimitError as e:
|
|
|
|
limit_msg = e.msg
|
|
|
|
limit_type = e.limit_type
|
2018-08-16 12:02:04 -04:00
|
|
|
|
2019-10-24 06:48:46 -04:00
|
|
|
try:
|
|
|
|
if (
|
|
|
|
limit_type == LimitBlockingTypes.MONTHLY_ACTIVE_USER
|
|
|
|
and not self._config.mau_limit_alerting
|
|
|
|
):
|
|
|
|
# We have hit the MAU limit, but MAU alerting is disabled:
|
|
|
|
# reset room if necessary and return
|
|
|
|
if currently_blocked:
|
|
|
|
self._remove_limit_block_notification(user_id, ref_events)
|
|
|
|
return
|
|
|
|
|
|
|
|
if currently_blocked and not limit_msg:
|
|
|
|
# Room is notifying of a block, when it ought not to be.
|
|
|
|
yield self._remove_limit_block_notification(user_id, ref_events)
|
|
|
|
elif not currently_blocked and limit_msg:
|
2018-08-17 10:21:34 -04:00
|
|
|
# Room is not notifying of a block, when it ought to be.
|
2019-10-24 06:48:46 -04:00
|
|
|
yield self._apply_limit_block_notification(
|
|
|
|
user_id, limit_msg, limit_type
|
2018-08-17 10:21:34 -04:00
|
|
|
)
|
|
|
|
except SynapseError as e:
|
|
|
|
logger.error("Error sending resource limits server notice: %s", e)
|
2018-08-16 12:02:04 -04:00
|
|
|
|
2019-10-24 06:48:46 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _remove_limit_block_notification(self, user_id, ref_events):
|
|
|
|
"""Utility method to remove limit block notifications from the server
|
|
|
|
notices room.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): user to notify
|
|
|
|
ref_events (list[str]): The event_ids of pinned events that are unrelated to
|
|
|
|
limit blocking and need to be preserved.
|
|
|
|
"""
|
|
|
|
content = {"pinned": ref_events}
|
|
|
|
yield self._server_notices_manager.send_notice(
|
|
|
|
user_id, content, EventTypes.Pinned, ""
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _apply_limit_block_notification(self, user_id, event_body, event_limit_type):
|
|
|
|
"""Utility method to apply limit block notifications in the server
|
|
|
|
notices room.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id (str): user to notify
|
|
|
|
event_body(str): The human readable text that describes the block.
|
|
|
|
event_limit_type(str): Specifies the type of block e.g. monthly active user
|
|
|
|
limit has been exceeded.
|
|
|
|
"""
|
|
|
|
content = {
|
|
|
|
"body": event_body,
|
|
|
|
"msgtype": ServerNoticeMsgType,
|
|
|
|
"server_notice_type": ServerNoticeLimitReached,
|
|
|
|
"admin_contact": self._config.admin_contact,
|
|
|
|
"limit_type": event_limit_type,
|
|
|
|
}
|
|
|
|
event = yield self._server_notices_manager.send_notice(
|
|
|
|
user_id, content, EventTypes.Message
|
|
|
|
)
|
|
|
|
|
|
|
|
content = {"pinned": [event.event_id]}
|
|
|
|
yield self._server_notices_manager.send_notice(
|
|
|
|
user_id, content, EventTypes.Pinned, ""
|
|
|
|
)
|
|
|
|
|
2018-08-16 12:02:04 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _check_and_set_tags(self, user_id, room_id):
|
|
|
|
"""
|
|
|
|
Since server notices rooms were originally not with tags,
|
|
|
|
important to check that tags have been set correctly
|
|
|
|
Args:
|
|
|
|
user_id(str): the user in question
|
|
|
|
room_id(str): the server notices room for that user
|
|
|
|
"""
|
2018-08-24 11:22:37 -04:00
|
|
|
tags = yield self._store.get_tags_for_room(user_id, room_id)
|
2018-08-16 12:02:04 -04:00
|
|
|
need_to_set_tag = True
|
2018-08-24 11:22:37 -04:00
|
|
|
if tags:
|
|
|
|
if SERVER_NOTICE_ROOM_TAG in tags:
|
2018-08-16 12:02:04 -04:00
|
|
|
# tag already present, nothing to do here
|
|
|
|
need_to_set_tag = False
|
|
|
|
if need_to_set_tag:
|
2018-08-24 09:50:03 -04:00
|
|
|
max_id = yield self._store.add_tag_to_room(
|
2018-08-24 09:44:16 -04:00
|
|
|
user_id, room_id, SERVER_NOTICE_ROOM_TAG, {}
|
2018-08-16 12:02:04 -04:00
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
self._notifier.on_new_event("account_data_key", max_id, users=[user_id])
|
2018-08-16 12:02:04 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _is_room_currently_blocked(self, room_id):
|
|
|
|
"""
|
|
|
|
Determines if the room is currently blocked
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room_id(str): The room id of the server notices room
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
bool: Is the room currently blocked
|
|
|
|
list: The list of pinned events that are unrelated to limit blocking
|
|
|
|
This list can be used as a convenience in the case where the block
|
|
|
|
is to be lifted and the remaining pinned event references need to be
|
|
|
|
preserved
|
|
|
|
"""
|
|
|
|
currently_blocked = False
|
|
|
|
pinned_state_event = None
|
|
|
|
try:
|
|
|
|
pinned_state_event = yield self._state.get_current_state(
|
|
|
|
room_id, event_type=EventTypes.Pinned
|
|
|
|
)
|
2018-08-16 12:32:22 -04:00
|
|
|
except AuthError:
|
2018-08-16 12:02:04 -04:00
|
|
|
# The user has yet to join the server notices room
|
|
|
|
pass
|
|
|
|
|
|
|
|
referenced_events = []
|
|
|
|
if pinned_state_event is not None:
|
2019-06-20 05:32:02 -04:00
|
|
|
referenced_events = list(pinned_state_event.content.get("pinned", []))
|
2018-08-16 12:02:04 -04:00
|
|
|
|
|
|
|
events = yield self._store.get_events(referenced_events)
|
2018-08-18 07:31:08 -04:00
|
|
|
for event_id, event in iteritems(events):
|
2018-08-22 12:00:29 -04:00
|
|
|
if event.type != EventTypes.Message:
|
|
|
|
continue
|
|
|
|
if event.content.get("msgtype") == ServerNoticeMsgType:
|
2018-08-16 12:02:04 -04:00
|
|
|
currently_blocked = True
|
|
|
|
# remove event in case we need to disable blocking later on.
|
|
|
|
if event_id in referenced_events:
|
|
|
|
referenced_events.remove(event.event_id)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return currently_blocked, referenced_events
|