From cfa74f84e7a14874697dd0b96e964ab4a8ec8a3d Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 6 Mar 2023 19:30:18 +0900 Subject: [PATCH 01/11] Remove instance users from user list to show --- plume-models/src/users.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index b3dd0368..8dad4081 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -431,6 +431,7 @@ impl User { pub fn get_local_page(conn: &Connection, (min, max): (i32, i32)) -> Result> { users::table .filter(users::instance_id.eq(Instance::get_local()?.id)) + .filter(users::role.ne(Role::Instance as i32)) .order(users::username.asc()) .offset(min.into()) .limit((max - min).into()) From ec030d500d80c1323a5564cad529131a264b351f Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 20 Mar 2023 00:19:42 +0900 Subject: [PATCH 02/11] Exclude instance user when counting local users --- plume-models/src/users.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 8dad4081..76043baf 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -186,6 +186,7 @@ impl User { pub fn count_local(conn: &Connection) -> Result { users::table .filter(users::instance_id.eq(Instance::get_local()?.id)) + .filter(users::role.ne(Role::Instance as i32)) .count() .get_result(conn) .map_err(Error::from) From a1a19e091a00644100e9475e88181c0158d79aec Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 20 Mar 2023 00:59:16 +0900 Subject: [PATCH 03/11] Define User::search_local_by_name() method --- plume-models/src/users.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 76043baf..82b7ab19 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -15,7 +15,10 @@ use activitystreams::{ prelude::*, }; use chrono::{NaiveDateTime, Utc}; -use diesel::{self, BelongingToDsl, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl}; +use diesel::{ + self, BelongingToDsl, BoolExpressionMethods, ExpressionMethods, OptionalExtension, QueryDsl, + RunQueryDsl, TextExpressionMethods, +}; use ldap3::{LdapConn, Scope, SearchEntry}; use openssl::{ hash::MessageDigest, @@ -204,6 +207,27 @@ impl User { } } + pub fn search_local_by_name( + conn: &Connection, + name: &str, + (min, max): (i32, i32), + ) -> Result> { + users::table + .filter(users::instance_id.eq(Instance::get_local()?.id)) + .filter(users::role.ne(Role::Instance as i32)) + // TODO: use `ilike` instead of `like` for PostgreSQL + .filter( + users::username + .like(format!("%{}%", name)) + .or(users::display_name.like(format!("%{}%", name))), + ) + .order(users::username.asc()) + .offset(min.into()) + .limit((max - min).into()) + .load::(conn) + .map_err(Error::from) + } + /** * TODO: Should create user record with normalized(lowercased) email */ From a394c3f21082f487c96f256cce8a1c9d5e5cafcf Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 20 Mar 2023 00:59:46 +0900 Subject: [PATCH 04/11] Define admin_search_user route --- src/routes/instance.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/routes/instance.rs b/src/routes/instance.rs index b4ac399b..90c0b764 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -160,7 +160,7 @@ pub fn toggle_block( )) } -#[get("/admin/users?")] +#[get("/admin/users?", rank = 2)] pub fn admin_users( _mod: Moderator, page: Option, @@ -171,6 +171,24 @@ pub fn admin_users( Ok(render!(instance::users( &(&conn, &rockets).to_context(), User::get_local_page(&conn, page.limits())?, + None, + page.0, + Page::total(User::count_local(&conn)? as i32) + ))) +} +#[get("/admin/users?&", rank = 1)] +pub fn admin_search_users( + _mod: Moderator, + user: String, + page: Option, + conn: DbConn, + rockets: PlumeRocket, +) -> Result { + let page = page.unwrap_or_default(); + Ok(render!(instance::users( + &(&conn, &rockets).to_context(), + User::search_local_by_name(&conn, &user, page.limits())?, + Some(user.as_str()), page.0, Page::total(User::count_local(&conn)? as i32) ))) From 3e9d9a459f90bb326d05c9801f4bc9c6946bc58b Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 20 Mar 2023 01:00:02 +0900 Subject: [PATCH 05/11] Enable admin_search_user route --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index 5e3c0e78..cd5fb96a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -157,6 +157,7 @@ Then try to restart Plume. routes::instance::admin_mod, routes::instance::admin_instances, routes::instance::admin_users, + routes::instance::admin_search_users, routes::instance::admin_email_blocklist, routes::instance::add_email_blocklist, routes::instance::delete_email_blocklist, From 98c73bb6dfd5e40a1edd5f23a6ce458b3242b686 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 20 Mar 2023 01:00:17 +0900 Subject: [PATCH 06/11] Add search form to user list page --- templates/instance/users.rs.html | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/templates/instance/users.rs.html b/templates/instance/users.rs.html index 8bcf48e7..5dc413b6 100644 --- a/templates/instance/users.rs.html +++ b/templates/instance/users.rs.html @@ -3,7 +3,7 @@ @use crate::template_utils::*; @use crate::routes::*; -@(ctx: BaseContext, users: Vec, page: i32, n_pages: i32) +@(ctx: BaseContext, users: Vec, user: Option<&str>, page: i32, n_pages: i32) @:base(ctx, i18n!(ctx.1, "Users"), {}, {}, {

@i18n!(ctx.1, "Users")

@@ -15,6 +15,12 @@ (&uri!(instance::admin_email_blocklist: page=_).to_string(), i18n!(ctx.1, "Email blocklist"), false) ]) +
+ + + +
+
- - +
+ + + +
From 33bd290679c77f9d3a4acc646a1f815e48ce7fd4 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 20 Mar 2023 01:21:39 +0900 Subject: [PATCH 08/11] Use DbConn in model tests --- plume-models/src/comments.rs | 1 + plume-models/src/follows.rs | 4 +++- plume-models/src/posts.rs | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/plume-models/src/comments.rs b/plume-models/src/comments.rs index 2f06dada..91e7c6c2 100644 --- a/plume-models/src/comments.rs +++ b/plume-models/src/comments.rs @@ -422,6 +422,7 @@ impl CommentTree { mod tests { use super::*; use crate::blogs::Blog; + use crate::db_conn::DbConn; use crate::inbox::{inbox, tests::fill_database, InboxResult}; use crate::safe_string::SafeString; use crate::tests::{db, format_datetime}; diff --git a/plume-models/src/follows.rs b/plume-models/src/follows.rs index 8f79430b..f20bdd79 100644 --- a/plume-models/src/follows.rs +++ b/plume-models/src/follows.rs @@ -232,7 +232,9 @@ impl IntoId for Follow { #[cfg(test)] mod tests { use super::*; - use crate::{tests::db, users::tests as user_tests, users::tests::fill_database}; + use crate::{ + db_conn::DbConn, tests::db, users::tests as user_tests, users::tests::fill_database, + }; use assert_json_diff::assert_json_eq; use diesel::Connection; use serde_json::{json, to_value}; diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index d9a63c3b..80fdba32 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -1027,6 +1027,7 @@ impl From for Arc { #[cfg(test)] mod tests { use super::*; + use crate::db_conn::DbConn; use crate::inbox::{inbox, tests::fill_database, InboxResult}; use crate::mentions::{Mention, NewMention}; use crate::safe_string::SafeString; From d6bb2bfb721e8f4e763193cadf393d844a108dd2 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 21 Mar 2023 18:49:01 +0900 Subject: [PATCH 09/11] Use unwrap_or_default() instead of unwrap_or("") --- templates/instance/users.rs.html | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/templates/instance/users.rs.html b/templates/instance/users.rs.html index 6a5db785..d55ce5fd 100644 --- a/templates/instance/users.rs.html +++ b/templates/instance/users.rs.html @@ -15,10 +15,9 @@ (&uri!(instance::admin_email_blocklist: page=_).to_string(), i18n!(ctx.1, "Email blocklist"), false) ]) - +
- - +
From 213628e400bd275bf60746d7ca9cc070c006afc6 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 21 Mar 2023 19:07:09 +0900 Subject: [PATCH 10/11] Don't use LIKE query when username is empty for user search --- src/routes/instance.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/routes/instance.rs b/src/routes/instance.rs index 90c0b764..9de9fee0 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -185,9 +185,15 @@ pub fn admin_search_users( rockets: PlumeRocket, ) -> Result { let page = page.unwrap_or_default(); + let users = if user.is_empty() { + User::get_local_page(&conn, page.limits())? + } else { + User::search_local_by_name(&conn, &user, page.limits())? + }; + Ok(render!(instance::users( &(&conn, &rockets).to_context(), - User::search_local_by_name(&conn, &user, page.limits())?, + users, Some(user.as_str()), page.0, Page::total(User::count_local(&conn)? as i32) From b9a09a25119e3307c3ab17bc05056fbfda2b971b Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 21 Mar 2023 19:15:20 +0900 Subject: [PATCH 11/11] Follow pagination user list page change --- templates/instance/users.rs.html | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/templates/instance/users.rs.html b/templates/instance/users.rs.html index d55ce5fd..1499fb52 100644 --- a/templates/instance/users.rs.html +++ b/templates/instance/users.rs.html @@ -53,5 +53,9 @@ } - @paginate(ctx.1, page, n_pages) + @if user.is_some() { + @paginate_param(ctx.1, page, n_pages, Some(format!("user={}", encode_query_param(user.unwrap_or_default())))) + } else { + @paginate(ctx.1, page, n_pages) + } })