1 use crate::{ApubObject, Crud, DeleteableOrRemoveable, Likeable, Saveable};
2 use chrono::NaiveDateTime;
3 use diesel::{dsl::*, result::Error, *};
22 comment_id: CommentId,
24 ) -> Result<Comment, Error>;
25 fn permadelete_for_creator(
27 for_creator_id: PersonId,
28 ) -> Result<Vec<Comment>, Error>;
31 comment_id: CommentId,
33 ) -> Result<Comment, Error>;
36 comment_id: CommentId,
38 ) -> Result<Comment, Error>;
39 fn update_removed_for_creator(
41 for_creator_id: PersonId,
43 ) -> Result<Vec<Comment>, Error>;
46 comment_id: CommentId,
48 ) -> Result<Comment, Error>;
51 comment_id: CommentId,
53 ) -> Result<Comment, Error>;
54 fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Comment, Error>;
57 impl Comment_ for Comment {
60 comment_id: CommentId,
62 ) -> Result<Self, Error> {
63 use lemmy_db_schema::schema::comment::dsl::*;
65 diesel::update(comment.find(comment_id))
66 .set(ap_id.eq(apub_id))
67 .get_result::<Self>(conn)
70 fn permadelete_for_creator(
72 for_creator_id: PersonId,
73 ) -> Result<Vec<Self>, Error> {
74 use lemmy_db_schema::schema::comment::dsl::*;
75 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
77 content.eq("*Permananently Deleted*"),
79 updated.eq(naive_now()),
81 .get_results::<Self>(conn)
86 comment_id: CommentId,
88 ) -> Result<Self, Error> {
89 use lemmy_db_schema::schema::comment::dsl::*;
90 diesel::update(comment.find(comment_id))
91 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
92 .get_result::<Self>(conn)
97 comment_id: CommentId,
99 ) -> Result<Self, Error> {
100 use lemmy_db_schema::schema::comment::dsl::*;
101 diesel::update(comment.find(comment_id))
102 .set((removed.eq(new_removed), updated.eq(naive_now())))
103 .get_result::<Self>(conn)
106 fn update_removed_for_creator(
108 for_creator_id: PersonId,
110 ) -> Result<Vec<Self>, Error> {
111 use lemmy_db_schema::schema::comment::dsl::*;
112 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
113 .set((removed.eq(new_removed), updated.eq(naive_now())))
114 .get_results::<Self>(conn)
119 comment_id: CommentId,
121 ) -> Result<Self, Error> {
122 use lemmy_db_schema::schema::comment::dsl::*;
123 diesel::update(comment.find(comment_id))
124 .set(read.eq(new_read))
125 .get_result::<Self>(conn)
130 comment_id: CommentId,
132 ) -> Result<Self, Error> {
133 use lemmy_db_schema::schema::comment::dsl::*;
134 diesel::update(comment.find(comment_id))
135 .set((content.eq(new_content), updated.eq(naive_now())))
136 .get_result::<Self>(conn)
139 fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Comment, Error> {
140 use lemmy_db_schema::schema::comment::dsl::*;
142 .values(comment_form)
146 .get_result::<Self>(conn)
150 impl Crud for Comment {
151 type Form = CommentForm;
152 type IdType = CommentId;
153 fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
154 use lemmy_db_schema::schema::comment::dsl::*;
155 comment.find(comment_id).first::<Self>(conn)
158 fn delete(conn: &PgConnection, comment_id: CommentId) -> Result<usize, Error> {
159 use lemmy_db_schema::schema::comment::dsl::*;
160 diesel::delete(comment.find(comment_id)).execute(conn)
163 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
164 use lemmy_db_schema::schema::comment::dsl::*;
166 .values(comment_form)
167 .get_result::<Self>(conn)
172 comment_id: CommentId,
173 comment_form: &CommentForm,
174 ) -> Result<Self, Error> {
175 use lemmy_db_schema::schema::comment::dsl::*;
176 diesel::update(comment.find(comment_id))
178 .get_result::<Self>(conn)
182 impl ApubObject for Comment {
183 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
187 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
188 use lemmy_db_schema::schema::comment::dsl::*;
189 comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
193 impl Likeable for CommentLike {
194 type Form = CommentLikeForm;
195 type IdType = CommentId;
196 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
197 use lemmy_db_schema::schema::comment_like::dsl::*;
198 insert_into(comment_like)
199 .values(comment_like_form)
200 .on_conflict((comment_id, person_id))
202 .set(comment_like_form)
203 .get_result::<Self>(conn)
208 comment_id: CommentId,
209 ) -> Result<usize, Error> {
210 use lemmy_db_schema::schema::comment_like::dsl;
213 .filter(dsl::comment_id.eq(comment_id))
214 .filter(dsl::person_id.eq(person_id)),
220 impl Saveable for CommentSaved {
221 type Form = CommentSavedForm;
222 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
223 use lemmy_db_schema::schema::comment_saved::dsl::*;
224 insert_into(comment_saved)
225 .values(comment_saved_form)
226 .on_conflict((comment_id, person_id))
228 .set(comment_saved_form)
229 .get_result::<Self>(conn)
231 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
232 use lemmy_db_schema::schema::comment_saved::dsl::*;
235 .filter(comment_id.eq(comment_saved_form.comment_id))
236 .filter(person_id.eq(comment_saved_form.person_id)),
242 impl DeleteableOrRemoveable for Comment {
243 fn blank_out_deleted_or_removed_info(mut self) -> Self {
244 self.content = "".into();
251 use crate::{establish_unpooled_connection, Crud, Likeable, Saveable};
252 use lemmy_db_schema::source::{
254 community::{Community, CommunityForm},
255 person::{Person, PersonForm},
258 use serial_test::serial;
263 let conn = establish_unpooled_connection();
265 let new_person = PersonForm {
266 name: "terry".into(),
267 ..PersonForm::default()
270 let inserted_person = Person::create(&conn, &new_person).unwrap();
272 let new_community = CommunityForm {
273 name: "test community".to_string(),
274 title: "nada".to_owned(),
275 ..CommunityForm::default()
278 let inserted_community = Community::create(&conn, &new_community).unwrap();
280 let new_post = PostForm {
281 name: "A test post".into(),
282 creator_id: inserted_person.id,
283 community_id: inserted_community.id,
284 ..PostForm::default()
287 let inserted_post = Post::create(&conn, &new_post).unwrap();
289 let comment_form = CommentForm {
290 content: "A test comment".into(),
291 creator_id: inserted_person.id,
292 post_id: inserted_post.id,
293 ..CommentForm::default()
296 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
298 let expected_comment = Comment {
299 id: inserted_comment.id,
300 content: "A test comment".into(),
301 creator_id: inserted_person.id,
302 post_id: inserted_post.id,
307 published: inserted_comment.published,
309 ap_id: inserted_comment.ap_id.to_owned(),
313 let child_comment_form = CommentForm {
314 content: "A child comment".into(),
315 creator_id: inserted_person.id,
316 post_id: inserted_post.id,
317 parent_id: Some(inserted_comment.id),
318 ..CommentForm::default()
321 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
324 let comment_like_form = CommentLikeForm {
325 comment_id: inserted_comment.id,
326 post_id: inserted_post.id,
327 person_id: inserted_person.id,
331 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
333 let expected_comment_like = CommentLike {
334 id: inserted_comment_like.id,
335 comment_id: inserted_comment.id,
336 post_id: inserted_post.id,
337 person_id: inserted_person.id,
338 published: inserted_comment_like.published,
343 let comment_saved_form = CommentSavedForm {
344 comment_id: inserted_comment.id,
345 person_id: inserted_person.id,
348 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
350 let expected_comment_saved = CommentSaved {
351 id: inserted_comment_saved.id,
352 comment_id: inserted_comment.id,
353 person_id: inserted_person.id,
354 published: inserted_comment_saved.published,
357 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
358 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
359 let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
360 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
361 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
362 Comment::delete(&conn, inserted_child_comment.id).unwrap();
363 Post::delete(&conn, inserted_post.id).unwrap();
364 Community::delete(&conn, inserted_community.id).unwrap();
365 Person::delete(&conn, inserted_person.id).unwrap();
367 assert_eq!(expected_comment, read_comment);
368 assert_eq!(expected_comment, inserted_comment);
369 assert_eq!(expected_comment, updated_comment);
370 assert_eq!(expected_comment_like, inserted_comment_like);
371 assert_eq!(expected_comment_saved, inserted_comment_saved);
374 inserted_child_comment.parent_id.unwrap()
376 assert_eq!(1, like_removed);
377 assert_eq!(1, saved_removed);
378 assert_eq!(1, num_deleted);