about summary refs log tree commit diff
path: root/src/micropub/mod.rs
blob: 95595cf50edd983b1b90f4e40593559c611ac675 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use warp::http::StatusCode;
use warp::{Filter, Rejection, reject::InvalidQuery};
use serde_json::{json, Value};
use serde::{Serialize, Deserialize};
use crate::database::{MicropubChannel, Storage};

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
enum QueryType {
    Source,
    Config,
    Channel,
    SyndicateTo
}

#[derive(Serialize, Deserialize)]
struct MicropubQuery {
    q: QueryType,
    url: Option<String>
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum ErrorType {
    InvalidRequest,
    InternalServerError
}

#[derive(Serialize, Deserialize)]
struct MicropubError {
    error: ErrorType,
    error_description: String
}

impl From<MicropubError> for StatusCode {
    fn from(err: MicropubError) -> Self {
        match err.error {
            ErrorType::InvalidRequest => StatusCode::BAD_REQUEST,
            ErrorType::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR
        }
    }
}

impl MicropubError {
    fn new(error: ErrorType, error_description: &str) -> Self {
        Self {
            error,
            error_description: error_description.to_owned()
        }
    }
}

pub fn query<D: Storage>(db: D) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
    warp::get()
        .map(move || db.clone())
        .and(crate::util::require_host())
        .and(warp::query::<MicropubQuery>())
        .then(|db: D, host: warp::host::Authority, query: MicropubQuery| async move {
            match query.q {
                QueryType::Config => {
                    let channels: Vec<MicropubChannel> = match db.get_channels(host.as_str()).await {
                        Ok(chans) => chans,
                        Err(err) => return warp::reply::json(&MicropubError::new(
                            ErrorType::InternalServerError,
                            &format!("Error fetching channels: {}", err)
                        ))
                    };
                    
                    warp::reply::json(json!({
                        "q": [
                            QueryType::Source,
                            QueryType::Config,
                            QueryType::Channel,
                            QueryType::SyndicateTo
                        ],
                        "channels": channels,
                        "_kittybox_authority": host.as_str()
                    }).as_object().unwrap())
                },
                _ => {
                    todo!()
                }
            }
        })
        .recover(|err: Rejection| async move {
            let error = if let Some(_) = err.find::<InvalidQuery>() {
                MicropubError::new(
                    ErrorType::InvalidRequest,
                    "Invalid query parameters sent. Try ?q=config to see what you can do."
                )
            } else {
                log::error!("Unhandled rejection: {:?}", err);
                MicropubError::new(
                    ErrorType::InternalServerError,
                    &format!("Unknown error: {:?}", err)
                )
            };

            Ok(warp::reply::with_status(warp::reply::json(&error), error.into()))
        })
}