]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/comment.rs
908aed86f2148522a654cc01d22b1ceb7b9016a7
[lemmy.git] / crates / db_schema / src / impls / comment.rs
1 use crate::{
2   newtypes::{CommentId, DbUrl, PersonId},
3   source::comment::{
4     Comment,
5     CommentForm,
6     CommentLike,
7     CommentLikeForm,
8     CommentSaved,
9     CommentSavedForm,
10   },
11   traits::{Crud, DeleteableOrRemoveable, Likeable, Saveable},
12   utils::naive_now,
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     source::{
213       comment::*,
214       community::{Community, CommunityForm},
215       person::{Person, PersonForm},
216       post::*,
217     },
218     traits::{Crud, Likeable, Saveable},
219     utils::establish_unpooled_connection,
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       public_key: Some("pubkey".to_string()),
231       ..PersonForm::default()
232     };
233
234     let inserted_person = Person::create(&conn, &new_person).unwrap();
235
236     let new_community = CommunityForm {
237       name: "test community".to_string(),
238       title: "nada".to_owned(),
239       public_key: Some("pubkey".to_string()),
240       ..CommunityForm::default()
241     };
242
243     let inserted_community = Community::create(&conn, &new_community).unwrap();
244
245     let new_post = PostForm {
246       name: "A test post".into(),
247       creator_id: inserted_person.id,
248       community_id: inserted_community.id,
249       ..PostForm::default()
250     };
251
252     let inserted_post = Post::create(&conn, &new_post).unwrap();
253
254     let comment_form = CommentForm {
255       content: "A test comment".into(),
256       creator_id: inserted_person.id,
257       post_id: inserted_post.id,
258       ..CommentForm::default()
259     };
260
261     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
262
263     let expected_comment = Comment {
264       id: inserted_comment.id,
265       content: "A test comment".into(),
266       creator_id: inserted_person.id,
267       post_id: inserted_post.id,
268       removed: false,
269       deleted: false,
270       read: false,
271       parent_id: None,
272       published: inserted_comment.published,
273       updated: None,
274       ap_id: inserted_comment.ap_id.to_owned(),
275       local: true,
276     };
277
278     let child_comment_form = CommentForm {
279       content: "A child comment".into(),
280       creator_id: inserted_person.id,
281       post_id: inserted_post.id,
282       parent_id: Some(inserted_comment.id),
283       ..CommentForm::default()
284     };
285
286     let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
287
288     // Comment Like
289     let comment_like_form = CommentLikeForm {
290       comment_id: inserted_comment.id,
291       post_id: inserted_post.id,
292       person_id: inserted_person.id,
293       score: 1,
294     };
295
296     let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
297
298     let expected_comment_like = CommentLike {
299       id: inserted_comment_like.id,
300       comment_id: inserted_comment.id,
301       post_id: inserted_post.id,
302       person_id: inserted_person.id,
303       published: inserted_comment_like.published,
304       score: 1,
305     };
306
307     // Comment Saved
308     let comment_saved_form = CommentSavedForm {
309       comment_id: inserted_comment.id,
310       person_id: inserted_person.id,
311     };
312
313     let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
314
315     let expected_comment_saved = CommentSaved {
316       id: inserted_comment_saved.id,
317       comment_id: inserted_comment.id,
318       person_id: inserted_person.id,
319       published: inserted_comment_saved.published,
320     };
321
322     let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
323     let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
324     let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
325     let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
326     let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
327     Comment::delete(&conn, inserted_child_comment.id).unwrap();
328     Post::delete(&conn, inserted_post.id).unwrap();
329     Community::delete(&conn, inserted_community.id).unwrap();
330     Person::delete(&conn, inserted_person.id).unwrap();
331
332     assert_eq!(expected_comment, read_comment);
333     assert_eq!(expected_comment, inserted_comment);
334     assert_eq!(expected_comment, updated_comment);
335     assert_eq!(expected_comment_like, inserted_comment_like);
336     assert_eq!(expected_comment_saved, inserted_comment_saved);
337     assert_eq!(
338       expected_comment.id,
339       inserted_child_comment.parent_id.unwrap()
340     );
341     assert_eq!(1, like_removed);
342     assert_eq!(1, saved_removed);
343     assert_eq!(1, num_deleted);
344   }
345 }