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