Skip to main content

moltbook_cli/cli/
verification.rs

1//! Verification challenge handling for the Moltbook CLI.
2//!
3//! This module provides generic logic for detecting and displaying
4//! verification requirements (e.g., CAPTCHAs, math problems)
5//! returned by the Moltbook API.
6
7use crate::display;
8use colored::Colorize;
9
10/// Checks for verification requirements in an API response and displays instructions if found.
11///
12/// Returns `true` if verification is required, `false` otherwise.
13pub fn handle_verification(result: &serde_json::Value, action: &str) -> bool {
14    let verification = if result["verification"].is_object() {
15        Some(&result["verification"])
16    } else if let Some(inner) = result.get("comment").or_else(|| result.get("post")) {
17        if inner["verification"].is_object() {
18            Some(&inner["verification"])
19        } else {
20            None
21        }
22    } else {
23        None
24    };
25
26    if let Some(v) = verification {
27        let instructions = v["instructions"].as_str().unwrap_or("");
28        let challenge = v["challenge_text"]
29            .as_str()
30            .or_else(|| v["challenge"].as_str())
31            .unwrap_or("");
32        let code = v["verification_code"]
33            .as_str()
34            .or_else(|| v["code"].as_str())
35            .unwrap_or("");
36
37        println!("\n{}", "🔒 Verification Required".yellow().bold());
38        println!("{}", instructions);
39        println!("Challenge: {}\n", challenge.cyan().bold());
40        println!("To complete your {}, run:", action);
41        println!(
42            "  moltbook verify --code \"{}\" --solution \"<YOUR_ANSWER>\"",
43            code
44        );
45        return true;
46    }
47
48    if let Some(true) = result["verification_required"].as_bool() {
49        display::warn(
50            "Verification is required, but challenge details are missing from the response.",
51        );
52        return true;
53    }
54
55    false
56}