summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock4
-rw-r--r--Cargo.toml2
-rw-r--r--src/routes/communities.rs20
-rw-r--r--src/routes/mod.rs71
4 files changed, 89 insertions, 8 deletions
diff --git a/Cargo.lock b/Cargo.lock
index a3d168a..fc4eb1c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -552,7 +552,7 @@ dependencies = [
[[package]]
name = "render"
version = "0.3.1"
-source = "git+https://github.com/vpzomtrrfrt/render.rs?rev=4c6e8c1#4c6e8c1a02cac79034c973e387bf1b836abff00c"
+source = "git+https://github.com/vpzomtrrfrt/render.rs?rev=0604925#060492504f5c8b0b0d9716af724bca2388689697"
dependencies = [
"render_macros",
]
@@ -560,7 +560,7 @@ dependencies = [
[[package]]
name = "render_macros"
version = "0.3.1"
-source = "git+https://github.com/vpzomtrrfrt/render.rs?rev=4c6e8c1#4c6e8c1a02cac79034c973e387bf1b836abff00c"
+source = "git+https://github.com/vpzomtrrfrt/render.rs?rev=0604925#060492504f5c8b0b0d9716af724bca2388689697"
dependencies = [
"proc-macro2",
"quote",
diff --git a/Cargo.toml b/Cargo.toml
index 0dee48d..3a75cb6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,7 +8,7 @@ license = "AGPL-3.0-or-later"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-render = { git = "https://github.com/vpzomtrrfrt/render.rs", rev = "4c6e8c1" }
+render = { git = "https://github.com/vpzomtrrfrt/render.rs", rev = "0604925" }
trout = "0.1.1"
hyper = "0.13.6"
hyper-tls = "0.4.1"
diff --git a/src/routes/communities.rs b/src/routes/communities.rs
index 3337862..45bca1b 100644
--- a/src/routes/communities.rs
+++ b/src/routes/communities.rs
@@ -2,6 +2,7 @@ use crate::routes::{
fetch_base_data, get_cookie_map, get_cookie_map_for_req, html_response, res_to_error,
with_auth, HTPage, PostItem, RespMinimalCommunityInfo, RespPostListPost,
};
+use std::collections::HashMap;
use std::sync::Arc;
async fn page_community(
@@ -131,7 +132,14 @@ async fn page_community_new_post(
</div>
<div>
<label>
- {"URL: "}<input r#type={"text"} name={"href"} required={"true"} />
+ {"URL: "}<input r#type={"text"} name={"href"} />
+ </label>
+ </div>
+ <div>
+ <label>
+ {"Text:"}
+ <br />
+ <textarea name={"content_text"}>{""}</textarea>
</label>
</div>
<div>
@@ -160,8 +168,14 @@ async fn handler_communities_new_post_submit(
let cookies = get_cookie_map(cookies_string)?;
let body = hyper::body::to_bytes(req.into_body()).await?;
- let mut body: serde_json::Value = serde_urlencoded::from_bytes(&body)?;
- body["community"] = community_id.into();
+ let mut body: HashMap<&str, serde_json::Value> = serde_urlencoded::from_bytes(&body)?;
+ body.insert("community", community_id.into());
+ if body.get("content_text").and_then(|x| x.as_str()) == Some("") {
+ body.remove("content_text");
+ }
+ if body.get("href").and_then(|x| x.as_str()) == Some("") {
+ body.remove("href");
+ }
let body = serde_json::to_vec(&body)?;
res_to_error(
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index 9167888..d27409c 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -101,7 +101,8 @@ struct RespMinimalAuthorInfo<'a> {
struct RespPostListPost<'a> {
id: i64,
title: &'a str,
- href: &'a str,
+ href: Option<&'a str>,
+ content_text: Option<&'a str>,
author: Option<RespMinimalAuthorInfo<'a>>,
created: &'a str,
community: RespMinimalCommunityInfo<'a>,
@@ -141,9 +142,10 @@ fn HTPage<'base_data, Children: render::Render>(
#[render::component]
fn PostItem<'post>(post: &'post RespPostListPost<'post>, in_community: bool) {
+ let post_link = format!("/posts/{}", post.id).into();
render::rsx! {
<li>
- <a href={post.href}>
+ <a href={post.href.map(Cow::from).unwrap_or(post_link)}>
{post.title}
</a>
<br />
@@ -387,6 +389,65 @@ async fn handler_new_community_submit(
.body("Successfully created.".into())?)
}
+async fn page_post(
+ params: (i64,),
+ ctx: Arc<crate::RouteContext>,
+ req: hyper::Request<hyper::Body>,
+) -> Result<hyper::Response<hyper::Body>, crate::Error> {
+ let (post_id,) = params;
+
+ let cookies = get_cookie_map_for_req(&req)?;
+
+ let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, &cookies).await?;
+
+ let api_res = res_to_error(
+ ctx.http_client
+ .request(with_auth(
+ hyper::Request::get(format!(
+ "{}/api/unstable/posts/{}",
+ ctx.backend_host, post_id
+ ))
+ .body(Default::default())?,
+ &cookies,
+ )?)
+ .await?,
+ )
+ .await?;
+ let api_res = hyper::body::to_bytes(api_res.into_body()).await?;
+
+ let post: RespPostListPost = serde_json::from_slice(&api_res)?;
+
+ Ok(html_response(render::html! {
+ <HTPage base_data={&base_data}>
+ <h1>{post.title}</h1>
+ <p>
+ {"Submitted by "}<UserLink user={post.author.as_ref()} />
+ {" to "}<CommunityLink community={&post.community} />
+ </p>
+ {
+ match post.href {
+ None => None,
+ Some(href) => {
+ Some(render::rsx! {
+ <p><a href={href}>{href}</a></p>
+ })
+ }
+ }
+ }
+ {
+ match post.content_text {
+ None => None,
+ Some(content_text) => {
+ Some(render::rsx! {
+ <p>{content_text}</p>
+ })
+ }
+ }
+ }
+ </HTPage>
+ }))
+}
+
async fn page_signup(
_: (),
ctx: Arc<crate::RouteContext>,
@@ -515,6 +576,12 @@ pub fn route_root() -> crate::RouteNode<()> {
),
)
.with_child(
+ "posts",
+ crate::RouteNode::new().with_child_parse::<i64, _>(
+ crate::RouteNode::new().with_handler_async("GET", page_post),
+ ),
+ )
+ .with_child(
"signup",
crate::RouteNode::new()
.with_handler_async("GET", page_signup)