add: post drafts
fix: allow question sender to view question
This commit is contained in:
parent
24162573ee
commit
f6cbeb9bd8
22 changed files with 642 additions and 100 deletions
75
crates/app/src/routes/api/v1/communities/drafts.rs
Normal file
75
crates/app/src/routes/api/v1/communities/drafts.rs
Normal file
|
@ -0,0 +1,75 @@
|
|||
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::{communities::PostDraft, ApiReturn, Error};
|
||||
use crate::{
|
||||
get_user_from_token,
|
||||
routes::api::v1::{CreatePostDraft, UpdatePostContent},
|
||||
State,
|
||||
};
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Json(req): Json<CreatePostDraft>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data
|
||||
.create_draft(PostDraft::new(req.content, user.id))
|
||||
.await
|
||||
{
|
||||
Ok(id) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Draft created".to_string(),
|
||||
payload: Some(id.to_string()),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data.delete_draft(id, user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Draft deleted".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_content_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Json(req): Json<UpdatePostContent>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data.update_draft_content(id, user, req.content).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Draft updated".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
pub mod communities;
|
||||
pub mod drafts;
|
||||
pub mod emojis;
|
||||
pub mod images;
|
||||
pub mod posts;
|
||||
|
|
|
@ -113,6 +113,13 @@ pub fn routes() -> Router {
|
|||
"/posts/{id}/context",
|
||||
post(communities::posts::update_context_request),
|
||||
)
|
||||
// drafts
|
||||
.route("/drafts", post(communities::drafts::create_request))
|
||||
.route("/drafts/{id}", delete(communities::drafts::delete_request))
|
||||
.route(
|
||||
"/drafts/{id}/content",
|
||||
post(communities::drafts::update_content_request),
|
||||
)
|
||||
// questions
|
||||
.route("/questions", post(communities::questions::create_request))
|
||||
.route(
|
||||
|
@ -581,3 +588,8 @@ pub struct AddOrRemoveStackUser {
|
|||
pub struct UpdateEmojiName {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreatePostDraft {
|
||||
pub content: String,
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ use axum::{
|
|||
response::{Html, IntoResponse},
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use serde::Deserialize;
|
||||
use tera::Context;
|
||||
use tetratto_core::model::{
|
||||
auth::User,
|
||||
|
@ -236,10 +237,19 @@ pub async fn search_request(
|
|||
))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreatePostProps {
|
||||
#[serde(default)]
|
||||
pub community: usize,
|
||||
#[serde(default)]
|
||||
pub from_draft: usize,
|
||||
}
|
||||
|
||||
/// `/communities/intents/post`
|
||||
pub async fn create_post_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Query(props): Query<CreatePostProps>,
|
||||
) -> impl IntoResponse {
|
||||
let data = data.read().await;
|
||||
let user = match get_user_from_token!(jar, data.0) {
|
||||
|
@ -271,9 +281,32 @@ pub async fn create_post_request(
|
|||
communities.push(community)
|
||||
}
|
||||
|
||||
// get draft
|
||||
let draft = if props.from_draft != 0 {
|
||||
match data.0.get_draft_by_id(props.from_draft).await {
|
||||
Ok(d) => {
|
||||
// drafts can only be used by their owner
|
||||
if d.owner == user.id { Some(d) } else { None }
|
||||
}
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let drafts = match data.0.get_drafts_by_user_all(user.id).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
};
|
||||
|
||||
// ...
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
|
||||
|
||||
context.insert("draft", &draft);
|
||||
context.insert("drafts", &drafts);
|
||||
context.insert("communities", &communities);
|
||||
context.insert("selected_community", &props.community);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
|
@ -1118,10 +1151,16 @@ pub async fn question_request(
|
|||
false
|
||||
};
|
||||
|
||||
let is_sender = if let Some(ref ua) = user {
|
||||
ua.id == question.owner
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
// check permissions
|
||||
let (can_read, _) = check_permissions!(community, jar, data, user);
|
||||
|
||||
if !can_read {
|
||||
if !can_read && !is_sender {
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||
));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue