Skip to main content

systemprompt_files/repository/ai/
mod.rs

1//! [`FileRepository`] queries for AI-generated files.
2//!
3//! Listing and counting of files flagged with `ai_content`, both globally and
4//! scoped to an owning user.
5
6use systemprompt_identifiers::{ContextId, SessionId, TraceId, UserId};
7
8use super::file::FileRepository;
9use crate::error::FilesResult;
10use crate::models::File;
11
12impl FileRepository {
13    pub async fn list_ai_images(&self, limit: i64, offset: i64) -> FilesResult<Vec<File>> {
14        let result = sqlx::query_as!(
15            File,
16            r#"
17            SELECT id, path, public_url, mime_type, size_bytes, ai_content, metadata, user_id as "user_id: UserId", session_id as "session_id: SessionId", trace_id as "trace_id: TraceId", context_id as "context_id: ContextId", created_at, updated_at, deleted_at
18            FROM files
19            WHERE ai_content = true AND deleted_at IS NULL
20            ORDER BY created_at DESC
21            LIMIT $1 OFFSET $2
22            "#,
23            limit,
24            offset
25        )
26        .fetch_all(self.pool.as_ref())
27        .await?;
28
29        Ok(result)
30    }
31
32    pub async fn list_ai_images_by_user(
33        &self,
34        user_id: &UserId,
35        limit: i64,
36        offset: i64,
37    ) -> FilesResult<Vec<File>> {
38        let user_id_str = user_id.as_str();
39        let result = sqlx::query_as!(
40            File,
41            r#"
42            SELECT id, path, public_url, mime_type, size_bytes, ai_content, metadata, user_id as "user_id: UserId", session_id as "session_id: SessionId", trace_id as "trace_id: TraceId", context_id as "context_id: ContextId", created_at, updated_at, deleted_at
43            FROM files
44            WHERE user_id = $1 AND ai_content = true AND deleted_at IS NULL
45            ORDER BY created_at DESC
46            LIMIT $2 OFFSET $3
47            "#,
48            user_id_str,
49            limit,
50            offset
51        )
52        .fetch_all(self.pool.as_ref())
53        .await?;
54
55        Ok(result)
56    }
57
58    pub async fn count_ai_images_by_user(&self, user_id: &UserId) -> FilesResult<i64> {
59        let user_id_str = user_id.as_str();
60        let count = sqlx::query_scalar!(
61            r#"
62            SELECT COUNT(*) as "count!"
63            FROM files
64            WHERE user_id = $1 AND ai_content = true AND deleted_at IS NULL
65            "#,
66            user_id_str
67        )
68        .fetch_one(self.pool.as_ref())
69        .await?;
70
71        Ok(count)
72    }
73
74    pub async fn count_ai_images(&self) -> FilesResult<i64> {
75        let count = sqlx::query_scalar!(
76            r#"
77            SELECT COUNT(*) as "count!"
78            FROM files
79            WHERE ai_content = true AND deleted_at IS NULL
80            "#
81        )
82        .fetch_one(self.pool.as_ref())
83        .await?;
84
85        Ok(count)
86    }
87}