1 use crate::{ApubObject, Crud, Likeable, Saveable};
2 use diesel::{dsl::*, result::Error, *};
16 fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: String) -> Result<Comment, Error>;
17 fn permadelete_for_creator(
20 ) -> Result<Vec<Comment>, Error>;
25 ) -> Result<Comment, Error>;
30 ) -> Result<Comment, Error>;
31 fn update_removed_for_creator(
35 ) -> Result<Vec<Comment>, Error>;
36 fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Comment, Error>;
41 ) -> Result<Comment, Error>;
44 impl Comment_ for Comment {
45 fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: String) -> Result<Self, Error> {
46 use lemmy_db_schema::schema::comment::dsl::*;
48 diesel::update(comment.find(comment_id))
49 .set(ap_id.eq(apub_id))
50 .get_result::<Self>(conn)
53 fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, Error> {
54 use lemmy_db_schema::schema::comment::dsl::*;
55 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
57 content.eq("*Permananently Deleted*"),
59 updated.eq(naive_now()),
61 .get_results::<Self>(conn)
68 ) -> Result<Self, Error> {
69 use lemmy_db_schema::schema::comment::dsl::*;
70 diesel::update(comment.find(comment_id))
71 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
72 .get_result::<Self>(conn)
79 ) -> Result<Self, Error> {
80 use lemmy_db_schema::schema::comment::dsl::*;
81 diesel::update(comment.find(comment_id))
82 .set((removed.eq(new_removed), updated.eq(naive_now())))
83 .get_result::<Self>(conn)
86 fn update_removed_for_creator(
90 ) -> Result<Vec<Self>, Error> {
91 use lemmy_db_schema::schema::comment::dsl::*;
92 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
93 .set((removed.eq(new_removed), updated.eq(naive_now())))
94 .get_results::<Self>(conn)
97 fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
98 use lemmy_db_schema::schema::comment::dsl::*;
99 diesel::update(comment.find(comment_id))
100 .set(read.eq(new_read))
101 .get_result::<Self>(conn)
108 ) -> Result<Self, Error> {
109 use lemmy_db_schema::schema::comment::dsl::*;
110 diesel::update(comment.find(comment_id))
111 .set((content.eq(new_content), updated.eq(naive_now())))
112 .get_result::<Self>(conn)
116 impl Crud<CommentForm> for Comment {
117 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
118 use lemmy_db_schema::schema::comment::dsl::*;
119 comment.find(comment_id).first::<Self>(conn)
122 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
123 use lemmy_db_schema::schema::comment::dsl::*;
124 diesel::delete(comment.find(comment_id)).execute(conn)
127 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
128 use lemmy_db_schema::schema::comment::dsl::*;
130 .values(comment_form)
131 .get_result::<Self>(conn)
137 comment_form: &CommentForm,
138 ) -> Result<Self, Error> {
139 use lemmy_db_schema::schema::comment::dsl::*;
140 diesel::update(comment.find(comment_id))
142 .get_result::<Self>(conn)
146 impl ApubObject<CommentForm> for Comment {
147 fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
148 use lemmy_db_schema::schema::comment::dsl::*;
149 comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
152 fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
153 use lemmy_db_schema::schema::comment::dsl::*;
155 .values(comment_form)
159 .get_result::<Self>(conn)
163 impl Likeable<CommentLikeForm> for CommentLike {
164 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
165 use lemmy_db_schema::schema::comment_like::dsl::*;
166 insert_into(comment_like)
167 .values(comment_like_form)
168 .on_conflict((comment_id, user_id))
170 .set(comment_like_form)
171 .get_result::<Self>(conn)
173 fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
174 use lemmy_db_schema::schema::comment_like::dsl;
177 .filter(dsl::comment_id.eq(comment_id))
178 .filter(dsl::user_id.eq(user_id)),
184 impl Saveable<CommentSavedForm> for CommentSaved {
185 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
186 use lemmy_db_schema::schema::comment_saved::dsl::*;
187 insert_into(comment_saved)
188 .values(comment_saved_form)
189 .on_conflict((comment_id, user_id))
191 .set(comment_saved_form)
192 .get_result::<Self>(conn)
194 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
195 use lemmy_db_schema::schema::comment_saved::dsl::*;
198 .filter(comment_id.eq(comment_saved_form.comment_id))
199 .filter(user_id.eq(comment_saved_form.user_id)),
207 use crate::{establish_unpooled_connection, Crud, Likeable, ListingType, Saveable, SortType};
208 use lemmy_db_schema::source::{
210 community::{Community, CommunityForm},
212 user::{UserForm, User_},
217 let conn = establish_unpooled_connection();
219 let new_user = UserForm {
220 name: "terry".into(),
221 preferred_username: None,
222 password_encrypted: "nope".into(),
224 matrix_user_id: None,
232 theme: "browser".into(),
233 default_sort_type: SortType::Hot as i16,
234 default_listing_type: ListingType::Subscribed as i16,
235 lang: "browser".into(),
237 send_notifications_to_email: false,
243 last_refreshed_at: None,
246 let inserted_user = User_::create(&conn, &new_user).unwrap();
248 let new_community = CommunityForm {
249 name: "test community".to_string(),
250 title: "nada".to_owned(),
253 creator_id: inserted_user.id,
262 last_refreshed_at: None,
268 let inserted_community = Community::create(&conn, &new_community).unwrap();
270 let new_post = PostForm {
271 name: "A test post".into(),
272 creator_id: inserted_user.id,
275 community_id: inserted_community.id,
283 embed_description: None,
291 let inserted_post = Post::create(&conn, &new_post).unwrap();
293 let comment_form = CommentForm {
294 content: "A test comment".into(),
295 creator_id: inserted_user.id,
296 post_id: inserted_post.id,
307 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
309 let expected_comment = Comment {
310 id: inserted_comment.id,
311 content: "A test comment".into(),
312 creator_id: inserted_user.id,
313 post_id: inserted_post.id,
318 published: inserted_comment.published,
320 ap_id: inserted_comment.ap_id.to_owned(),
324 let child_comment_form = CommentForm {
325 content: "A child comment".into(),
326 creator_id: inserted_user.id,
327 post_id: inserted_post.id,
328 parent_id: Some(inserted_comment.id),
338 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
341 let comment_like_form = CommentLikeForm {
342 comment_id: inserted_comment.id,
343 post_id: inserted_post.id,
344 user_id: inserted_user.id,
348 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
350 let expected_comment_like = CommentLike {
351 id: inserted_comment_like.id,
352 comment_id: inserted_comment.id,
353 post_id: inserted_post.id,
354 user_id: inserted_user.id,
355 published: inserted_comment_like.published,
360 let comment_saved_form = CommentSavedForm {
361 comment_id: inserted_comment.id,
362 user_id: inserted_user.id,
365 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
367 let expected_comment_saved = CommentSaved {
368 id: inserted_comment_saved.id,
369 comment_id: inserted_comment.id,
370 user_id: inserted_user.id,
371 published: inserted_comment_saved.published,
374 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
375 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
376 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
377 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
378 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
379 Comment::delete(&conn, inserted_child_comment.id).unwrap();
380 Post::delete(&conn, inserted_post.id).unwrap();
381 Community::delete(&conn, inserted_community.id).unwrap();
382 User_::delete(&conn, inserted_user.id).unwrap();
384 assert_eq!(expected_comment, read_comment);
385 assert_eq!(expected_comment, inserted_comment);
386 assert_eq!(expected_comment, updated_comment);
387 assert_eq!(expected_comment_like, inserted_comment_like);
388 assert_eq!(expected_comment_saved, inserted_comment_saved);
391 inserted_child_comment.parent_id.unwrap()
393 assert_eq!(1, like_removed);
394 assert_eq!(1, saved_removed);
395 assert_eq!(1, num_deleted);