Skip to main content

max / makenotwork

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