use super::{PgPool, Uuid}; /// Batch-check which threads have at least one mention of the given user. #[tracing::instrument(skip_all)] pub async fn get_threads_with_mentions_for_user( pool: &PgPool, user_id: Uuid, thread_ids: &[Uuid], ) -> Result, sqlx::Error> { if thread_ids.is_empty() { return Ok(Vec::new()); } sqlx::query_scalar!( "SELECT DISTINCT p.thread_id FROM post_mentions pm JOIN posts p ON p.id = pm.post_id WHERE pm.mentioned_user_id = $1 AND p.thread_id = ANY($2)", user_id, thread_ids, ) .fetch_all(pool) .await } /// Resolve usernames to user IDs, filtered to community members. #[tracing::instrument(skip_all)] pub async fn resolve_usernames_in_community( pool: &PgPool, community_id: Uuid, usernames: &[String], ) -> Result, sqlx::Error> { if usernames.is_empty() { return Ok(std::collections::HashMap::new()); } let rows = sqlx::query!( "SELECT u.username, u.mnw_account_id FROM users u WHERE u.username = ANY($1) AND u.mnw_account_id IN (SELECT user_id FROM memberships WHERE community_id = $2)", usernames, community_id, ) .fetch_all(pool) .await?; Ok(rows .into_iter() .map(|r| (r.username, r.mnw_account_id)) .collect()) }