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

Always add local users to the user directory #10796

Merged
merged 5 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions changelog.d/10796.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplify the internal logic which maintains the user directory database tables.
7 changes: 2 additions & 5 deletions synapse/handlers/deactivate_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,8 @@ async def activate_account(self, user_id: str) -> None:
"""
# Add the user to the directory, if necessary.
user = UserID.from_string(user_id)
if self.hs.config.user_directory_search_all_users:
profile = await self.store.get_profileinfo(user.localpart)
await self.user_directory_handler.handle_local_profile_change(
user_id, profile
)
profile = await self.store.get_profileinfo(user.localpart)
await self.user_directory_handler.handle_local_profile_change(user_id, profile)

# Ensure the user is not marked as erased.
await self.store.mark_user_not_erased(user_id)
Expand Down
18 changes: 8 additions & 10 deletions synapse/handlers/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,10 @@ async def set_displayname(
target_user.localpart, displayname_to_set
)

if self.hs.config.user_directory_search_all_users:
profile = await self.store.get_profileinfo(target_user.localpart)
await self.user_directory_handler.handle_local_profile_change(
target_user.to_string(), profile
)
profile = await self.store.get_profileinfo(target_user.localpart)
await self.user_directory_handler.handle_local_profile_change(
target_user.to_string(), profile
)

await self._update_join_states(requester, target_user)

Expand Down Expand Up @@ -300,11 +299,10 @@ async def set_avatar_url(
target_user.localpart, avatar_url_to_set
)

if self.hs.config.user_directory_search_all_users:
profile = await self.store.get_profileinfo(target_user.localpart)
await self.user_directory_handler.handle_local_profile_change(
target_user.to_string(), profile
)
profile = await self.store.get_profileinfo(target_user.localpart)
await self.user_directory_handler.handle_local_profile_change(
target_user.to_string(), profile
)

await self._update_join_states(requester, target_user)

Expand Down
9 changes: 4 additions & 5 deletions synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,10 @@ async def register_user(
shadow_banned=shadow_banned,
)

if self.hs.config.user_directory_search_all_users:
profile = await self.store.get_profileinfo(localpart)
await self.user_directory_handler.handle_local_profile_change(
user_id, profile
)
profile = await self.store.get_profileinfo(localpart)
await self.user_directory_handler.handle_local_profile_change(
user_id, profile
)

else:
# autogen a sequential user ID
Expand Down
27 changes: 10 additions & 17 deletions synapse/storage/databases/main/user_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,17 @@ def _make_staging_area(txn):
self.db_pool.simple_insert_many_txn(txn, TEMP_TABLE + "_rooms", rooms)
del rooms

# If search all users is on, get all the users we want to add.
if self.hs.config.user_directory_search_all_users:
sql = (
"CREATE TABLE IF NOT EXISTS "
+ TEMP_TABLE
+ "_users(user_id TEXT NOT NULL)"
)
txn.execute(sql)
sql = (
"CREATE TABLE IF NOT EXISTS "
+ TEMP_TABLE
+ "_users(user_id TEXT NOT NULL)"
)
txn.execute(sql)

txn.execute("SELECT name FROM users")
users = [{"user_id": x[0]} for x in txn.fetchall()]
txn.execute("SELECT name FROM users")
users = [{"user_id": x[0]} for x in txn.fetchall()]

self.db_pool.simple_insert_many_txn(txn, TEMP_TABLE + "_users", users)
self.db_pool.simple_insert_many_txn(txn, TEMP_TABLE + "_users", users)
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

new_pos = await self.get_max_stream_id_in_current_state_deltas()
await self.db_pool.runInteraction(
Expand Down Expand Up @@ -265,13 +263,8 @@ def _get_next_batch(txn):

async def _populate_user_directory_process_users(self, progress, batch_size):
"""
If search_all_users is enabled, add all of the users to the user directory.
Add all local users to the user directory.
"""
if not self.hs.config.user_directory_search_all_users:
await self.db_pool.updates._end_background_update(
"populate_user_directory_process_users"
)
return 1

def _get_next_batch(txn):
sql = "SELECT user_id FROM %s LIMIT %s" % (
Expand Down
7 changes: 5 additions & 2 deletions tests/handlers/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import synapse.types
from synapse.api.errors import AuthError, SynapseError
from synapse.rest import admin
from synapse.types import UserID

from tests import unittest
Expand All @@ -25,6 +26,8 @@
class ProfileTestCase(unittest.HomeserverTestCase):
"""Tests profile management."""

servlets = [admin.register_servlets]

def make_homeserver(self, reactor, clock):
self.mock_federation = Mock()
self.mock_registry = Mock()
Expand All @@ -46,11 +49,11 @@ def register_query_handler(query_type, handler):
def prepare(self, reactor, clock, hs):
self.store = hs.get_datastore()

self.frank = UserID.from_string("@1234ABCD:test")
self.frank = UserID.from_string("@1234abcd:test")
self.bob = UserID.from_string("@4567:test")
self.alice = UserID.from_string("@alice:remote")

self.get_success(self.store.create_profile(self.frank.localpart))
self.get_success(self.register_user(self.frank.localpart, "frankpassword"))

self.handler = hs.get_profile_handler()

Expand Down
12 changes: 6 additions & 6 deletions tests/rest/client/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,12 @@ class RoomJoinRatelimitTestCase(RoomBase):
room.register_servlets,
]

def prepare(self, reactor, clock, homeserver):
super().prepare(reactor, clock, homeserver)
# profile changes expect that the user is actually registered
user = UserID.from_string(self.user_id)
self.get_success(self.register_user(user.localpart, "supersecretpassword"))

@unittest.override_config(
{"rc_joins": {"local": {"per_second": 0.5, "burst_count": 3}}}
)
Expand Down Expand Up @@ -813,12 +819,6 @@ def test_join_local_ratelimit_profile_change(self):
# join in a second.
room_ids.append(self.helper.create_room_as(self.user_id))

# Create a profile for the user, since it hasn't been done on registration.
store = self.hs.get_datastore()
self.get_success(
store.create_profile(UserID.from_string(self.user_id).localpart)
)

# Update the display name for the user.
path = "/_matrix/client/r0/profile/%s/displayname" % self.user_id
channel = self.make_request("PUT", path, {"displayname": "John Doe"})
Expand Down