2020-06-25 18:49:20 +00:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
2020-06-28 10:01:18 +00:00
|
|
|
use diesel::result::Error;
|
2020-09-26 12:07:32 +00:00
|
|
|
use rocket::{http::{Cookies, Status}, response::status, Rocket};
|
2020-09-26 11:34:53 +00:00
|
|
|
use rocket::response::status::Custom;
|
2020-06-25 12:42:36 +00:00
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
use serde_json::Value;
|
2020-06-25 18:49:20 +00:00
|
|
|
|
2021-07-29 06:04:00 +00:00
|
|
|
use crate::{api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}, utils::domain::get_domain};
|
2020-07-01 17:55:27 +00:00
|
|
|
use crate::api::guards::db::DbConn;
|
2020-09-26 14:29:32 +00:00
|
|
|
use crate::core::paste::{entity::Paste, service::{create_paste, fetch_paste, update_paste}};
|
2020-09-26 11:34:53 +00:00
|
|
|
use crate::core::users::service::{create_or_fetch_user, fetch_user};
|
2020-06-26 02:44:19 +00:00
|
|
|
use crate::utils::phonetic_key;
|
2020-07-01 17:55:27 +00:00
|
|
|
use crate::utils::users::get_session_id;
|
2020-09-26 14:29:32 +00:00
|
|
|
use crate::core::users::entity::User;
|
2020-06-26 02:44:19 +00:00
|
|
|
|
2020-06-25 12:42:36 +00:00
|
|
|
#[post("/", data = "<paste>")]
|
2020-07-01 17:55:27 +00:00
|
|
|
fn create(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
|
2020-06-25 12:42:36 +00:00
|
|
|
// Check if frontend sent a session cookie
|
2020-07-01 17:55:27 +00:00
|
|
|
let user_id = get_session_id(&mut ck);
|
2020-06-25 12:42:36 +00:00
|
|
|
|
2020-06-25 18:49:20 +00:00
|
|
|
// Create or fetch already existing user
|
|
|
|
let user = match create_or_fetch_user(user_id, &conn) {
|
|
|
|
Ok(user) => user,
|
2020-07-01 17:55:27 +00:00
|
|
|
Err(_) => {
|
|
|
|
return internal_server_error();
|
2020-06-26 11:14:07 +00:00
|
|
|
}
|
2020-06-25 18:49:20 +00:00
|
|
|
};
|
|
|
|
|
2020-06-25 12:42:36 +00:00
|
|
|
let new_paste = paste.deref_mut();
|
|
|
|
if new_paste.id.is_none() {
|
|
|
|
new_paste.id = Some(phonetic_key::get_random_id());
|
|
|
|
}
|
|
|
|
|
2020-06-25 18:49:20 +00:00
|
|
|
new_paste.belongs_to = Some(user.id);
|
|
|
|
|
2020-06-25 12:42:36 +00:00
|
|
|
match create_paste(new_paste, &conn) {
|
2020-06-26 11:14:07 +00:00
|
|
|
Ok(_) => status::Custom(
|
|
|
|
Status::Created,
|
|
|
|
Json(json!({
|
|
|
|
"msg": "Successfully created paste",
|
|
|
|
"paste_id": new_paste.id
|
|
|
|
})),
|
|
|
|
),
|
2020-07-01 17:55:27 +00:00
|
|
|
Err(_) => internal_server_error(),
|
2020-06-25 12:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 19:23:39 +00:00
|
|
|
#[get("/<id>")]
|
2020-09-26 11:34:53 +00:00
|
|
|
fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
|
|
|
|
let user_id = get_session_id(&mut ck);
|
|
|
|
|
|
|
|
let r_user = fetch_user(user_id, &conn);
|
|
|
|
let user = match r_user {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(e) => {
|
2020-09-26 14:29:32 +00:00
|
|
|
match e.downcast_ref::<Error>() {
|
|
|
|
Some(Error::NotFound) => User::new(),
|
|
|
|
_ => return internal_server_error(),
|
|
|
|
}
|
2020-09-26 11:34:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-25 19:23:39 +00:00
|
|
|
let paste = match fetch_paste(id, &conn) {
|
|
|
|
Ok(paste) => paste,
|
2020-07-01 17:55:27 +00:00
|
|
|
Err(e) => {
|
|
|
|
return match e.downcast_ref::<Error>() {
|
|
|
|
Some(Error::NotFound) => not_found(),
|
|
|
|
_ => internal_server_error(),
|
|
|
|
};
|
2020-06-25 19:23:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-26 11:34:53 +00:00
|
|
|
let belongs_to = paste.belongs_to.as_ref().unwrap();
|
|
|
|
|
|
|
|
return if user.id == *belongs_to {
|
|
|
|
Custom(Status::Ok, Json(json!({
|
|
|
|
"id": paste.id,
|
|
|
|
"belongs_to": *belongs_to,
|
|
|
|
"is_url": paste.is_url.unwrap(),
|
|
|
|
"content": paste.content,
|
|
|
|
"is_owner": true
|
|
|
|
})))
|
|
|
|
} else {
|
|
|
|
Custom(Status::Ok, Json(json!(paste)))
|
|
|
|
};
|
2020-06-25 19:23:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 12:07:32 +00:00
|
|
|
#[patch("/", data = "<paste>")]
|
|
|
|
fn update(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<Value>> {
|
|
|
|
// Check if frontend sent a session cookie
|
|
|
|
let user_id = get_session_id(&mut ck);
|
|
|
|
|
|
|
|
// Create or fetch already existing user
|
|
|
|
let user = match fetch_user(user_id, &conn) {
|
|
|
|
Ok(user) => user,
|
|
|
|
Err(_) => {
|
|
|
|
return not_found();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_paste = paste.deref_mut();
|
|
|
|
|
|
|
|
if new_paste.id.is_none() {
|
|
|
|
return not_found();
|
|
|
|
}
|
|
|
|
|
|
|
|
new_paste.belongs_to = match fetch_paste(new_paste.id.as_ref().unwrap().clone(), &conn) {
|
|
|
|
Ok(paste) => paste.belongs_to,
|
|
|
|
Err(_) => return internal_server_error()
|
|
|
|
};
|
|
|
|
|
|
|
|
if new_paste.belongs_to.is_some() {
|
|
|
|
if *new_paste.belongs_to.as_ref().unwrap() == user.id {
|
|
|
|
match update_paste(new_paste, &conn) {
|
|
|
|
Ok(_) => status::Custom(
|
|
|
|
Status::Created,
|
|
|
|
Json(json!({
|
|
|
|
"msg": "Successfully created paste",
|
|
|
|
"paste_id": new_paste.id
|
|
|
|
})),
|
|
|
|
),
|
|
|
|
Err(_) => internal_server_error(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
forbidden()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unprocessable_entity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 06:04:00 +00:00
|
|
|
#[post("/", data = "<input>")]
|
|
|
|
fn anonymous(input: String, conn: DbConn) -> String {
|
|
|
|
let mut new_paste = Paste {
|
|
|
|
id: Some(phonetic_key::get_random_id()),
|
|
|
|
belongs_to: None,
|
|
|
|
content: input,
|
|
|
|
is_url: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
dbg!(new_paste.id.clone());
|
|
|
|
|
|
|
|
match create_paste(&mut new_paste, &conn) {
|
|
|
|
Ok(_) => format!("{}/{}", get_domain(), new_paste.id.unwrap()),
|
|
|
|
Err(_) => String::from("Internal Server Error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:42:36 +00:00
|
|
|
pub fn fuel(rocket: Rocket) -> Rocket {
|
2021-07-29 06:04:00 +00:00
|
|
|
rocket
|
|
|
|
.mount("/api/paste", routes![create, fetch, update])
|
|
|
|
.mount("/", routes![anonymous])
|
2020-06-26 11:14:07 +00:00
|
|
|
}
|