summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
authorMatthew Hodgson <matthew@matrix.org>2017-11-29 18:27:05 +0000
committerMatthew Hodgson <matthew@matrix.org>2017-11-29 18:27:05 +0000
commit3241c7aac3dc114a6abce46e5d241f42ed35a7fe (patch)
tree81dc0c83a96b1f0a720c8fc3f73c40bea0c0a03c /synapse/storage
parentAdd user_directory_include_pattern config param to expand search results to a... (diff)
downloadsynapse-3241c7aac3dc114a6abce46e5d241f42ed35a7fe.tar.xz
untested WIP but might actually work
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/_base.py2
-rw-r--r--synapse/storage/profile.py14
-rw-r--r--synapse/storage/user_directory.py35
3 files changed, 38 insertions, 13 deletions
diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py
index e6eefdd6fe..0fdf49a2fd 100644
--- a/synapse/storage/_base.py
+++ b/synapse/storage/_base.py
@@ -547,7 +547,7 @@ class SQLBaseStore(object):
     def _simple_select_one(self, table, keyvalues, retcols,
                            allow_none=False, desc="_simple_select_one"):
         """Executes a SELECT query on the named table, which is expected to
-        return a single row, returning a single column from it.
+        return a single row, returning multiple columns from it.
 
         Args:
             table : string giving the table name
diff --git a/synapse/storage/profile.py b/synapse/storage/profile.py
index beea3102fc..484ad59b62 100644
--- a/synapse/storage/profile.py
+++ b/synapse/storage/profile.py
@@ -15,6 +15,8 @@
 
 from twisted.internet import defer
 
+from synapse.storage.roommember import ProfileInfo
+
 from ._base import SQLBaseStore
 
 
@@ -26,6 +28,18 @@ class ProfileStore(SQLBaseStore):
             desc="create_profile",
         )
 
+    def get_profileinfo(self, user_localpart):
+        profile = self._simple_select_one(
+            table="profiles",
+            keyvalues={"user_id": user_localpart},
+            retcols=("displayname", "avatar_url"),
+            desc="get_profileinfo",
+        )
+        return ProfileInfo(
+            avatar_url=profile.avatar_url,
+            displayname=profile.displayname,
+        )
+
     def get_profile_displayname(self, user_localpart):
         return self._simple_select_one_onecol(
             table="profiles",
diff --git a/synapse/storage/user_directory.py b/synapse/storage/user_directory.py
index 1022cdf7d0..1cc73e3878 100644
--- a/synapse/storage/user_directory.py
+++ b/synapse/storage/user_directory.py
@@ -164,7 +164,7 @@ class UserDirectoryStore(SQLBaseStore):
             )
 
             if isinstance(self.database_engine, PostgresEngine):
-                # We weight the loclpart most highly, then display name and finally
+                # We weight the localpart most highly, then display name and finally
                 # server name
                 if new_entry:
                     sql = """
@@ -317,6 +317,16 @@ class UserDirectoryStore(SQLBaseStore):
         rows = yield self._execute("get_all_rooms", None, sql)
         defer.returnValue([room_id for room_id, in rows])
 
+    @defer.inlineCallbacks
+    def get_all_local_users(self):
+        """Get all local users
+        """
+        sql = """
+            SELECT name FROM users
+        """
+        rows = yield self._execute("get_all_local_users", None, sql)
+        defer.returnValue([name for name, in rows])
+
     def add_users_who_share_room(self, room_id, share_private, user_id_tuples):
         """Insert entries into the users_who_share_rooms table. The first
         user should be a local user.
@@ -630,7 +640,12 @@ class UserDirectoryStore(SQLBaseStore):
                 }
         """
 
-        include_pattern = self.hs.config.user_directory_include_pattern or "%";
+        include_pattern_clause = ""
+        if self.hs.config.user_directory_include_pattern:
+            include_pattern_clause = "OR d.user_id LIKE '%s'" % (
+                self.hs.config.user_directory_include_pattern
+            )
+
         logger.error("include pattern is %s" % (include_pattern))
 
         if isinstance(self.database_engine, PostgresEngine):
@@ -651,9 +666,7 @@ class UserDirectoryStore(SQLBaseStore):
                     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 OR
-                     d.user_id LIKE ?)
+                    (s.user_id IS NOT NULL OR p.user_id IS NOT NULL %s)
                     AND vector @@ to_tsquery('english', ?)
                 ORDER BY
                     (CASE WHEN s.user_id IS NOT NULL THEN 4.0 ELSE 1.0 END)
@@ -677,8 +690,8 @@ class UserDirectoryStore(SQLBaseStore):
                     display_name IS NULL,
                     avatar_url IS NULL
                 LIMIT ?
-            """
-            args = (user_id, include_pattern, full_query, exact_query, prefix_query, limit + 1,)
+            """ % ( include_pattern_clause )
+            args = (user_id, full_query, exact_query, prefix_query, limit + 1,)
         elif isinstance(self.database_engine, Sqlite3Engine):
             search_query = _parse_query_sqlite(search_term)
 
@@ -692,17 +705,15 @@ class UserDirectoryStore(SQLBaseStore):
                     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 OR
-                     d.user_id LIKE ?)
+                    (s.user_id IS NOT NULL OR p.user_id IS NOT NULL %s)
                     AND value MATCH ?
                 ORDER BY
                     rank(matchinfo(user_directory_search)) DESC,
                     display_name IS NULL,
                     avatar_url IS NULL
                 LIMIT ?
-            """
-            args = (user_id, include_pattern, search_query, limit + 1)
+            """ % ( include_pattern_clause )
+            args = (user_id, search_query, limit + 1)
         else:
             # This should be unreachable.
             raise Exception("Unrecognized database engine")