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