]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/comment.rs
Moving matrix_user_id to person table. #1438
[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   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<CommentForm, CommentId> for Comment {
139   fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
140     use lemmy_db_schema::schema::comment::dsl::*;
141     comment.find(comment_id).first::<Self>(conn)
142   }
143
144   fn delete(conn: &PgConnection, comment_id: CommentId) -> Result<usize, Error> {
145     use lemmy_db_schema::schema::comment::dsl::*;
146     diesel::delete(comment.find(comment_id)).execute(conn)
147   }
148
149   fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
150     use lemmy_db_schema::schema::comment::dsl::*;
151     insert_into(comment)
152       .values(comment_form)
153       .get_result::<Self>(conn)
154   }
155
156   fn update(
157     conn: &PgConnection,
158     comment_id: CommentId,
159     comment_form: &CommentForm,
160   ) -> Result<Self, Error> {
161     use lemmy_db_schema::schema::comment::dsl::*;
162     diesel::update(comment.find(comment_id))
163       .set(comment_form)
164       .get_result::<Self>(conn)
165   }
166 }
167
168 impl ApubObject<CommentForm> for Comment {
169   fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
170     use lemmy_db_schema::schema::comment::dsl::*;
171     comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
172   }
173
174   fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
175     use lemmy_db_schema::schema::comment::dsl::*;
176     insert_into(comment)
177       .values(comment_form)
178       .on_conflict(ap_id)
179       .do_update()
180       .set(comment_form)
181       .get_result::<Self>(conn)
182   }
183 }
184
185 impl Likeable<CommentLikeForm, CommentId> for CommentLike {
186   fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
187     use lemmy_db_schema::schema::comment_like::dsl::*;
188     insert_into(comment_like)
189       .values(comment_like_form)
190       .on_conflict((comment_id, person_id))
191       .do_update()
192       .set(comment_like_form)
193       .get_result::<Self>(conn)
194   }
195   fn remove(
196     conn: &PgConnection,
197     person_id: PersonId,
198     comment_id: CommentId,
199   ) -> Result<usize, Error> {
200     use lemmy_db_schema::schema::comment_like::dsl;
201     diesel::delete(
202       dsl::comment_like
203         .filter(dsl::comment_id.eq(comment_id))
204         .filter(dsl::person_id.eq(person_id)),
205     )
206     .execute(conn)
207   }
208 }
209
210 impl Saveable<CommentSavedForm> for CommentSaved {
211   fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
212     use lemmy_db_schema::schema::comment_saved::dsl::*;
213     insert_into(comment_saved)
214       .values(comment_saved_form)
215       .on_conflict((comment_id, person_id))
216       .do_update()
217       .set(comment_saved_form)
218       .get_result::<Self>(conn)
219   }
220   fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
221     use lemmy_db_schema::schema::comment_saved::dsl::*;
222     diesel::delete(
223       comment_saved
224         .filter(comment_id.eq(comment_saved_form.comment_id))
225         .filter(person_id.eq(comment_saved_form.person_id)),
226     )
227     .execute(conn)
228   }
229 }
230
231 #[cfg(test)]
232 mod tests {
233   use crate::{establish_unpooled_connection, Crud, Likeable, Saveable};
234   use lemmy_db_schema::source::{
235     comment::*,
236     community::{Community, CommunityForm},
237     person::{Person, PersonForm},
238     post::*,
239   };
240   use serial_test::serial;
241
242   #[test]
243   #[serial]
244   fn test_crud() {
245     let conn = establish_unpooled_connection();
246
247     let new_person = PersonForm {
248       name: "terry".into(),
249       preferred_username: None,
250       avatar: None,
251       banner: None,
252       banned: None,
253       deleted: None,
254       published: None,
255       updated: None,
256       actor_id: None,
257       bio: None,
258       local: None,
259       private_key: None,
260       public_key: None,
261       last_refreshed_at: None,
262       inbox_url: None,
263       shared_inbox_url: None,
264       matrix_user_id: None,
265     };
266
267     let inserted_person = Person::create(&conn, &new_person).unwrap();
268
269     let new_community = CommunityForm {
270       name: "test community".to_string(),
271       title: "nada".to_owned(),
272       description: None,
273       creator_id: inserted_person.id,
274       removed: None,
275       deleted: None,
276       updated: None,
277       nsfw: false,
278       actor_id: None,
279       local: true,
280       private_key: None,
281       public_key: None,
282       last_refreshed_at: None,
283       published: None,
284       banner: None,
285       icon: None,
286       inbox_url: None,
287       shared_inbox_url: None,
288       followers_url: None,
289     };
290
291     let inserted_community = Community::create(&conn, &new_community).unwrap();
292
293     let new_post = PostForm {
294       name: "A test post".into(),
295       creator_id: inserted_person.id,
296       url: None,
297       body: None,
298       community_id: inserted_community.id,
299       removed: None,
300       deleted: None,
301       locked: None,
302       stickied: None,
303       updated: None,
304       nsfw: false,
305       embed_title: None,
306       embed_description: None,
307       embed_html: None,
308       thumbnail_url: None,
309       ap_id: None,
310       local: true,
311       published: None,
312     };
313
314     let inserted_post = Post::create(&conn, &new_post).unwrap();
315
316     let comment_form = CommentForm {
317       content: "A test comment".into(),
318       creator_id: inserted_person.id,
319       post_id: inserted_post.id,
320       removed: None,
321       deleted: None,
322       read: None,
323       parent_id: None,
324       published: None,
325       updated: None,
326       ap_id: None,
327       local: true,
328     };
329
330     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
331
332     let expected_comment = Comment {
333       id: inserted_comment.id,
334       content: "A test comment".into(),
335       creator_id: inserted_person.id,
336       post_id: inserted_post.id,
337       removed: false,
338       deleted: false,
339       read: false,
340       parent_id: None,
341       published: inserted_comment.published,
342       updated: None,
343       ap_id: inserted_comment.ap_id.to_owned(),
344       local: true,
345     };
346
347     let child_comment_form = CommentForm {
348       content: "A child comment".into(),
349       creator_id: inserted_person.id,
350       post_id: inserted_post.id,
351       parent_id: Some(inserted_comment.id),
352       removed: None,
353       deleted: None,
354       read: None,
355       published: None,
356       updated: None,
357       ap_id: None,
358       local: true,
359     };
360
361     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
362
363     // Comment Like
364     let comment_like_form = CommentLikeForm {
365       comment_id: inserted_comment.id,
366       post_id: inserted_post.id,
367       person_id: inserted_person.id,
368       score: 1,
369     };
370
371     let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
372
373     let expected_comment_like = CommentLike {
374       id: inserted_comment_like.id,
375       comment_id: inserted_comment.id,
376       post_id: inserted_post.id,
377       person_id: inserted_person.id,
378       published: inserted_comment_like.published,
379       score: 1,
380     };
381
382     // Comment Saved
383     let comment_saved_form = CommentSavedForm {
384       comment_id: inserted_comment.id,
385       person_id: inserted_person.id,
386     };
387
388     let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
389
390     let expected_comment_saved = CommentSaved {
391       id: inserted_comment_saved.id,
392       comment_id: inserted_comment.id,
393       person_id: inserted_person.id,
394       published: inserted_comment_saved.published,
395     };
396
397     let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
398     let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
399     let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
400     let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
401     let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
402     Comment::delete(&conn, inserted_child_comment.id).unwrap();
403     Post::delete(&conn, inserted_post.id).unwrap();
404     Community::delete(&conn, inserted_community.id).unwrap();
405     Person::delete(&conn, inserted_person.id).unwrap();
406
407     assert_eq!(expected_comment, read_comment);
408     assert_eq!(expected_comment, inserted_comment);
409     assert_eq!(expected_comment, updated_comment);
410     assert_eq!(expected_comment_like, inserted_comment_like);
411     assert_eq!(expected_comment_saved, inserted_comment_saved);
412     assert_eq!(
413       expected_comment.id,
414       inserted_child_comment.parent_id.unwrap()
415     );
416     assert_eq!(1, like_removed);
417     assert_eq!(1, saved_removed);
418     assert_eq!(1, num_deleted);
419   }
420 }