diff options
author | Colin Reeder <colin@vpzom.click> | 2020-08-01 15:27:29 -0600 |
---|---|---|
committer | Colin Reeder <colin@vpzom.click> | 2020-08-01 15:29:24 -0600 |
commit | 30eb3636cc7ccc968f4244eef7c39a4cf0a317ff (patch) | |
tree | 14d54c45ca85651529e6a4f92e753588a8298e84 /src | |
parent | e2e37561e002fd7dbf9087a8721198ca335d33d4 (diff) |
Add Preview feature for posts (#78)
Diffstat (limited to 'src')
-rw-r--r-- | src/resp_types.rs | 5 | ||||
-rw-r--r-- | src/routes/communities.rs | 71 |
2 files changed, 74 insertions, 2 deletions
diff --git a/src/resp_types.rs b/src/resp_types.rs index b51b26c..96b1626 100644 --- a/src/resp_types.rs +++ b/src/resp_types.rs @@ -228,6 +228,11 @@ pub struct JustUser<'a> { } #[derive(Deserialize, Debug)] +pub struct JustContentHTML<'a> { + pub content_html: Cow<'a, str>, +} + +#[derive(Deserialize, Debug)] pub struct RespList<'a, T: std::fmt::Debug + 'a> { pub items: Vec<T>, pub next_page: Option<Cow<'a, str>>, diff --git a/src/routes/communities.rs b/src/routes/communities.rs index 4ffefce..fc2bb07 100644 --- a/src/routes/communities.rs +++ b/src/routes/communities.rs @@ -1,6 +1,7 @@ use crate::components::{CommunityLink, HTPage, MaybeFillInput, MaybeFillTextArea, PostItem}; use crate::resp_types::{ - RespCommunityInfoMaybeYour, RespMinimalCommunityInfo, RespPostListPost, RespYourFollow, + JustContentHTML, RespCommunityInfoMaybeYour, RespMinimalCommunityInfo, RespPostListPost, + RespYourFollow, }; use crate::routes::{ fetch_base_data, for_client, get_cookie_map_for_headers, get_cookie_map_for_req, html_response, @@ -516,7 +517,8 @@ async fn page_community_new_post( let cookies = get_cookie_map_for_req(&req)?; - page_community_new_post_inner(community_id, req.headers(), &cookies, ctx, None, None).await + page_community_new_post_inner(community_id, req.headers(), &cookies, ctx, None, None, None) + .await } async fn page_community_new_post_inner( @@ -526,6 +528,7 @@ async fn page_community_new_post_inner( ctx: Arc<crate::RouteContext>, display_error: Option<String>, prev_values: Option<&HashMap<&str, serde_json::Value>>, + display_preview: Option<&str>, ) -> Result<hyper::Response<hyper::Body>, crate::Error> { let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, headers, &cookies).await?; let lang = crate::get_lang_for_headers(headers); @@ -534,6 +537,8 @@ async fn page_community_new_post_inner( let title = lang.tr("post_new", None); + let display_preview = display_preview.map(|x| ammonia::clean(&x)); + Ok(html_response(render::html! { <HTPage base_data={&base_data} lang={&lang} title={&title}> <h1>{title.as_ref()}</h1> @@ -570,8 +575,16 @@ async fn page_community_new_post_inner( </label> <div> <button r#type={"submit"}>{lang.tr("submit", None)}</button> + <button r#type={"submit"} name={"preview"}>{lang.tr("preview", None)}</button> </div> </form> + { + display_preview.as_deref().map(|html| { + render::rsx! { + <div class={"preview"}>{render::raw!(html)}</div> + } + }) + } </HTPage> })) } @@ -588,6 +601,59 @@ async fn handler_communities_new_post_submit( let body = hyper::body::to_bytes(body).await?; let mut body: HashMap<&str, serde_json::Value> = serde_urlencoded::from_bytes(&body)?; + if body.contains_key("preview") { + let md = body + .get("content_markdown") + .and_then(|x| x.as_str()) + .unwrap_or(""); + let preview_res = res_to_error( + ctx.http_client + .request(for_client( + hyper::Request::post(format!( + "{}/api/unstable/misc/render_markdown", + ctx.backend_host + )) + .body( + serde_json::to_vec(&serde_json::json!({ "content_markdown": md }))?.into(), + )?, + &req_parts.headers, + &cookies, + )?) + .await?, + ) + .await; + return match preview_res { + Ok(preview_res) => { + let preview_res = hyper::body::to_bytes(preview_res.into_body()).await?; + let preview_res: JustContentHTML = serde_json::from_slice(&preview_res)?; + + page_community_new_post_inner( + community_id, + &req_parts.headers, + &cookies, + ctx, + None, + Some(&body), + Some(&preview_res.content_html), + ) + .await + } + Err(crate::Error::RemoteError((_, message))) => { + page_community_new_post_inner( + community_id, + &req_parts.headers, + &cookies, + ctx, + Some(message), + Some(&body), + None, + ) + .await + } + Err(other) => Err(other), + }; + } + body.insert("community", community_id.into()); if body.get("content_markdown").and_then(|x| x.as_str()) == Some("") { body.remove("content_markdown"); @@ -631,6 +697,7 @@ async fn handler_communities_new_post_submit( ctx, Some(message), Some(&body), + None, ) .await } |