switch to a simpler 'search_all_users' button as per review feedback

This commit is contained in:
Matthew Hodgson 2017-12-04 14:58:39 +00:00
parent f397153dfc
commit 1bd40ca73e
5 changed files with 30 additions and 37 deletions

View File

@ -22,19 +22,20 @@ class UserDirectoryConfig(Config):
""" """
def read_config(self, config): def read_config(self, config):
self.user_directory_include_pattern = None self.user_directory_search_all_users = False
user_directory_config = config.get("user_directory", None) user_directory_config = config.get("user_directory", None)
if user_directory_config: if user_directory_config:
self.user_directory_include_pattern = ( self.user_directory_search_all_users = (
user_directory_config.get("include_pattern", None) user_directory_config.get("search_all_users", False)
) )
def default_config(self, config_dir_path, server_name, **kwargs): def default_config(self, config_dir_path, server_name, **kwargs):
return """ return """
# User Directory configuration # User Directory configuration
# 'include_pattern' defines an optional SQL LIKE pattern when querying the # 'search_all_users' defines whether to search all users visible to your HS
# user directory in addition to publicly visible users. Defaults to None. # when searching the user directory, rather than limiting to users visible
# in public rooms. Defaults to false.
# #
#user_directory: #user_directory:
# include_pattern: "%%:%s" # search_all_users: false
""" % (server_name) """

View File

@ -141,7 +141,7 @@ class ProfileHandler(BaseHandler):
target_user.localpart, new_displayname target_user.localpart, new_displayname
) )
if self.hs.config.user_directory_include_pattern: if self.hs.config.user_directory_search_all_users:
profile = yield self.store.get_profileinfo(target_user.localpart) profile = yield self.store.get_profileinfo(target_user.localpart)
yield self.user_directory_handler.handle_local_profile_change( yield self.user_directory_handler.handle_local_profile_change(
target_user.to_string(), profile target_user.to_string(), profile
@ -191,7 +191,7 @@ class ProfileHandler(BaseHandler):
target_user.localpart, new_avatar_url target_user.localpart, new_avatar_url
) )
if self.hs.config.user_directory_include_pattern: if self.hs.config.user_directory_search_all_users:
profile = yield self.store.get_profileinfo(target_user.localpart) profile = yield self.store.get_profileinfo(target_user.localpart)
yield self.user_directory_handler.handle_local_profile_change( yield self.user_directory_handler.handle_local_profile_change(
target_user.user_id, profile target_user.user_id, profile

View File

@ -167,7 +167,7 @@ class RegistrationHandler(BaseHandler):
admin=admin, admin=admin,
) )
if self.hs.config.user_directory_include_pattern: if self.hs.config.user_directory_search_all_users:
profile = yield self.store.get_profileinfo(localpart) profile = yield self.store.get_profileinfo(localpart)
yield self.user_directory_handler.handle_local_profile_change( yield self.user_directory_handler.handle_local_profile_change(
user_id, profile user_id, profile

View File

@ -54,7 +54,7 @@ class UserDirectoryHandler(object):
self.notifier = hs.get_notifier() self.notifier = hs.get_notifier()
self.is_mine_id = hs.is_mine_id self.is_mine_id = hs.is_mine_id
self.update_user_directory = hs.config.update_user_directory self.update_user_directory = hs.config.update_user_directory
self.include_pattern = hs.config.user_directory_include_pattern self.search_all_users = hs.config.user_directory_search_all_users
# When start up for the first time we need to populate the user_directory. # When start up for the first time we need to populate the user_directory.
# This is a set of user_id's we've inserted already # This is a set of user_id's we've inserted already
@ -169,7 +169,7 @@ class UserDirectoryHandler(object):
logger.info("Processed all rooms.") logger.info("Processed all rooms.")
if self.include_pattern: if self.search_all_users:
num_processed_users = 0 num_processed_users = 0
user_ids = yield self.store.get_all_local_users() user_ids = yield self.store.get_all_local_users()
logger.info("Doing initial update of user directory. %d users", len(user_ids)) logger.info("Doing initial update of user directory. %d users", len(user_ids))
@ -413,7 +413,7 @@ class UserDirectoryHandler(object):
def _handle_local_user(self, user_id): def _handle_local_user(self, user_id):
"""Adds a new local roomless user into the user_directory_search table. """Adds a new local roomless user into the user_directory_search table.
Used to populate up the user index when we have an Used to populate up the user index when we have an
user_directory_include_pattern specified. user_directory_search_all_users specified.
""" """
logger.debug("Adding new local user to dir, %r", user_id) logger.debug("Adding new local user to dir, %r", user_id)

View File

@ -640,17 +640,19 @@ class UserDirectoryStore(SQLBaseStore):
} }
""" """
include_pattern_join = ""
include_pattern_where_clause = ""
if self.hs.config.user_directory_include_pattern:
include_pattern_join = """
LEFT JOIN (
SELECT user_id FROM user_directory
WHERE user_id LIKE '%s'
) AS ld USING (user_id)
""" % ( self.hs.config.user_directory_include_pattern )
include_pattern_where_clause = "OR ld.user_id IS NOT NULL" if self.hs.config.user_directory_search_all_users:
join_clause = ""
where_clause = "?<>''" # naughty hack to keep the same number of binds
else:
join_clause = """
LEFT JOIN users_in_public_rooms AS p USING (user_id)
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_clause = "(s.user_id IS NOT NULL OR p.user_id IS NOT NULL)"
if isinstance(self.database_engine, PostgresEngine): if isinstance(self.database_engine, PostgresEngine):
full_query, exact_query, prefix_query = _parse_query_postgres(search_term) full_query, exact_query, prefix_query = _parse_query_postgres(search_term)
@ -664,14 +666,9 @@ class UserDirectoryStore(SQLBaseStore):
SELECT d.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 AS d USING (user_id) INNER JOIN user_directory AS d USING (user_id)
LEFT JOIN users_in_public_rooms AS p USING (user_id)
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)
%s %s
WHERE WHERE
(s.user_id IS NOT NULL OR p.user_id IS NOT NULL %s) %s
AND vector @@ to_tsquery('english', ?) AND vector @@ to_tsquery('english', ?)
ORDER BY ORDER BY
(CASE WHEN s.user_id IS NOT NULL THEN 4.0 ELSE 1.0 END) (CASE WHEN s.user_id IS NOT NULL THEN 4.0 ELSE 1.0 END)
@ -695,7 +692,7 @@ class UserDirectoryStore(SQLBaseStore):
display_name IS NULL, display_name IS NULL,
avatar_url IS NULL avatar_url IS NULL
LIMIT ? LIMIT ?
""" % ( include_pattern_join, include_pattern_where_clause ) """ % ( join_clause, where_clause )
args = (user_id, 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)
@ -704,21 +701,16 @@ class UserDirectoryStore(SQLBaseStore):
SELECT d.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 AS d USING (user_id) INNER JOIN user_directory AS d USING (user_id)
LEFT JOIN users_in_public_rooms AS p USING (user_id)
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)
%s %s
WHERE WHERE
(s.user_id IS NOT NULL OR p.user_id IS NOT NULL %s) %s
AND value MATCH ? 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 ?
""" % ( include_pattern_join, include_pattern_where_clause ) """ % ( join_clause, where_clause )
args = (user_id, search_query, limit + 1) args = (user_id, search_query, limit + 1)
else: else:
# This should be unreachable. # This should be unreachable.