| 1 |
use super::{DateTime, PgPool, Utc, Uuid}; |
| 2 |
|
| 3 |
#[derive(sqlx::FromRow)] |
| 4 |
pub struct AdminCommunityRow { |
| 5 |
pub id: Uuid, |
| 6 |
pub name: String, |
| 7 |
pub slug: String, |
| 8 |
pub suspended_at: Option<DateTime<Utc>>, |
| 9 |
pub suspension_reason: Option<String>, |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
#[tracing::instrument(skip_all)] |
| 14 |
pub async fn list_all_communities( |
| 15 |
pool: &PgPool, |
| 16 |
limit: i64, |
| 17 |
) -> Result<Vec<AdminCommunityRow>, sqlx::Error> { |
| 18 |
sqlx::query_as!( |
| 19 |
AdminCommunityRow, |
| 20 |
r#"SELECT id, name, slug, |
| 21 |
suspended_at AS "suspended_at: chrono::DateTime<chrono::Utc>", |
| 22 |
suspension_reason |
| 23 |
FROM communities ORDER BY name LIMIT $1"#, |
| 24 |
limit, |
| 25 |
) |
| 26 |
.fetch_all(pool) |
| 27 |
.await |
| 28 |
} |
| 29 |
|
| 30 |
#[derive(sqlx::FromRow)] |
| 31 |
pub struct AdminUserRow { |
| 32 |
pub id: Uuid, |
| 33 |
pub username: String, |
| 34 |
pub display_name: Option<String>, |
| 35 |
pub suspended_at: Option<DateTime<Utc>>, |
| 36 |
pub suspension_reason: Option<String>, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
#[tracing::instrument(skip_all)] |
| 41 |
pub async fn search_users(pool: &PgPool, query: &str) -> Result<Vec<AdminUserRow>, sqlx::Error> { |
| 42 |
let escaped = query |
| 43 |
.replace('\\', "\\\\") |
| 44 |
.replace('%', "\\%") |
| 45 |
.replace('_', "\\_"); |
| 46 |
sqlx::query_as!( |
| 47 |
AdminUserRow, |
| 48 |
r#"SELECT mnw_account_id AS id, username, display_name, |
| 49 |
suspended_at AS "suspended_at: chrono::DateTime<chrono::Utc>", |
| 50 |
suspension_reason |
| 51 |
FROM users |
| 52 |
WHERE username ILIKE $1 ESCAPE '\' |
| 53 |
ORDER BY username |
| 54 |
LIMIT 50"#, |
| 55 |
format!("{escaped}%"), |
| 56 |
) |
| 57 |
.fetch_all(pool) |
| 58 |
.await |
| 59 |
} |
| 60 |
|