|
| 1 | +//! Shared Telegram moderation summaries and message helpers. |
| 2 | +
|
| 3 | +use chrono::{DateTime, Utc}; |
| 4 | +use tracing::warn; |
| 5 | + |
| 6 | +pub struct QueueCount { |
| 7 | + pub label: &'static str, |
| 8 | + pub count: i64, |
| 9 | + pub oldest: Option<DateTime<Utc>>, |
| 10 | +} |
| 11 | + |
| 12 | +pub async fn send_telegram_text( |
| 13 | + http: &reqwest::Client, |
| 14 | + bot_token: &str, |
| 15 | + chat_id: &str, |
| 16 | + text: &str, |
| 17 | +) { |
| 18 | + let url = format!("https://api.telegram.org/bot{bot_token}/sendMessage"); |
| 19 | + let payload = serde_json::json!({ |
| 20 | + "chat_id": chat_id, |
| 21 | + "text": text, |
| 22 | + "parse_mode": "Markdown", |
| 23 | + "disable_web_page_preview": true, |
| 24 | + }); |
| 25 | + if let Err(e) = http.post(&url).json(&payload).send().await { |
| 26 | + warn!("telegram sendMessage failed: {e}"); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +pub fn format_pending_age(oldest: Option<DateTime<Utc>>) -> String { |
| 31 | + let Some(oldest) = oldest else { |
| 32 | + return "unknown".to_string(); |
| 33 | + }; |
| 34 | + let delta = Utc::now().signed_duration_since(oldest); |
| 35 | + let minutes = delta.num_minutes().max(0); |
| 36 | + let hours = minutes / 60; |
| 37 | + let days = hours / 24; |
| 38 | + if days > 0 { |
| 39 | + format!("{days}d {}h", hours % 24) |
| 40 | + } else if hours > 0 { |
| 41 | + format!("{hours}h {}m", minutes % 60) |
| 42 | + } else { |
| 43 | + format!("{minutes}m") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +pub async fn collect_pending_counts(pool: &sqlx::PgPool) -> Result<Vec<QueueCount>, sqlx::Error> { |
| 48 | + let contribution = pending_count( |
| 49 | + pool, |
| 50 | + "SELECT COUNT(*), MIN(created_at) FROM contributions WHERE status = 'PENDING'", |
| 51 | + ) |
| 52 | + .await?; |
| 53 | + let metadata = pending_count( |
| 54 | + pool, |
| 55 | + "SELECT COUNT(*), MIN(created_at) FROM metadata_suggestions WHERE status = 'pending'", |
| 56 | + ) |
| 57 | + .await?; |
| 58 | + let stream = pending_count( |
| 59 | + pool, |
| 60 | + "SELECT COUNT(*), MIN(created_at) FROM stream_suggestions WHERE status = 'PENDING'", |
| 61 | + ) |
| 62 | + .await?; |
| 63 | + let episode = pending_count( |
| 64 | + pool, |
| 65 | + "SELECT COUNT(*), MIN(created_at) FROM episode_suggestions WHERE status = 'pending'", |
| 66 | + ) |
| 67 | + .await?; |
| 68 | + let annotation = pending_count( |
| 69 | + pool, |
| 70 | + r#" |
| 71 | + WITH unlinked_streams AS ( |
| 72 | + SELECT DISTINCT sf.stream_id |
| 73 | + FROM stream_file sf |
| 74 | + INNER JOIN stream s ON s.id = sf.stream_id |
| 75 | + LEFT JOIN file_media_link fml_any ON fml_any.file_id = sf.id |
| 76 | + WHERE s.is_active = true |
| 77 | + AND s.is_blocked = false |
| 78 | + AND fml_any.id IS NULL |
| 79 | + ), |
| 80 | + null_episode_pairs AS ( |
| 81 | + SELECT DISTINCT sf.stream_id, fml_series.media_id |
| 82 | + FROM stream_file sf |
| 83 | + INNER JOIN stream s ON s.id = sf.stream_id |
| 84 | + INNER JOIN file_media_link fml_series ON fml_series.file_id = sf.id |
| 85 | + INNER JOIN stream_media_link sml |
| 86 | + ON sml.stream_id = sf.stream_id |
| 87 | + AND sml.media_id = fml_series.media_id |
| 88 | + INNER JOIN media m ON m.id = fml_series.media_id |
| 89 | + WHERE s.is_active = true |
| 90 | + AND s.is_blocked = false |
| 91 | + AND m.type = 'SERIES' |
| 92 | + AND fml_series.episode_number IS NULL |
| 93 | + ), |
| 94 | + unmapped_pairs AS ( |
| 95 | + SELECT DISTINCT us.stream_id, m.id AS media_id |
| 96 | + FROM unlinked_streams us |
| 97 | + INNER JOIN stream_media_link sml ON sml.stream_id = us.stream_id |
| 98 | + INNER JOIN media m ON sml.media_id = m.id |
| 99 | + WHERE m.type = 'SERIES' |
| 100 | + UNION |
| 101 | + SELECT nep.stream_id, nep.media_id |
| 102 | + FROM null_episode_pairs nep |
| 103 | + ), |
| 104 | + annotated_streams AS ( |
| 105 | + SELECT DISTINCT s.id AS stream_id, s.created_at |
| 106 | + FROM unmapped_pairs up |
| 107 | + INNER JOIN stream s ON s.id = up.stream_id |
| 108 | + ) |
| 109 | + SELECT COUNT(*), MIN(created_at) FROM annotated_streams |
| 110 | + "#, |
| 111 | + ) |
| 112 | + .await?; |
| 113 | + |
| 114 | + Ok(vec![ |
| 115 | + QueueCount { |
| 116 | + label: "Content Imports", |
| 117 | + count: contribution.0, |
| 118 | + oldest: contribution.1, |
| 119 | + }, |
| 120 | + QueueCount { |
| 121 | + label: "Metadata Suggestions", |
| 122 | + count: metadata.0, |
| 123 | + oldest: metadata.1, |
| 124 | + }, |
| 125 | + QueueCount { |
| 126 | + label: "Stream Suggestions", |
| 127 | + count: stream.0, |
| 128 | + oldest: stream.1, |
| 129 | + }, |
| 130 | + QueueCount { |
| 131 | + label: "Episode Suggestions", |
| 132 | + count: episode.0, |
| 133 | + oldest: episode.1, |
| 134 | + }, |
| 135 | + QueueCount { |
| 136 | + label: "File Annotation Requests", |
| 137 | + count: annotation.0, |
| 138 | + oldest: annotation.1, |
| 139 | + }, |
| 140 | + ]) |
| 141 | +} |
| 142 | + |
| 143 | +pub fn format_pending_queues_section(queues: &[QueueCount]) -> Vec<String> { |
| 144 | + let total_pending: i64 = queues.iter().map(|q| q.count).sum(); |
| 145 | + if total_pending == 0 { |
| 146 | + return Vec::new(); |
| 147 | + } |
| 148 | + |
| 149 | + let mut lines = vec![ |
| 150 | + "*Pending Moderation*".to_string(), |
| 151 | + format!("*Total Pending*: `{total_pending}`"), |
| 152 | + String::new(), |
| 153 | + ]; |
| 154 | + for item in queues { |
| 155 | + if item.count <= 0 { |
| 156 | + continue; |
| 157 | + } |
| 158 | + let oldest_age = format_pending_age(item.oldest); |
| 159 | + lines.push(format!( |
| 160 | + "- *{}*: `{}` (oldest `{oldest_age}`)", |
| 161 | + item.label, item.count |
| 162 | + )); |
| 163 | + } |
| 164 | + lines |
| 165 | +} |
| 166 | + |
| 167 | +async fn pending_count( |
| 168 | + pool: &sqlx::PgPool, |
| 169 | + sql: &str, |
| 170 | +) -> Result<(i64, Option<DateTime<Utc>>), sqlx::Error> { |
| 171 | + use sqlx::Row; |
| 172 | + let row = sqlx::query(sqlx::AssertSqlSafe(sql)) |
| 173 | + .fetch_one(pool) |
| 174 | + .await?; |
| 175 | + let count: i64 = row.try_get(0)?; |
| 176 | + let oldest: Option<DateTime<Utc>> = row.try_get(1)?; |
| 177 | + Ok((count, oldest)) |
| 178 | +} |
0 commit comments