From 7b3d894e96e4c815af751609d5dd25d90e9813a0 Mon Sep 17 00:00:00 2001 From: xfy Date: Thu, 11 Jun 2026 14:18:49 +0800 Subject: [PATCH] feat(api): add CheckPendingStatus server function Accepts Vec of comment IDs, returns their current status. IDs not found in DB return status 'gone'. Empty vec returns early. Used by client to prune localStorage pending comments that are no longer pending (approved/spam/trash/deleted). --- src/api/comments/check.rs | 47 +++++++++++++++++++++++++++++++++++++++ src/api/comments/mod.rs | 2 ++ 2 files changed, 49 insertions(+) create mode 100644 src/api/comments/check.rs diff --git a/src/api/comments/check.rs b/src/api/comments/check.rs new file mode 100644 index 0000000..63425e9 --- /dev/null +++ b/src/api/comments/check.rs @@ -0,0 +1,47 @@ +use dioxus::prelude::*; + +#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +pub struct PendingStatusItem { + pub id: i64, + pub status: String, +} + +#[server(CheckPendingStatus, "/api")] +pub async fn check_pending_status(ids: Vec) -> Result, ServerFnError> { + #[cfg(feature = "server")] + { + use crate::db::pool::get_conn; + use crate::api::error::AppError; + + if ids.is_empty() { + return Ok(vec![]); + } + + let client = get_conn().await.map_err(AppError::db_conn)?; + + let rows = client + .query( + "SELECT id, status FROM comments WHERE id = ANY($1)", + &[&ids], + ) + .await + .map_err(AppError::query)?; + + let found: std::collections::HashMap = rows + .iter() + .map(|r| (r.get::<_, i64>(0), r.get::<_, String>(1))) + .collect(); + + let result: Vec = ids + .into_iter() + .map(|id| { + let status = found.get(&id).cloned().unwrap_or_else(|| "gone".to_string()); + PendingStatusItem { id, status } + }) + .collect(); + + Ok(result) + } + #[cfg(not(feature = "server"))] + unreachable!() +} diff --git a/src/api/comments/mod.rs b/src/api/comments/mod.rs index 6a91202..42730fc 100644 --- a/src/api/comments/mod.rs +++ b/src/api/comments/mod.rs @@ -7,12 +7,14 @@ mod create; mod read; mod update; mod list; +mod check; pub use types::*; pub use create::create_comment; pub use read::{get_comments, get_comment_count}; pub use update::{approve_comment, spam_comment, trash_comment, batch_update_comment_status}; pub use list::{get_pending_comments, get_pending_count, get_all_comments}; +pub use check::check_pending_status; #[cfg(feature = "server")] pub use markdown::render_comment_markdown;