Skip to main content

max / makenotwork

1.4 KB · 51 lines History Blame Raw
1 use super::{PgPool, Uuid};
2
3 /// Batch-check which threads have at least one mention of the given user.
4 #[tracing::instrument(skip_all)]
5 pub async fn get_threads_with_mentions_for_user(
6 pool: &PgPool,
7 user_id: Uuid,
8 thread_ids: &[Uuid],
9 ) -> Result<Vec<Uuid>, sqlx::Error> {
10 if thread_ids.is_empty() {
11 return Ok(Vec::new());
12 }
13 sqlx::query_scalar!(
14 "SELECT DISTINCT p.thread_id
15 FROM post_mentions pm
16 JOIN posts p ON p.id = pm.post_id
17 WHERE pm.mentioned_user_id = $1
18 AND p.thread_id = ANY($2)",
19 user_id,
20 thread_ids,
21 )
22 .fetch_all(pool)
23 .await
24 }
25
26 /// Resolve usernames to user IDs, filtered to community members.
27 #[tracing::instrument(skip_all)]
28 pub async fn resolve_usernames_in_community(
29 pool: &PgPool,
30 community_id: Uuid,
31 usernames: &[String],
32 ) -> Result<std::collections::HashMap<String, Uuid>, sqlx::Error> {
33 if usernames.is_empty() {
34 return Ok(std::collections::HashMap::new());
35 }
36 let rows = sqlx::query!(
37 "SELECT u.username, u.mnw_account_id
38 FROM users u
39 WHERE u.username = ANY($1)
40 AND u.mnw_account_id IN (SELECT user_id FROM memberships WHERE community_id = $2)",
41 usernames,
42 community_id,
43 )
44 .fetch_all(pool)
45 .await?;
46 Ok(rows
47 .into_iter()
48 .map(|r| (r.username, r.mnw_account_id))
49 .collect())
50 }
51