Skip to content
Snippets Groups Projects
Commit 987486f8 authored by roadscape's avatar roadscape
Browse files

add list_comms sort

parent 89ac82de
No related branches found
No related tags found
No related merge requests found
...@@ -139,40 +139,43 @@ async def list_subscribers(context, community): ...@@ -139,40 +139,43 @@ async def list_subscribers(context, community):
str(r['created_at'])) for r in rows] str(r['created_at'])) for r in rows]
@return_error_info @return_error_info
async def list_communities(context, last='', limit=100, query=None, observer=None): async def list_communities(context, last='', limit=100, query=None, sort='rank', observer=None):
"""List all communities, paginated. Returns lite community list.""" """List all communities, paginated. Returns lite community list."""
# pylint: disable=too-many-arguments, too-many-locals
limit = valid_limit(limit, 100) limit = valid_limit(limit, 100)
db = context['db'] db = context['db']
assert not query, 'query not yet supported' assert not query, 'query not yet supported'
assert sort in ('rank', 'new', 'subs'), 'invalid sort'
seek = '' where = []
if last: field, order = dict(
seek = """AND rank > (SELECT rank rank=('rank', 'ASC'),
FROM hive_communities new=('created_at', 'DESC'),
WHERE name = :last)""" subs=('subscribers', 'DESC'))[sort]
if field == 'rank':
where.append('rank > 0')
sql = """SELECT id FROM hive_communities if last:
WHERE rank > 0 AND (num_pending > 0 OR LENGTH(about) > 3) %s field_cmp = '>' if order == 'ASC' else '<'
ORDER BY rank LIMIT :limit""" % seek where.append("""%s %s (SELECT %s FROM hive_communities
WHERE name = :last)"""
% (field, field_cmp, field))
filt = 'WHERE ' + ' AND '.join(where) if where else ''
sql = """SELECT id FROM hive_communities %s
ORDER BY %s %s LIMIT :limit""" % (filt, field, order)
ids = await db.query_col(sql, last=last, limit=limit) ids = await db.query_col(sql, last=last, limit=limit)
if not ids: return [] if not ids: return []
# append observer context, leadership data
communities = await load_communities(db, ids, lite=True) communities = await load_communities(db, ids, lite=True)
if observer: if observer:
observer_id = await get_account_id(db, observer) observer_id = await get_account_id(db, observer)
await _append_observer_subs(db, communities, observer_id) await _append_observer_subs(db, communities, observer_id)
await _append_observer_roles(db, communities, observer_id) await _append_observer_roles(db, communities, observer_id)
await _append_admins(db, communities)
sql = """SELECT community_id, ha.name FROM hive_roles hr
JOIN hive_accounts ha ON hr.account_id = ha.id
WHERE role_id = 6 AND community_id IN :ids"""
admins = await db.query_all(sql, ids=tuple(ids))
for row in admins:
_id = row[0]
if 'admins' not in communities[_id]:
communities[_id]['admins'] = list()
communities[_id]['admins'].append(row[1])
return [communities[_id] for _id in ids] return [communities[_id] for _id in ids]
...@@ -291,6 +294,17 @@ async def _append_observer_subs(db, communities, observer_id): ...@@ -291,6 +294,17 @@ async def _append_observer_subs(db, communities, observer_id):
for cid, comm in communities.items(): for cid, comm in communities.items():
comm['context']['subscribed'] = cid in subs comm['context']['subscribed'] = cid in subs
async def _append_admins(db, communities):
ids = communities.keys()
sql = """SELECT community_id, ha.name FROM hive_roles hr
JOIN hive_accounts ha ON hr.account_id = ha.id
WHERE role_id = 6 AND community_id IN :ids"""
for row in await db.query_all(sql, ids=tuple(ids)):
_id = row[0]
if 'admins' not in communities[_id]:
communities[_id]['admins'] = list()
communities[_id]['admins'].append(row[1])
# Stats # Stats
# ----- # -----
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment