Skip to main content

tuitbot_server/
state.rs

1//! Shared application state for the tuitbot server.
2
3use std::collections::HashMap;
4use std::net::IpAddr;
5use std::path::PathBuf;
6use std::sync::Arc;
7use std::time::{Instant, SystemTime};
8
9use tokio::sync::{broadcast, Mutex, RwLock};
10use tokio_util::sync::CancellationToken;
11use tuitbot_core::automation::circuit_breaker::CircuitBreaker;
12use tuitbot_core::automation::Runtime;
13use tuitbot_core::config::{ConnectorConfig, ContentSourcesConfig, DeploymentMode};
14use tuitbot_core::content::ContentGenerator;
15use tuitbot_core::storage::DbPool;
16
17use crate::ws::WsEvent;
18
19/// Pending OAuth PKCE state for connector link flows.
20pub struct PendingOAuth {
21    /// The PKCE code verifier needed to complete the token exchange.
22    pub code_verifier: String,
23    /// When this entry was created (for 10-minute expiry).
24    pub created_at: Instant,
25}
26
27/// Shared application state accessible by all route handlers.
28pub struct AppState {
29    /// SQLite connection pool.
30    pub db: DbPool,
31    /// Path to the configuration file.
32    pub config_path: PathBuf,
33    /// Data directory for media storage (parent of config file).
34    pub data_dir: PathBuf,
35    /// Broadcast channel sender for real-time WebSocket events.
36    pub event_tx: broadcast::Sender<WsEvent>,
37    /// Local bearer token for API authentication.
38    pub api_token: String,
39    /// Bcrypt hash of the web login passphrase (None if not configured).
40    pub passphrase_hash: RwLock<Option<String>>,
41    /// Last-observed mtime of the `passphrase_hash` file (for detecting out-of-band resets).
42    pub passphrase_hash_mtime: RwLock<Option<SystemTime>>,
43    /// Host address the server is bound to.
44    pub bind_host: String,
45    /// Port the server is listening on.
46    pub bind_port: u16,
47    /// Per-IP login attempt tracking for rate limiting: (count, window_start).
48    pub login_attempts: Mutex<HashMap<IpAddr, (u32, Instant)>>,
49    /// Per-account automation runtimes (keyed by account_id).
50    pub runtimes: Mutex<HashMap<String, Runtime>>,
51    /// Per-account content generators for AI assist endpoints.
52    pub content_generators: Mutex<HashMap<String, Arc<ContentGenerator>>>,
53    /// Optional circuit breaker for X API rate-limit protection.
54    pub circuit_breaker: Option<Arc<CircuitBreaker>>,
55    /// Cancellation token for the Watchtower filesystem watcher (None if not running).
56    pub watchtower_cancel: Option<CancellationToken>,
57    /// Content sources configuration for the Watchtower.
58    pub content_sources: ContentSourcesConfig,
59    /// Connector configuration for remote source OAuth flows.
60    pub connector_config: ConnectorConfig,
61    /// Deployment mode (desktop, self_host, or cloud).
62    pub deployment_mode: DeploymentMode,
63    /// Provider backend ("", "x_api", or "scraper").
64    pub provider_backend: String,
65    /// Pending OAuth PKCE challenges keyed by state parameter.
66    pub pending_oauth: Mutex<HashMap<String, PendingOAuth>>,
67}