Skip to main content

tuitbot_server/routes/content/
list.rs

1//! List endpoints for tweets and threads.
2
3use std::sync::Arc;
4
5use axum::extract::{Query, State};
6use axum::Json;
7use serde::Deserialize;
8use serde_json::{json, Value};
9use tuitbot_core::storage::threads;
10
11use crate::account::AccountContext;
12use crate::error::ApiError;
13use crate::state::AppState;
14
15/// Query parameters for the tweets endpoint.
16#[derive(Deserialize)]
17pub struct TweetsQuery {
18    /// Maximum number of tweets to return (default: 50).
19    #[serde(default = "default_tweet_limit")]
20    pub limit: u32,
21}
22
23fn default_tweet_limit() -> u32 {
24    50
25}
26
27/// Query parameters for the threads endpoint.
28#[derive(Deserialize)]
29pub struct ThreadsQuery {
30    /// Maximum number of threads to return (default: 20).
31    #[serde(default = "default_thread_limit")]
32    pub limit: u32,
33}
34
35fn default_thread_limit() -> u32 {
36    20
37}
38
39/// `GET /api/content/tweets` — recent original tweets posted.
40pub async fn list_tweets(
41    State(state): State<Arc<AppState>>,
42    ctx: AccountContext,
43    Query(params): Query<TweetsQuery>,
44) -> Result<Json<Value>, ApiError> {
45    let tweets =
46        threads::get_recent_original_tweets_for(&state.db, &ctx.account_id, params.limit).await?;
47    Ok(Json(json!(tweets)))
48}
49
50/// `GET /api/content/threads` — recent threads posted.
51pub async fn list_threads(
52    State(state): State<Arc<AppState>>,
53    ctx: AccountContext,
54    Query(params): Query<ThreadsQuery>,
55) -> Result<Json<Value>, ApiError> {
56    let threads = threads::get_recent_threads_for(&state.db, &ctx.account_id, params.limit).await?;
57    Ok(Json(json!(threads)))
58}