mirror of
https://mau.dev/maunium/synapse.git
synced 2024-10-01 01:36:05 -04:00
Merge pull request #2831 from matrix-org/rav/fix-userdir-search-again
Fix SQL for user search
This commit is contained in:
commit
4c65b98e4a
@ -641,13 +641,12 @@ class UserDirectoryStore(SQLBaseStore):
|
||||
"""
|
||||
|
||||
if self.hs.config.user_directory_search_all_users:
|
||||
# dummy to keep the number of binds & aliases the same
|
||||
# make s.user_id null to keep the ordering algorithm happy
|
||||
join_clause = """
|
||||
LEFT JOIN (
|
||||
SELECT NULL as user_id WHERE NULL = ?
|
||||
) AS s USING (user_id)"
|
||||
CROSS JOIN (SELECT NULL as user_id) AS s
|
||||
"""
|
||||
where_clause = ""
|
||||
join_args = ()
|
||||
where_clause = "1=1"
|
||||
else:
|
||||
join_clause = """
|
||||
LEFT JOIN users_in_public_rooms AS p USING (user_id)
|
||||
@ -656,6 +655,7 @@ class UserDirectoryStore(SQLBaseStore):
|
||||
WHERE user_id = ? AND share_private
|
||||
) AS s USING (user_id)
|
||||
"""
|
||||
join_args = (user_id,)
|
||||
where_clause = "(s.user_id IS NOT NULL OR p.user_id IS NOT NULL)"
|
||||
|
||||
if isinstance(self.database_engine, PostgresEngine):
|
||||
@ -697,7 +697,7 @@ class UserDirectoryStore(SQLBaseStore):
|
||||
avatar_url IS NULL
|
||||
LIMIT ?
|
||||
""" % (join_clause, where_clause)
|
||||
args = (user_id, full_query, exact_query, prefix_query, limit + 1,)
|
||||
args = join_args + (full_query, exact_query, prefix_query, limit + 1,)
|
||||
elif isinstance(self.database_engine, Sqlite3Engine):
|
||||
search_query = _parse_query_sqlite(search_term)
|
||||
|
||||
@ -715,7 +715,7 @@ class UserDirectoryStore(SQLBaseStore):
|
||||
avatar_url IS NULL
|
||||
LIMIT ?
|
||||
""" % (join_clause, where_clause)
|
||||
args = (user_id, search_query, limit + 1)
|
||||
args = join_args + (search_query, limit + 1)
|
||||
else:
|
||||
# This should be unreachable.
|
||||
raise Exception("Unrecognized database engine")
|
||||
|
88
tests/storage/test_user_directory.py
Normal file
88
tests/storage/test_user_directory.py
Normal file
@ -0,0 +1,88 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# 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.
|
||||
|
||||
from twisted.internet import defer
|
||||
|
||||
from synapse.storage import UserDirectoryStore
|
||||
from synapse.storage.roommember import ProfileInfo
|
||||
from tests import unittest
|
||||
from tests.utils import setup_test_homeserver
|
||||
|
||||
ALICE = "@alice:a"
|
||||
BOB = "@bob:b"
|
||||
BOBBY = "@bobby:a"
|
||||
|
||||
|
||||
class UserDirectoryStoreTestCase(unittest.TestCase):
|
||||
@defer.inlineCallbacks
|
||||
def setUp(self):
|
||||
self.hs = yield setup_test_homeserver()
|
||||
self.store = UserDirectoryStore(None, self.hs)
|
||||
|
||||
# alice and bob are both in !room_id. bobby is not but shares
|
||||
# a homeserver with alice.
|
||||
yield self.store.add_profiles_to_user_dir(
|
||||
"!room:id",
|
||||
{
|
||||
ALICE: ProfileInfo(None, "alice"),
|
||||
BOB: ProfileInfo(None, "bob"),
|
||||
BOBBY: ProfileInfo(None, "bobby")
|
||||
},
|
||||
)
|
||||
yield self.store.add_users_to_public_room(
|
||||
"!room:id",
|
||||
[ALICE, BOB],
|
||||
)
|
||||
yield self.store.add_users_who_share_room(
|
||||
"!room:id",
|
||||
False,
|
||||
(
|
||||
(ALICE, BOB),
|
||||
(BOB, ALICE),
|
||||
),
|
||||
)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def test_search_user_dir(self):
|
||||
# normally when alice searches the directory she should just find
|
||||
# bob because bobby doesn't share a room with her.
|
||||
r = yield self.store.search_user_dir(ALICE, "bob", 10)
|
||||
self.assertFalse(r["limited"])
|
||||
self.assertEqual(1, len(r["results"]))
|
||||
self.assertDictEqual(r["results"][0], {
|
||||
"user_id": BOB,
|
||||
"display_name": "bob",
|
||||
"avatar_url": None,
|
||||
})
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def test_search_user_dir_all_users(self):
|
||||
self.hs.config.user_directory_search_all_users = True
|
||||
try:
|
||||
r = yield self.store.search_user_dir(ALICE, "bob", 10)
|
||||
self.assertFalse(r["limited"])
|
||||
self.assertEqual(2, len(r["results"]))
|
||||
self.assertDictEqual(r["results"][0], {
|
||||
"user_id": BOB,
|
||||
"display_name": "bob",
|
||||
"avatar_url": None,
|
||||
})
|
||||
self.assertDictEqual(r["results"][1], {
|
||||
"user_id": BOBBY,
|
||||
"display_name": "bobby",
|
||||
"avatar_url": None,
|
||||
})
|
||||
finally:
|
||||
self.hs.config.user_directory_search_all_users = False
|
@ -59,6 +59,7 @@ def setup_test_homeserver(name="test", datastore=None, config=None, **kargs):
|
||||
config.email_enable_notifs = False
|
||||
config.block_non_admin_invites = False
|
||||
config.federation_domain_whitelist = None
|
||||
config.user_directory_search_all_users = False
|
||||
|
||||
# disable user directory updates, because they get done in the
|
||||
# background, which upsets the test runner.
|
||||
|
Loading…
Reference in New Issue
Block a user