1 use crate::{ApubObject, Crud, Likeable, Saveable};
2 use diesel::{dsl::*, result::Error, *};
17 fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: String) -> Result<Comment, Error>;
18 fn permadelete_for_creator(
21 ) -> Result<Vec<Comment>, Error>;
26 ) -> Result<Comment, Error>;
31 ) -> Result<Comment, Error>;
32 fn update_removed_for_creator(
36 ) -> Result<Vec<Comment>, Error>;
37 fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Comment, Error>;
42 ) -> Result<Comment, Error>;
45 impl Comment_ for Comment {
46 fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: String) -> Result<Self, Error> {
47 use lemmy_db_schema::schema::comment::dsl::*;
49 diesel::update(comment.find(comment_id))
50 .set(ap_id.eq(apub_id))
51 .get_result::<Self>(conn)
54 fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, Error> {
55 use lemmy_db_schema::schema::comment::dsl::*;
56 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
58 content.eq("*Permananently Deleted*"),
60 updated.eq(naive_now()),
62 .get_results::<Self>(conn)
69 ) -> Result<Self, Error> {
70 use lemmy_db_schema::schema::comment::dsl::*;
71 diesel::update(comment.find(comment_id))
72 .set((deleted.eq(new_deleted), updated.eq(naive_now())))
73 .get_result::<Self>(conn)
80 ) -> Result<Self, Error> {
81 use lemmy_db_schema::schema::comment::dsl::*;
82 diesel::update(comment.find(comment_id))
83 .set((removed.eq(new_removed), updated.eq(naive_now())))
84 .get_result::<Self>(conn)
87 fn update_removed_for_creator(
91 ) -> Result<Vec<Self>, Error> {
92 use lemmy_db_schema::schema::comment::dsl::*;
93 diesel::update(comment.filter(creator_id.eq(for_creator_id)))
94 .set((removed.eq(new_removed), updated.eq(naive_now())))
95 .get_results::<Self>(conn)
98 fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
99 use lemmy_db_schema::schema::comment::dsl::*;
100 diesel::update(comment.find(comment_id))
101 .set(read.eq(new_read))
102 .get_result::<Self>(conn)
109 ) -> Result<Self, Error> {
110 use lemmy_db_schema::schema::comment::dsl::*;
111 diesel::update(comment.find(comment_id))
112 .set((content.eq(new_content), updated.eq(naive_now())))
113 .get_result::<Self>(conn)
117 impl Crud<CommentForm> for Comment {
118 fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
119 use lemmy_db_schema::schema::comment::dsl::*;
120 comment.find(comment_id).first::<Self>(conn)
123 fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
124 use lemmy_db_schema::schema::comment::dsl::*;
125 diesel::delete(comment.find(comment_id)).execute(conn)
128 fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
129 use lemmy_db_schema::schema::comment::dsl::*;
131 .values(comment_form)
132 .get_result::<Self>(conn)
138 comment_form: &CommentForm,
139 ) -> Result<Self, Error> {
140 use lemmy_db_schema::schema::comment::dsl::*;
141 diesel::update(comment.find(comment_id))
143 .get_result::<Self>(conn)
147 impl ApubObject<CommentForm> for Comment {
148 fn read_from_apub_id(conn: &PgConnection, object_id: &Url) -> Result<Self, Error> {
149 use lemmy_db_schema::schema::comment::dsl::*;
150 comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
153 fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
154 use lemmy_db_schema::schema::comment::dsl::*;
156 .values(comment_form)
160 .get_result::<Self>(conn)
164 impl Likeable<CommentLikeForm> for CommentLike {
165 fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
166 use lemmy_db_schema::schema::comment_like::dsl::*;
167 insert_into(comment_like)
168 .values(comment_like_form)
169 .on_conflict((comment_id, user_id))
171 .set(comment_like_form)
172 .get_result::<Self>(conn)
174 fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
175 use lemmy_db_schema::schema::comment_like::dsl;
178 .filter(dsl::comment_id.eq(comment_id))
179 .filter(dsl::user_id.eq(user_id)),
185 impl Saveable<CommentSavedForm> for CommentSaved {
186 fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
187 use lemmy_db_schema::schema::comment_saved::dsl::*;
188 insert_into(comment_saved)
189 .values(comment_saved_form)
190 .on_conflict((comment_id, user_id))
192 .set(comment_saved_form)
193 .get_result::<Self>(conn)
195 fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
196 use lemmy_db_schema::schema::comment_saved::dsl::*;
199 .filter(comment_id.eq(comment_saved_form.comment_id))
200 .filter(user_id.eq(comment_saved_form.user_id)),
208 use crate::{establish_unpooled_connection, Crud, Likeable, ListingType, Saveable, SortType};
209 use lemmy_db_schema::source::{
211 community::{Community, CommunityForm},
213 user::{UserForm, User_},
218 let conn = establish_unpooled_connection();
220 let new_user = UserForm {
221 name: "terry".into(),
222 preferred_username: None,
223 password_encrypted: "nope".into(),
225 matrix_user_id: None,
233 theme: "browser".into(),
234 default_sort_type: SortType::Hot as i16,
235 default_listing_type: ListingType::Subscribed as i16,
236 lang: "browser".into(),
238 send_notifications_to_email: false,
244 last_refreshed_at: None,
247 let inserted_user = User_::create(&conn, &new_user).unwrap();
249 let new_community = CommunityForm {
250 name: "test community".to_string(),
251 title: "nada".to_owned(),
254 creator_id: inserted_user.id,
263 last_refreshed_at: None,
269 let inserted_community = Community::create(&conn, &new_community).unwrap();
271 let new_post = PostForm {
272 name: "A test post".into(),
273 creator_id: inserted_user.id,
276 community_id: inserted_community.id,
284 embed_description: None,
292 let inserted_post = Post::create(&conn, &new_post).unwrap();
294 let comment_form = CommentForm {
295 content: "A test comment".into(),
296 creator_id: inserted_user.id,
297 post_id: inserted_post.id,
308 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
310 let expected_comment = Comment {
311 id: inserted_comment.id,
312 content: "A test comment".into(),
313 creator_id: inserted_user.id,
314 post_id: inserted_post.id,
319 published: inserted_comment.published,
321 ap_id: inserted_comment.ap_id.to_owned(),
325 let child_comment_form = CommentForm {
326 content: "A child comment".into(),
327 creator_id: inserted_user.id,
328 post_id: inserted_post.id,
329 parent_id: Some(inserted_comment.id),
339 let inserted_child_comment = Comment::create(&conn, &child_comment_form).unwrap();
342 let comment_like_form = CommentLikeForm {
343 comment_id: inserted_comment.id,
344 post_id: inserted_post.id,
345 user_id: inserted_user.id,
349 let inserted_comment_like = CommentLike::like(&conn, &comment_like_form).unwrap();
351 let expected_comment_like = CommentLike {
352 id: inserted_comment_like.id,
353 comment_id: inserted_comment.id,
354 post_id: inserted_post.id,
355 user_id: inserted_user.id,
356 published: inserted_comment_like.published,
361 let comment_saved_form = CommentSavedForm {
362 comment_id: inserted_comment.id,
363 user_id: inserted_user.id,
366 let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
368 let expected_comment_saved = CommentSaved {
369 id: inserted_comment_saved.id,
370 comment_id: inserted_comment.id,
371 user_id: inserted_user.id,
372 published: inserted_comment_saved.published,
375 let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
376 let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
377 let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
378 let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
379 let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
380 Comment::delete(&conn, inserted_child_comment.id).unwrap();
381 Post::delete(&conn, inserted_post.id).unwrap();
382 Community::delete(&conn, inserted_community.id).unwrap();
383 User_::delete(&conn, inserted_user.id).unwrap();
385 assert_eq!(expected_comment, read_comment);
386 assert_eq!(expected_comment, inserted_comment);
387 assert_eq!(expected_comment, updated_comment);
388 assert_eq!(expected_comment_like, inserted_comment_like);
389 assert_eq!(expected_comment_saved, inserted_comment_saved);
392 inserted_child_comment.parent_id.unwrap()
394 assert_eq!(1, like_removed);
395 assert_eq!(1, saved_removed);
396 assert_eq!(1, num_deleted);