3 use crate::schema::{comment, comment_like, comment_saved};
5 // WITH RECURSIVE MyTree AS (
6 // SELECT * FROM comment WHERE parent_id IS NULL
8 // SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
10 // SELECT * FROM MyTree;
12 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
14 #[table_name = "comment"]
19 pub parent_id: Option<i32>,
23 pub published: chrono::NaiveDateTime,
24 pub updated: Option<chrono::NaiveDateTime>,
28 #[derive(Insertable, AsChangeset, Clone)]
29 #[table_name = "comment"]
30 pub struct CommentForm {
33 pub parent_id: Option<i32>,
35 pub removed: Option<bool>,
36 pub read: Option<bool>,
37 pub updated: Option<chrono::NaiveDateTime>,
38 pub deleted: Option<bool>,
41 impl Crud<CommentForm> for Comment {
42 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
43 use crate::schema::comment::dsl::*;
44 comment.find(comment_id).first::<Self>(conn)
47 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
48 use crate::schema::comment::dsl::*;
49 diesel::delete(comment.find(comment_id)).execute(conn)
52 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
53 use crate::schema::comment::dsl::*;
56 .get_result::<Self>(conn)
62 comment_form: &CommentForm,
63 ) -> Result<Self, Error> {
64 use crate::schema::comment::dsl::*;
65 diesel::update(comment.find(comment_id))
67 .get_result::<Self>(conn)
71 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
72 #[belongs_to(Comment)]
73 #[table_name = "comment_like"]
74 pub struct CommentLike {
80 pub published: chrono::NaiveDateTime,
83 #[derive(Insertable, AsChangeset, Clone)]
84 #[table_name = "comment_like"]
85 pub struct CommentLikeForm {
92 impl Likeable<CommentLikeForm> for CommentLike {
93 fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> {
94 use crate::schema::comment_like::dsl::*;
96 .filter(comment_id.eq(comment_id_from))
100 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
101 use crate::schema::comment_like::dsl::*;
102 insert_into(comment_like)
103 .values(comment_like_form)
104 .get_result::<Self>(conn)
106 fn remove(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<usize, Error> {
107 use crate::schema::comment_like::dsl::*;
110 .filter(comment_id.eq(comment_like_form.comment_id))
111 .filter(user_id.eq(comment_like_form.user_id)),
118 pub fn from_post(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
119 use crate::schema::comment_like::dsl::*;
121 .filter(post_id.eq(post_id_from))
126 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
127 #[belongs_to(Comment)]
128 #[table_name = "comment_saved"]
129 pub struct CommentSaved {
133 pub published: chrono::NaiveDateTime,
136 #[derive(Insertable, AsChangeset, Clone)]
137 #[table_name = "comment_saved"]
138 pub struct CommentSavedForm {
143 impl Saveable<CommentSavedForm> for CommentSaved {
144 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
145 use crate::schema::comment_saved::dsl::*;
146 insert_into(comment_saved)
147 .values(comment_saved_form)
148 .get_result::<Self>(conn)
150 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
151 use crate::schema::comment_saved::dsl::*;
154 .filter(comment_id.eq(comment_saved_form.comment_id))
155 .filter(user_id.eq(comment_saved_form.user_id)),
163 use super::super::community::*;
164 use super::super::post::*;
165 use super::super::user::*;
169 let conn = establish_unpooled_connection();
171 let new_user = UserForm {
172 name: "terry".into(),
173 fedi_name: "rrf".into(),
174 preferred_username: None,
175 password_encrypted: "nope".into(),
177 matrix_user_id: None,
183 theme: "darkly".into(),
184 default_sort_type: SortType::Hot as i16,
185 default_listing_type: ListingType::Subscribed as i16,
186 lang: "browser".into(),
188 send_notifications_to_email: false,
191 let inserted_user = User_::create(&conn, &new_user).unwrap();
193 let new_community = CommunityForm {
194 name: "test community".to_string(),
195 title: "nada".to_owned(),
198 creator_id: inserted_user.id,
205 let inserted_community = Community::create(&conn, &new_community).unwrap();
207 let new_post = PostForm {
208 name: "A test post".into(),
209 creator_id: inserted_user.id,
212 community_id: inserted_community.id,
221 let inserted_post = Post::create(&conn, &new_post).unwrap();
223 let comment_form = CommentForm {
224 content: "A test comment".into(),
225 creator_id: inserted_user.id,
226 post_id: inserted_post.id,
234 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
236 let expected_comment = Comment {
237 id: inserted_comment.id,
238 content: "A test comment".into(),
239 creator_id: inserted_user.id,
240 post_id: inserted_post.id,
245 published: inserted_comment.published,
249 let child_comment_form = CommentForm {
250 content: "A child comment".into(),
251 creator_id: inserted_user.id,
252 post_id: inserted_post.id,
253 parent_id: Some(inserted_comment.id),
260 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
263 let comment_like_form = CommentLikeForm {
264 comment_id: inserted_comment.id,
265 post_id: inserted_post.id,
266 user_id: inserted_user.id,
270 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
272 let expected_comment_like = CommentLike {
273 id: inserted_comment_like.id,
274 comment_id: inserted_comment.id,
275 post_id: inserted_post.id,
276 user_id: inserted_user.id,
277 published: inserted_comment_like.published,
282 let comment_saved_form = CommentSavedForm {
283 comment_id: inserted_comment.id,
284 user_id: inserted_user.id,
287 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
289 let expected_comment_saved = CommentSaved {
290 id: inserted_comment_saved.id,
291 comment_id: inserted_comment.id,
292 user_id: inserted_user.id,
293 published: inserted_comment_saved.published,
296 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
297 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
298 let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
299 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
300 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
301 Comment::delete(&conn, inserted_child_comment.id).unwrap();
302 Post::delete(&conn, inserted_post.id).unwrap();
303 Community::delete(&conn, inserted_community.id).unwrap();
304 User_::delete(&conn, inserted_user.id).unwrap();
306 assert_eq!(expected_comment, read_comment);
307 assert_eq!(expected_comment, inserted_comment);
308 assert_eq!(expected_comment, updated_comment);
309 assert_eq!(expected_comment_like, inserted_comment_like);
310 assert_eq!(expected_comment_saved, inserted_comment_saved);
313 inserted_child_comment.parent_id.unwrap()
315 assert_eq!(1, like_removed);
316 assert_eq!(1, saved_removed);
317 assert_eq!(1, num_deleted);