Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Support for putting %(consent_uri)s in messages #3271

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions synapse/config/consent_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@
# version: 1.0
# server_notice_content:
# msgtype: m.text
# body: |
# Pls do consent kthx
# block_events_error: |
# You can't send any messages until you consent to the privacy policy.
# body: >-
# To continue using this homeserver you must review and agree to the
# terms and conditions at %(consent_uri)s
# block_events_error: >-
# To continue using this homeserver you must review and agree to the
# terms and conditions at %(consent_uri)s
#
"""


Expand Down
5 changes: 4 additions & 1 deletion synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,11 @@ def assert_accepted_privacy_policy(self, requester):
return

consent_uri = self._consent_uri_builder.build_user_consent_uri(user_id)
msg = self.config.block_events_without_consent_error % {
'consent_uri': consent_uri,
}
raise ConsentNotGivenError(
msg=self.config.block_events_without_consent_error,
msg=msg,
consent_uri=consent_uri,
)

Expand Down
41 changes: 39 additions & 2 deletions synapse/server_notices/consent_server_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
# limitations under the License.
import logging

from six import (iteritems, string_types)
from twisted.internet import defer

from synapse.api.errors import SynapseError
from synapse.api.urls import ConsentURIBuilder
from synapse.config import ConfigError
from synapse.types import get_localpart_from_id

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -52,6 +55,8 @@ def __init__(self, hs):
"key.",
)

self._consent_uri_builder = ConsentURIBuilder(hs.config)

@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
Expand Down Expand Up @@ -81,10 +86,18 @@ def maybe_send_server_notice_to_user(self, user_id):
# we've already sent a notice to the user
return

# need to send a message
# need to send a message.
try:
consent_uri = self._consent_uri_builder.build_user_consent_uri(
get_localpart_from_id(user_id),
)
content = copy_with_str_subst(
self._server_notice_content, {
'consent_uri': consent_uri,
},
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't like that the idea of magically going and rewriting everything like this, but am prepared to accept it for now since presumably we need the flexibility.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah seems like flexibility ftw right now

yield self._server_notices_manager.send_notice(
user_id, self._server_notice_content,
user_id, content,
)
yield self._store.user_set_consent_server_notice_sent(
user_id, self._current_consent_version,
Expand All @@ -93,3 +106,27 @@ def maybe_send_server_notice_to_user(self, user_id):
logger.error("Error sending server notice about user consent: %s", e)
finally:
self._users_in_progress.remove(user_id)


def copy_with_str_subst(x, substitutions):
"""Deep-copy a structure, carrying out string substitions on any strings

Args:
x (object): structure to be copied
substitutions (object): substitutions to be made - passed into the
string '%' operator

Returns:
copy of x
"""
if isinstance(x, string_types):
return x % substitutions
if isinstance(x, dict):
return {
k: copy_with_str_subst(v, substitutions) for (k, v) in iteritems(x)
}
if isinstance(x, (list, tuple)):
return [copy_with_str_subst(y) for y in x]

# assume it's uninterested and can be shallow-copied.
return x