6 AdminPurgeCommunityForm,
18 ModBanFromCommunityForm,
26 ModRemoveCommunityForm,
32 ModTransferCommunityForm,
35 utils::{get_conn, DbPool},
37 use diesel::{dsl::insert_into, result::Error, QueryDsl};
38 use diesel_async::RunQueryDsl;
41 impl Crud for ModRemovePost {
42 type InsertForm = ModRemovePostForm;
43 type UpdateForm = ModRemovePostForm;
45 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
46 use crate::schema::mod_remove_post::dsl::mod_remove_post;
47 let conn = &mut get_conn(pool).await?;
48 mod_remove_post.find(from_id).first::<Self>(conn).await
51 async fn create(pool: &DbPool, form: &ModRemovePostForm) -> Result<Self, Error> {
52 use crate::schema::mod_remove_post::dsl::mod_remove_post;
53 let conn = &mut get_conn(pool).await?;
54 insert_into(mod_remove_post)
56 .get_result::<Self>(conn)
60 async fn update(pool: &DbPool, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
61 use crate::schema::mod_remove_post::dsl::mod_remove_post;
62 let conn = &mut get_conn(pool).await?;
63 diesel::update(mod_remove_post.find(from_id))
65 .get_result::<Self>(conn)
71 impl Crud for ModLockPost {
72 type InsertForm = ModLockPostForm;
73 type UpdateForm = ModLockPostForm;
75 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
76 use crate::schema::mod_lock_post::dsl::mod_lock_post;
77 let conn = &mut get_conn(pool).await?;
78 mod_lock_post.find(from_id).first::<Self>(conn).await
81 async fn create(pool: &DbPool, form: &ModLockPostForm) -> Result<Self, Error> {
82 use crate::schema::mod_lock_post::dsl::mod_lock_post;
83 let conn = &mut get_conn(pool).await?;
84 insert_into(mod_lock_post)
86 .get_result::<Self>(conn)
90 async fn update(pool: &DbPool, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
91 use crate::schema::mod_lock_post::dsl::mod_lock_post;
92 let conn = &mut get_conn(pool).await?;
93 diesel::update(mod_lock_post.find(from_id))
95 .get_result::<Self>(conn)
101 impl Crud for ModStickyPost {
102 type InsertForm = ModStickyPostForm;
103 type UpdateForm = ModStickyPostForm;
105 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
106 use crate::schema::mod_sticky_post::dsl::mod_sticky_post;
107 let conn = &mut get_conn(pool).await?;
108 mod_sticky_post.find(from_id).first::<Self>(conn).await
111 async fn create(pool: &DbPool, form: &ModStickyPostForm) -> Result<Self, Error> {
112 use crate::schema::mod_sticky_post::dsl::mod_sticky_post;
113 let conn = &mut get_conn(pool).await?;
114 insert_into(mod_sticky_post)
116 .get_result::<Self>(conn)
120 async fn update(pool: &DbPool, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> {
121 use crate::schema::mod_sticky_post::dsl::mod_sticky_post;
122 let conn = &mut get_conn(pool).await?;
123 diesel::update(mod_sticky_post.find(from_id))
125 .get_result::<Self>(conn)
131 impl Crud for ModRemoveComment {
132 type InsertForm = ModRemoveCommentForm;
133 type UpdateForm = ModRemoveCommentForm;
135 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
136 use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
137 let conn = &mut get_conn(pool).await?;
138 mod_remove_comment.find(from_id).first::<Self>(conn).await
141 async fn create(pool: &DbPool, form: &ModRemoveCommentForm) -> Result<Self, Error> {
142 use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
143 let conn = &mut get_conn(pool).await?;
144 insert_into(mod_remove_comment)
146 .get_result::<Self>(conn)
150 async fn update(pool: &DbPool, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
151 use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
152 let conn = &mut get_conn(pool).await?;
153 diesel::update(mod_remove_comment.find(from_id))
155 .get_result::<Self>(conn)
161 impl Crud for ModRemoveCommunity {
162 type InsertForm = ModRemoveCommunityForm;
163 type UpdateForm = ModRemoveCommunityForm;
165 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
166 use crate::schema::mod_remove_community::dsl::mod_remove_community;
167 let conn = &mut get_conn(pool).await?;
168 mod_remove_community.find(from_id).first::<Self>(conn).await
171 async fn create(pool: &DbPool, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
172 use crate::schema::mod_remove_community::dsl::mod_remove_community;
173 let conn = &mut get_conn(pool).await?;
174 insert_into(mod_remove_community)
176 .get_result::<Self>(conn)
183 form: &ModRemoveCommunityForm,
184 ) -> Result<Self, Error> {
185 use crate::schema::mod_remove_community::dsl::mod_remove_community;
186 let conn = &mut get_conn(pool).await?;
187 diesel::update(mod_remove_community.find(from_id))
189 .get_result::<Self>(conn)
195 impl Crud for ModBanFromCommunity {
196 type InsertForm = ModBanFromCommunityForm;
197 type UpdateForm = ModBanFromCommunityForm;
199 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
200 use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
201 let conn = &mut get_conn(pool).await?;
202 mod_ban_from_community
208 async fn create(pool: &DbPool, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
209 use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
210 let conn = &mut get_conn(pool).await?;
211 insert_into(mod_ban_from_community)
213 .get_result::<Self>(conn)
220 form: &ModBanFromCommunityForm,
221 ) -> Result<Self, Error> {
222 use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
223 let conn = &mut get_conn(pool).await?;
224 diesel::update(mod_ban_from_community.find(from_id))
226 .get_result::<Self>(conn)
232 impl Crud for ModBan {
233 type InsertForm = ModBanForm;
234 type UpdateForm = ModBanForm;
236 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
237 use crate::schema::mod_ban::dsl::mod_ban;
238 let conn = &mut get_conn(pool).await?;
239 mod_ban.find(from_id).first::<Self>(conn).await
242 async fn create(pool: &DbPool, form: &ModBanForm) -> Result<Self, Error> {
243 use crate::schema::mod_ban::dsl::mod_ban;
244 let conn = &mut get_conn(pool).await?;
247 .get_result::<Self>(conn)
251 async fn update(pool: &DbPool, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
252 use crate::schema::mod_ban::dsl::mod_ban;
253 let conn = &mut get_conn(pool).await?;
254 diesel::update(mod_ban.find(from_id))
256 .get_result::<Self>(conn)
262 impl Crud for ModHideCommunity {
263 type InsertForm = ModHideCommunityForm;
264 type UpdateForm = ModHideCommunityForm;
267 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
268 use crate::schema::mod_hide_community::dsl::mod_hide_community;
269 let conn = &mut get_conn(pool).await?;
270 mod_hide_community.find(from_id).first::<Self>(conn).await
273 async fn create(pool: &DbPool, form: &ModHideCommunityForm) -> Result<Self, Error> {
274 use crate::schema::mod_hide_community::dsl::mod_hide_community;
275 let conn = &mut get_conn(pool).await?;
276 insert_into(mod_hide_community)
278 .get_result::<Self>(conn)
282 async fn update(pool: &DbPool, from_id: i32, form: &ModHideCommunityForm) -> Result<Self, Error> {
283 use crate::schema::mod_hide_community::dsl::mod_hide_community;
284 let conn = &mut get_conn(pool).await?;
285 diesel::update(mod_hide_community.find(from_id))
287 .get_result::<Self>(conn)
293 impl Crud for ModAddCommunity {
294 type InsertForm = ModAddCommunityForm;
295 type UpdateForm = ModAddCommunityForm;
297 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
298 use crate::schema::mod_add_community::dsl::mod_add_community;
299 let conn = &mut get_conn(pool).await?;
300 mod_add_community.find(from_id).first::<Self>(conn).await
303 async fn create(pool: &DbPool, form: &ModAddCommunityForm) -> Result<Self, Error> {
304 use crate::schema::mod_add_community::dsl::mod_add_community;
305 let conn = &mut get_conn(pool).await?;
306 insert_into(mod_add_community)
308 .get_result::<Self>(conn)
312 async fn update(pool: &DbPool, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
313 use crate::schema::mod_add_community::dsl::mod_add_community;
314 let conn = &mut get_conn(pool).await?;
315 diesel::update(mod_add_community.find(from_id))
317 .get_result::<Self>(conn)
323 impl Crud for ModTransferCommunity {
324 type InsertForm = ModTransferCommunityForm;
325 type UpdateForm = ModTransferCommunityForm;
327 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
328 use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
329 let conn = &mut get_conn(pool).await?;
330 mod_transfer_community
336 async fn create(pool: &DbPool, form: &ModTransferCommunityForm) -> Result<Self, Error> {
337 use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
338 let conn = &mut get_conn(pool).await?;
339 insert_into(mod_transfer_community)
341 .get_result::<Self>(conn)
348 form: &ModTransferCommunityForm,
349 ) -> Result<Self, Error> {
350 use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
351 let conn = &mut get_conn(pool).await?;
352 diesel::update(mod_transfer_community.find(from_id))
354 .get_result::<Self>(conn)
360 impl Crud for ModAdd {
361 type InsertForm = ModAddForm;
362 type UpdateForm = ModAddForm;
364 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
365 use crate::schema::mod_add::dsl::mod_add;
366 let conn = &mut get_conn(pool).await?;
367 mod_add.find(from_id).first::<Self>(conn).await
370 async fn create(pool: &DbPool, form: &ModAddForm) -> Result<Self, Error> {
371 use crate::schema::mod_add::dsl::mod_add;
372 let conn = &mut get_conn(pool).await?;
375 .get_result::<Self>(conn)
379 async fn update(pool: &DbPool, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
380 use crate::schema::mod_add::dsl::mod_add;
381 let conn = &mut get_conn(pool).await?;
382 diesel::update(mod_add.find(from_id))
384 .get_result::<Self>(conn)
390 impl Crud for AdminPurgePerson {
391 type InsertForm = AdminPurgePersonForm;
392 type UpdateForm = AdminPurgePersonForm;
394 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
395 use crate::schema::admin_purge_person::dsl::admin_purge_person;
396 let conn = &mut get_conn(pool).await?;
397 admin_purge_person.find(from_id).first::<Self>(conn).await
400 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
401 use crate::schema::admin_purge_person::dsl::admin_purge_person;
402 let conn = &mut get_conn(pool).await?;
403 insert_into(admin_purge_person)
405 .get_result::<Self>(conn)
409 async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result<Self, Error> {
410 use crate::schema::admin_purge_person::dsl::admin_purge_person;
411 let conn = &mut get_conn(pool).await?;
412 diesel::update(admin_purge_person.find(from_id))
414 .get_result::<Self>(conn)
420 impl Crud for AdminPurgeCommunity {
421 type InsertForm = AdminPurgeCommunityForm;
422 type UpdateForm = AdminPurgeCommunityForm;
424 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
425 use crate::schema::admin_purge_community::dsl::admin_purge_community;
426 let conn = &mut get_conn(pool).await?;
427 admin_purge_community
433 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
434 use crate::schema::admin_purge_community::dsl::admin_purge_community;
435 let conn = &mut get_conn(pool).await?;
436 insert_into(admin_purge_community)
438 .get_result::<Self>(conn)
442 async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result<Self, Error> {
443 use crate::schema::admin_purge_community::dsl::admin_purge_community;
444 let conn = &mut get_conn(pool).await?;
445 diesel::update(admin_purge_community.find(from_id))
447 .get_result::<Self>(conn)
453 impl Crud for AdminPurgePost {
454 type InsertForm = AdminPurgePostForm;
455 type UpdateForm = AdminPurgePostForm;
457 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
458 use crate::schema::admin_purge_post::dsl::admin_purge_post;
459 let conn = &mut get_conn(pool).await?;
460 admin_purge_post.find(from_id).first::<Self>(conn).await
463 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
464 use crate::schema::admin_purge_post::dsl::admin_purge_post;
465 let conn = &mut get_conn(pool).await?;
466 insert_into(admin_purge_post)
468 .get_result::<Self>(conn)
472 async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result<Self, Error> {
473 use crate::schema::admin_purge_post::dsl::admin_purge_post;
474 let conn = &mut get_conn(pool).await?;
475 diesel::update(admin_purge_post.find(from_id))
477 .get_result::<Self>(conn)
483 impl Crud for AdminPurgeComment {
484 type InsertForm = AdminPurgeCommentForm;
485 type UpdateForm = AdminPurgeCommentForm;
487 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
488 use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
489 let conn = &mut get_conn(pool).await?;
490 admin_purge_comment.find(from_id).first::<Self>(conn).await
493 async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
494 use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
495 let conn = &mut get_conn(pool).await?;
496 insert_into(admin_purge_comment)
498 .get_result::<Self>(conn)
502 async fn update(pool: &DbPool, from_id: i32, form: &Self::InsertForm) -> Result<Self, Error> {
503 use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
504 let conn = &mut get_conn(pool).await?;
505 diesel::update(admin_purge_comment.find(from_id))
507 .get_result::<Self>(conn)
516 comment::{Comment, CommentInsertForm},
517 community::{Community, CommunityInsertForm},
527 ModBanFromCommunityForm,
531 ModRemoveCommentForm,
533 ModRemoveCommunityForm,
539 person::{Person, PersonInsertForm},
540 post::{Post, PostInsertForm},
543 utils::build_db_pool_for_tests,
545 use serial_test::serial;
549 async fn test_crud() {
550 let pool = &build_db_pool_for_tests().await;
552 let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
554 let new_mod = PersonInsertForm::builder()
555 .name("the mod".into())
556 .public_key("pubkey".to_string())
557 .instance_id(inserted_instance.id)
560 let inserted_mod = Person::create(pool, &new_mod).await.unwrap();
562 let new_person = PersonInsertForm::builder()
564 .public_key("pubkey".to_string())
565 .instance_id(inserted_instance.id)
568 let inserted_person = Person::create(pool, &new_person).await.unwrap();
570 let new_community = CommunityInsertForm::builder()
571 .name("mod_community".to_string())
572 .title("nada".to_owned())
573 .public_key("pubkey".to_string())
574 .instance_id(inserted_instance.id)
577 let inserted_community = Community::create(pool, &new_community).await.unwrap();
579 let new_post = PostInsertForm::builder()
580 .name("A test post thweep".into())
581 .creator_id(inserted_person.id)
582 .community_id(inserted_community.id)
585 let inserted_post = Post::create(pool, &new_post).await.unwrap();
587 let comment_form = CommentInsertForm::builder()
588 .content("A test comment".into())
589 .creator_id(inserted_person.id)
590 .post_id(inserted_post.id)
593 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
595 // Now the actual tests
598 let mod_remove_post_form = ModRemovePostForm {
599 mod_person_id: inserted_mod.id,
600 post_id: inserted_post.id,
604 let inserted_mod_remove_post = ModRemovePost::create(pool, &mod_remove_post_form)
607 let read_mod_remove_post = ModRemovePost::read(pool, inserted_mod_remove_post.id)
610 let expected_mod_remove_post = ModRemovePost {
611 id: inserted_mod_remove_post.id,
612 post_id: inserted_post.id,
613 mod_person_id: inserted_mod.id,
616 when_: inserted_mod_remove_post.when_,
621 let mod_lock_post_form = ModLockPostForm {
622 mod_person_id: inserted_mod.id,
623 post_id: inserted_post.id,
626 let inserted_mod_lock_post = ModLockPost::create(pool, &mod_lock_post_form)
629 let read_mod_lock_post = ModLockPost::read(pool, inserted_mod_lock_post.id)
632 let expected_mod_lock_post = ModLockPost {
633 id: inserted_mod_lock_post.id,
634 post_id: inserted_post.id,
635 mod_person_id: inserted_mod.id,
637 when_: inserted_mod_lock_post.when_,
642 let mod_sticky_post_form = ModStickyPostForm {
643 mod_person_id: inserted_mod.id,
644 post_id: inserted_post.id,
647 let inserted_mod_sticky_post = ModStickyPost::create(pool, &mod_sticky_post_form)
650 let read_mod_sticky_post = ModStickyPost::read(pool, inserted_mod_sticky_post.id)
653 let expected_mod_sticky_post = ModStickyPost {
654 id: inserted_mod_sticky_post.id,
655 post_id: inserted_post.id,
656 mod_person_id: inserted_mod.id,
657 stickied: Some(true),
658 when_: inserted_mod_sticky_post.when_,
663 let mod_remove_comment_form = ModRemoveCommentForm {
664 mod_person_id: inserted_mod.id,
665 comment_id: inserted_comment.id,
669 let inserted_mod_remove_comment = ModRemoveComment::create(pool, &mod_remove_comment_form)
672 let read_mod_remove_comment = ModRemoveComment::read(pool, inserted_mod_remove_comment.id)
675 let expected_mod_remove_comment = ModRemoveComment {
676 id: inserted_mod_remove_comment.id,
677 comment_id: inserted_comment.id,
678 mod_person_id: inserted_mod.id,
681 when_: inserted_mod_remove_comment.when_,
686 let mod_remove_community_form = ModRemoveCommunityForm {
687 mod_person_id: inserted_mod.id,
688 community_id: inserted_community.id,
693 let inserted_mod_remove_community =
694 ModRemoveCommunity::create(pool, &mod_remove_community_form)
697 let read_mod_remove_community =
698 ModRemoveCommunity::read(pool, inserted_mod_remove_community.id)
701 let expected_mod_remove_community = ModRemoveCommunity {
702 id: inserted_mod_remove_community.id,
703 community_id: inserted_community.id,
704 mod_person_id: inserted_mod.id,
708 when_: inserted_mod_remove_community.when_,
711 // ban from community
713 let mod_ban_from_community_form = ModBanFromCommunityForm {
714 mod_person_id: inserted_mod.id,
715 other_person_id: inserted_person.id,
716 community_id: inserted_community.id,
721 let inserted_mod_ban_from_community =
722 ModBanFromCommunity::create(pool, &mod_ban_from_community_form)
725 let read_mod_ban_from_community =
726 ModBanFromCommunity::read(pool, inserted_mod_ban_from_community.id)
729 let expected_mod_ban_from_community = ModBanFromCommunity {
730 id: inserted_mod_ban_from_community.id,
731 community_id: inserted_community.id,
732 mod_person_id: inserted_mod.id,
733 other_person_id: inserted_person.id,
737 when_: inserted_mod_ban_from_community.when_,
742 let mod_ban_form = ModBanForm {
743 mod_person_id: inserted_mod.id,
744 other_person_id: inserted_person.id,
749 let inserted_mod_ban = ModBan::create(pool, &mod_ban_form).await.unwrap();
750 let read_mod_ban = ModBan::read(pool, inserted_mod_ban.id).await.unwrap();
751 let expected_mod_ban = ModBan {
752 id: inserted_mod_ban.id,
753 mod_person_id: inserted_mod.id,
754 other_person_id: inserted_person.id,
758 when_: inserted_mod_ban.when_,
763 let mod_add_community_form = ModAddCommunityForm {
764 mod_person_id: inserted_mod.id,
765 other_person_id: inserted_person.id,
766 community_id: inserted_community.id,
769 let inserted_mod_add_community = ModAddCommunity::create(pool, &mod_add_community_form)
772 let read_mod_add_community = ModAddCommunity::read(pool, inserted_mod_add_community.id)
775 let expected_mod_add_community = ModAddCommunity {
776 id: inserted_mod_add_community.id,
777 community_id: inserted_community.id,
778 mod_person_id: inserted_mod.id,
779 other_person_id: inserted_person.id,
780 removed: Some(false),
781 when_: inserted_mod_add_community.when_,
786 let mod_add_form = ModAddForm {
787 mod_person_id: inserted_mod.id,
788 other_person_id: inserted_person.id,
791 let inserted_mod_add = ModAdd::create(pool, &mod_add_form).await.unwrap();
792 let read_mod_add = ModAdd::read(pool, inserted_mod_add.id).await.unwrap();
793 let expected_mod_add = ModAdd {
794 id: inserted_mod_add.id,
795 mod_person_id: inserted_mod.id,
796 other_person_id: inserted_person.id,
797 removed: Some(false),
798 when_: inserted_mod_add.when_,
801 Comment::delete(pool, inserted_comment.id).await.unwrap();
802 Post::delete(pool, inserted_post.id).await.unwrap();
803 Community::delete(pool, inserted_community.id)
806 Person::delete(pool, inserted_person.id).await.unwrap();
807 Person::delete(pool, inserted_mod.id).await.unwrap();
808 Instance::delete(pool, inserted_instance.id).await.unwrap();
810 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
811 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
812 assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
813 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
814 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
815 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
816 assert_eq!(expected_mod_ban, read_mod_ban);
817 assert_eq!(expected_mod_add_community, read_mod_add_community);
818 assert_eq!(expected_mod_add, read_mod_add);