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