]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/comment.rs
eca5328de1eb65ff160281af1d7d5dd7f541f742
[lemmy.git] / crates / db_schema / src / impls / comment.rs
1 use crate::{
2   newtypes::{CommentId, DbUrl, PersonId},
3   source::comment::{
4     Comment,
5     CommentForm,
6     CommentLike,
7     CommentLikeForm,
8     CommentSaved,
9     CommentSavedForm,
10   },
11   traits::{Crud, DeleteableOrRemoveable, Likeable, Saveable},
12   utils::naive_now,
13 };
14 use diesel::{dsl::*, result::Error, *};
15 use diesel_ltree::Ltree;
16 use url::Url;
17
18 impl Comment {
19   pub fn update_ap_id(
20     conn: &mut PgConnection,
21     comment_id: CommentId,
22     apub_id: DbUrl,
23   ) -> Result<Self, Error> {
24     use crate::schema::comment::dsl::*;
25
26     diesel::update(comment.find(comment_id))
27       .set(ap_id.eq(apub_id))
28       .get_result::<Self>(conn)
29   }
30
31   pub fn permadelete_for_creator(
32     conn: &mut PgConnection,
33     for_creator_id: PersonId,
34   ) -> Result<Vec<Self>, Error> {
35     use crate::schema::comment::dsl::*;
36     diesel::update(comment.filter(creator_id.eq(for_creator_id)))
37       .set((
38         content.eq("*Permananently Deleted*"),
39         deleted.eq(true),
40         updated.eq(naive_now()),
41       ))
42       .get_results::<Self>(conn)
43   }
44
45   pub fn update_deleted(
46     conn: &mut PgConnection,
47     comment_id: CommentId,
48     new_deleted: bool,
49   ) -> Result<Self, Error> {
50     use crate::schema::comment::dsl::*;
51     diesel::update(comment.find(comment_id))
52       .set((deleted.eq(new_deleted), updated.eq(naive_now())))
53       .get_result::<Self>(conn)
54   }
55
56   pub fn update_removed(
57     conn: &mut PgConnection,
58     comment_id: CommentId,
59     new_removed: bool,
60   ) -> Result<Self, Error> {
61     use crate::schema::comment::dsl::*;
62     diesel::update(comment.find(comment_id))
63       .set((removed.eq(new_removed), updated.eq(naive_now())))
64       .get_result::<Self>(conn)
65   }
66
67   pub fn update_removed_for_creator(
68     conn: &mut PgConnection,
69     for_creator_id: PersonId,
70     new_removed: bool,
71   ) -> Result<Vec<Self>, Error> {
72     use crate::schema::comment::dsl::*;
73     diesel::update(comment.filter(creator_id.eq(for_creator_id)))
74       .set((removed.eq(new_removed), updated.eq(naive_now())))
75       .get_results::<Self>(conn)
76   }
77
78   pub fn create(
79     conn: &mut PgConnection,
80     comment_form: &CommentForm,
81     parent_path: Option<&Ltree>,
82   ) -> Result<Comment, Error> {
83     use crate::schema::comment::dsl::*;
84
85     // Insert, to get the id
86     let inserted_comment = insert_into(comment)
87       .values(comment_form)
88       .on_conflict(ap_id)
89       .do_update()
90       .set(comment_form)
91       .get_result::<Self>(conn);
92
93     if let Ok(comment_insert) = inserted_comment {
94       let comment_id = comment_insert.id;
95
96       // You need to update the ltree column
97       let ltree = Ltree(if let Some(parent_path) = parent_path {
98         // The previous parent will already have 0 in it
99         // Append this comment id
100         format!("{}.{}", parent_path.0, comment_id)
101       } else {
102         // '0' is always the first path, append to that
103         format!("{}.{}", 0, comment_id)
104       });
105
106       let updated_comment = diesel::update(comment.find(comment_id))
107         .set(path.eq(ltree))
108         .get_result::<Self>(conn);
109
110       // Update the child count for the parent comment_aggregates
111       // You could do this with a trigger, but since you have to do this manually anyway,
112       // you can just have it here
113       if let Some(parent_path) = parent_path {
114         // You have to update counts for all parents, not just the immediate one
115         // TODO if the performance of this is terrible, it might be better to do this as part of a
116         // scheduled query... although the counts would often be wrong.
117         //
118         // The child_count query for reference:
119         // select c.id, c.path, count(c2.id) as child_count from comment c
120         // left join comment c2 on c2.path <@ c.path and c2.path != c.path
121         // group by c.id
122
123         let top_parent = format!("0.{}", parent_path.0.split('.').collect::<Vec<&str>>()[1]);
124         let update_child_count_stmt = format!(
125           "
126 update comment_aggregates ca set child_count = c.child_count
127 from (
128   select c.id, c.path, count(c2.id) as child_count from comment c
129   join comment c2 on c2.path <@ c.path and c2.path != c.path
130   and c.path <@ '{}'
131   group by c.id
132 ) as c
133 where ca.comment_id = c.id",
134           top_parent
135         );
136
137         sql_query(update_child_count_stmt).execute(conn)?;
138       }
139       updated_comment
140     } else {
141       inserted_comment
142     }
143   }
144   pub fn read_from_apub_id(conn: &mut PgConnection, object_id: Url) -> Result<Option<Self>, Error> {
145     use crate::schema::comment::dsl::*;
146     let object_id: DbUrl = object_id.into();
147     Ok(
148       comment
149         .filter(ap_id.eq(object_id))
150         .first::<Comment>(conn)
151         .ok()
152         .map(Into::into),
153     )
154   }
155
156   pub fn parent_comment_id(&self) -> Option<CommentId> {
157     let mut ltree_split: Vec<&str> = self.path.0.split('.').collect();
158     ltree_split.remove(0); // The first is always 0
159     if ltree_split.len() > 1 {
160       ltree_split[ltree_split.len() - 2]
161         .parse::<i32>()
162         .map(CommentId)
163         .ok()
164     } else {
165       None
166     }
167   }
168 }
169
170 impl Crud for Comment {
171   type Form = CommentForm;
172   type IdType = CommentId;
173   fn read(conn: &mut PgConnection, comment_id: CommentId) -> Result<Self, Error> {
174     use crate::schema::comment::dsl::*;
175     comment.find(comment_id).first::<Self>(conn)
176   }
177
178   fn delete(conn: &mut PgConnection, comment_id: CommentId) -> Result<usize, Error> {
179     use crate::schema::comment::dsl::*;
180     diesel::delete(comment.find(comment_id)).execute(conn)
181   }
182
183   /// This is unimplemented, use [[Comment::create]]
184   fn create(_conn: &mut PgConnection, _comment_form: &CommentForm) -> Result<Self, Error> {
185     unimplemented!();
186   }
187
188   fn update(
189     conn: &mut PgConnection,
190     comment_id: CommentId,
191     comment_form: &CommentForm,
192   ) -> Result<Self, Error> {
193     use crate::schema::comment::dsl::*;
194     diesel::update(comment.find(comment_id))
195       .set(comment_form)
196       .get_result::<Self>(conn)
197   }
198 }
199
200 impl Likeable for CommentLike {
201   type Form = CommentLikeForm;
202   type IdType = CommentId;
203   fn like(conn: &mut PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
204     use crate::schema::comment_like::dsl::*;
205     insert_into(comment_like)
206       .values(comment_like_form)
207       .on_conflict((comment_id, person_id))
208       .do_update()
209       .set(comment_like_form)
210       .get_result::<Self>(conn)
211   }
212   fn remove(
213     conn: &mut PgConnection,
214     person_id: PersonId,
215     comment_id: CommentId,
216   ) -> Result<usize, Error> {
217     use crate::schema::comment_like::dsl;
218     diesel::delete(
219       dsl::comment_like
220         .filter(dsl::comment_id.eq(comment_id))
221         .filter(dsl::person_id.eq(person_id)),
222     )
223     .execute(conn)
224   }
225 }
226
227 impl Saveable for CommentSaved {
228   type Form = CommentSavedForm;
229   fn save(conn: &mut PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
230     use crate::schema::comment_saved::dsl::*;
231     insert_into(comment_saved)
232       .values(comment_saved_form)
233       .on_conflict((comment_id, person_id))
234       .do_update()
235       .set(comment_saved_form)
236       .get_result::<Self>(conn)
237   }
238   fn unsave(
239     conn: &mut PgConnection,
240     comment_saved_form: &CommentSavedForm,
241   ) -> Result<usize, Error> {
242     use crate::schema::comment_saved::dsl::*;
243     diesel::delete(
244       comment_saved
245         .filter(comment_id.eq(comment_saved_form.comment_id))
246         .filter(person_id.eq(comment_saved_form.person_id)),
247     )
248     .execute(conn)
249   }
250 }
251
252 impl DeleteableOrRemoveable for Comment {
253   fn blank_out_deleted_or_removed_info(mut self) -> Self {
254     self.content = "".into();
255     self
256   }
257 }
258
259 #[cfg(test)]
260 mod tests {
261   use crate::{
262     newtypes::LanguageId,
263     source::{
264       comment::*,
265       community::{Community, CommunityForm},
266       person::{Person, PersonForm},
267       post::*,
268     },
269     traits::{Crud, Likeable, Saveable},
270     utils::establish_unpooled_connection,
271   };
272   use diesel_ltree::Ltree;
273   use serial_test::serial;
274
275   #[test]
276   #[serial]
277   fn test_crud() {
278     let conn = &mut establish_unpooled_connection();
279
280     let new_person = PersonForm {
281       name: "terry".into(),
282       public_key: Some("pubkey".to_string()),
283       ..PersonForm::default()
284     };
285
286     let inserted_person = Person::create(conn, &new_person).unwrap();
287
288     let new_community = CommunityForm {
289       name: "test community".to_string(),
290       title: "nada".to_owned(),
291       public_key: Some("pubkey".to_string()),
292       ..CommunityForm::default()
293     };
294
295     let inserted_community = Community::create(conn, &new_community).unwrap();
296
297     let new_post = PostForm {
298       name: "A test post".into(),
299       creator_id: inserted_person.id,
300       community_id: inserted_community.id,
301       ..PostForm::default()
302     };
303
304     let inserted_post = Post::create(conn, &new_post).unwrap();
305
306     let comment_form = CommentForm {
307       content: "A test comment".into(),
308       creator_id: inserted_person.id,
309       post_id: inserted_post.id,
310       ..CommentForm::default()
311     };
312
313     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
314
315     let expected_comment = Comment {
316       id: inserted_comment.id,
317       content: "A test comment".into(),
318       creator_id: inserted_person.id,
319       post_id: inserted_post.id,
320       removed: false,
321       deleted: false,
322       path: Ltree(format!("0.{}", inserted_comment.id)),
323       published: inserted_comment.published,
324       updated: None,
325       ap_id: inserted_comment.ap_id.to_owned(),
326       distinguished: false,
327       local: true,
328       language_id: LanguageId::default(),
329     };
330
331     let child_comment_form = CommentForm {
332       content: "A child comment".into(),
333       creator_id: inserted_person.id,
334       post_id: inserted_post.id,
335       // path: Some(text2ltree(inserted_comment.id),
336       ..CommentForm::default()
337     };
338
339     let inserted_child_comment =
340       Comment::create(conn, &child_comment_form, Some(&inserted_comment.path)).unwrap();
341
342     // Comment Like
343     let comment_like_form = CommentLikeForm {
344       comment_id: inserted_comment.id,
345       post_id: inserted_post.id,
346       person_id: inserted_person.id,
347       score: 1,
348     };
349
350     let inserted_comment_like = CommentLike::like(conn, &comment_like_form).unwrap();
351
352     let expected_comment_like = CommentLike {
353       id: inserted_comment_like.id,
354       comment_id: inserted_comment.id,
355       post_id: inserted_post.id,
356       person_id: inserted_person.id,
357       published: inserted_comment_like.published,
358       score: 1,
359     };
360
361     // Comment Saved
362     let comment_saved_form = CommentSavedForm {
363       comment_id: inserted_comment.id,
364       person_id: inserted_person.id,
365     };
366
367     let inserted_comment_saved = CommentSaved::save(conn, &comment_saved_form).unwrap();
368
369     let expected_comment_saved = CommentSaved {
370       id: inserted_comment_saved.id,
371       comment_id: inserted_comment.id,
372       person_id: inserted_person.id,
373       published: inserted_comment_saved.published,
374     };
375
376     let read_comment = Comment::read(conn, inserted_comment.id).unwrap();
377     let updated_comment = Comment::update(conn, inserted_comment.id, &comment_form).unwrap();
378     let like_removed = CommentLike::remove(conn, inserted_person.id, inserted_comment.id).unwrap();
379     let saved_removed = CommentSaved::unsave(conn, &comment_saved_form).unwrap();
380     let num_deleted = Comment::delete(conn, inserted_comment.id).unwrap();
381     Comment::delete(conn, inserted_child_comment.id).unwrap();
382     Post::delete(conn, inserted_post.id).unwrap();
383     Community::delete(conn, inserted_community.id).unwrap();
384     Person::delete(conn, inserted_person.id).unwrap();
385
386     assert_eq!(expected_comment, read_comment);
387     assert_eq!(expected_comment, inserted_comment);
388     assert_eq!(expected_comment, updated_comment);
389     assert_eq!(expected_comment_like, inserted_comment_like);
390     assert_eq!(expected_comment_saved, inserted_comment_saved);
391     assert_eq!(
392       format!("0.{}.{}", expected_comment.id, inserted_child_comment.id),
393       inserted_child_comment.path.0,
394     );
395     assert_eq!(1, like_removed);
396     assert_eq!(1, saved_removed);
397     assert_eq!(1, num_deleted);
398   }
399 }