diff --git a/otter/src/server/web/users.rs b/otter/src/server/web/users.rs index 560bdd2..dd54c0e 100644 --- a/otter/src/server/web/users.rs +++ b/otter/src/server/web/users.rs @@ -4,6 +4,7 @@ use axum::{ http::HeaderMap, routing::{delete, get}, }; +use serde::Deserialize; use crate::{ server::{ @@ -22,24 +23,46 @@ pub fn router(ctx: Context) -> Router { )) } -pub async fn get_users( +#[derive(Deserialize, Clone)] +struct UserFilter { + username: Option, +} + +impl From for gpodder::UserFilter { + fn from(value: UserFilter) -> Self { + Self { + username: value.username, + } + } +} + +impl ToQuery for UserFilter { + fn to_query(self) -> crate::web::Query { + crate::web::Query::default().opt_parameter("username", self.username) + } +} + +async fn get_users( State(ctx): State, headers: HeaderMap, Extension(session): Extension, Query(page): Query, + Query(filter): Query, ) -> AppResult>> { let next_page = page.next_page(); + let filter_clone = filter.clone(); + let user_id = session.user.id; let users = tokio::task::spawn_blocking(move || { ctx.store .admin(&session.user)? - .paginated_users(page.into(), &gpodder::UserFilter::default()) + .paginated_users(page.into(), &filter.into()) }) .await .unwrap()?; - let next_page_query = - (users.len() == next_page.per_page as usize).then_some(next_page.to_query()); + let next_page_query = (users.len() == next_page.per_page as usize) + .then_some(next_page.to_query().join(filter_clone)); Ok(View::Users(users, user_id, next_page_query) .page(&headers) diff --git a/otter/src/web/query.rs b/otter/src/web/query.rs index 409afd5..e8585ea 100644 --- a/otter/src/web/query.rs +++ b/otter/src/web/query.rs @@ -23,6 +23,15 @@ impl Query { self } + + /// Convenience method for adding possibly empty parameter values from options + pub fn opt_parameter(self, key: impl ToString, value: Option) -> Self { + if let Some(value) = value { + self.parameter(key, value) + } else { + self + } + } } /// Allows objects to be converted into queries diff --git a/otter/src/web/templates/views/users.html b/otter/src/web/templates/views/users.html index 22dee43..f66f3fe 100644 --- a/otter/src/web/templates/views/users.html +++ b/otter/src/web/templates/views/users.html @@ -1,15 +1,24 @@

Users

- + +
- {% for user in users %} + {%- for user in users %} - {% endfor %} + {% endfor -%} {%- if next_page_query %}
Username
{{ user.username }}