Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 71da8dd

Browse files
match user list api options to synapse
Fixes: #169
1 parent dc2a532 commit 71da8dd

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

synadm/api.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,12 @@ def __init__(self, log, user, token, base_url, admin_path, timeout, debug,
454454
self.user = user
455455

456456
def user_list(self, _from, _limit, _guests, _deactivated,
457-
_name, _user_id, _admin=None):
457+
_name, _user_id, _admin=None, order_by=None, _dir=None,
458+
not_user_type=None, locked=False):
458459
"""List and search users
459460
461+
https://element-hq.github.io/synapse/latest/admin_api/user_admin_api.html#list-accounts
462+
460463
Args:
461464
_from (int): offsets user list by this number, used for pagination
462465
_limit (int): maximum number of users returned, used for pagination
@@ -467,6 +470,19 @@ def user_list(self, _from, _limit, _guests, _deactivated,
467470
_user_id (string): fully qualified Matrix user ID to search for
468471
_admin (bool or None): whether to filter for admins. a None
469472
does not filter.
473+
order_by (string or None): Method of sorting returned users. See
474+
Synapse's documentation for valid choices. None uses Synapse
475+
defaults. Note that methods other than "name" or
476+
"creation_ts" may be slow and cause a major load as they
477+
have no database index.
478+
_dir (string or None): Direction of sorting. "f" for forwards
479+
(default or when None), "b" for backwards/reversed.
480+
not_user_type (string or list or None): User type to exclude.
481+
Supports list for multiple values. Possible values are
482+
"bot", "support", or "" (to exclude users without a user
483+
type). Defaults to None for no filtering.
484+
locked (bool): Include locked users. Defaults to not
485+
including them.
470486
471487
Returns:
472488
string: JSON string containing the found users
@@ -478,14 +494,19 @@ def user_list(self, _from, _limit, _guests, _deactivated,
478494
else None),
479495
"deactivated": "true" if _deactivated else None,
480496
"name": _name,
481-
"user_id": _user_id
497+
"user_id": _user_id,
498+
"order_by": order_by,
499+
"dir": _dir,
500+
"not_user_type": not_user_type,
501+
"locked": str(locked).lower()
482502
}
483503
if _admin is not None:
484504
params["admins"] = str(_admin).lower()
485505
return self.query("get", "v2/users", params=params)
486506

487-
def user_list_paginate(self, _limit, _guests, _deactivated,
488-
_name, _user_id, _from="0", admin=None):
507+
def user_list_paginate(self, _limit, _guests, _deactivated, _name,
508+
_user_id, _from="0", admin=None, order_by=None,
509+
_dir=None, not_user_type=None, locked=False):
489510
# documentation is mostly duplicated from user_list...
490511
"""Yields API responses for all of the pagination.
491512
@@ -500,14 +521,28 @@ def user_list_paginate(self, _limit, _guests, _deactivated,
500521
_user_id (string): Fully qualified Matrix user ID to search for.
501522
_from (string): Offsets user list by this number, used for
502523
pagination.
524+
order_by (string or None): Method of sorting returned users. See
525+
Synapse's documentation for valid choices. None uses Synapse
526+
defaults. Note that methods other than "name" or
527+
"creation_ts" may be slow and cause a major load as they
528+
have no database index.
529+
_dir (string or None): Direction of sorting. "f" for forwards
530+
(default or when None), "b" for backwards/reversed.
531+
not_user_type (string or list or None): User type to exclude.
532+
Supports list for multiple values. Possible values are
533+
"bot", "support", or "" (to exclude users without a user
534+
type). Defaults to None for no filtering.
535+
locked (bool): Include locked users. Defaults to not
536+
including them.
503537
504538
Yields:
505539
dict: The Admin API response for listing accounts.
506540
https://element-hq.github.io/synapse/latest/admin_api/user_admin_api.html#list-accounts
507541
"""
508542
while _from is not None:
509543
response = self.user_list(_from, _limit, _guests, _deactivated,
510-
_name, _user_id, admin)
544+
_name, _user_id, admin, order_by,
545+
_dir, not_user_type, locked)
511546
yield response
512547
_from = response.get("next_token", None)
513548

synadm/cli/user.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ def user():
5757
"--admins/--non-admins", "-a/-A", default=None,
5858
help="""Whether to filter for admins, or non-admins. If not specified,
5959
no admin filter is applied.""")
60+
@click.option(
61+
"--sort-by", type=str, help="""Method of sorting users. Valid options
62+
are name, is_guest, admin, user_type, deactivated, shadow_banned,
63+
displayname, avatar_url, creation_ts, and last_seen_ts.""")
64+
@click.option(
65+
"--reverse", is_flag=True, default=False, type=bool, help="""Display
66+
items in reverse order, from last to first.""")
67+
@click.option(
68+
"--exclude-user-type", type=str, default=None, multiple=True,
69+
help="""Exclude users of specific user types. Can be specified multiple
70+
times. Valid values are "bot", "support", or "" (excludes those without
71+
user types).""")
72+
@click.option(
73+
"--locked", "-L", type=bool, default=False, is_flag=True,
74+
help="""Include locked users in the user list""")
6075
@optgroup.group(
6176
"Search options",
6277
cls=MutuallyExclusiveOptionGroup,
@@ -71,13 +86,15 @@ def user():
7186
help="""Search users by ID - filters to only return users with Matrix IDs
7287
(@user:server) that contain this value""")
7388
@click.pass_obj
74-
def list_user_cmd(helper, from_, limit, guests, deactivated, name, user_id,
75-
admins):
89+
def list_user_cmd(helper, from_, limit, guests, deactivated, name, sort_by,
90+
reverse, exclude_user_type, locked, user_id, admins):
7691
""" List users, search for users.
7792
"""
7893
mxid = helper.generate_mxid(user_id)
94+
_dir = "b" if reverse else None
7995
users = helper.api.user_list(from_, limit, guests, deactivated, name,
80-
mxid, admins)
96+
mxid, admins, sort_by, _dir,
97+
exclude_user_type, locked)
8198
if users is None:
8299
click.echo("Users could not be fetched.")
83100
raise SystemExit(1)

0 commit comments

Comments
 (0)