3 use crate::apub::{make_apub_endpoint, EndpointType};
5 use crate::schema::{comment, comment_like, comment_saved};
7 // WITH RECURSIVE MyTree AS (
8 // SELECT * FROM comment WHERE parent_id IS NULL
10 // SELECT m.* FROM comment AS m JOIN MyTree AS t ON m.parent_id = t.id
12 // SELECT * FROM MyTree;
14 #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
16 #[table_name = "comment"]
21 pub parent_id: Option<i32>,
25 pub published: chrono::NaiveDateTime,
26 pub updated: Option<chrono::NaiveDateTime>,
32 #[derive(Insertable, AsChangeset, Clone)]
33 #[table_name = "comment"]
34 pub struct CommentForm {
37 pub parent_id: Option<i32>,
39 pub removed: Option<bool>,
40 pub read: Option<bool>,
41 pub updated: Option<chrono::NaiveDateTime>,
42 pub deleted: Option<bool>,
47 impl Crud<CommentForm> for Comment {
48 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
49 use crate::schema::comment::dsl::*;
50 comment.find(comment_id).first::<Self>(conn)
53 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
54 use crate::schema::comment::dsl::*;
55 diesel::delete(comment.find(comment_id)).execute(conn)
58 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
59 use crate::schema::comment::dsl::*;
62 .get_result::<Self>(conn)
68 comment_form: &CommentForm,
69 ) -> Result<Self, Error> {
70 use crate::schema::comment::dsl::*;
71 diesel::update(comment.find(comment_id))
73 .get_result::<Self>(conn)
78 pub fn update_ap_id(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
79 use crate::schema::comment::dsl::*;
81 let apid = make_apub_endpoint(EndpointType::Comment, &comment_id.to_string()).to_string();
82 diesel::update(comment.find(comment_id))
84 .get_result::<Self>(conn)
87 pub fn mark_as_read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
88 use crate::schema::comment::dsl::*;
90 diesel::update(comment.find(comment_id))
92 .get_result::<Self>(conn)
95 pub fn permadelete(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
96 use crate::schema::comment::dsl::*;
98 diesel::update(comment.find(comment_id))
100 content.eq("*Permananently Deleted*"),
102 updated.eq(naive_now()),
104 .get_result::<Self>(conn)
108 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)]
109 #[belongs_to(Comment)]
110 #[table_name = "comment_like"]
111 pub struct CommentLike {
117 pub published: chrono::NaiveDateTime,
120 #[derive(Insertable, AsChangeset, Clone)]
121 #[table_name = "comment_like"]
122 pub struct CommentLikeForm {
129 impl Likeable<CommentLikeForm> for CommentLike {
130 fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> {
131 use crate::schema::comment_like::dsl::*;
133 .filter(comment_id.eq(comment_id_from))
137 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
138 use crate::schema::comment_like::dsl::*;
139 insert_into(comment_like)
140 .values(comment_like_form)
141 .get_result::<Self>(conn)
143 fn remove(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<usize, Error> {
144 use crate::schema::comment_like::dsl::*;
147 .filter(comment_id.eq(comment_like_form.comment_id))
148 .filter(user_id.eq(comment_like_form.user_id)),
155 pub fn from_post(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
156 use crate::schema::comment_like::dsl::*;
158 .filter(post_id.eq(post_id_from))
163 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
164 #[belongs_to(Comment)]
165 #[table_name = "comment_saved"]
166 pub struct CommentSaved {
170 pub published: chrono::NaiveDateTime,
173 #[derive(Insertable, AsChangeset, Clone)]
174 #[table_name = "comment_saved"]
175 pub struct CommentSavedForm {
180 impl Saveable<CommentSavedForm> for CommentSaved {
181 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
182 use crate::schema::comment_saved::dsl::*;
183 insert_into(comment_saved)
184 .values(comment_saved_form)
185 .get_result::<Self>(conn)
187 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
188 use crate::schema::comment_saved::dsl::*;
191 .filter(comment_id.eq(comment_saved_form.comment_id))
192 .filter(user_id.eq(comment_saved_form.user_id)),
200 use super::super::community::*;
201 use super::super::post::*;
202 use super::super::user::*;
206 let conn = establish_unpooled_connection();
208 let new_user = UserForm {
209 name: "terry".into(),
210 preferred_username: None,
211 password_encrypted: "nope".into(),
213 matrix_user_id: None,
219 theme: "darkly".into(),
220 default_sort_type: SortType::Hot as i16,
221 default_listing_type: ListingType::Subscribed as i16,
222 lang: "browser".into(),
224 send_notifications_to_email: false,
225 actor_id: "changeme".into(),
230 last_refreshed_at: None,
233 let inserted_user = User_::create(&conn, &new_user).unwrap();
235 let new_community = CommunityForm {
236 name: "test community".to_string(),
237 title: "nada".to_owned(),
240 creator_id: inserted_user.id,
245 actor_id: "changeme".into(),
249 last_refreshed_at: None,
253 let inserted_community = Community::create(&conn, &new_community).unwrap();
255 let new_post = PostForm {
256 name: "A test post".into(),
257 creator_id: inserted_user.id,
260 community_id: inserted_community.id,
268 embed_description: None,
271 ap_id: "changeme".into(),
276 let inserted_post = Post::create(&conn, &new_post).unwrap();
278 let comment_form = CommentForm {
279 content: "A test comment".into(),
280 creator_id: inserted_user.id,
281 post_id: inserted_post.id,
287 ap_id: "changeme".into(),
291 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
293 let expected_comment = Comment {
294 id: inserted_comment.id,
295 content: "A test comment".into(),
296 creator_id: inserted_user.id,
297 post_id: inserted_post.id,
302 published: inserted_comment.published,
304 ap_id: "changeme".into(),
308 let child_comment_form = CommentForm {
309 content: "A child comment".into(),
310 creator_id: inserted_user.id,
311 post_id: inserted_post.id,
312 parent_id: Some(inserted_comment.id),
317 ap_id: "changeme".into(),
321 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
324 let comment_like_form = CommentLikeForm {
325 comment_id: inserted_comment.id,
326 post_id: inserted_post.id,
327 user_id: inserted_user.id,
331 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
333 let expected_comment_like = CommentLike {
334 id: inserted_comment_like.id,
335 comment_id: inserted_comment.id,
336 post_id: inserted_post.id,
337 user_id: inserted_user.id,
338 published: inserted_comment_like.published,
343 let comment_saved_form = CommentSavedForm {
344 comment_id: inserted_comment.id,
345 user_id: inserted_user.id,
348 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
350 let expected_comment_saved = CommentSaved {
351 id: inserted_comment_saved.id,
352 comment_id: inserted_comment.id,
353 user_id: inserted_user.id,
354 published: inserted_comment_saved.published,
357 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
358 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
359 let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
360 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
361 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
362 Comment::delete(&conn, inserted_child_comment.id).unwrap();
363 Post::delete(&conn, inserted_post.id).unwrap();
364 Community::delete(&conn, inserted_community.id).unwrap();
365 User_::delete(&conn, inserted_user.id).unwrap();
367 assert_eq!(expected_comment, read_comment);
368 assert_eq!(expected_comment, inserted_comment);
369 assert_eq!(expected_comment, updated_comment);
370 assert_eq!(expected_comment_like, inserted_comment_like);
371 assert_eq!(expected_comment_saved, inserted_comment_saved);
374 inserted_child_comment.parent_id.unwrap()
376 assert_eq!(1, like_removed);
377 assert_eq!(1, saved_removed);
378 assert_eq!(1, num_deleted);