4 utils::{get_conn, DbPool},
6 use diesel::{dsl::*, result::Error, QueryDsl};
7 use diesel_async::RunQueryDsl;
10 impl Crud for ModRemovePost {
11 type InsertForm = ModRemovePostForm;
12 type UpdateForm = ModRemovePostForm;
14 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
15 use crate::schema::mod_remove_post::dsl::*;
16 let conn = &mut get_conn(pool).await?;
17 mod_remove_post.find(from_id).first::<Self>(conn).await
20 async fn create(pool: &DbPool, form: &ModRemovePostForm) -> Result<Self, Error> {
21 use crate::schema::mod_remove_post::dsl::*;
22 let conn = &mut get_conn(pool).await?;
23 insert_into(mod_remove_post)
25 .get_result::<Self>(conn)
29 async fn update(pool: &DbPool, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
30 use crate::schema::mod_remove_post::dsl::*;
31 let conn = &mut get_conn(pool).await?;
32 diesel::update(mod_remove_post.find(from_id))
34 .get_result::<Self>(conn)
40 impl Crud for ModLockPost {
41 type InsertForm = ModLockPostForm;
42 type UpdateForm = ModLockPostForm;
44 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
45 use crate::schema::mod_lock_post::dsl::*;
46 let conn = &mut get_conn(pool).await?;
47 mod_lock_post.find(from_id).first::<Self>(conn).await
50 async fn create(pool: &DbPool, form: &ModLockPostForm) -> Result<Self, Error> {
51 use crate::schema::mod_lock_post::dsl::*;
52 let conn = &mut get_conn(pool).await?;
53 insert_into(mod_lock_post)
55 .get_result::<Self>(conn)
59 async fn update(pool: &DbPool, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
60 use crate::schema::mod_lock_post::dsl::*;
61 let conn = &mut get_conn(pool).await?;
62 diesel::update(mod_lock_post.find(from_id))
64 .get_result::<Self>(conn)
70 impl Crud for ModStickyPost {
71 type InsertForm = ModStickyPostForm;
72 type UpdateForm = ModStickyPostForm;
74 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
75 use crate::schema::mod_sticky_post::dsl::*;
76 let conn = &mut get_conn(pool).await?;
77 mod_sticky_post.find(from_id).first::<Self>(conn).await
80 async fn create(pool: &DbPool, form: &ModStickyPostForm) -> Result<Self, Error> {
81 use crate::schema::mod_sticky_post::dsl::*;
82 let conn = &mut get_conn(pool).await?;
83 insert_into(mod_sticky_post)
85 .get_result::<Self>(conn)
89 async fn update(pool: &DbPool, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> {
90 use crate::schema::mod_sticky_post::dsl::*;
91 let conn = &mut get_conn(pool).await?;
92 diesel::update(mod_sticky_post.find(from_id))
94 .get_result::<Self>(conn)
100 impl Crud for ModRemoveComment {
101 type InsertForm = ModRemoveCommentForm;
102 type UpdateForm = ModRemoveCommentForm;
104 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
105 use crate::schema::mod_remove_comment::dsl::*;
106 let conn = &mut get_conn(pool).await?;
107 mod_remove_comment.find(from_id).first::<Self>(conn).await
110 async fn create(pool: &DbPool, form: &ModRemoveCommentForm) -> Result<Self, Error> {
111 use crate::schema::mod_remove_comment::dsl::*;
112 let conn = &mut get_conn(pool).await?;
113 insert_into(mod_remove_comment)
115 .get_result::<Self>(conn)
119 async fn update(pool: &DbPool, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
120 use crate::schema::mod_remove_comment::dsl::*;
121 let conn = &mut get_conn(pool).await?;
122 diesel::update(mod_remove_comment.find(from_id))
124 .get_result::<Self>(conn)
130 impl Crud for ModRemoveCommunity {
131 type InsertForm = ModRemoveCommunityForm;
132 type UpdateForm = ModRemoveCommunityForm;
134 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
135 use crate::schema::mod_remove_community::dsl::*;
136 let conn = &mut get_conn(pool).await?;
137 mod_remove_community.find(from_id).first::<Self>(conn).await
140 async fn create(pool: &DbPool, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
141 use crate::schema::mod_remove_community::dsl::*;
142 let conn = &mut get_conn(pool).await?;
143 insert_into(mod_remove_community)
145 .get_result::<Self>(conn)
152 form: &ModRemoveCommunityForm,
153 ) -> Result<Self, Error> {
154 use crate::schema::mod_remove_community::dsl::*;
155 let conn = &mut get_conn(pool).await?;
156 diesel::update(mod_remove_community.find(from_id))
158 .get_result::<Self>(conn)
164 impl Crud for ModBanFromCommunity {
165 type InsertForm = ModBanFromCommunityForm;
166 type UpdateForm = ModBanFromCommunityForm;
168 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
169 use crate::schema::mod_ban_from_community::dsl::*;
170 let conn = &mut get_conn(pool).await?;
171 mod_ban_from_community
177 async fn create(pool: &DbPool, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
178 use crate::schema::mod_ban_from_community::dsl::*;
179 let conn = &mut get_conn(pool).await?;
180 insert_into(mod_ban_from_community)
182 .get_result::<Self>(conn)
189 form: &ModBanFromCommunityForm,
190 ) -> Result<Self, Error> {
191 use crate::schema::mod_ban_from_community::dsl::*;
192 let conn = &mut get_conn(pool).await?;
193 diesel::update(mod_ban_from_community.find(from_id))
195 .get_result::<Self>(conn)
201 impl Crud for ModBan {
202 type InsertForm = ModBanForm;
203 type UpdateForm = ModBanForm;
205 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
206 use crate::schema::mod_ban::dsl::*;
207 let conn = &mut get_conn(pool).await?;
208 mod_ban.find(from_id).first::<Self>(conn).await
211 async fn create(pool: &DbPool, form: &ModBanForm) -> Result<Self, Error> {
212 use crate::schema::mod_ban::dsl::*;
213 let conn = &mut get_conn(pool).await?;
216 .get_result::<Self>(conn)
220 async fn update(pool: &DbPool, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
221 use crate::schema::mod_ban::dsl::*;
222 let conn = &mut get_conn(pool).await?;
223 diesel::update(mod_ban.find(from_id))
225 .get_result::<Self>(conn)
231 impl Crud for ModHideCommunity {
232 type InsertForm = ModHideCommunityForm;
233 type UpdateForm = ModHideCommunityForm;
236 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
237 use crate::schema::mod_hide_community::dsl::*;
238 let conn = &mut get_conn(pool).await?;
239 mod_hide_community.find(from_id).first::<Self>(conn).await
242 async fn create(pool: &DbPool, form: &ModHideCommunityForm) -> Result<Self, Error> {
243 use crate::schema::mod_hide_community::dsl::*;
244 let conn = &mut get_conn(pool).await?;
245 insert_into(mod_hide_community)
247 .get_result::<Self>(conn)
251 async fn update(pool: &DbPool, from_id: i32, form: &ModHideCommunityForm) -> Result<Self, Error> {
252 use crate::schema::mod_hide_community::dsl::*;
253 let conn = &mut get_conn(pool).await?;
254 diesel::update(mod_hide_community.find(from_id))
256 .get_result::<Self>(conn)
262 impl Crud for ModAddCommunity {
263 type InsertForm = ModAddCommunityForm;
264 type UpdateForm = ModAddCommunityForm;
266 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
267 use crate::schema::mod_add_community::dsl::*;
268 let conn = &mut get_conn(pool).await?;
269 mod_add_community.find(from_id).first::<Self>(conn).await
272 async fn create(pool: &DbPool, form: &ModAddCommunityForm) -> Result<Self, Error> {
273 use crate::schema::mod_add_community::dsl::*;
274 let conn = &mut get_conn(pool).await?;
275 insert_into(mod_add_community)
277 .get_result::<Self>(conn)
281 async fn update(pool: &DbPool, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
282 use crate::schema::mod_add_community::dsl::*;
283 let conn = &mut get_conn(pool).await?;
284 diesel::update(mod_add_community.find(from_id))
286 .get_result::<Self>(conn)
292 impl Crud for ModTransferCommunity {
293 type InsertForm = ModTransferCommunityForm;
294 type UpdateForm = ModTransferCommunityForm;
296 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
297 use crate::schema::mod_transfer_community::dsl::*;
298 let conn = &mut get_conn(pool).await?;
299 mod_transfer_community
305 async fn create(pool: &DbPool, form: &ModTransferCommunityForm) -> Result<Self, Error> {
306 use crate::schema::mod_transfer_community::dsl::*;
307 let conn = &mut get_conn(pool).await?;
308 insert_into(mod_transfer_community)
310 .get_result::<Self>(conn)
317 form: &ModTransferCommunityForm,
318 ) -> Result<Self, Error> {
319 use crate::schema::mod_transfer_community::dsl::*;
320 let conn = &mut get_conn(pool).await?;
321 diesel::update(mod_transfer_community.find(from_id))
323 .get_result::<Self>(conn)
329 impl Crud for ModAdd {
330 type InsertForm = ModAddForm;
331 type UpdateForm = ModAddForm;
333 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
334 use crate::schema::mod_add::dsl::*;
335 let conn = &mut get_conn(pool).await?;
336 mod_add.find(from_id).first::<Self>(conn).await
339 async fn create(pool: &DbPool, form: &ModAddForm) -> Result<Self, Error> {
340 use crate::schema::mod_add::dsl::*;
341 let conn = &mut get_conn(pool).await?;
344 .get_result::<Self>(conn)
348 async fn update(pool: &DbPool, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
349 use crate::schema::mod_add::dsl::*;
350 let conn = &mut get_conn(pool).await?;
351 diesel::update(mod_add.find(from_id))
353 .get_result::<Self>(conn)
359 impl Crud for AdminPurgePerson {
360 type InsertForm = AdminPurgePersonForm;
361 type UpdateForm = AdminPurgePersonForm;
363 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
364 use crate::schema::admin_purge_person::dsl::*;
365 let conn = &mut get_conn(pool).await?;
366 admin_purge_person.find(from_id).first::<Self>(conn).await
369 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
370 use crate::schema::admin_purge_person::dsl::*;
371 let conn = &mut get_conn(pool).await?;
372 insert_into(admin_purge_person)
374 .get_result::<Self>(conn)
378 async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result<Self, Error> {
379 use crate::schema::admin_purge_person::dsl::*;
380 let conn = &mut get_conn(pool).await?;
381 diesel::update(admin_purge_person.find(from_id))
383 .get_result::<Self>(conn)
389 impl Crud for AdminPurgeCommunity {
390 type InsertForm = AdminPurgeCommunityForm;
391 type UpdateForm = AdminPurgeCommunityForm;
393 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
394 use crate::schema::admin_purge_community::dsl::*;
395 let conn = &mut get_conn(pool).await?;
396 admin_purge_community
402 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
403 use crate::schema::admin_purge_community::dsl::*;
404 let conn = &mut get_conn(pool).await?;
405 insert_into(admin_purge_community)
407 .get_result::<Self>(conn)
411 async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result<Self, Error> {
412 use crate::schema::admin_purge_community::dsl::*;
413 let conn = &mut get_conn(pool).await?;
414 diesel::update(admin_purge_community.find(from_id))
416 .get_result::<Self>(conn)
422 impl Crud for AdminPurgePost {
423 type InsertForm = AdminPurgePostForm;
424 type UpdateForm = AdminPurgePostForm;
426 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
427 use crate::schema::admin_purge_post::dsl::*;
428 let conn = &mut get_conn(pool).await?;
429 admin_purge_post.find(from_id).first::<Self>(conn).await
432 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
433 use crate::schema::admin_purge_post::dsl::*;
434 let conn = &mut get_conn(pool).await?;
435 insert_into(admin_purge_post)
437 .get_result::<Self>(conn)
441 async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result<Self, Error> {
442 use crate::schema::admin_purge_post::dsl::*;
443 let conn = &mut get_conn(pool).await?;
444 diesel::update(admin_purge_post.find(from_id))
446 .get_result::<Self>(conn)
452 impl Crud for AdminPurgeComment {
453 type InsertForm = AdminPurgeCommentForm;
454 type UpdateForm = AdminPurgeCommentForm;
456 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
457 use crate::schema::admin_purge_comment::dsl::*;
458 let conn = &mut get_conn(pool).await?;
459 admin_purge_comment.find(from_id).first::<Self>(conn).await
462 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
463 use crate::schema::admin_purge_comment::dsl::*;
464 let conn = &mut get_conn(pool).await?;
465 insert_into(admin_purge_comment)
467 .get_result::<Self>(conn)
471 async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result<Self, Error> {
472 use crate::schema::admin_purge_comment::dsl::*;
473 let conn = &mut get_conn(pool).await?;
474 diesel::update(admin_purge_comment.find(from_id))
476 .get_result::<Self>(conn)
484 source::{comment::*, community::*, instance::Instance, moderator::*, person::*, post::*},
486 utils::build_db_pool_for_tests,
488 use serial_test::serial;
491 async fn test_crud() {
492 let pool = &build_db_pool_for_tests().await;
494 let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
496 let new_mod = PersonInsertForm::builder()
497 .name("the mod".into())
498 .public_key("pubkey".to_string())
499 .instance_id(inserted_instance.id)
502 let inserted_mod = Person::create(pool, &new_mod).await.unwrap();
504 let new_person = PersonInsertForm::builder()
506 .public_key("pubkey".to_string())
507 .instance_id(inserted_instance.id)
510 let inserted_person = Person::create(pool, &new_person).await.unwrap();
512 let new_community = CommunityInsertForm::builder()
513 .name("mod_community".to_string())
514 .title("nada".to_owned())
515 .public_key("pubkey".to_string())
516 .instance_id(inserted_instance.id)
519 let inserted_community = Community::create(pool, &new_community).await.unwrap();
521 let new_post = PostInsertForm::builder()
522 .name("A test post thweep".into())
523 .creator_id(inserted_person.id)
524 .community_id(inserted_community.id)
527 let inserted_post = Post::create(pool, &new_post).await.unwrap();
529 let comment_form = CommentInsertForm::builder()
530 .content("A test comment".into())
531 .creator_id(inserted_person.id)
532 .post_id(inserted_post.id)
535 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
537 // Now the actual tests
540 let mod_remove_post_form = ModRemovePostForm {
541 mod_person_id: inserted_mod.id,
542 post_id: inserted_post.id,
546 let inserted_mod_remove_post = ModRemovePost::create(pool, &mod_remove_post_form)
549 let read_mod_remove_post = ModRemovePost::read(pool, inserted_mod_remove_post.id)
552 let expected_mod_remove_post = ModRemovePost {
553 id: inserted_mod_remove_post.id,
554 post_id: inserted_post.id,
555 mod_person_id: inserted_mod.id,
558 when_: inserted_mod_remove_post.when_,
563 let mod_lock_post_form = ModLockPostForm {
564 mod_person_id: inserted_mod.id,
565 post_id: inserted_post.id,
568 let inserted_mod_lock_post = ModLockPost::create(pool, &mod_lock_post_form)
571 let read_mod_lock_post = ModLockPost::read(pool, inserted_mod_lock_post.id)
574 let expected_mod_lock_post = ModLockPost {
575 id: inserted_mod_lock_post.id,
576 post_id: inserted_post.id,
577 mod_person_id: inserted_mod.id,
579 when_: inserted_mod_lock_post.when_,
584 let mod_sticky_post_form = ModStickyPostForm {
585 mod_person_id: inserted_mod.id,
586 post_id: inserted_post.id,
589 let inserted_mod_sticky_post = ModStickyPost::create(pool, &mod_sticky_post_form)
592 let read_mod_sticky_post = ModStickyPost::read(pool, inserted_mod_sticky_post.id)
595 let expected_mod_sticky_post = ModStickyPost {
596 id: inserted_mod_sticky_post.id,
597 post_id: inserted_post.id,
598 mod_person_id: inserted_mod.id,
599 stickied: Some(true),
600 when_: inserted_mod_sticky_post.when_,
605 let mod_remove_comment_form = ModRemoveCommentForm {
606 mod_person_id: inserted_mod.id,
607 comment_id: inserted_comment.id,
611 let inserted_mod_remove_comment = ModRemoveComment::create(pool, &mod_remove_comment_form)
614 let read_mod_remove_comment = ModRemoveComment::read(pool, inserted_mod_remove_comment.id)
617 let expected_mod_remove_comment = ModRemoveComment {
618 id: inserted_mod_remove_comment.id,
619 comment_id: inserted_comment.id,
620 mod_person_id: inserted_mod.id,
623 when_: inserted_mod_remove_comment.when_,
628 let mod_remove_community_form = ModRemoveCommunityForm {
629 mod_person_id: inserted_mod.id,
630 community_id: inserted_community.id,
635 let inserted_mod_remove_community =
636 ModRemoveCommunity::create(pool, &mod_remove_community_form)
639 let read_mod_remove_community =
640 ModRemoveCommunity::read(pool, inserted_mod_remove_community.id)
643 let expected_mod_remove_community = ModRemoveCommunity {
644 id: inserted_mod_remove_community.id,
645 community_id: inserted_community.id,
646 mod_person_id: inserted_mod.id,
650 when_: inserted_mod_remove_community.when_,
653 // ban from community
655 let mod_ban_from_community_form = ModBanFromCommunityForm {
656 mod_person_id: inserted_mod.id,
657 other_person_id: inserted_person.id,
658 community_id: inserted_community.id,
663 let inserted_mod_ban_from_community =
664 ModBanFromCommunity::create(pool, &mod_ban_from_community_form)
667 let read_mod_ban_from_community =
668 ModBanFromCommunity::read(pool, inserted_mod_ban_from_community.id)
671 let expected_mod_ban_from_community = ModBanFromCommunity {
672 id: inserted_mod_ban_from_community.id,
673 community_id: inserted_community.id,
674 mod_person_id: inserted_mod.id,
675 other_person_id: inserted_person.id,
679 when_: inserted_mod_ban_from_community.when_,
684 let mod_ban_form = ModBanForm {
685 mod_person_id: inserted_mod.id,
686 other_person_id: inserted_person.id,
691 let inserted_mod_ban = ModBan::create(pool, &mod_ban_form).await.unwrap();
692 let read_mod_ban = ModBan::read(pool, inserted_mod_ban.id).await.unwrap();
693 let expected_mod_ban = ModBan {
694 id: inserted_mod_ban.id,
695 mod_person_id: inserted_mod.id,
696 other_person_id: inserted_person.id,
700 when_: inserted_mod_ban.when_,
705 let mod_add_community_form = ModAddCommunityForm {
706 mod_person_id: inserted_mod.id,
707 other_person_id: inserted_person.id,
708 community_id: inserted_community.id,
711 let inserted_mod_add_community = ModAddCommunity::create(pool, &mod_add_community_form)
714 let read_mod_add_community = ModAddCommunity::read(pool, inserted_mod_add_community.id)
717 let expected_mod_add_community = ModAddCommunity {
718 id: inserted_mod_add_community.id,
719 community_id: inserted_community.id,
720 mod_person_id: inserted_mod.id,
721 other_person_id: inserted_person.id,
722 removed: Some(false),
723 when_: inserted_mod_add_community.when_,
728 let mod_add_form = ModAddForm {
729 mod_person_id: inserted_mod.id,
730 other_person_id: inserted_person.id,
733 let inserted_mod_add = ModAdd::create(pool, &mod_add_form).await.unwrap();
734 let read_mod_add = ModAdd::read(pool, inserted_mod_add.id).await.unwrap();
735 let expected_mod_add = ModAdd {
736 id: inserted_mod_add.id,
737 mod_person_id: inserted_mod.id,
738 other_person_id: inserted_person.id,
739 removed: Some(false),
740 when_: inserted_mod_add.when_,
743 Comment::delete(pool, inserted_comment.id).await.unwrap();
744 Post::delete(pool, inserted_post.id).await.unwrap();
745 Community::delete(pool, inserted_community.id)
748 Person::delete(pool, inserted_person.id).await.unwrap();
749 Person::delete(pool, inserted_mod.id).await.unwrap();
750 Instance::delete(pool, inserted_instance.id).await.unwrap();
752 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
753 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
754 assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
755 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
756 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
757 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
758 assert_eq!(expected_mod_ban, read_mod_ban);
759 assert_eq!(expected_mod_add_community, read_mod_add_community);
760 assert_eq!(expected_mod_add, read_mod_add);