6 AdminPurgeCommunityForm,
18 ModBanFromCommunityForm,
28 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 ModFeaturePost {
102 type InsertForm = ModFeaturePostForm;
103 type UpdateForm = ModFeaturePostForm;
105 async fn read(pool: &DbPool, from_id: i32) -> Result<Self, Error> {
106 use crate::schema::mod_feature_post::dsl::mod_feature_post;
107 let conn = &mut get_conn(pool).await?;
108 mod_feature_post.find(from_id).first::<Self>(conn).await
111 async fn create(pool: &DbPool, form: &ModFeaturePostForm) -> Result<Self, Error> {
112 use crate::schema::mod_feature_post::dsl::mod_feature_post;
113 let conn = &mut get_conn(pool).await?;
114 insert_into(mod_feature_post)
116 .get_result::<Self>(conn)
120 async fn update(pool: &DbPool, from_id: i32, form: &ModFeaturePostForm) -> Result<Self, Error> {
121 use crate::schema::mod_feature_post::dsl::mod_feature_post;
122 let conn = &mut get_conn(pool).await?;
123 diesel::update(mod_feature_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,
533 ModRemoveCommentForm,
535 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::read_or_create(pool, "my_domain.tld".to_string())
556 let new_mod = PersonInsertForm::builder()
557 .name("the mod".into())
558 .public_key("pubkey".to_string())
559 .instance_id(inserted_instance.id)
562 let inserted_mod = Person::create(pool, &new_mod).await.unwrap();
564 let new_person = PersonInsertForm::builder()
566 .public_key("pubkey".to_string())
567 .instance_id(inserted_instance.id)
570 let inserted_person = Person::create(pool, &new_person).await.unwrap();
572 let new_community = CommunityInsertForm::builder()
573 .name("mod_community".to_string())
574 .title("nada".to_owned())
575 .public_key("pubkey".to_string())
576 .instance_id(inserted_instance.id)
579 let inserted_community = Community::create(pool, &new_community).await.unwrap();
581 let new_post = PostInsertForm::builder()
582 .name("A test post thweep".into())
583 .creator_id(inserted_person.id)
584 .community_id(inserted_community.id)
587 let inserted_post = Post::create(pool, &new_post).await.unwrap();
589 let comment_form = CommentInsertForm::builder()
590 .content("A test comment".into())
591 .creator_id(inserted_person.id)
592 .post_id(inserted_post.id)
595 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
597 // Now the actual tests
600 let mod_remove_post_form = ModRemovePostForm {
601 mod_person_id: inserted_mod.id,
602 post_id: inserted_post.id,
606 let inserted_mod_remove_post = ModRemovePost::create(pool, &mod_remove_post_form)
609 let read_mod_remove_post = ModRemovePost::read(pool, inserted_mod_remove_post.id)
612 let expected_mod_remove_post = ModRemovePost {
613 id: inserted_mod_remove_post.id,
614 post_id: inserted_post.id,
615 mod_person_id: inserted_mod.id,
618 when_: inserted_mod_remove_post.when_,
623 let mod_lock_post_form = ModLockPostForm {
624 mod_person_id: inserted_mod.id,
625 post_id: inserted_post.id,
628 let inserted_mod_lock_post = ModLockPost::create(pool, &mod_lock_post_form)
631 let read_mod_lock_post = ModLockPost::read(pool, inserted_mod_lock_post.id)
634 let expected_mod_lock_post = ModLockPost {
635 id: inserted_mod_lock_post.id,
636 post_id: inserted_post.id,
637 mod_person_id: inserted_mod.id,
639 when_: inserted_mod_lock_post.when_,
644 let mod_feature_post_form = ModFeaturePostForm {
645 mod_person_id: inserted_mod.id,
646 post_id: inserted_post.id,
648 is_featured_community: true,
650 let inserted_mod_feature_post = ModFeaturePost::create(pool, &mod_feature_post_form)
653 let read_mod_feature_post = ModFeaturePost::read(pool, inserted_mod_feature_post.id)
656 let expected_mod_feature_post = ModFeaturePost {
657 id: inserted_mod_feature_post.id,
658 post_id: inserted_post.id,
659 mod_person_id: inserted_mod.id,
661 is_featured_community: true,
662 when_: inserted_mod_feature_post.when_,
667 let mod_remove_comment_form = ModRemoveCommentForm {
668 mod_person_id: inserted_mod.id,
669 comment_id: inserted_comment.id,
673 let inserted_mod_remove_comment = ModRemoveComment::create(pool, &mod_remove_comment_form)
676 let read_mod_remove_comment = ModRemoveComment::read(pool, inserted_mod_remove_comment.id)
679 let expected_mod_remove_comment = ModRemoveComment {
680 id: inserted_mod_remove_comment.id,
681 comment_id: inserted_comment.id,
682 mod_person_id: inserted_mod.id,
685 when_: inserted_mod_remove_comment.when_,
690 let mod_remove_community_form = ModRemoveCommunityForm {
691 mod_person_id: inserted_mod.id,
692 community_id: inserted_community.id,
697 let inserted_mod_remove_community =
698 ModRemoveCommunity::create(pool, &mod_remove_community_form)
701 let read_mod_remove_community =
702 ModRemoveCommunity::read(pool, inserted_mod_remove_community.id)
705 let expected_mod_remove_community = ModRemoveCommunity {
706 id: inserted_mod_remove_community.id,
707 community_id: inserted_community.id,
708 mod_person_id: inserted_mod.id,
712 when_: inserted_mod_remove_community.when_,
715 // ban from community
717 let mod_ban_from_community_form = ModBanFromCommunityForm {
718 mod_person_id: inserted_mod.id,
719 other_person_id: inserted_person.id,
720 community_id: inserted_community.id,
725 let inserted_mod_ban_from_community =
726 ModBanFromCommunity::create(pool, &mod_ban_from_community_form)
729 let read_mod_ban_from_community =
730 ModBanFromCommunity::read(pool, inserted_mod_ban_from_community.id)
733 let expected_mod_ban_from_community = ModBanFromCommunity {
734 id: inserted_mod_ban_from_community.id,
735 community_id: inserted_community.id,
736 mod_person_id: inserted_mod.id,
737 other_person_id: inserted_person.id,
741 when_: inserted_mod_ban_from_community.when_,
746 let mod_ban_form = ModBanForm {
747 mod_person_id: inserted_mod.id,
748 other_person_id: inserted_person.id,
753 let inserted_mod_ban = ModBan::create(pool, &mod_ban_form).await.unwrap();
754 let read_mod_ban = ModBan::read(pool, inserted_mod_ban.id).await.unwrap();
755 let expected_mod_ban = ModBan {
756 id: inserted_mod_ban.id,
757 mod_person_id: inserted_mod.id,
758 other_person_id: inserted_person.id,
762 when_: inserted_mod_ban.when_,
767 let mod_add_community_form = ModAddCommunityForm {
768 mod_person_id: inserted_mod.id,
769 other_person_id: inserted_person.id,
770 community_id: inserted_community.id,
773 let inserted_mod_add_community = ModAddCommunity::create(pool, &mod_add_community_form)
776 let read_mod_add_community = ModAddCommunity::read(pool, inserted_mod_add_community.id)
779 let expected_mod_add_community = ModAddCommunity {
780 id: inserted_mod_add_community.id,
781 community_id: inserted_community.id,
782 mod_person_id: inserted_mod.id,
783 other_person_id: inserted_person.id,
785 when_: inserted_mod_add_community.when_,
790 let mod_add_form = ModAddForm {
791 mod_person_id: inserted_mod.id,
792 other_person_id: inserted_person.id,
795 let inserted_mod_add = ModAdd::create(pool, &mod_add_form).await.unwrap();
796 let read_mod_add = ModAdd::read(pool, inserted_mod_add.id).await.unwrap();
797 let expected_mod_add = ModAdd {
798 id: inserted_mod_add.id,
799 mod_person_id: inserted_mod.id,
800 other_person_id: inserted_person.id,
802 when_: inserted_mod_add.when_,
805 Comment::delete(pool, inserted_comment.id).await.unwrap();
806 Post::delete(pool, inserted_post.id).await.unwrap();
807 Community::delete(pool, inserted_community.id)
810 Person::delete(pool, inserted_person.id).await.unwrap();
811 Person::delete(pool, inserted_mod.id).await.unwrap();
812 Instance::delete(pool, inserted_instance.id).await.unwrap();
814 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
815 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
816 assert_eq!(expected_mod_feature_post, read_mod_feature_post);
817 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
818 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
819 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
820 assert_eq!(expected_mod_ban, read_mod_ban);
821 assert_eq!(expected_mod_add_community, read_mod_add_community);
822 assert_eq!(expected_mod_add, read_mod_add);