]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/comment.rs
Rewrite delete activities (#1699)
[lemmy.git] / crates / db_queries / src / source / comment.rs
1 use crate::{ApubObject, Crud, DeleteableOrRemoveable, Likeable, Saveable};
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{
4   naive_now,
5   source::comment::{
6     Comment,
7     CommentForm,
8     CommentLike,
9     CommentLikeForm,
10     CommentSaved,
11     CommentSavedForm,
12   },
13   CommentId,
14   DbUrl,
15   PersonId,
16 };
17
18 pub trait Comment_ {
19   fn update_ap_id(
20     conn: &PgConnection,
21     comment_id: CommentId,
22     apub_id: DbUrl,
23   ) -> Result<Comment, Error>;
24   fn permadelete_for_creator(
25     conn: &PgConnection,
26     for_creator_id: PersonId,
27   ) -> Result<Vec<Comment>, Error>;
28   fn update_deleted(
29     conn: &PgConnection,
30     comment_id: CommentId,
31     new_deleted: bool,
32   ) -> Result<Comment, Error>;
33   fn update_removed(
34     conn: &PgConnection,
35     comment_id: CommentId,
36     new_removed: bool,
37   ) -> Result<Comment, Error>;
38   fn update_removed_for_creator(
39     conn: &PgConnection,
40     for_creator_id: PersonId,
41     new_removed: bool,
42   ) -> Result<Vec<Comment>, Error>;
43   fn update_read(
44     conn: &PgConnection,
45     comment_id: CommentId,
46     new_read: bool,
47   ) -> Result<Comment, Error>;
48   fn update_content(
49     conn: &PgConnection,
50     comment_id: CommentId,
51     new_content: &str,
52   ) -> Result<Comment, Error>;
53 }
54
55 impl Comment_ for Comment {
56   fn update_ap_id(
57     conn: &PgConnection,
58     comment_id: CommentId,
59     apub_id: DbUrl,
60   ) -> Result<Self, Error> {
61     use lemmy_db_schema::schema::comment::dsl::*;
62
63     diesel::update(comment.find(comment_id))
64       .set(ap_id.eq(apub_id))
65       .get_result::<Self>(conn)
66   }
67
68   fn permadelete_for_creator(
69     conn: &PgConnection,
70     for_creator_id: PersonId,
71   ) -> Result<Vec<Self>, Error> {
72     use lemmy_db_schema::schema::comment::dsl::*;
73     diesel::update(comment.filter(creator_id.eq(for_creator_id)))
74       .set((
75         content.eq("*Permananently Deleted*"),
76         deleted.eq(true),
77         updated.eq(naive_now()),
78       ))
79       .get_results::<Self>(conn)
80   }
81
82   fn update_deleted(
83     conn: &PgConnection,
84     comment_id: CommentId,
85     new_deleted: bool,
86   ) -> Result<Self, Error> {
87     use lemmy_db_schema::schema::comment::dsl::*;
88     diesel::update(comment.find(comment_id))
89       .set((deleted.eq(new_deleted), updated.eq(naive_now())))
90       .get_result::<Self>(conn)
91   }
92
93   fn update_removed(
94     conn: &PgConnection,
95     comment_id: CommentId,
96     new_removed: bool,
97   ) -> Result<Self, Error> {
98     use lemmy_db_schema::schema::comment::dsl::*;
99     diesel::update(comment.find(comment_id))
100       .set((removed.eq(new_removed), updated.eq(naive_now())))
101       .get_result::<Self>(conn)
102   }
103
104   fn update_removed_for_creator(
105     conn: &PgConnection,
106     for_creator_id: PersonId,
107     new_removed: bool,
108   ) -> Result<Vec<Self>, Error> {
109     use lemmy_db_schema::schema::comment::dsl::*;
110     diesel::update(comment.filter(creator_id.eq(for_creator_id)))
111       .set((removed.eq(new_removed), updated.eq(naive_now())))
112       .get_results::<Self>(conn)
113   }
114
115   fn update_read(
116     conn: &PgConnection,
117     comment_id: CommentId,
118     new_read: bool,
119   ) -> Result<Self, Error> {
120     use lemmy_db_schema::schema::comment::dsl::*;
121     diesel::update(comment.find(comment_id))
122       .set(read.eq(new_read))
123       .get_result::<Self>(conn)
124   }
125
126   fn update_content(
127     conn: &PgConnection,
128     comment_id: CommentId,
129     new_content: &str,
130   ) -> Result<Self, Error> {
131     use lemmy_db_schema::schema::comment::dsl::*;
132     diesel::update(comment.find(comment_id))
133       .set((content.eq(new_content), updated.eq(naive_now())))
134       .get_result::<Self>(conn)
135   }
136 }
137
138 impl Crud for Comment {
139   type Form = CommentForm;
140   type IdType = CommentId;
141   fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
142     use lemmy_db_schema::schema::comment::dsl::*;
143     comment.find(comment_id).first::<Self>(conn)
144   }
145
146   fn delete(conn: &PgConnection, comment_id: CommentId) -> Result<usize, Error> {
147     use lemmy_db_schema::schema::comment::dsl::*;
148     diesel::delete(comment.find(comment_id)).execute(conn)
149   }
150
151   fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
152     use lemmy_db_schema::schema::comment::dsl::*;
153     insert_into(comment)
154       .values(comment_form)
155       .get_result::<Self>(conn)
156   }
157
158   fn update(
159     conn: &PgConnection,
160     comment_id: CommentId,
161     comment_form: &CommentForm,
162   ) -> Result<Self, Error> {
163     use lemmy_db_schema::schema::comment::dsl::*;
164     diesel::update(comment.find(comment_id))
165       .set(comment_form)
166       .get_result::<Self>(conn)
167   }
168 }
169
170 impl ApubObject for Comment {
171   type Form = CommentForm;
172   fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
173     use lemmy_db_schema::schema::comment::dsl::*;
174     comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
175   }
176
177   fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
178     use lemmy_db_schema::schema::comment::dsl::*;
179     insert_into(comment)
180       .values(comment_form)
181       .on_conflict(ap_id)
182       .do_update()
183       .set(comment_form)
184       .get_result::<Self>(conn)
185   }
186 }
187
188 impl Likeable for CommentLike {
189   type Form = CommentLikeForm;
190   type IdType = CommentId;
191   fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
192     use lemmy_db_schema::schema::comment_like::dsl::*;
193     insert_into(comment_like)
194       .values(comment_like_form)
195       .on_conflict((comment_id, person_id))
196       .do_update()
197       .set(comment_like_form)
198       .get_result::<Self>(conn)
199   }
200   fn remove(
201     conn: &PgConnection,
202     person_id: PersonId,
203     comment_id: CommentId,
204   ) -> Result<usize, Error> {
205     use lemmy_db_schema::schema::comment_like::dsl;
206     diesel::delete(
207       dsl::comment_like
208         .filter(dsl::comment_id.eq(comment_id))
209         .filter(dsl::person_id.eq(person_id)),
210     )
211     .execute(conn)
212   }
213 }
214
215 impl Saveable for CommentSaved {
216   type Form = CommentSavedForm;
217   fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
218     use lemmy_db_schema::schema::comment_saved::dsl::*;
219     insert_into(comment_saved)
220       .values(comment_saved_form)
221       .on_conflict((comment_id, person_id))
222       .do_update()
223       .set(comment_saved_form)
224       .get_result::<Self>(conn)
225   }
226   fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
227     use lemmy_db_schema::schema::comment_saved::dsl::*;
228     diesel::delete(
229       comment_saved
230         .filter(comment_id.eq(comment_saved_form.comment_id))
231         .filter(person_id.eq(comment_saved_form.person_id)),
232     )
233     .execute(conn)
234   }
235 }
236
237 impl DeleteableOrRemoveable for Comment {
238   fn blank_out_deleted_or_removed_info(mut self) -> Self {
239     self.content = "".into();
240     self
241   }
242 }
243
244 #[cfg(test)]
245 mod tests {
246   use crate::{establish_unpooled_connection, Crud, Likeable, Saveable};
247   use lemmy_db_schema::source::{
248     comment::*,
249     community::{Community, CommunityForm},
250     person::{Person, PersonForm},
251     post::*,
252   };
253   use serial_test::serial;
254
255   #[test]
256   #[serial]
257   fn test_crud() {
258     let conn = establish_unpooled_connection();
259
260     let new_person = PersonForm {
261       name: "terry".into(),
262       ..PersonForm::default()
263     };
264
265     let inserted_person = Person::create(&conn, &new_person).unwrap();
266
267     let new_community = CommunityForm {
268       name: "test community".to_string(),
269       title: "nada".to_owned(),
270       ..CommunityForm::default()
271     };
272
273     let inserted_community = Community::create(&conn, &new_community).unwrap();
274
275     let new_post = PostForm {
276       name: "A test post".into(),
277       creator_id: inserted_person.id,
278       community_id: inserted_community.id,
279       ..PostForm::default()
280     };
281
282     let inserted_post = Post::create(&conn, &new_post).unwrap();
283
284     let comment_form = CommentForm {
285       content: "A test comment".into(),
286       creator_id: inserted_person.id,
287       post_id: inserted_post.id,
288       ..CommentForm::default()
289     };
290
291     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
292
293     let expected_comment = Comment {
294       id: inserted_comment.id,
295       content: "A test comment".into(),
296       creator_id: inserted_person.id,
297       post_id: inserted_post.id,
298       removed: false,
299       deleted: false,
300       read: false,
301       parent_id: None,
302       published: inserted_comment.published,
303       updated: None,
304       ap_id: inserted_comment.ap_id.to_owned(),
305       local: true,
306     };
307
308     let child_comment_form = CommentForm {
309       content: "A child comment".into(),
310       creator_id: inserted_person.id,
311       post_id: inserted_post.id,
312       parent_id: Some(inserted_comment.id),
313       ..CommentForm::default()
314     };
315
316     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
317
318     // Comment Like
319     let comment_like_form = CommentLikeForm {
320       comment_id: inserted_comment.id,
321       post_id: inserted_post.id,
322       person_id: inserted_person.id,
323       score: 1,
324     };
325
326     let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
327
328     let expected_comment_like = CommentLike {
329       id: inserted_comment_like.id,
330       comment_id: inserted_comment.id,
331       post_id: inserted_post.id,
332       person_id: inserted_person.id,
333       published: inserted_comment_like.published,
334       score: 1,
335     };
336
337     // Comment Saved
338     let comment_saved_form = CommentSavedForm {
339       comment_id: inserted_comment.id,
340       person_id: inserted_person.id,
341     };
342
343     let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
344
345     let expected_comment_saved = CommentSaved {
346       id: inserted_comment_saved.id,
347       comment_id: inserted_comment.id,
348       person_id: inserted_person.id,
349       published: inserted_comment_saved.published,
350     };
351
352     let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
353     let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
354     let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
355     let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
356     let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
357     Comment::delete(&conn, inserted_child_comment.id).unwrap();
358     Post::delete(&conn, inserted_post.id).unwrap();
359     Community::delete(&conn, inserted_community.id).unwrap();
360     Person::delete(&conn, inserted_person.id).unwrap();
361
362     assert_eq!(expected_comment, read_comment);
363     assert_eq!(expected_comment, inserted_comment);
364     assert_eq!(expected_comment, updated_comment);
365     assert_eq!(expected_comment_like, inserted_comment_like);
366     assert_eq!(expected_comment_saved, inserted_comment_saved);
367     assert_eq!(
368       expected_comment.id,
369       inserted_child_comment.parent_id.unwrap()
370     );
371     assert_eq!(1, like_removed);
372     assert_eq!(1, saved_removed);
373     assert_eq!(1, num_deleted);
374   }
375 }