Skip to main content

systemprompt_users/repository/banned_ip/
listing.rs

1use anyhow::Result;
2
3use super::BannedIpRepository;
4use super::types::BannedIp;
5
6impl BannedIpRepository {
7    pub async fn list_active_bans(&self, limit: i64) -> Result<Vec<BannedIp>> {
8        let bans = sqlx::query_as!(
9            BannedIp,
10            r#"
11            SELECT
12                ip_address,
13                reason,
14                banned_at,
15                expires_at,
16                ban_count,
17                last_offense_path,
18                last_user_agent,
19                is_permanent,
20                source_fingerprint,
21                ban_source,
22                associated_session_ids
23            FROM banned_ips
24            WHERE expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP
25            ORDER BY banned_at DESC
26            LIMIT $1
27            "#,
28            limit
29        )
30        .fetch_all(&*self.pool)
31        .await?;
32
33        Ok(bans)
34    }
35
36    pub async fn list_bans_by_source(&self, ban_source: &str, limit: i64) -> Result<Vec<BannedIp>> {
37        let bans = sqlx::query_as!(
38            BannedIp,
39            r#"
40            SELECT
41                ip_address,
42                reason,
43                banned_at,
44                expires_at,
45                ban_count,
46                last_offense_path,
47                last_user_agent,
48                is_permanent,
49                source_fingerprint,
50                ban_source,
51                associated_session_ids
52            FROM banned_ips
53            WHERE ban_source = $1
54              AND (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)
55            ORDER BY banned_at DESC
56            LIMIT $2
57            "#,
58            ban_source,
59            limit
60        )
61        .fetch_all(&*self.pool)
62        .await?;
63
64        Ok(bans)
65    }
66
67    pub async fn list_bans_by_fingerprint(&self, fingerprint: &str) -> Result<Vec<BannedIp>> {
68        let bans = sqlx::query_as!(
69            BannedIp,
70            r#"
71            SELECT
72                ip_address,
73                reason,
74                banned_at,
75                expires_at,
76                ban_count,
77                last_offense_path,
78                last_user_agent,
79                is_permanent,
80                source_fingerprint,
81                ban_source,
82                associated_session_ids
83            FROM banned_ips
84            WHERE source_fingerprint = $1
85            ORDER BY banned_at DESC
86            "#,
87            fingerprint
88        )
89        .fetch_all(&*self.pool)
90        .await?;
91
92        Ok(bans)
93    }
94
95    pub async fn count_active_bans(&self) -> Result<i64> {
96        let result = sqlx::query_scalar!(
97            r#"
98            SELECT COUNT(*)::BIGINT as "count!"
99            FROM banned_ips
100            WHERE expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP
101            "#
102        )
103        .fetch_one(&*self.pool)
104        .await?;
105
106        Ok(result)
107    }
108}