1 use crate::{ApubObject, Crud, DeleteableOrRemoveable, Likeable, Saveable};
2 use diesel::{dsl::*, result::Error, *};
21 comment_id: CommentId,
23 ) -> Result<Comment, Error>;
24 fn permadelete_for_creator(
26 for_creator_id: PersonId,
27 ) -> Result<Vec<Comment>, Error>;
30 comment_id: CommentId,
32 ) -> Result<Comment, Error>;
35 comment_id: CommentId,
37 ) -> Result<Comment, Error>;
38 fn update_removed_for_creator(
40 for_creator_id: PersonId,
42 ) -> Result<Vec<Comment>, Error>;
45 comment_id: CommentId,
47 ) -> Result<Comment, Error>;
50 comment_id: CommentId,
52 ) -> Result<Comment, Error>;
55 impl Comment_ for Comment {
58 comment_id: CommentId,
60 ) -> Result<Self, Error> {
61 use lemmy_db_schema::schema::comment::dsl::*;
63 diesel::update(comment.find(comment_id))
64 .set(ap_id.eq(apub_id))
65 .get_result::<Self>(conn)
68 fn permadelete_for_creator(
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)))
75 content.eq("*Permananently Deleted*"),
77 updated.eq(naive_now()),
79 .get_results::<Self>(conn)
84 comment_id: CommentId,
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)
95 comment_id: CommentId,
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)
104 fn update_removed_for_creator(
106 for_creator_id: PersonId,
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)
117 comment_id: CommentId,
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)
128 comment_id: CommentId,
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)
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)
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)
149 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
150 use lemmy_db_schema::schema::comment::dsl::*;
152 .values(comment_form)
153 .get_result::<Self>(conn)
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))
164 .get_result::<Self>(conn)
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)
174 fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
175 use lemmy_db_schema::schema::comment::dsl::*;
177 .values(comment_form)
181 .get_result::<Self>(conn)
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))
192 .set(comment_like_form)
193 .get_result::<Self>(conn)
198 comment_id: CommentId,
199 ) -> Result<usize, Error> {
200 use lemmy_db_schema::schema::comment_like::dsl;
203 .filter(dsl::comment_id.eq(comment_id))
204 .filter(dsl::person_id.eq(person_id)),
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))
217 .set(comment_saved_form)
218 .get_result::<Self>(conn)
220 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
221 use lemmy_db_schema::schema::comment_saved::dsl::*;
224 .filter(comment_id.eq(comment_saved_form.comment_id))
225 .filter(person_id.eq(comment_saved_form.person_id)),
231 impl DeleteableOrRemoveable for Comment {
232 fn blank_out_deleted_or_removed_info(mut self) -> Self {
233 self.content = "".into();
240 use crate::{establish_unpooled_connection, Crud, Likeable, Saveable};
241 use lemmy_db_schema::source::{
243 community::{Community, CommunityForm},
244 person::{Person, PersonForm},
247 use serial_test::serial;
252 let conn = establish_unpooled_connection();
254 let new_person = PersonForm {
255 name: "terry".into(),
256 ..PersonForm::default()
259 let inserted_person = Person::create(&conn, &new_person).unwrap();
261 let new_community = CommunityForm {
262 name: "test community".to_string(),
263 title: "nada".to_owned(),
264 ..CommunityForm::default()
267 let inserted_community = Community::create(&conn, &new_community).unwrap();
269 let new_post = PostForm {
270 name: "A test post".into(),
271 creator_id: inserted_person.id,
272 community_id: inserted_community.id,
273 ..PostForm::default()
276 let inserted_post = Post::create(&conn, &new_post).unwrap();
278 let comment_form = CommentForm {
279 content: "A test comment".into(),
280 creator_id: inserted_person.id,
281 post_id: inserted_post.id,
282 ..CommentForm::default()
285 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
287 let expected_comment = Comment {
288 id: inserted_comment.id,
289 content: "A test comment".into(),
290 creator_id: inserted_person.id,
291 post_id: inserted_post.id,
296 published: inserted_comment.published,
298 ap_id: inserted_comment.ap_id.to_owned(),
302 let child_comment_form = CommentForm {
303 content: "A child comment".into(),
304 creator_id: inserted_person.id,
305 post_id: inserted_post.id,
306 parent_id: Some(inserted_comment.id),
307 ..CommentForm::default()
310 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
313 let comment_like_form = CommentLikeForm {
314 comment_id: inserted_comment.id,
315 post_id: inserted_post.id,
316 person_id: inserted_person.id,
320 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
322 let expected_comment_like = CommentLike {
323 id: inserted_comment_like.id,
324 comment_id: inserted_comment.id,
325 post_id: inserted_post.id,
326 person_id: inserted_person.id,
327 published: inserted_comment_like.published,
332 let comment_saved_form = CommentSavedForm {
333 comment_id: inserted_comment.id,
334 person_id: inserted_person.id,
337 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
339 let expected_comment_saved = CommentSaved {
340 id: inserted_comment_saved.id,
341 comment_id: inserted_comment.id,
342 person_id: inserted_person.id,
343 published: inserted_comment_saved.published,
346 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
347 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
348 let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
349 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
350 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
351 Comment::delete(&conn, inserted_child_comment.id).unwrap();
352 Post::delete(&conn, inserted_post.id).unwrap();
353 Community::delete(&conn, inserted_community.id).unwrap();
354 Person::delete(&conn, inserted_person.id).unwrap();
356 assert_eq!(expected_comment, read_comment);
357 assert_eq!(expected_comment, inserted_comment);
358 assert_eq!(expected_comment, updated_comment);
359 assert_eq!(expected_comment_like, inserted_comment_like);
360 assert_eq!(expected_comment_saved, inserted_comment_saved);
363 inserted_child_comment.parent_id.unwrap()
365 assert_eq!(1, like_removed);
366 assert_eq!(1, saved_removed);
367 assert_eq!(1, num_deleted);