paste: return an extra parameter if the current user is the owner
Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
parent
28fe55b8db
commit
e001d87f3e
@ -1,19 +1,19 @@
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use diesel::result::Error;
|
||||
use rocket::response::status::Custom;
|
||||
use rocket::{
|
||||
http::{Cookies, Status},
|
||||
response::status,
|
||||
Rocket,
|
||||
};
|
||||
use rocket::response::status::Custom;
|
||||
use rocket_contrib::json::Json;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::api::catchers::{internal_server_error, not_found};
|
||||
use crate::api::guards::db::DbConn;
|
||||
use crate::core::paste::{entity::Paste, service::create_paste, service::fetch_paste};
|
||||
use crate::core::users::service::create_or_fetch_user;
|
||||
use crate::core::users::service::{create_or_fetch_user, fetch_user};
|
||||
use crate::utils::phonetic_key;
|
||||
use crate::utils::users::get_session_id;
|
||||
|
||||
@ -50,7 +50,20 @@ fn create(mut paste: Json<Paste>, conn: DbConn, mut ck: Cookies) -> Custom<Json<
|
||||
}
|
||||
|
||||
#[get("/<id>")]
|
||||
fn fetch(id: String, conn: DbConn) -> Custom<Json<Value>> {
|
||||
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) => {
|
||||
return match e.downcast_ref::<Error>() {
|
||||
Some(Error::NotFound) => not_found(),
|
||||
_ => internal_server_error(),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
let paste = match fetch_paste(id, &conn) {
|
||||
Ok(paste) => paste,
|
||||
Err(e) => {
|
||||
@ -61,7 +74,19 @@ fn fetch(id: String, conn: DbConn) -> Custom<Json<Value>> {
|
||||
}
|
||||
};
|
||||
|
||||
Custom(Status::Ok, Json(json!(paste)))
|
||||
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)))
|
||||
};
|
||||
}
|
||||
|
||||
pub fn fuel(rocket: Rocket) -> Rocket {
|
||||
|
Loading…
Reference in New Issue
Block a user