Include users who share room with requester in user directory

This commit is contained in:
Erik Johnston 2017-06-15 10:00:28 +01:00
parent 4564b05483
commit a9d6fa8b2b
3 changed files with 47 additions and 26 deletions

View file

@ -71,7 +71,7 @@ class UserDirectoyHandler(object):
# we start populating the user directory # we start populating the user directory
self.clock.call_later(0, self.notify_new_event) self.clock.call_later(0, self.notify_new_event)
def search_users(self, search_term, limit): def search_users(self, user_id, search_term, limit):
"""Searches for users in directory """Searches for users in directory
Returns: Returns:
@ -88,7 +88,7 @@ class UserDirectoyHandler(object):
] ]
} }
""" """
return self.store.search_user_dir(search_term, limit) return self.store.search_user_dir(user_id, search_term, limit)
@defer.inlineCallbacks @defer.inlineCallbacks
def notify_new_event(self): def notify_new_event(self):

View file

@ -55,7 +55,9 @@ class UserDirectorySearchRestServlet(RestServlet):
] ]
} }
""" """
yield self.auth.get_user_by_req(request, allow_guest=False) requester = yield self.auth.get_user_by_req(request, allow_guest=False)
user_id = requester.user.to_string()
body = parse_json_object_from_request(request) body = parse_json_object_from_request(request)
limit = body.get("limit", 10) limit = body.get("limit", 10)
@ -66,7 +68,9 @@ class UserDirectorySearchRestServlet(RestServlet):
except: except:
raise SynapseError(400, "`search_term` is required field") raise SynapseError(400, "`search_term` is required field")
results = yield self.user_directory_handler.search_users(search_term, limit) results = yield self.user_directory_handler.search_users(
user_id, search_term, limit,
)
defer.returnValue((200, results)) defer.returnValue((200, results))

View file

@ -611,7 +611,7 @@ class UserDirectoryStore(SQLBaseStore):
) )
@defer.inlineCallbacks @defer.inlineCallbacks
def search_user_dir(self, search_term, limit): def search_user_dir(self, user_id, search_term, limit):
"""Searches for users in directory """Searches for users in directory
Returns: Returns:
@ -637,46 +637,63 @@ class UserDirectoryStore(SQLBaseStore):
# The array of numbers are the weights for the various part of the # The array of numbers are the weights for the various part of the
# search: (domain, _, display name, localpart) # search: (domain, _, display name, localpart)
sql = """ sql = """
SELECT user_id, display_name, avatar_url SELECT d.user_id, display_name, avatar_url
FROM user_directory_search FROM user_directory_search
INNER JOIN user_directory USING (user_id) INNER JOIN user_directory AS d USING (user_id)
INNER JOIN users_in_pubic_room USING (user_id) LEFT JOIN users_in_pubic_room AS p USING (user_id)
WHERE vector @@ to_tsquery('english', ?) LEFT JOIN (
SELECT other_user_id AS user_id FROM users_who_share_rooms
WHERE user_id = ? AND share_private
) AS s USING (user_id)
WHERE
(s.user_id IS NOT NULL OR p.user_id IS NOT NULL)
AND vector @@ to_tsquery('english', ?)
ORDER BY ORDER BY
2 * ts_rank_cd( (CASE WHEN s.user_id IS NOT NULL THEN 4.0 ELSE 1.0 END)
'{0.1, 0.1, 0.9, 1.0}', * (CASE WHEN display_name IS NOT NULL THEN 1.2 ELSE 1.0 END)
vector, * (CASE WHEN avatar_url IS NOT NULL THEN 1.2 ELSE 1.0 END)
to_tsquery('english', ?), * (
8 3 * ts_rank_cd(
) '{0.1, 0.1, 0.9, 1.0}',
+ ts_rank_cd( vector,
'{0.1, 0.1, 0.9, 1.0}', to_tsquery('english', ?),
vector, 8
to_tsquery('english', ?), )
8 + ts_rank_cd(
'{0.1, 0.1, 0.9, 1.0}',
vector,
to_tsquery('english', ?),
8
)
) )
DESC, DESC,
display_name IS NULL, display_name IS NULL,
avatar_url IS NULL avatar_url IS NULL
LIMIT ? LIMIT ?
""" """
args = (full_query, exact_query, prefix_query, limit + 1,) args = (user_id, full_query, exact_query, prefix_query, limit + 1,)
elif isinstance(self.database_engine, Sqlite3Engine): elif isinstance(self.database_engine, Sqlite3Engine):
search_query = _parse_query_sqlite(search_term) search_query = _parse_query_sqlite(search_term)
sql = """ sql = """
SELECT user_id, display_name, avatar_url SELECT d.user_id, display_name, avatar_url
FROM user_directory_search FROM user_directory_search
INNER JOIN user_directory USING (user_id) INNER JOIN user_directory AS d USING (user_id)
INNER JOIN users_in_pubic_room USING (user_id) LEFT JOIN users_in_pubic_room AS p USING (user_id)
WHERE value MATCH ? LEFT JOIN (
SELECT other_user_id AS user_id FROM users_who_share_rooms
WHERE user_id = ? AND share_private
) AS s USING (user_id)
WHERE
(s.user_id IS NOT NULL OR p.user_id IS NOT NULL)
AND value MATCH ?
ORDER BY ORDER BY
rank(matchinfo(user_directory_search)) DESC, rank(matchinfo(user_directory_search)) DESC,
display_name IS NULL, display_name IS NULL,
avatar_url IS NULL avatar_url IS NULL
LIMIT ? LIMIT ?
""" """
args = (search_query, limit + 1) args = (user_id, search_query, limit + 1)
else: else:
# This should be unreachable. # This should be unreachable.
raise Exception("Unrecognized database engine") raise Exception("Unrecognized database engine")