@@ -503,7 +503,8 @@ def get_users_paginate(
503503 self , start , limit , name = None , guests = True , deactivated = False
504504 ):
505505 """Function to retrieve a paginated list of users from
506- users list. This will return a json list of users.
506+ users list. This will return a json list of users and the
507+ total number of users matching the filter criteria.
507508
508509 Args:
509510 start (int): start number to begin the query from
@@ -512,35 +513,44 @@ def get_users_paginate(
512513 guests (bool): whether to in include guest users
513514 deactivated (bool): whether to include deactivated users
514515 Returns:
515- defer.Deferred: resolves to list[dict[str, Any]]
516+ defer.Deferred: resolves to list[dict[str, Any]], int
516517 """
517- name_filter = {}
518- if name :
519- name_filter ["name" ] = "%" + name + "%"
520-
521- attr_filter = {}
522- if not guests :
523- attr_filter ["is_guest" ] = 0
524- if not deactivated :
525- attr_filter ["deactivated" ] = 0
526-
527- return self .db .simple_select_list_paginate (
528- desc = "get_users_paginate" ,
529- table = "users" ,
530- orderby = "name" ,
531- start = start ,
532- limit = limit ,
533- filters = name_filter ,
534- keyvalues = attr_filter ,
535- retcols = [
536- "name" ,
537- "password_hash" ,
538- "is_guest" ,
539- "admin" ,
540- "user_type" ,
541- "deactivated" ,
542- ],
543- )
518+
519+ def get_users_paginate_txn (txn ):
520+ filters = []
521+ args = []
522+
523+ if name :
524+ filters .append ("name LIKE ?" )
525+ args .append ("%" + name + "%" )
526+
527+ if not guests :
528+ filters .append ("is_guest = 0" )
529+
530+ if not deactivated :
531+ filters .append ("deactivated = 0" )
532+
533+ where_clause = "WHERE " + " AND " .join (filters ) if len (filters ) > 0 else ""
534+
535+ sql = "SELECT COUNT(*) as total_users FROM users %s" % (where_clause )
536+ txn .execute (sql , args )
537+ count = txn .fetchone ()[0 ]
538+
539+ args = [self .hs .config .server_name ] + args + [limit , start ]
540+ sql = """
541+ SELECT name, user_type, is_guest, admin, deactivated, displayname, avatar_url
542+ FROM users as u
543+ LEFT JOIN profiles AS p ON u.name = '@' || p.user_id || ':' || ?
544+ {}
545+ ORDER BY u.name LIMIT ? OFFSET ?
546+ """ .format (
547+ where_clause
548+ )
549+ txn .execute (sql , args )
550+ users = self .db .cursor_to_dict (txn )
551+ return users , count
552+
553+ return self .db .runInteraction ("get_users_paginate_txn" , get_users_paginate_txn )
544554
545555 def search_users (self , term ):
546556 """Function to search users list for one or more users with
0 commit comments