3 newtypes::{CommentId, DbUrl, PersonId},
12 traits::{Crud, DeleteableOrRemoveable, Likeable, Saveable},
14 use chrono::NaiveDateTime;
15 use diesel::{dsl::*, result::Error, *};
16 use lemmy_apub_lib::traits::ApubObject;
17 use lemmy_utils::LemmyError;
23 comment_id: CommentId,
25 ) -> Result<Self, Error> {
26 use crate::schema::comment::dsl::*;
28 diesel::update(comment.find(comment_id))
29 .set(ap_id.eq(apub_id))
30 .get_result::<Self>(conn)
33 pub fn permadelete_for_creator(
35 for_creator_id: PersonId,
36 ) -> Result<Vec<Self>, Error> {
37 use crate::schema::comment::dsl::*;
38 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
40 content.eq("*Permananently Deleted*"),
42 updated.eq(naive_now()),
44 .get_results::<Self>(conn)
47 pub fn update_deleted(
49 comment_id: CommentId,
51 ) -> Result<Self, Error> {
52 use crate::schema::comment::dsl::*;
53 diesel::update(comment.find(comment_id))
54 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
55 .get_result::<Self>(conn)
58 pub fn update_removed(
60 comment_id: CommentId,
62 ) -> Result<Self, Error> {
63 use crate::schema::comment::dsl::*;
64 diesel::update(comment.find(comment_id))
65 .set((removed.eq(new_removed), updated.eq(naive_now())))
66 .get_result::<Self>(conn)
69 pub fn update_removed_for_creator(
71 for_creator_id: PersonId,
73 ) -> Result<Vec<Self>, Error> {
74 use crate::schema::comment::dsl::*;
75 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
76 .set((removed.eq(new_removed), updated.eq(naive_now())))
77 .get_results::<Self>(conn)
82 comment_id: CommentId,
84 ) -> Result<Self, Error> {
85 use crate::schema::comment::dsl::*;
86 diesel::update(comment.find(comment_id))
87 .set(read.eq(new_read))
88 .get_result::<Self>(conn)
91 pub fn update_content(
93 comment_id: CommentId,
95 ) -> Result<Self, Error> {
96 use crate::schema::comment::dsl::*;
97 diesel::update(comment.find(comment_id))
98 .set((content.eq(new_content), updated.eq(naive_now())))
99 .get_result::<Self>(conn)
102 pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Comment, Error> {
103 use crate::schema::comment::dsl::*;
105 .values(comment_form)
109 .get_result::<Self>(conn)
113 impl Crud for Comment {
114 type Form = CommentForm;
115 type IdType = CommentId;
116 fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
117 use crate::schema::comment::dsl::*;
118 comment.find(comment_id).first::<Self>(conn)
121 fn delete(conn: &PgConnection, comment_id: CommentId) -> Result<usize, Error> {
122 use crate::schema::comment::dsl::*;
123 diesel::delete(comment.find(comment_id)).execute(conn)
126 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
127 use crate::schema::comment::dsl::*;
129 .values(comment_form)
130 .get_result::<Self>(conn)
135 comment_id: CommentId,
136 comment_form: &CommentForm,
137 ) -> Result<Self, Error> {
138 use crate::schema::comment::dsl::*;
139 diesel::update(comment.find(comment_id))
141 .get_result::<Self>(conn)
145 impl Likeable for CommentLike {
146 type Form = CommentLikeForm;
147 type IdType = CommentId;
148 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
149 use crate::schema::comment_like::dsl::*;
150 insert_into(comment_like)
151 .values(comment_like_form)
152 .on_conflict((comment_id, person_id))
154 .set(comment_like_form)
155 .get_result::<Self>(conn)
160 comment_id: CommentId,
161 ) -> Result<usize, Error> {
162 use crate::schema::comment_like::dsl;
165 .filter(dsl::comment_id.eq(comment_id))
166 .filter(dsl::person_id.eq(person_id)),
172 impl Saveable for CommentSaved {
173 type Form = CommentSavedForm;
174 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
175 use crate::schema::comment_saved::dsl::*;
176 insert_into(comment_saved)
177 .values(comment_saved_form)
178 .on_conflict((comment_id, person_id))
180 .set(comment_saved_form)
181 .get_result::<Self>(conn)
183 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
184 use crate::schema::comment_saved::dsl::*;
187 .filter(comment_id.eq(comment_saved_form.comment_id))
188 .filter(person_id.eq(comment_saved_form.person_id)),
194 impl DeleteableOrRemoveable for Comment {
195 fn blank_out_deleted_or_removed_info(mut self) -> Self {
196 self.content = "".into();
201 impl ApubObject for Comment {
202 type DataType = PgConnection;
204 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
208 fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
209 use crate::schema::comment::dsl::*;
210 let object_id: DbUrl = object_id.into();
211 Ok(comment.filter(ap_id.eq(object_id)).first::<Self>(conn).ok())
214 fn delete(self, conn: &PgConnection) -> Result<(), LemmyError> {
215 Comment::update_deleted(conn, self.id, true)?;
223 establish_unpooled_connection,
226 community::{Community, CommunityForm},
227 person::{Person, PersonForm},
230 traits::{Crud, Likeable, Saveable},
232 use serial_test::serial;
237 let conn = establish_unpooled_connection();
239 let new_person = PersonForm {
240 name: "terry".into(),
241 ..PersonForm::default()
244 let inserted_person = Person::create(&conn, &new_person).unwrap();
246 let new_community = CommunityForm {
247 name: "test community".to_string(),
248 title: "nada".to_owned(),
249 ..CommunityForm::default()
252 let inserted_community = Community::create(&conn, &new_community).unwrap();
254 let new_post = PostForm {
255 name: "A test post".into(),
256 creator_id: inserted_person.id,
257 community_id: inserted_community.id,
258 ..PostForm::default()
261 let inserted_post = Post::create(&conn, &new_post).unwrap();
263 let comment_form = CommentForm {
264 content: "A test comment".into(),
265 creator_id: inserted_person.id,
266 post_id: inserted_post.id,
267 ..CommentForm::default()
270 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
272 let expected_comment = Comment {
273 id: inserted_comment.id,
274 content: "A test comment".into(),
275 creator_id: inserted_person.id,
276 post_id: inserted_post.id,
281 published: inserted_comment.published,
283 ap_id: inserted_comment.ap_id.to_owned(),
287 let child_comment_form = CommentForm {
288 content: "A child comment".into(),
289 creator_id: inserted_person.id,
290 post_id: inserted_post.id,
291 parent_id: Some(inserted_comment.id),
292 ..CommentForm::default()
295 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
298 let comment_like_form = CommentLikeForm {
299 comment_id: inserted_comment.id,
300 post_id: inserted_post.id,
301 person_id: inserted_person.id,
305 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
307 let expected_comment_like = CommentLike {
308 id: inserted_comment_like.id,
309 comment_id: inserted_comment.id,
310 post_id: inserted_post.id,
311 person_id: inserted_person.id,
312 published: inserted_comment_like.published,
317 let comment_saved_form = CommentSavedForm {
318 comment_id: inserted_comment.id,
319 person_id: inserted_person.id,
322 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
324 let expected_comment_saved = CommentSaved {
325 id: inserted_comment_saved.id,
326 comment_id: inserted_comment.id,
327 person_id: inserted_person.id,
328 published: inserted_comment_saved.published,
331 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
332 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
333 let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
334 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
335 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
336 Comment::delete(&conn, inserted_child_comment.id).unwrap();
337 Post::delete(&conn, inserted_post.id).unwrap();
338 Community::delete(&conn, inserted_community.id).unwrap();
339 Person::delete(&conn, inserted_person.id).unwrap();
341 assert_eq!(expected_comment, read_comment);
342 assert_eq!(expected_comment, inserted_comment);
343 assert_eq!(expected_comment, updated_comment);
344 assert_eq!(expected_comment_like, inserted_comment_like);
345 assert_eq!(expected_comment_saved, inserted_comment_saved);
348 inserted_child_comment.parent_id.unwrap()
350 assert_eq!(1, like_removed);
351 assert_eq!(1, saved_removed);
352 assert_eq!(1, num_deleted);