2018-05-17 18:35:31 +02:00
|
|
|
# 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
|
2021-03-24 11:48:46 +01:00
|
|
|
from typing import TYPE_CHECKING, Any, Set
|
2018-05-17 18:35:31 +02:00
|
|
|
|
|
|
|
from synapse.api.errors import SynapseError
|
2018-05-23 16:24:31 +02:00
|
|
|
from synapse.api.urls import ConsentURIBuilder
|
2018-05-17 18:35:31 +02:00
|
|
|
from synapse.config import ConfigError
|
2018-05-23 16:24:31 +02:00
|
|
|
from synapse.types import get_localpart_from_id
|
2018-05-17 18:35:31 +02:00
|
|
|
|
2021-03-24 11:48:46 +01:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2018-05-17 18:35:31 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-09-04 12:54:56 +02:00
|
|
|
class ConsentServerNotices:
|
2018-05-17 18:35:31 +02:00
|
|
|
"""Keeps track of whether we need to send users server_notices about
|
|
|
|
privacy policy consent, and sends one if we do.
|
|
|
|
"""
|
2019-06-20 11:32:02 +02:00
|
|
|
|
2021-03-24 11:48:46 +01:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2018-05-17 18:35:31 +02:00
|
|
|
self._server_notices_manager = hs.get_server_notices_manager()
|
2022-02-23 12:04:02 +01:00
|
|
|
self._store = hs.get_datastores().main
|
2018-05-17 18:35:31 +02:00
|
|
|
|
2021-07-15 12:02:43 +02:00
|
|
|
self._users_in_progress: Set[str] = set()
|
2018-05-17 18:35:31 +02:00
|
|
|
|
2021-09-23 13:13:34 +02:00
|
|
|
self._current_consent_version = hs.config.consent.user_consent_version
|
|
|
|
self._server_notice_content = (
|
|
|
|
hs.config.consent.user_consent_server_notice_content
|
|
|
|
)
|
|
|
|
self._send_to_guests = hs.config.consent.user_consent_server_notice_to_guests
|
2018-05-17 18:35:31 +02:00
|
|
|
|
|
|
|
if self._server_notice_content is not None:
|
|
|
|
if not self._server_notices_manager.is_enabled():
|
|
|
|
raise ConfigError(
|
|
|
|
"user_consent configuration requires server notices, but "
|
2019-06-20 11:32:02 +02:00
|
|
|
"server notices are not enabled."
|
2018-05-17 18:35:31 +02:00
|
|
|
)
|
2019-06-20 11:32:02 +02:00
|
|
|
if "body" not in self._server_notice_content:
|
2018-05-17 18:35:31 +02:00
|
|
|
raise ConfigError(
|
2019-11-21 13:00:14 +01:00
|
|
|
"user_consent server_notice_consent must contain a 'body' key."
|
2018-05-17 18:35:31 +02:00
|
|
|
)
|
|
|
|
|
2018-05-23 16:24:31 +02:00
|
|
|
self._consent_uri_builder = ConsentURIBuilder(hs.config)
|
|
|
|
|
2020-07-31 22:22:06 +02:00
|
|
|
async def maybe_send_server_notice_to_user(self, user_id: str) -> None:
|
2018-05-17 18:35:31 +02:00
|
|
|
"""Check if we need to send a notice to this user, and does so if so
|
|
|
|
|
|
|
|
Args:
|
2020-07-31 22:22:06 +02:00
|
|
|
user_id: user to check
|
2018-05-17 18:35:31 +02:00
|
|
|
"""
|
|
|
|
if self._server_notice_content is None:
|
|
|
|
# not enabled
|
|
|
|
return
|
|
|
|
|
2021-09-23 13:13:34 +02:00
|
|
|
# A consent version must be given.
|
|
|
|
assert self._current_consent_version is not None
|
|
|
|
|
2018-05-17 18:35:31 +02:00
|
|
|
# make sure we don't send two messages to the same user at once
|
|
|
|
if user_id in self._users_in_progress:
|
|
|
|
return
|
|
|
|
self._users_in_progress.add(user_id)
|
|
|
|
try:
|
2020-05-01 16:15:36 +02:00
|
|
|
u = await self._store.get_user_by_id(user_id)
|
2018-05-17 18:35:31 +02:00
|
|
|
|
2021-03-24 11:48:46 +01:00
|
|
|
# The user doesn't exist.
|
|
|
|
if u is None:
|
|
|
|
return
|
|
|
|
|
2018-05-25 12:36:43 +02:00
|
|
|
if u["is_guest"] and not self._send_to_guests:
|
|
|
|
# don't send to guests
|
|
|
|
return
|
|
|
|
|
2018-05-17 18:35:31 +02:00
|
|
|
if u["consent_version"] == self._current_consent_version:
|
|
|
|
# user has already consented
|
|
|
|
return
|
|
|
|
|
|
|
|
if u["consent_server_notice_sent"] == self._current_consent_version:
|
|
|
|
# we've already sent a notice to the user
|
|
|
|
return
|
|
|
|
|
2018-05-23 16:24:31 +02:00
|
|
|
# need to send a message.
|
2018-05-17 18:35:31 +02:00
|
|
|
try:
|
2018-05-23 16:24:31 +02:00
|
|
|
consent_uri = self._consent_uri_builder.build_user_consent_uri(
|
2019-06-20 11:32:02 +02:00
|
|
|
get_localpart_from_id(user_id)
|
2018-05-23 16:24:31 +02:00
|
|
|
)
|
|
|
|
content = copy_with_str_subst(
|
2019-06-20 11:32:02 +02:00
|
|
|
self._server_notice_content, {"consent_uri": consent_uri}
|
2018-05-17 18:35:31 +02:00
|
|
|
)
|
2020-05-01 16:15:36 +02:00
|
|
|
await self._server_notices_manager.send_notice(user_id, content)
|
|
|
|
await self._store.user_set_consent_server_notice_sent(
|
2019-06-20 11:32:02 +02:00
|
|
|
user_id, self._current_consent_version
|
2018-05-17 18:35:31 +02:00
|
|
|
)
|
|
|
|
except SynapseError as e:
|
|
|
|
logger.error("Error sending server notice about user consent: %s", e)
|
|
|
|
finally:
|
|
|
|
self._users_in_progress.remove(user_id)
|
2018-05-23 16:24:31 +02:00
|
|
|
|
|
|
|
|
2020-07-31 22:22:06 +02:00
|
|
|
def copy_with_str_subst(x: Any, substitutions: Any) -> Any:
|
2020-10-23 18:38:40 +02:00
|
|
|
"""Deep-copy a structure, carrying out string substitutions on any strings
|
2018-05-23 16:24:31 +02:00
|
|
|
|
|
|
|
Args:
|
2022-11-16 16:25:24 +01:00
|
|
|
x: structure to be copied
|
|
|
|
substitutions: substitutions to be made - passed into the string '%' operator
|
2018-05-23 16:24:31 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
copy of x
|
|
|
|
"""
|
2020-06-16 14:51:47 +02:00
|
|
|
if isinstance(x, str):
|
2018-05-23 16:24:31 +02:00
|
|
|
return x % substitutions
|
|
|
|
if isinstance(x, dict):
|
2020-06-15 13:03:36 +02:00
|
|
|
return {k: copy_with_str_subst(v, substitutions) for (k, v) in x.items()}
|
2018-05-23 16:24:31 +02:00
|
|
|
if isinstance(x, (list, tuple)):
|
2020-07-31 22:22:06 +02:00
|
|
|
return [copy_with_str_subst(y, substitutions) for y in x]
|
2018-05-23 16:24:31 +02:00
|
|
|
|
|
|
|
# assume it's uninterested and can be shallow-copied.
|
|
|
|
return x
|