Compare commits

...

3 Commits

Author SHA1 Message Date
Luke Barnard
ea14a142cc flake8 2017-11-06 16:50:33 +00:00
Luke Barnard
b8da5d0790 Use static column name insertion
Otherwise we end up trying to ORDER BY "is_admin", the literal string.
2017-11-06 16:16:24 +00:00
Luke Barnard
4e241fad78 WIP: Return admins of a group first in the list of group users
This introduces a ORDER BY that can be optionally added to _simple_select_list transactions'
2017-11-06 15:45:20 +00:00
3 changed files with 23 additions and 8 deletions

View File

@@ -426,14 +426,15 @@ class GroupsServerHandler(object):
for user_result in user_results:
g_user_id = user_result["user_id"]
is_public = user_result["is_public"]
is_admin = user_result["is_admin"]
entry = {"user_id": g_user_id}
profile = yield self.profile_handler.get_profile_from_cache(g_user_id)
entry.update(profile)
if not is_public:
entry["is_public"] = False
entry["is_public"] = bool(is_public)
entry["is_admin"] = bool(is_admin)
if not self.is_mine_id(g_user_id):
attestation = yield self.store.get_remote_attestation(group_id, g_user_id)

View File

@@ -619,7 +619,7 @@ class SQLBaseStore(object):
)
def _simple_select_list(self, table, keyvalues, retcols,
desc="_simple_select_list"):
desc="_simple_select_list", order=None, order_asc=True):
"""Executes a SELECT query on the named table, which may return zero or
more rows, returning the result as a list of dicts.
@@ -629,17 +629,20 @@ class SQLBaseStore(object):
column names and values to select the rows with, or None to not
apply a WHERE clause.
retcols (iterable[str]): the names of the columns to return
order (str): order the select by this column
order_asc (bool): order ascending (if order specified). Default True.
Returns:
defer.Deferred: resolves to list[dict[str, Any]]
"""
return self.runInteraction(
desc,
self._simple_select_list_txn,
table, keyvalues, retcols
table, keyvalues, retcols, order, order_asc
)
@classmethod
def _simple_select_list_txn(cls, txn, table, keyvalues, retcols):
def _simple_select_list_txn(cls, txn, table, keyvalues, retcols,
order=None, order_asc=True):
"""Executes a SELECT query on the named table, which may return zero or
more rows, returning the result as a list of dicts.
@@ -650,20 +653,29 @@ class SQLBaseStore(object):
column names and values to select the rows with, or None to not
apply a WHERE clause.
retcols (iterable[str]): the names of the columns to return
order (str): order the select by this column
order_asc (bool): order ascending (if order specified). Default True.
"""
query_values = []
if keyvalues:
sql = "SELECT %s FROM %s WHERE %s" % (
", ".join(retcols),
table,
" AND ".join("%s = ?" % (k, ) for k in keyvalues)
)
txn.execute(sql, keyvalues.values())
query_values.extend(keyvalues.values())
else:
sql = "SELECT %s FROM %s" % (
", ".join(retcols),
table
)
txn.execute(sql)
if order:
sql = sql + " ORDER BY %s %s" % (
order, "ASC" if order_asc else "DESC"
)
txn.execute(sql, query_values)
return cls.cursor_to_dict(txn)

View File

@@ -54,8 +54,10 @@ class GroupServerStore(SQLBaseStore):
return self._simple_select_list(
table="group_users",
keyvalues=keyvalues,
retcols=("user_id", "is_public",),
retcols=("user_id", "is_public", "is_admin",),
desc="get_users_in_group",
order="is_admin",
order_asc=False, # Order descending: admins first
)
def get_invited_users_in_group(self, group_id):