forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

server_ban.rs63 lines, 1.6 KiBHistoryRawDownload
1use common::{
2 error::Error,
3 model::{Id, ServerBan, ServerBanKind},
4};
5
6use super::{Connection, all, one, run};
7use crate::error::{ServerError, ensure_with};
8
9pub async fn insert(
10 kind: ServerBanKind,
11 value: &str,
12 reason: Option<&str>,
13 db: &Connection,
14) -> Result<(), ServerError> {
15 run(
16 db,
17 "INSERT OR IGNORE INTO server_ban (kind, value, reason) VALUES (?1, ?2, ?3)",
18 (kind.as_str(), value, reason),
19 )
20 .await?;
21
22 Ok(())
23}
24
25pub async fn list(db: &Connection) -> Result<Vec<ServerBan>, ServerError> {
26 all(
27 db,
28 "SELECT id, kind, value, reason FROM server_ban ORDER BY kind, value",
29 (),
30 |r| {
31 Ok(ServerBan {
32 id: r.get()?,
33 kind: r.named()?,
34 value: r.get()?,
35 reason: r.get()?,
36 })
37 },
38 )
39 .await
40}
41
42async fn exists(kind: ServerBanKind, value: &str, db: &Connection) -> Result<bool, ServerError> {
43 one(
44 db,
45 "SELECT EXISTS (SELECT 1 FROM server_ban WHERE kind = ?1 AND value = ?2)",
46 (kind.as_str(), value),
47 |r| r.get(),
48 )
49 .await
50}
51
52pub async fn username_banned(username: &str, db: &Connection) -> Result<bool, ServerError> {
53 exists(ServerBanKind::Username, username, db).await
54}
55
56pub async fn ip_banned(ip: &str, db: &Connection) -> Result<bool, ServerError> {
57 exists(ServerBanKind::Ip, ip, db).await
58}
59
60pub async fn remove(id: Id, db: &Connection) -> Result<(), ServerError> {
61 let deleted = run(db, "DELETE FROM server_ban WHERE id = ?1", (id,)).await?;
62 ensure_with(deleted == 1, Error::NotFound)
63}