aboutsummaryrefslogtreecommitdiffstats
path: root/atuin-server/src/handlers/history.rs
blob: 546e5a29652643214fe8005aa6270f7dbeadb827 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use axum::extract::Query;
use axum::{Extension, Json};
use http::StatusCode;

use crate::database::{Database, Postgres};
use crate::models::{NewHistory, User};
use atuin_common::api::*;

pub async fn count(
    user: User,
    db: Extension<Postgres>,
) -> Result<Json<CountResponse>, ErrorResponseStatus<'static>> {
    match db.count_history(&user).await {
        Ok(count) => Ok(Json(CountResponse { count })),
        Err(_) => Err(ErrorResponse::reply("failed to query history count")
            .with_status(StatusCode::INTERNAL_SERVER_ERROR)),
    }
}

pub async fn list(
    req: Query<SyncHistoryRequest>,
    user: User,
    db: Extension<Postgres>,
) -> Result<Json<SyncHistoryResponse>, ErrorResponseStatus<'static>> {
    let history = db
        .list_history(
            &user,
            req.sync_ts.naive_utc(),
            req.history_ts.naive_utc(),
            &req.host,
        )
        .await;

    if let Err(e) = history {
        error!("failed to load history: {}", e);
        return Err(ErrorResponse::reply("failed to load history")
            .with_status(StatusCode::INTERNAL_SERVER_ERROR));
    }

    let history: Vec<String> = history
        .unwrap()
        .iter()
        .map(|i| i.data.to_string())
        .collect();

    debug!(
        "loaded {} items of history for user {}",
        history.len(),
        user.id
    );

    Ok(Json(SyncHistoryResponse { history }))
}

pub async fn add(
    Json(req): Json<Vec<AddHistoryRequest>>,
    user: User,
    db: Extension<Postgres>,
) -> Result<(), ErrorResponseStatus<'static>> {
    debug!("request to add {} history items", req.len());

    let history: Vec<NewHistory> = req
        .into_iter()
        .map(|h| NewHistory {
            client_id: h.id,
            user_id: user.id,
            hostname: h.hostname,
            timestamp: h.timestamp.naive_utc(),
            data: h.data,
        })
        .collect();

    if let Err(e) = db.add_history(&history).await {
        error!("failed to add history: {}", e);

        return Err(ErrorResponse::reply("failed to add history")
            .with_status(StatusCode::INTERNAL_SERVER_ERROR));
    };

    Ok(())
}