Skip to main content

max / makenotwork

1.7 KB · 53 lines History Blame Raw
1 //! Cross-service integration helpers: CSRF-token convenience and Multithreaded
2 //! (MT) discussion-thread stats.
3
4 use tower_sessions::Session;
5
6 use crate::config::Config;
7
8 /// Get or create a CSRF token for the session, returning `None` on failure.
9 ///
10 /// Convenience wrapper for templates that need an `Option<String>`.
11 pub async fn get_csrf_token(session: &Session) -> Option<String> {
12 crate::csrf::get_or_create_token(session).await.ok()
13 }
14
15 /// Fetch MT discussion thread stats (URL + post count) for a linked thread.
16 /// Returns (discussion_url, discussion_count), both None if MT unavailable or no linked thread.
17 pub async fn fetch_discussion_info(
18 integrations: &crate::Integrations,
19 config: &Config,
20 mt_thread_id: Option<crate::db::MtThreadId>,
21 project_slug: &str,
22 category_slug: &str,
23 ) -> (Option<String>, Option<i64>) {
24 let Some(thread_id) = mt_thread_id else {
25 return (None, None);
26 };
27 let Some(ref mt) = integrations.mt_client else {
28 return (None, None);
29 };
30 let Some(ref mt_base_url) = config.integrations.mt_base_url else {
31 return (None, None);
32 };
33
34 let url = format!("{mt_base_url}/p/{project_slug}/{category_slug}/{thread_id}");
35
36 match tokio::time::timeout(
37 std::time::Duration::from_secs(2),
38 mt.get_thread_stats(thread_id),
39 )
40 .await
41 {
42 Ok(Ok(stats)) => (Some(url), Some(stats.post_count)),
43 Ok(Err(e)) => {
44 tracing::debug!(error = ?e, "failed to fetch MT thread stats");
45 (Some(url), None)
46 }
47 Err(_) => {
48 tracing::debug!("MT thread stats request timed out");
49 (Some(url), None)
50 }
51 }
52 }
53