2018-08-09 17:16:00 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2018-08-09 17:16:00 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import logging
|
2021-03-24 06:48:46 -04:00
|
|
|
from typing import TYPE_CHECKING, List, Tuple
|
2018-08-09 17:16:00 -04:00
|
|
|
|
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-09 17:16:00 -04:00
|
|
|
|
2021-03-24 06:48:46 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2018-08-09 17:16:00 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class ResourceLimitsServerNotices:
|
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
|
|
|
|
2021-03-24 06:48:46 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2018-08-09 17:16:00 -04:00
|
|
|
self._server_notices_manager = hs.get_server_notices_manager()
|
2022-02-23 06:04:02 -05:00
|
|
|
self._store = hs.get_datastores().main
|
2022-06-06 04:24:12 -04:00
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2022-06-14 04:51:15 -04:00
|
|
|
self._auth_blocking = hs.get_auth_blocking()
|
2018-08-17 10:21:34 -04:00
|
|
|
self._config = hs.config
|
2018-08-09 17:16:00 -04:00
|
|
|
self._resouce_limited = False
|
2021-01-20 05:44:52 -05:00
|
|
|
self._account_data_handler = hs.get_account_data_handler()
|
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()
|
|
|
|
|
2020-05-22 16:47:07 -04:00
|
|
|
self._enabled = (
|
2021-09-29 06:44:15 -04:00
|
|
|
hs.config.server.limit_usage_by_mau
|
2020-05-22 16:47:07 -04:00
|
|
|
and self._server_notices_manager.is_enabled()
|
2021-09-29 06:44:15 -04:00
|
|
|
and not hs.config.server.hs_disabled
|
2020-05-22 16:47:07 -04:00
|
|
|
)
|
|
|
|
|
2020-07-31 16:22:06 -04:00
|
|
|
async def maybe_send_server_notice_to_user(self, user_id: str) -> None:
|
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:
|
2020-07-31 16:22:06 -04:00
|
|
|
user_id: user to check
|
2018-08-09 17:16:00 -04:00
|
|
|
"""
|
2020-05-22 16:47:07 -04:00
|
|
|
if not self._enabled:
|
2018-08-24 06:31:31 -04:00
|
|
|
return
|
|
|
|
|
2020-05-01 10:15:36 -04:00
|
|
|
timestamp = await self._store.user_last_seen_monthly_active(user_id)
|
2018-08-16 12:02:04 -04:00
|
|
|
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
|
|
|
|
2022-05-13 09:30:15 -04:00
|
|
|
# Check if there's a server notice room for this user.
|
|
|
|
room_id = await self._server_notices_manager.maybe_get_notice_room_for_user(
|
Server notices: Dissociate room creation/lookup from invite (#7199)
Fixes #6815
Before figuring out whether we should alert a user on MAU, we call get_notice_room_for_user to get some info on the existing server notices room for this user. This function, if the room doesn't exist, creates it and invites the user in it. This means that, if we decide later that no server notice is needed, the user gets invited in a room with no message in it. This happens at every restart of the server, since the room ID returned by get_notice_room_for_user is cached.
This PR fixes that by moving the inviting bit to a dedicated function, that's only called when the server actually needs to send a notice to the user. A potential issue with this approach is that the room that's created by get_notice_room_for_user doesn't match how that same function looks for an existing room (i.e. it creates a room that doesn't have an invite or a join for the current user in it, so it could lead to a new room being created each time a user syncs), but I'm not sure this is a problem given it's cached until the server restarts, so that function won't run very often.
It also renames get_notice_room_for_user into get_or_create_notice_room_for_user to make what it does clearer.
2020-04-04 11:27:45 -04:00
|
|
|
user_id
|
|
|
|
)
|
2018-08-16 12:02:04 -04:00
|
|
|
|
2022-05-13 09:30:15 -04:00
|
|
|
if room_id is not None:
|
|
|
|
# Determine current state of room
|
|
|
|
currently_blocked, ref_events = await self._is_room_currently_blocked(
|
|
|
|
room_id
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
currently_blocked = False
|
|
|
|
ref_events = []
|
2018-08-16 12:02:04 -04:00
|
|
|
|
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.
|
2022-06-14 04:51:15 -04:00
|
|
|
await self._auth_blocking.check_auth_blocking()
|
2019-10-24 06:48:46 -04:00
|
|
|
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
|
2021-09-29 06:44:15 -04:00
|
|
|
and not self._config.server.mau_limit_alerting
|
2019-10-24 06:48:46 -04:00
|
|
|
):
|
|
|
|
# We have hit the MAU limit, but MAU alerting is disabled:
|
|
|
|
# reset room if necessary and return
|
|
|
|
if currently_blocked:
|
2020-05-01 10:15:36 -04:00
|
|
|
await self._remove_limit_block_notification(user_id, ref_events)
|
2019-10-24 06:48:46 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
if currently_blocked and not limit_msg:
|
|
|
|
# Room is notifying of a block, when it ought not to be.
|
2020-05-01 10:15:36 -04:00
|
|
|
await self._remove_limit_block_notification(user_id, ref_events)
|
2019-10-24 06:48:46 -04:00
|
|
|
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.
|
2020-05-01 10:15:36 -04:00
|
|
|
await self._apply_limit_block_notification(
|
2024-09-02 07:39:04 -04:00
|
|
|
user_id,
|
|
|
|
limit_msg,
|
|
|
|
limit_type, # type: ignore
|
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
|
|
|
|
2020-07-31 16:22:06 -04:00
|
|
|
async def _remove_limit_block_notification(
|
|
|
|
self, user_id: str, ref_events: List[str]
|
|
|
|
) -> None:
|
2019-10-24 06:48:46 -04:00
|
|
|
"""Utility method to remove limit block notifications from the server
|
|
|
|
notices room.
|
|
|
|
|
|
|
|
Args:
|
2020-07-31 16:22:06 -04:00
|
|
|
user_id: user to notify
|
|
|
|
ref_events: The event_ids of pinned events that are unrelated to
|
|
|
|
limit blocking and need to be preserved.
|
2019-10-24 06:48:46 -04:00
|
|
|
"""
|
|
|
|
content = {"pinned": ref_events}
|
2020-05-01 10:15:36 -04:00
|
|
|
await self._server_notices_manager.send_notice(
|
2019-10-24 06:48:46 -04:00
|
|
|
user_id, content, EventTypes.Pinned, ""
|
|
|
|
)
|
|
|
|
|
2020-05-01 10:15:36 -04:00
|
|
|
async def _apply_limit_block_notification(
|
2020-07-31 16:22:06 -04:00
|
|
|
self, user_id: str, event_body: str, event_limit_type: str
|
|
|
|
) -> None:
|
2019-10-24 06:48:46 -04:00
|
|
|
"""Utility method to apply limit block notifications in the server
|
|
|
|
notices room.
|
|
|
|
|
|
|
|
Args:
|
2020-07-31 16:22:06 -04:00
|
|
|
user_id: user to notify
|
|
|
|
event_body: The human readable text that describes the block.
|
|
|
|
event_limit_type: Specifies the type of block e.g. monthly active user
|
|
|
|
limit has been exceeded.
|
2019-10-24 06:48:46 -04:00
|
|
|
"""
|
|
|
|
content = {
|
|
|
|
"body": event_body,
|
|
|
|
"msgtype": ServerNoticeMsgType,
|
|
|
|
"server_notice_type": ServerNoticeLimitReached,
|
2021-09-29 06:44:15 -04:00
|
|
|
"admin_contact": self._config.server.admin_contact,
|
2019-10-24 06:48:46 -04:00
|
|
|
"limit_type": event_limit_type,
|
|
|
|
}
|
2020-05-01 10:15:36 -04:00
|
|
|
event = await self._server_notices_manager.send_notice(
|
2019-10-24 06:48:46 -04:00
|
|
|
user_id, content, EventTypes.Message
|
|
|
|
)
|
|
|
|
|
|
|
|
content = {"pinned": [event.event_id]}
|
2020-05-01 10:15:36 -04:00
|
|
|
await self._server_notices_manager.send_notice(
|
2019-10-24 06:48:46 -04:00
|
|
|
user_id, content, EventTypes.Pinned, ""
|
|
|
|
)
|
|
|
|
|
2020-07-31 16:22:06 -04:00
|
|
|
async def _is_room_currently_blocked(self, room_id: str) -> Tuple[bool, List[str]]:
|
2018-08-16 12:02:04 -04:00
|
|
|
"""
|
|
|
|
Determines if the room is currently blocked
|
|
|
|
|
|
|
|
Args:
|
2020-07-31 16:22:06 -04:00
|
|
|
room_id: The room id of the server notices room
|
2018-08-16 12:02:04 -04:00
|
|
|
|
|
|
|
Returns:
|
2022-11-16 10:25:24 -05:00
|
|
|
Tuple of:
|
|
|
|
Is the room currently blocked
|
|
|
|
|
|
|
|
The list of pinned event IDs 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
|
2018-08-16 12:02:04 -04:00
|
|
|
"""
|
|
|
|
currently_blocked = False
|
|
|
|
pinned_state_event = None
|
|
|
|
try:
|
2022-06-06 04:24:12 -04:00
|
|
|
pinned_state_event = (
|
|
|
|
await self._storage_controllers.state.get_current_state_event(
|
|
|
|
room_id, event_type=EventTypes.Pinned, state_key=""
|
|
|
|
)
|
2018-08-16 12:02:04 -04:00
|
|
|
)
|
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
|
|
|
|
|
2021-07-15 06:02:43 -04:00
|
|
|
referenced_events: List[str] = []
|
2018-08-16 12:02:04 -04:00
|
|
|
if pinned_state_event is not None:
|
2018-08-23 11:20:51 -04:00
|
|
|
referenced_events = list(pinned_state_event.content.get("pinned", []))
|
2018-08-16 12:02:04 -04:00
|
|
|
|
2020-05-01 15:28:59 -04:00
|
|
|
events = await self._store.get_events(referenced_events)
|
2020-06-15 07:03:36 -04:00
|
|
|
for event_id, event in events.items():
|
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
|