mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-05-24 09:10:49 +00:00
ignore
command (#59)
* New table `ignored_users` and queries * Update sqlx to 0.8 * Implement `ignore` command and add needed hooks
This commit is contained in:
parent
a36fa87964
commit
7c0cbea716
30 changed files with 580 additions and 383 deletions
60
youmubot-db-sql/src/models/ignore_list.rs
Normal file
60
youmubot-db-sql/src/models/ignore_list.rs
Normal 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(())
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue