Create and display comments
This commit is contained in:
		
							parent
							
								
									424517fab0
								
							
						
					
					
						commit
						a3d73cb2c4
					
				| @ -89,7 +89,10 @@ fn main() { | ||||
|             routes::posts::activity_details, | ||||
|             routes::posts::new, | ||||
|             routes::posts::new_auth, | ||||
|             routes::posts::create | ||||
|             routes::posts::create, | ||||
|             
 | ||||
|             routes::comments::new, | ||||
|             routes::comments::create | ||||
|         ]) | ||||
|         .manage(init_pool()) | ||||
|         .attach(Template::fairing()) | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| use chrono; | ||||
| use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; | ||||
| 
 | ||||
| use models::users::User; | ||||
| use schema::comments; | ||||
| 
 | ||||
| #[derive(Queryable, Identifiable, Serialize)] | ||||
| @ -43,4 +44,14 @@ impl Comment { | ||||
|             .expect("Error loading comment by id") | ||||
|             .into_iter().nth(0) | ||||
|     } | ||||
| 
 | ||||
|     pub fn for_post(conn: &PgConnection, post_id: i32) -> Vec<Comment> { | ||||
|         comments::table.filter(comments::post_id.eq(post_id)) | ||||
|             .load::<Comment>(conn) | ||||
|             .expect("Error loading comment by post id") | ||||
|     } | ||||
| 
 | ||||
|     pub fn get_author(&self, conn: &PgConnection) -> User { | ||||
|         User::get(conn, self.author_id).unwrap() | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										38
									
								
								src/routes/comments.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/routes/comments.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,38 @@ | ||||
| use rocket::request::Form; | ||||
| use rocket::response::Redirect; | ||||
| use rocket_contrib::Template; | ||||
| 
 | ||||
| use db_conn::DbConn; | ||||
| use models::comments::*; | ||||
| use models::posts::Post; | ||||
| use models::users::User; | ||||
| 
 | ||||
| #[get("/~/<_blog>/<slug>/comment")] | ||||
| fn new(_blog: String, slug: String, _user: User, conn: DbConn) -> Template { | ||||
|     let post = Post::find_by_slug(&*conn, slug).unwrap(); | ||||
|     Template::render("comments/new", json!({ | ||||
|         "post": post | ||||
|     })) | ||||
| } | ||||
| 
 | ||||
| #[derive(FromForm)] | ||||
| struct NewCommentForm { | ||||
|     pub content: String, | ||||
|     pub respond_to: Option<i32> | ||||
| } | ||||
| 
 | ||||
| #[post("/~/<_blog>/<slug>/comment", data = "<data>")] | ||||
| fn create(_blog: String, slug: String, data: Form<NewCommentForm>, user: User, conn: DbConn) -> Redirect { | ||||
|     let post = Post::find_by_slug(&*conn, slug).unwrap(); | ||||
|     let form = data.get(); | ||||
|     Comment::insert(&*conn, NewComment { | ||||
|         content: form.content.clone(), | ||||
|         in_response_to_id: form.respond_to, | ||||
|         post_id: post.id, | ||||
|         author_id: user.id, | ||||
|         ap_url: None, | ||||
|         sensitive: false, | ||||
|         spoiler_text: "".to_string() | ||||
|     }); | ||||
|     Redirect::to("") | ||||
| } | ||||
| @ -1,4 +1,5 @@ | ||||
| pub mod blogs; | ||||
| pub mod comments; | ||||
| pub mod instance; | ||||
| pub mod posts; | ||||
| pub mod session; | ||||
|  | ||||
| @ -2,6 +2,7 @@ use heck::KebabCase; | ||||
| use rocket::request::Form; | ||||
| use rocket::response::Redirect; | ||||
| use rocket_contrib::Template; | ||||
| use serde_json; | ||||
| use std::collections::HashMap; | ||||
| 
 | ||||
| use activity_pub::{context, activity_pub, ActivityPub}; | ||||
| @ -10,6 +11,7 @@ use activity_pub::object::Object; | ||||
| use activity_pub::outbox::broadcast; | ||||
| use db_conn::DbConn; | ||||
| use models::blogs::*; | ||||
| use models::comments::Comment; | ||||
| use models::post_authors::*; | ||||
| use models::posts::*; | ||||
| use models::users::User; | ||||
| @ -19,9 +21,16 @@ use utils; | ||||
| fn details(blog: String, slug: String, conn: DbConn) -> Template { | ||||
|     let blog = Blog::find_by_actor_id(&*conn, blog).unwrap(); | ||||
|     let post = Post::find_by_slug(&*conn, slug).unwrap(); | ||||
|     let comments = Comment::for_post(&*conn, post.id);    
 | ||||
|     Template::render("posts/details", json!({ | ||||
|         "post": post, | ||||
|         "blog": blog | ||||
|         "blog": blog, | ||||
|         "comments": comments.into_iter().map(|c| { | ||||
|             json!({ | ||||
|                 "content": c.content, | ||||
|                 "author": c.get_author(&*conn) | ||||
|             }) | ||||
|         }).collect::<Vec<serde_json::Value>>() | ||||
|     })) | ||||
| } | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										20
									
								
								templates/comments/new.tera
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								templates/comments/new.tera
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,20 @@ | ||||
| {% extends "base" %} | ||||
| 
 | ||||
| {% block title %} | ||||
| Comment "{{ post.title }}" | ||||
| {% endblock title %} | ||||
| 
 | ||||
| {% block content %} | ||||
| <h1>Comment "{{ post.title }}"</h1> | ||||
| <form method="post"> | ||||
|     <label for="content">Content</label> | ||||
|     <textarea name="content"></textarea> | ||||
| 
 | ||||
|     {% if responding_to %} | ||||
|         <input name="respond_to" value="{{ responding_to }}"> | ||||
|     {% endif %} | ||||
| 
 | ||||
|     <input type="submit" value="Submit comment"/>             | ||||
| </form> | ||||
| {% endblock content %} | ||||
| 
 | ||||
| @ -5,11 +5,20 @@ | ||||
| {% endblock title %} | ||||
| 
 | ||||
| {% block content %} | ||||
| <h1>{{ post.title }}</h1> | ||||
| <p>Published in {{ blog.title }}</p> | ||||
| <hr> | ||||
| <p> | ||||
|     {{ post.content | safe }} | ||||
| </p> | ||||
| <p>License: {{ post.license }}</p> | ||||
|     <h1>{{ post.title }}</h1> | ||||
|     <p>Published in {{ blog.title }}</p> | ||||
|     <hr> | ||||
|     <p> | ||||
|         {{ post.content | safe }} | ||||
|     </p> | ||||
|     <p>License: {{ post.license }}</p> | ||||
|     <hr> | ||||
|     <h2>Comments</h2> | ||||
|     {% for comment in comments %} | ||||
|         <div> | ||||
|             <b>{{ comment.author.display_name }}</b> | ||||
|             <div>{{ comment.content | safe }}</div> | ||||
|         </div> | ||||
|     {% endfor %} | ||||
|     <a href="comment">Comment</a> | ||||
| {% endblock content %} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user