Skip to main content

max / makenotwork

1.4 KB · 49 lines History Blame Raw
1 //! footnote reads, batched over a post set rather than per post
2
3 use super::{DateTime, PgPool, Utc, Uuid};
4
5 #[derive(sqlx::FromRow)]
6 pub struct FootnoteWithAuthor {
7 pub id: Uuid,
8 pub post_id: Uuid,
9 pub author_id: Uuid,
10 pub author_name: String,
11 pub author_username: String,
12 pub body_html: String,
13 pub created_at: DateTime<Utc>,
14 }
15
16 /// Batch-fetch footnotes for a set of post IDs, joined with author info.
17 #[tracing::instrument(skip_all)]
18 pub async fn list_footnotes_for_posts(
19 pool: &PgPool,
20 post_ids: &[Uuid],
21 ) -> Result<Vec<FootnoteWithAuthor>, sqlx::Error> {
22 sqlx::query_as!(
23 FootnoteWithAuthor,
24 r#"SELECT f.id, f.post_id, f.author_id,
25 COALESCE(u.display_name, u.username) AS "author_name!",
26 u.username AS author_username,
27 f.body_html,
28 f.created_at AS "created_at: chrono::DateTime<chrono::Utc>"
29 FROM post_footnotes f
30 JOIN users u ON u.mnw_account_id = f.author_id
31 WHERE f.post_id = ANY($1)
32 ORDER BY f.created_at"#,
33 post_ids,
34 )
35 .fetch_all(pool)
36 .await
37 }
38
39 /// Count footnotes on a specific post.
40 #[tracing::instrument(skip_all)]
41 pub async fn count_footnotes_for_post(pool: &PgPool, post_id: Uuid) -> Result<i64, sqlx::Error> {
42 sqlx::query_scalar!(
43 r#"SELECT COUNT(*) AS "count!" FROM post_footnotes WHERE post_id = $1"#,
44 post_id,
45 )
46 .fetch_one(pool)
47 .await
48 }
49