ignore command (#59)
Some checks are pending
Build and Test / Format check (push) Waiting to run
Build and Test / Lint (push) Waiting to run
Build and Test / Test (push) Waiting to run
Build and Test / Build (push) Waiting to run

* New table `ignored_users` and queries

* Update sqlx to 0.8

* Implement `ignore` command and add needed hooks
This commit is contained in:
Natsu Kagami 2025-03-27 15:13:00 +01:00 committed by GitHub
parent a36fa87964
commit 7c0cbea716
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 580 additions and 383 deletions

View file

@ -0,0 +1,60 @@
use super::*;
use sqlx::Executor;
/// An ignored user in the ignored user list.
#[derive(Clone, Debug)]
pub struct IgnoredUser {
pub id: i64,
pub username: String,
pub ignored_since: DateTime,
}
impl IgnoredUser {
/// Returns a list of all ignored users.
pub async fn get_all<'a, E>(conn: E) -> Result<Vec<Self>>
where
E: Executor<'a, Database = Database>,
{
Ok(query_as!(
IgnoredUser,
r#"SELECT
id,
username,
ignored_since as "ignored_since: DateTime"
FROM ignored_users
ORDER BY id ASC"#
)
.fetch_all(conn)
.await?)
}
/// Add an user to ignore list.
pub async fn add<'a, E>(conn: E, user_id: i64, username: String) -> Result<Self>
where
E: Executor<'a, Database = Database>,
{
Ok(query_as!(
IgnoredUser,
r#"INSERT INTO ignored_users(id, username) VALUES (?, ?)
ON CONFLICT (id) DO UPDATE SET username = excluded.username
RETURNING id,
username,
ignored_since as "ignored_since: DateTime""#,
user_id,
username
)
.fetch_one(conn)
.await?)
}
// Remove an user from ignore list.
pub async fn remove<'a, E>(conn: E, user_id: i64) -> Result<()>
where
E: Executor<'a, Database = Database>,
{
query!(r#"DELETE FROM ignored_users WHERE id = ?"#, user_id)
.execute(conn)
.await?;
Ok(())
}
}

View file

@ -4,5 +4,6 @@ use sqlx::{query, query_as, Executor};
/// The DateTime used in the package.
pub type DateTime = chrono::DateTime<chrono::Utc>;
pub mod ignore_list;
pub mod osu;
pub mod osu_user;