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;
46 async fn create(pool: &mut DbPool<'_>, form: &ModRemovePostForm) -> Result<Self, Error> {
47 use crate::schema::mod_remove_post::dsl::mod_remove_post;
48 let conn = &mut get_conn(pool).await?;
49 insert_into(mod_remove_post)
51 .get_result::<Self>(conn)
56 pool: &mut DbPool<'_>,
58 form: &ModRemovePostForm,
59 ) -> Result<Self, Error> {
60 use crate::schema::mod_remove_post::dsl::mod_remove_post;
61 let conn = &mut get_conn(pool).await?;
62 diesel::update(mod_remove_post.find(from_id))
64 .get_result::<Self>(conn)
70 impl Crud for ModLockPost {
71 type InsertForm = ModLockPostForm;
72 type UpdateForm = ModLockPostForm;
75 async fn create(pool: &mut DbPool<'_>, form: &ModLockPostForm) -> Result<Self, Error> {
76 use crate::schema::mod_lock_post::dsl::mod_lock_post;
77 let conn = &mut get_conn(pool).await?;
78 insert_into(mod_lock_post)
80 .get_result::<Self>(conn)
85 pool: &mut DbPool<'_>,
87 form: &ModLockPostForm,
88 ) -> Result<Self, Error> {
89 use crate::schema::mod_lock_post::dsl::mod_lock_post;
90 let conn = &mut get_conn(pool).await?;
91 diesel::update(mod_lock_post.find(from_id))
93 .get_result::<Self>(conn)
99 impl Crud for ModFeaturePost {
100 type InsertForm = ModFeaturePostForm;
101 type UpdateForm = ModFeaturePostForm;
104 async fn create(pool: &mut DbPool<'_>, form: &ModFeaturePostForm) -> Result<Self, Error> {
105 use crate::schema::mod_feature_post::dsl::mod_feature_post;
106 let conn = &mut get_conn(pool).await?;
107 insert_into(mod_feature_post)
109 .get_result::<Self>(conn)
114 pool: &mut DbPool<'_>,
116 form: &ModFeaturePostForm,
117 ) -> Result<Self, Error> {
118 use crate::schema::mod_feature_post::dsl::mod_feature_post;
119 let conn = &mut get_conn(pool).await?;
120 diesel::update(mod_feature_post.find(from_id))
122 .get_result::<Self>(conn)
128 impl Crud for ModRemoveComment {
129 type InsertForm = ModRemoveCommentForm;
130 type UpdateForm = ModRemoveCommentForm;
133 async fn create(pool: &mut DbPool<'_>, form: &ModRemoveCommentForm) -> Result<Self, Error> {
134 use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
135 let conn = &mut get_conn(pool).await?;
136 insert_into(mod_remove_comment)
138 .get_result::<Self>(conn)
143 pool: &mut DbPool<'_>,
145 form: &ModRemoveCommentForm,
146 ) -> Result<Self, Error> {
147 use crate::schema::mod_remove_comment::dsl::mod_remove_comment;
148 let conn = &mut get_conn(pool).await?;
149 diesel::update(mod_remove_comment.find(from_id))
151 .get_result::<Self>(conn)
157 impl Crud for ModRemoveCommunity {
158 type InsertForm = ModRemoveCommunityForm;
159 type UpdateForm = ModRemoveCommunityForm;
162 async fn create(pool: &mut DbPool<'_>, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
163 use crate::schema::mod_remove_community::dsl::mod_remove_community;
164 let conn = &mut get_conn(pool).await?;
165 insert_into(mod_remove_community)
167 .get_result::<Self>(conn)
172 pool: &mut DbPool<'_>,
174 form: &ModRemoveCommunityForm,
175 ) -> Result<Self, Error> {
176 use crate::schema::mod_remove_community::dsl::mod_remove_community;
177 let conn = &mut get_conn(pool).await?;
178 diesel::update(mod_remove_community.find(from_id))
180 .get_result::<Self>(conn)
186 impl Crud for ModBanFromCommunity {
187 type InsertForm = ModBanFromCommunityForm;
188 type UpdateForm = ModBanFromCommunityForm;
191 async fn create(pool: &mut DbPool<'_>, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
192 use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
193 let conn = &mut get_conn(pool).await?;
194 insert_into(mod_ban_from_community)
196 .get_result::<Self>(conn)
201 pool: &mut DbPool<'_>,
203 form: &ModBanFromCommunityForm,
204 ) -> Result<Self, Error> {
205 use crate::schema::mod_ban_from_community::dsl::mod_ban_from_community;
206 let conn = &mut get_conn(pool).await?;
207 diesel::update(mod_ban_from_community.find(from_id))
209 .get_result::<Self>(conn)
215 impl Crud for ModBan {
216 type InsertForm = ModBanForm;
217 type UpdateForm = ModBanForm;
220 async fn create(pool: &mut DbPool<'_>, form: &ModBanForm) -> Result<Self, Error> {
221 use crate::schema::mod_ban::dsl::mod_ban;
222 let conn = &mut get_conn(pool).await?;
225 .get_result::<Self>(conn)
229 async fn update(pool: &mut DbPool<'_>, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
230 use crate::schema::mod_ban::dsl::mod_ban;
231 let conn = &mut get_conn(pool).await?;
232 diesel::update(mod_ban.find(from_id))
234 .get_result::<Self>(conn)
240 impl Crud for ModHideCommunity {
241 type InsertForm = ModHideCommunityForm;
242 type UpdateForm = ModHideCommunityForm;
245 async fn create(pool: &mut DbPool<'_>, form: &ModHideCommunityForm) -> Result<Self, Error> {
246 use crate::schema::mod_hide_community::dsl::mod_hide_community;
247 let conn = &mut get_conn(pool).await?;
248 insert_into(mod_hide_community)
250 .get_result::<Self>(conn)
255 pool: &mut DbPool<'_>,
257 form: &ModHideCommunityForm,
258 ) -> Result<Self, Error> {
259 use crate::schema::mod_hide_community::dsl::mod_hide_community;
260 let conn = &mut get_conn(pool).await?;
261 diesel::update(mod_hide_community.find(from_id))
263 .get_result::<Self>(conn)
269 impl Crud for ModAddCommunity {
270 type InsertForm = ModAddCommunityForm;
271 type UpdateForm = ModAddCommunityForm;
274 async fn create(pool: &mut DbPool<'_>, form: &ModAddCommunityForm) -> Result<Self, Error> {
275 use crate::schema::mod_add_community::dsl::mod_add_community;
276 let conn = &mut get_conn(pool).await?;
277 insert_into(mod_add_community)
279 .get_result::<Self>(conn)
284 pool: &mut DbPool<'_>,
286 form: &ModAddCommunityForm,
287 ) -> Result<Self, Error> {
288 use crate::schema::mod_add_community::dsl::mod_add_community;
289 let conn = &mut get_conn(pool).await?;
290 diesel::update(mod_add_community.find(from_id))
292 .get_result::<Self>(conn)
298 impl Crud for ModTransferCommunity {
299 type InsertForm = ModTransferCommunityForm;
300 type UpdateForm = ModTransferCommunityForm;
303 async fn create(pool: &mut DbPool<'_>, form: &ModTransferCommunityForm) -> Result<Self, Error> {
304 use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
305 let conn = &mut get_conn(pool).await?;
306 insert_into(mod_transfer_community)
308 .get_result::<Self>(conn)
313 pool: &mut DbPool<'_>,
315 form: &ModTransferCommunityForm,
316 ) -> Result<Self, Error> {
317 use crate::schema::mod_transfer_community::dsl::mod_transfer_community;
318 let conn = &mut get_conn(pool).await?;
319 diesel::update(mod_transfer_community.find(from_id))
321 .get_result::<Self>(conn)
327 impl Crud for ModAdd {
328 type InsertForm = ModAddForm;
329 type UpdateForm = ModAddForm;
332 async fn create(pool: &mut DbPool<'_>, form: &ModAddForm) -> Result<Self, Error> {
333 use crate::schema::mod_add::dsl::mod_add;
334 let conn = &mut get_conn(pool).await?;
337 .get_result::<Self>(conn)
341 async fn update(pool: &mut DbPool<'_>, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
342 use crate::schema::mod_add::dsl::mod_add;
343 let conn = &mut get_conn(pool).await?;
344 diesel::update(mod_add.find(from_id))
346 .get_result::<Self>(conn)
352 impl Crud for AdminPurgePerson {
353 type InsertForm = AdminPurgePersonForm;
354 type UpdateForm = AdminPurgePersonForm;
357 async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
358 use crate::schema::admin_purge_person::dsl::admin_purge_person;
359 let conn = &mut get_conn(pool).await?;
360 insert_into(admin_purge_person)
362 .get_result::<Self>(conn)
367 pool: &mut DbPool<'_>,
369 form: &Self::InsertForm,
370 ) -> Result<Self, Error> {
371 use crate::schema::admin_purge_person::dsl::admin_purge_person;
372 let conn = &mut get_conn(pool).await?;
373 diesel::update(admin_purge_person.find(from_id))
375 .get_result::<Self>(conn)
381 impl Crud for AdminPurgeCommunity {
382 type InsertForm = AdminPurgeCommunityForm;
383 type UpdateForm = AdminPurgeCommunityForm;
386 async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
387 use crate::schema::admin_purge_community::dsl::admin_purge_community;
388 let conn = &mut get_conn(pool).await?;
389 insert_into(admin_purge_community)
391 .get_result::<Self>(conn)
396 pool: &mut DbPool<'_>,
398 form: &Self::InsertForm,
399 ) -> Result<Self, Error> {
400 use crate::schema::admin_purge_community::dsl::admin_purge_community;
401 let conn = &mut get_conn(pool).await?;
402 diesel::update(admin_purge_community.find(from_id))
404 .get_result::<Self>(conn)
410 impl Crud for AdminPurgePost {
411 type InsertForm = AdminPurgePostForm;
412 type UpdateForm = AdminPurgePostForm;
415 async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
416 use crate::schema::admin_purge_post::dsl::admin_purge_post;
417 let conn = &mut get_conn(pool).await?;
418 insert_into(admin_purge_post)
420 .get_result::<Self>(conn)
425 pool: &mut DbPool<'_>,
427 form: &Self::InsertForm,
428 ) -> Result<Self, Error> {
429 use crate::schema::admin_purge_post::dsl::admin_purge_post;
430 let conn = &mut get_conn(pool).await?;
431 diesel::update(admin_purge_post.find(from_id))
433 .get_result::<Self>(conn)
439 impl Crud for AdminPurgeComment {
440 type InsertForm = AdminPurgeCommentForm;
441 type UpdateForm = AdminPurgeCommentForm;
444 async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
445 use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
446 let conn = &mut get_conn(pool).await?;
447 insert_into(admin_purge_comment)
449 .get_result::<Self>(conn)
454 pool: &mut DbPool<'_>,
456 form: &Self::InsertForm,
457 ) -> Result<Self, Error> {
458 use crate::schema::admin_purge_comment::dsl::admin_purge_comment;
459 let conn = &mut get_conn(pool).await?;
460 diesel::update(admin_purge_comment.find(from_id))
462 .get_result::<Self>(conn)
469 #![allow(clippy::unwrap_used)]
470 #![allow(clippy::indexing_slicing)]
474 comment::{Comment, CommentInsertForm},
475 community::{Community, CommunityInsertForm},
485 ModBanFromCommunityForm,
491 ModRemoveCommentForm,
493 ModRemoveCommunityForm,
497 person::{Person, PersonInsertForm},
498 post::{Post, PostInsertForm},
501 utils::build_db_pool_for_tests,
503 use serial_test::serial;
507 async fn test_crud() {
508 let pool = &build_db_pool_for_tests().await;
509 let pool = &mut pool.into();
511 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
515 let new_mod = PersonInsertForm::builder()
516 .name("the mod".into())
517 .public_key("pubkey".to_string())
518 .instance_id(inserted_instance.id)
521 let inserted_mod = Person::create(pool, &new_mod).await.unwrap();
523 let new_person = PersonInsertForm::builder()
525 .public_key("pubkey".to_string())
526 .instance_id(inserted_instance.id)
529 let inserted_person = Person::create(pool, &new_person).await.unwrap();
531 let new_community = CommunityInsertForm::builder()
532 .name("mod_community".to_string())
533 .title("nada".to_owned())
534 .public_key("pubkey".to_string())
535 .instance_id(inserted_instance.id)
538 let inserted_community = Community::create(pool, &new_community).await.unwrap();
540 let new_post = PostInsertForm::builder()
541 .name("A test post thweep".into())
542 .creator_id(inserted_person.id)
543 .community_id(inserted_community.id)
546 let inserted_post = Post::create(pool, &new_post).await.unwrap();
548 let comment_form = CommentInsertForm::builder()
549 .content("A test comment".into())
550 .creator_id(inserted_person.id)
551 .post_id(inserted_post.id)
554 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
556 // Now the actual tests
559 let mod_remove_post_form = ModRemovePostForm {
560 mod_person_id: inserted_mod.id,
561 post_id: inserted_post.id,
565 let inserted_mod_remove_post = ModRemovePost::create(pool, &mod_remove_post_form)
568 let read_mod_remove_post = ModRemovePost::read(pool, inserted_mod_remove_post.id)
571 let expected_mod_remove_post = ModRemovePost {
572 id: inserted_mod_remove_post.id,
573 post_id: inserted_post.id,
574 mod_person_id: inserted_mod.id,
577 when_: inserted_mod_remove_post.when_,
582 let mod_lock_post_form = ModLockPostForm {
583 mod_person_id: inserted_mod.id,
584 post_id: inserted_post.id,
587 let inserted_mod_lock_post = ModLockPost::create(pool, &mod_lock_post_form)
590 let read_mod_lock_post = ModLockPost::read(pool, inserted_mod_lock_post.id)
593 let expected_mod_lock_post = ModLockPost {
594 id: inserted_mod_lock_post.id,
595 post_id: inserted_post.id,
596 mod_person_id: inserted_mod.id,
598 when_: inserted_mod_lock_post.when_,
603 let mod_feature_post_form = ModFeaturePostForm {
604 mod_person_id: inserted_mod.id,
605 post_id: inserted_post.id,
607 is_featured_community: true,
609 let inserted_mod_feature_post = ModFeaturePost::create(pool, &mod_feature_post_form)
612 let read_mod_feature_post = ModFeaturePost::read(pool, inserted_mod_feature_post.id)
615 let expected_mod_feature_post = ModFeaturePost {
616 id: inserted_mod_feature_post.id,
617 post_id: inserted_post.id,
618 mod_person_id: inserted_mod.id,
620 is_featured_community: true,
621 when_: inserted_mod_feature_post.when_,
626 let mod_remove_comment_form = ModRemoveCommentForm {
627 mod_person_id: inserted_mod.id,
628 comment_id: inserted_comment.id,
632 let inserted_mod_remove_comment = ModRemoveComment::create(pool, &mod_remove_comment_form)
635 let read_mod_remove_comment = ModRemoveComment::read(pool, inserted_mod_remove_comment.id)
638 let expected_mod_remove_comment = ModRemoveComment {
639 id: inserted_mod_remove_comment.id,
640 comment_id: inserted_comment.id,
641 mod_person_id: inserted_mod.id,
644 when_: inserted_mod_remove_comment.when_,
649 let mod_remove_community_form = ModRemoveCommunityForm {
650 mod_person_id: inserted_mod.id,
651 community_id: inserted_community.id,
656 let inserted_mod_remove_community =
657 ModRemoveCommunity::create(pool, &mod_remove_community_form)
660 let read_mod_remove_community =
661 ModRemoveCommunity::read(pool, inserted_mod_remove_community.id)
664 let expected_mod_remove_community = ModRemoveCommunity {
665 id: inserted_mod_remove_community.id,
666 community_id: inserted_community.id,
667 mod_person_id: inserted_mod.id,
671 when_: inserted_mod_remove_community.when_,
674 // ban from community
676 let mod_ban_from_community_form = ModBanFromCommunityForm {
677 mod_person_id: inserted_mod.id,
678 other_person_id: inserted_person.id,
679 community_id: inserted_community.id,
684 let inserted_mod_ban_from_community =
685 ModBanFromCommunity::create(pool, &mod_ban_from_community_form)
688 let read_mod_ban_from_community =
689 ModBanFromCommunity::read(pool, inserted_mod_ban_from_community.id)
692 let expected_mod_ban_from_community = ModBanFromCommunity {
693 id: inserted_mod_ban_from_community.id,
694 community_id: inserted_community.id,
695 mod_person_id: inserted_mod.id,
696 other_person_id: inserted_person.id,
700 when_: inserted_mod_ban_from_community.when_,
705 let mod_ban_form = ModBanForm {
706 mod_person_id: inserted_mod.id,
707 other_person_id: inserted_person.id,
712 let inserted_mod_ban = ModBan::create(pool, &mod_ban_form).await.unwrap();
713 let read_mod_ban = ModBan::read(pool, inserted_mod_ban.id).await.unwrap();
714 let expected_mod_ban = ModBan {
715 id: inserted_mod_ban.id,
716 mod_person_id: inserted_mod.id,
717 other_person_id: inserted_person.id,
721 when_: inserted_mod_ban.when_,
726 let mod_add_community_form = ModAddCommunityForm {
727 mod_person_id: inserted_mod.id,
728 other_person_id: inserted_person.id,
729 community_id: inserted_community.id,
732 let inserted_mod_add_community = ModAddCommunity::create(pool, &mod_add_community_form)
735 let read_mod_add_community = ModAddCommunity::read(pool, inserted_mod_add_community.id)
738 let expected_mod_add_community = ModAddCommunity {
739 id: inserted_mod_add_community.id,
740 community_id: inserted_community.id,
741 mod_person_id: inserted_mod.id,
742 other_person_id: inserted_person.id,
744 when_: inserted_mod_add_community.when_,
749 let mod_add_form = ModAddForm {
750 mod_person_id: inserted_mod.id,
751 other_person_id: inserted_person.id,
754 let inserted_mod_add = ModAdd::create(pool, &mod_add_form).await.unwrap();
755 let read_mod_add = ModAdd::read(pool, inserted_mod_add.id).await.unwrap();
756 let expected_mod_add = ModAdd {
757 id: inserted_mod_add.id,
758 mod_person_id: inserted_mod.id,
759 other_person_id: inserted_person.id,
761 when_: inserted_mod_add.when_,
764 Comment::delete(pool, inserted_comment.id).await.unwrap();
765 Post::delete(pool, inserted_post.id).await.unwrap();
766 Community::delete(pool, inserted_community.id)
769 Person::delete(pool, inserted_person.id).await.unwrap();
770 Person::delete(pool, inserted_mod.id).await.unwrap();
771 Instance::delete(pool, inserted_instance.id).await.unwrap();
773 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
774 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
775 assert_eq!(expected_mod_feature_post, read_mod_feature_post);
776 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
777 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
778 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
779 assert_eq!(expected_mod_ban, read_mod_ban);
780 assert_eq!(expected_mod_add_community, read_mod_add_community);
781 assert_eq!(expected_mod_add, read_mod_add);