Merge pull request #5420 from matrix-org/babolivier/userdir_hide_users

Add configuration option to hide new users from the user directory
This commit is contained in:
Brendan Abolivier 2019-06-14 10:59:45 +01:00 committed by GitHub
commit ad566df746
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 0 deletions

1
changelog.d/5420.feature Normal file
View File

@ -0,0 +1 @@
Add configuration option to hide new users from the user directory.

View File

@ -308,6 +308,11 @@ listeners:
# #
#allow_per_room_profiles: false #allow_per_room_profiles: false
# Whether to show the users on this homeserver in the user directory. Defaults to
# 'true'.
#
#show_users_in_user_directory: false
## TLS ## ## TLS ##

View File

@ -202,6 +202,12 @@ class ServerConfig(Config):
# events with profile information that differ from the target's global profile. # events with profile information that differ from the target's global profile.
self.allow_per_room_profiles = config.get("allow_per_room_profiles", True) self.allow_per_room_profiles = config.get("allow_per_room_profiles", True)
# Whether to show the users on this homeserver in the user directory. Defaults to
# True.
self.show_users_in_user_directory = config.get(
"show_users_in_user_directory", True,
)
self.listeners = [] self.listeners = []
for listener in config.get("listeners", []): for listener in config.get("listeners", []):
if not isinstance(listener.get("port", None), int): if not isinstance(listener.get("port", None), int):
@ -631,6 +637,11 @@ class ServerConfig(Config):
# Defaults to 'true'. # Defaults to 'true'.
# #
#allow_per_room_profiles: false #allow_per_room_profiles: false
# Whether to show the users on this homeserver in the user directory. Defaults to
# 'true'.
#
#show_users_in_user_directory: false
""" % locals() """ % locals()
def read_arguments(self, args): def read_arguments(self, args):

View File

@ -74,6 +74,8 @@ class RegistrationHandler(BaseHandler):
) )
self._server_notices_mxid = hs.config.server_notices_mxid self._server_notices_mxid = hs.config.server_notices_mxid
self._show_in_user_directory = self.hs.config.show_users_in_user_directory
if hs.config.worker_app: if hs.config.worker_app:
self._register_client = ReplicationRegisterServlet.make_client(hs) self._register_client = ReplicationRegisterServlet.make_client(hs)
self._register_device_client = ( self._register_device_client = (
@ -298,6 +300,14 @@ class RegistrationHandler(BaseHandler):
user_id, threepid_dict, None, False, user_id, threepid_dict, None, False,
) )
# Prevent the new user from showing up in the user directory if the server
# mandates it.
if not self._show_in_user_directory:
yield self.store.add_account_data_for_user(
user_id, "im.vector.hide_profile", {'hide_profile': True},
)
yield self.profile_handler.set_active(user, False, True)
defer.returnValue((user_id, token)) defer.returnValue((user_id, token))
@defer.inlineCallbacks @defer.inlineCallbacks

View File

@ -19,8 +19,12 @@ import datetime
import json import json
import os import os
from mock import Mock
import pkg_resources import pkg_resources
from twisted.internet import defer
import synapse.rest.admin import synapse.rest.admin
from synapse.api.constants import LoginType from synapse.api.constants import LoginType
from synapse.api.errors import Codes from synapse.api.errors import Codes
@ -200,6 +204,53 @@ class RegisterRestServletTestCase(unittest.HomeserverTestCase):
self.assertEquals(channel.result["code"], b"200", channel.result) self.assertEquals(channel.result["code"], b"200", channel.result)
class RegisterHideProfileTestCase(unittest.HomeserverTestCase):
servlets = [
synapse.rest.admin.register_servlets_for_client_rest_resource,
]
def make_homeserver(self, reactor, clock):
self.url = b"/_matrix/client/r0/register"
config = self.default_config()
config["enable_registration"] = True
config["show_users_in_user_directory"] = False
config["replicate_user_profiles_to"] = ["fakeserver"]
mock_http_client = Mock(spec=[
"get_json",
"post_json_get_json",
])
mock_http_client.post_json_get_json.return_value = defer.succeed((200, "{}"))
self.hs = self.setup_test_homeserver(
config=config,
simple_http_client=mock_http_client,
)
return self.hs
def test_profile_hidden(self):
user_id = self.register_user("kermit", "monkey")
post_json = self.hs.get_simple_http_client().post_json_get_json
# We expect post_json_get_json to have been called twice: once with the original
# profile and once with the None profile resulting from the request to hide it
# from the user directory.
self.assertEqual(post_json.call_count, 2, post_json.call_args_list)
# Get the args (and not kwargs) passed to post_json.
args = post_json.call_args[0]
# Make sure the last call was attempting to replicate profiles.
split_uri = args[0].split("/")
self.assertEqual(split_uri[len(split_uri) - 1], "replicate_profiles", args[0])
# Make sure the last profile update was overriding the user's profile to None.
self.assertEqual(args[1]["batch"][user_id], None, args[1])
class AccountValidityTestCase(unittest.HomeserverTestCase): class AccountValidityTestCase(unittest.HomeserverTestCase):
servlets = [ servlets = [