Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop' of github.com:matrix-org/synapse into anoa/cle…
Browse files Browse the repository at this point in the history
…an_up_admin_api_docs

* 'develop' of github.com:matrix-org/synapse:
  Return total number of users and profile attributes in admin users endpoint (#6881)
  Add some replication tests (#7278)
  • Loading branch information
anoadragon453 committed Apr 28, 2020
2 parents 84654ae + 04dd7d1 commit ad7233c
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 47 deletions.
1 change: 1 addition & 0 deletions changelog.d/6881.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return total number of users and profile attributes in admin users endpoint. Contributed by Awesome Technologies Innovationslabor GmbH.
1 change: 1 addition & 0 deletions changelog.d/7278.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add some unit tests for replication.
11 changes: 8 additions & 3 deletions docs/admin_api/user_admin_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,22 @@ A JSON body is returned with the following shape:
"is_guest": 0,
"admin": 0,
"user_type": null,
"deactivated": 0
"deactivated": 0,
"displayname": <User One>,
"avatar_url": null
}, {
"name": "<user_id2>",
"password_hash": "<password_hash2>",
"is_guest": 0,
"admin": 1,
"user_type": null,
"deactivated": 0
"deactivated": 0,
"displayname": <User Two>,
"avatar_url": "<avatar_url>"
}
],
"next_token": "100"
"next_token": "100",
"total": 200
}
To paginate, check for ``next_token`` and if present, call the endpoint again
Expand Down
8 changes: 4 additions & 4 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ async def on_GET(self, request):
guests = parse_boolean(request, "guests", default=True)
deactivated = parse_boolean(request, "deactivated", default=False)

users = await self.store.get_users_paginate(
users, total = await self.store.get_users_paginate(
start, limit, user_id, guests, deactivated
)
ret = {"users": users}
ret = {"users": users, "total": total}
if len(users) >= limit:
ret["next_token"] = str(start + len(users))

Expand Down Expand Up @@ -199,7 +199,7 @@ async def on_PUT(self, request, user_id):
user_id, threepid["medium"], threepid["address"], current_time
)

if "avatar_url" in body:
if "avatar_url" in body and type(body["avatar_url"]) == str:
await self.profile_handler.set_avatar_url(
target_user, requester, body["avatar_url"], True
)
Expand Down Expand Up @@ -276,7 +276,7 @@ async def on_PUT(self, request, user_id):
user_id, threepid["medium"], threepid["address"], current_time
)

if "avatar_url" in body:
if "avatar_url" in body and type(body["avatar_url"]) == str:
await self.profile_handler.set_avatar_url(
user_id, requester, body["avatar_url"], True
)
Expand Down
68 changes: 39 additions & 29 deletions synapse/storage/data_stores/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ def get_users_paginate(
self, start, limit, name=None, guests=True, deactivated=False
):
"""Function to retrieve a paginated list of users from
users list. This will return a json list of users.
users list. This will return a json list of users and the
total number of users matching the filter criteria.
Args:
start (int): start number to begin the query from
Expand All @@ -512,35 +513,44 @@ def get_users_paginate(
guests (bool): whether to in include guest users
deactivated (bool): whether to include deactivated users
Returns:
defer.Deferred: resolves to list[dict[str, Any]]
defer.Deferred: resolves to list[dict[str, Any]], int
"""
name_filter = {}
if name:
name_filter["name"] = "%" + name + "%"

attr_filter = {}
if not guests:
attr_filter["is_guest"] = 0
if not deactivated:
attr_filter["deactivated"] = 0

return self.db.simple_select_list_paginate(
desc="get_users_paginate",
table="users",
orderby="name",
start=start,
limit=limit,
filters=name_filter,
keyvalues=attr_filter,
retcols=[
"name",
"password_hash",
"is_guest",
"admin",
"user_type",
"deactivated",
],
)

def get_users_paginate_txn(txn):
filters = []
args = []

if name:
filters.append("name LIKE ?")
args.append("%" + name + "%")

if not guests:
filters.append("is_guest = 0")

if not deactivated:
filters.append("deactivated = 0")

where_clause = "WHERE " + " AND ".join(filters) if len(filters) > 0 else ""

sql = "SELECT COUNT(*) as total_users FROM users %s" % (where_clause)
txn.execute(sql, args)
count = txn.fetchone()[0]

args = [self.hs.config.server_name] + args + [limit, start]
sql = """
SELECT name, user_type, is_guest, admin, deactivated, displayname, avatar_url
FROM users as u
LEFT JOIN profiles AS p ON u.name = '@' || p.user_id || ':' || ?
{}
ORDER BY u.name LIMIT ? OFFSET ?
""".format(
where_clause
)
txn.execute(sql, args)
users = self.db.cursor_to_dict(txn)
return users, count

return self.db.runInteraction("get_users_paginate_txn", get_users_paginate_txn)

def search_users(self, term):
"""Function to search users list for one or more users with
Expand Down
Loading

0 comments on commit ad7233c

Please sign in to comment.