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