1 use crate::{source::moderator::*, traits::Crud};
2 use diesel::{dsl::*, result::Error, *};
4 impl Crud for ModRemovePost {
5 type Form = ModRemovePostForm;
7 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
8 use crate::schema::mod_remove_post::dsl::*;
9 mod_remove_post.find(from_id).first::<Self>(conn)
12 fn create(conn: &mut PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
13 use crate::schema::mod_remove_post::dsl::*;
14 insert_into(mod_remove_post)
16 .get_result::<Self>(conn)
20 conn: &mut PgConnection,
22 form: &ModRemovePostForm,
23 ) -> Result<Self, Error> {
24 use crate::schema::mod_remove_post::dsl::*;
25 diesel::update(mod_remove_post.find(from_id))
27 .get_result::<Self>(conn)
31 impl Crud for ModLockPost {
32 type Form = ModLockPostForm;
34 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
35 use crate::schema::mod_lock_post::dsl::*;
36 mod_lock_post.find(from_id).first::<Self>(conn)
39 fn create(conn: &mut PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
40 use crate::schema::mod_lock_post::dsl::*;
41 insert_into(mod_lock_post)
43 .get_result::<Self>(conn)
46 fn update(conn: &mut PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
47 use crate::schema::mod_lock_post::dsl::*;
48 diesel::update(mod_lock_post.find(from_id))
50 .get_result::<Self>(conn)
54 impl Crud for ModStickyPost {
55 type Form = ModStickyPostForm;
57 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
58 use crate::schema::mod_sticky_post::dsl::*;
59 mod_sticky_post.find(from_id).first::<Self>(conn)
62 fn create(conn: &mut PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
63 use crate::schema::mod_sticky_post::dsl::*;
64 insert_into(mod_sticky_post)
66 .get_result::<Self>(conn)
70 conn: &mut PgConnection,
72 form: &ModStickyPostForm,
73 ) -> Result<Self, Error> {
74 use crate::schema::mod_sticky_post::dsl::*;
75 diesel::update(mod_sticky_post.find(from_id))
77 .get_result::<Self>(conn)
81 impl Crud for ModRemoveComment {
82 type Form = ModRemoveCommentForm;
84 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
85 use crate::schema::mod_remove_comment::dsl::*;
86 mod_remove_comment.find(from_id).first::<Self>(conn)
89 fn create(conn: &mut PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
90 use crate::schema::mod_remove_comment::dsl::*;
91 insert_into(mod_remove_comment)
93 .get_result::<Self>(conn)
97 conn: &mut PgConnection,
99 form: &ModRemoveCommentForm,
100 ) -> Result<Self, Error> {
101 use crate::schema::mod_remove_comment::dsl::*;
102 diesel::update(mod_remove_comment.find(from_id))
104 .get_result::<Self>(conn)
108 impl Crud for ModRemoveCommunity {
109 type Form = ModRemoveCommunityForm;
111 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
112 use crate::schema::mod_remove_community::dsl::*;
113 mod_remove_community.find(from_id).first::<Self>(conn)
116 fn create(conn: &mut PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
117 use crate::schema::mod_remove_community::dsl::*;
118 insert_into(mod_remove_community)
120 .get_result::<Self>(conn)
124 conn: &mut PgConnection,
126 form: &ModRemoveCommunityForm,
127 ) -> Result<Self, Error> {
128 use crate::schema::mod_remove_community::dsl::*;
129 diesel::update(mod_remove_community.find(from_id))
131 .get_result::<Self>(conn)
135 impl Crud for ModBanFromCommunity {
136 type Form = ModBanFromCommunityForm;
138 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
139 use crate::schema::mod_ban_from_community::dsl::*;
140 mod_ban_from_community.find(from_id).first::<Self>(conn)
143 fn create(conn: &mut PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
144 use crate::schema::mod_ban_from_community::dsl::*;
145 insert_into(mod_ban_from_community)
147 .get_result::<Self>(conn)
151 conn: &mut PgConnection,
153 form: &ModBanFromCommunityForm,
154 ) -> Result<Self, Error> {
155 use crate::schema::mod_ban_from_community::dsl::*;
156 diesel::update(mod_ban_from_community.find(from_id))
158 .get_result::<Self>(conn)
162 impl Crud for ModBan {
163 type Form = ModBanForm;
165 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
166 use crate::schema::mod_ban::dsl::*;
167 mod_ban.find(from_id).first::<Self>(conn)
170 fn create(conn: &mut PgConnection, form: &ModBanForm) -> Result<Self, Error> {
171 use crate::schema::mod_ban::dsl::*;
172 insert_into(mod_ban).values(form).get_result::<Self>(conn)
175 fn update(conn: &mut PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
176 use crate::schema::mod_ban::dsl::*;
177 diesel::update(mod_ban.find(from_id))
179 .get_result::<Self>(conn)
183 impl Crud for ModHideCommunity {
184 type Form = ModHideCommunityForm;
187 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
188 use crate::schema::mod_hide_community::dsl::*;
189 mod_hide_community.find(from_id).first::<Self>(conn)
192 fn create(conn: &mut PgConnection, form: &ModHideCommunityForm) -> Result<Self, Error> {
193 use crate::schema::mod_hide_community::dsl::*;
194 insert_into(mod_hide_community)
196 .get_result::<Self>(conn)
200 conn: &mut PgConnection,
202 form: &ModHideCommunityForm,
203 ) -> Result<Self, Error> {
204 use crate::schema::mod_hide_community::dsl::*;
205 diesel::update(mod_hide_community.find(from_id))
207 .get_result::<Self>(conn)
211 impl Crud for ModAddCommunity {
212 type Form = ModAddCommunityForm;
214 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
215 use crate::schema::mod_add_community::dsl::*;
216 mod_add_community.find(from_id).first::<Self>(conn)
219 fn create(conn: &mut PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
220 use crate::schema::mod_add_community::dsl::*;
221 insert_into(mod_add_community)
223 .get_result::<Self>(conn)
227 conn: &mut PgConnection,
229 form: &ModAddCommunityForm,
230 ) -> Result<Self, Error> {
231 use crate::schema::mod_add_community::dsl::*;
232 diesel::update(mod_add_community.find(from_id))
234 .get_result::<Self>(conn)
238 impl Crud for ModTransferCommunity {
239 type Form = ModTransferCommunityForm;
241 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
242 use crate::schema::mod_transfer_community::dsl::*;
243 mod_transfer_community.find(from_id).first::<Self>(conn)
246 fn create(conn: &mut PgConnection, form: &ModTransferCommunityForm) -> Result<Self, Error> {
247 use crate::schema::mod_transfer_community::dsl::*;
248 insert_into(mod_transfer_community)
250 .get_result::<Self>(conn)
254 conn: &mut PgConnection,
256 form: &ModTransferCommunityForm,
257 ) -> Result<Self, Error> {
258 use crate::schema::mod_transfer_community::dsl::*;
259 diesel::update(mod_transfer_community.find(from_id))
261 .get_result::<Self>(conn)
265 impl Crud for ModAdd {
266 type Form = ModAddForm;
268 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
269 use crate::schema::mod_add::dsl::*;
270 mod_add.find(from_id).first::<Self>(conn)
273 fn create(conn: &mut PgConnection, form: &ModAddForm) -> Result<Self, Error> {
274 use crate::schema::mod_add::dsl::*;
275 insert_into(mod_add).values(form).get_result::<Self>(conn)
278 fn update(conn: &mut PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
279 use crate::schema::mod_add::dsl::*;
280 diesel::update(mod_add.find(from_id))
282 .get_result::<Self>(conn)
286 impl Crud for AdminPurgePerson {
287 type Form = AdminPurgePersonForm;
289 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
290 use crate::schema::admin_purge_person::dsl::*;
291 admin_purge_person.find(from_id).first::<Self>(conn)
294 fn create(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error> {
295 use crate::schema::admin_purge_person::dsl::*;
296 insert_into(admin_purge_person)
298 .get_result::<Self>(conn)
301 fn update(conn: &mut PgConnection, from_id: i32, form: &Self::Form) -> Result<Self, Error> {
302 use crate::schema::admin_purge_person::dsl::*;
303 diesel::update(admin_purge_person.find(from_id))
305 .get_result::<Self>(conn)
309 impl Crud for AdminPurgeCommunity {
310 type Form = AdminPurgeCommunityForm;
312 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
313 use crate::schema::admin_purge_community::dsl::*;
314 admin_purge_community.find(from_id).first::<Self>(conn)
317 fn create(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error> {
318 use crate::schema::admin_purge_community::dsl::*;
319 insert_into(admin_purge_community)
321 .get_result::<Self>(conn)
324 fn update(conn: &mut PgConnection, from_id: i32, form: &Self::Form) -> Result<Self, Error> {
325 use crate::schema::admin_purge_community::dsl::*;
326 diesel::update(admin_purge_community.find(from_id))
328 .get_result::<Self>(conn)
332 impl Crud for AdminPurgePost {
333 type Form = AdminPurgePostForm;
335 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
336 use crate::schema::admin_purge_post::dsl::*;
337 admin_purge_post.find(from_id).first::<Self>(conn)
340 fn create(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error> {
341 use crate::schema::admin_purge_post::dsl::*;
342 insert_into(admin_purge_post)
344 .get_result::<Self>(conn)
347 fn update(conn: &mut PgConnection, from_id: i32, form: &Self::Form) -> Result<Self, Error> {
348 use crate::schema::admin_purge_post::dsl::*;
349 diesel::update(admin_purge_post.find(from_id))
351 .get_result::<Self>(conn)
355 impl Crud for AdminPurgeComment {
356 type Form = AdminPurgeCommentForm;
358 fn read(conn: &mut PgConnection, from_id: i32) -> Result<Self, Error> {
359 use crate::schema::admin_purge_comment::dsl::*;
360 admin_purge_comment.find(from_id).first::<Self>(conn)
363 fn create(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error> {
364 use crate::schema::admin_purge_comment::dsl::*;
365 insert_into(admin_purge_comment)
367 .get_result::<Self>(conn)
370 fn update(conn: &mut PgConnection, from_id: i32, form: &Self::Form) -> Result<Self, Error> {
371 use crate::schema::admin_purge_comment::dsl::*;
372 diesel::update(admin_purge_comment.find(from_id))
374 .get_result::<Self>(conn)
381 source::{comment::*, community::*, moderator::*, person::*, post::*},
383 utils::establish_unpooled_connection,
385 use serial_test::serial;
391 let conn = &mut establish_unpooled_connection();
393 let new_mod = PersonForm {
394 name: "the mod".into(),
395 public_key: Some("pubkey".to_string()),
396 ..PersonForm::default()
399 let inserted_mod = Person::create(conn, &new_mod).unwrap();
401 let new_person = PersonForm {
403 public_key: Some("pubkey".to_string()),
404 ..PersonForm::default()
407 let inserted_person = Person::create(conn, &new_person).unwrap();
409 let new_community = CommunityForm {
410 name: "mod_community".to_string(),
411 title: "nada".to_owned(),
412 public_key: Some("pubkey".to_string()),
413 ..CommunityForm::default()
416 let inserted_community = Community::create(conn, &new_community).unwrap();
418 let new_post = PostForm {
419 name: "A test post thweep".into(),
420 creator_id: inserted_person.id,
421 community_id: inserted_community.id,
422 ..PostForm::default()
425 let inserted_post = Post::create(conn, &new_post).unwrap();
427 let comment_form = CommentForm {
428 content: "A test comment".into(),
429 creator_id: inserted_person.id,
430 post_id: inserted_post.id,
431 ..CommentForm::default()
434 let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
436 // Now the actual tests
439 let mod_remove_post_form = ModRemovePostForm {
440 mod_person_id: inserted_mod.id,
441 post_id: inserted_post.id,
445 let inserted_mod_remove_post = ModRemovePost::create(conn, &mod_remove_post_form).unwrap();
446 let read_mod_remove_post = ModRemovePost::read(conn, inserted_mod_remove_post.id).unwrap();
447 let expected_mod_remove_post = ModRemovePost {
448 id: inserted_mod_remove_post.id,
449 post_id: inserted_post.id,
450 mod_person_id: inserted_mod.id,
453 when_: inserted_mod_remove_post.when_,
458 let mod_lock_post_form = ModLockPostForm {
459 mod_person_id: inserted_mod.id,
460 post_id: inserted_post.id,
463 let inserted_mod_lock_post = ModLockPost::create(conn, &mod_lock_post_form).unwrap();
464 let read_mod_lock_post = ModLockPost::read(conn, inserted_mod_lock_post.id).unwrap();
465 let expected_mod_lock_post = ModLockPost {
466 id: inserted_mod_lock_post.id,
467 post_id: inserted_post.id,
468 mod_person_id: inserted_mod.id,
470 when_: inserted_mod_lock_post.when_,
475 let mod_sticky_post_form = ModStickyPostForm {
476 mod_person_id: inserted_mod.id,
477 post_id: inserted_post.id,
480 let inserted_mod_sticky_post = ModStickyPost::create(conn, &mod_sticky_post_form).unwrap();
481 let read_mod_sticky_post = ModStickyPost::read(conn, inserted_mod_sticky_post.id).unwrap();
482 let expected_mod_sticky_post = ModStickyPost {
483 id: inserted_mod_sticky_post.id,
484 post_id: inserted_post.id,
485 mod_person_id: inserted_mod.id,
486 stickied: Some(true),
487 when_: inserted_mod_sticky_post.when_,
492 let mod_remove_comment_form = ModRemoveCommentForm {
493 mod_person_id: inserted_mod.id,
494 comment_id: inserted_comment.id,
498 let inserted_mod_remove_comment =
499 ModRemoveComment::create(conn, &mod_remove_comment_form).unwrap();
500 let read_mod_remove_comment =
501 ModRemoveComment::read(conn, inserted_mod_remove_comment.id).unwrap();
502 let expected_mod_remove_comment = ModRemoveComment {
503 id: inserted_mod_remove_comment.id,
504 comment_id: inserted_comment.id,
505 mod_person_id: inserted_mod.id,
508 when_: inserted_mod_remove_comment.when_,
513 let mod_remove_community_form = ModRemoveCommunityForm {
514 mod_person_id: inserted_mod.id,
515 community_id: inserted_community.id,
520 let inserted_mod_remove_community =
521 ModRemoveCommunity::create(conn, &mod_remove_community_form).unwrap();
522 let read_mod_remove_community =
523 ModRemoveCommunity::read(conn, inserted_mod_remove_community.id).unwrap();
524 let expected_mod_remove_community = ModRemoveCommunity {
525 id: inserted_mod_remove_community.id,
526 community_id: inserted_community.id,
527 mod_person_id: inserted_mod.id,
531 when_: inserted_mod_remove_community.when_,
534 // ban from community
536 let mod_ban_from_community_form = ModBanFromCommunityForm {
537 mod_person_id: inserted_mod.id,
538 other_person_id: inserted_person.id,
539 community_id: inserted_community.id,
544 let inserted_mod_ban_from_community =
545 ModBanFromCommunity::create(conn, &mod_ban_from_community_form).unwrap();
546 let read_mod_ban_from_community =
547 ModBanFromCommunity::read(conn, inserted_mod_ban_from_community.id).unwrap();
548 let expected_mod_ban_from_community = ModBanFromCommunity {
549 id: inserted_mod_ban_from_community.id,
550 community_id: inserted_community.id,
551 mod_person_id: inserted_mod.id,
552 other_person_id: inserted_person.id,
556 when_: inserted_mod_ban_from_community.when_,
561 let mod_ban_form = ModBanForm {
562 mod_person_id: inserted_mod.id,
563 other_person_id: inserted_person.id,
568 let inserted_mod_ban = ModBan::create(conn, &mod_ban_form).unwrap();
569 let read_mod_ban = ModBan::read(conn, inserted_mod_ban.id).unwrap();
570 let expected_mod_ban = ModBan {
571 id: inserted_mod_ban.id,
572 mod_person_id: inserted_mod.id,
573 other_person_id: inserted_person.id,
577 when_: inserted_mod_ban.when_,
582 let mod_add_community_form = ModAddCommunityForm {
583 mod_person_id: inserted_mod.id,
584 other_person_id: inserted_person.id,
585 community_id: inserted_community.id,
588 let inserted_mod_add_community =
589 ModAddCommunity::create(conn, &mod_add_community_form).unwrap();
590 let read_mod_add_community =
591 ModAddCommunity::read(conn, inserted_mod_add_community.id).unwrap();
592 let expected_mod_add_community = ModAddCommunity {
593 id: inserted_mod_add_community.id,
594 community_id: inserted_community.id,
595 mod_person_id: inserted_mod.id,
596 other_person_id: inserted_person.id,
597 removed: Some(false),
598 when_: inserted_mod_add_community.when_,
603 let mod_add_form = ModAddForm {
604 mod_person_id: inserted_mod.id,
605 other_person_id: inserted_person.id,
608 let inserted_mod_add = ModAdd::create(conn, &mod_add_form).unwrap();
609 let read_mod_add = ModAdd::read(conn, inserted_mod_add.id).unwrap();
610 let expected_mod_add = ModAdd {
611 id: inserted_mod_add.id,
612 mod_person_id: inserted_mod.id,
613 other_person_id: inserted_person.id,
614 removed: Some(false),
615 when_: inserted_mod_add.when_,
618 Comment::delete(conn, inserted_comment.id).unwrap();
619 Post::delete(conn, inserted_post.id).unwrap();
620 Community::delete(conn, inserted_community.id).unwrap();
621 Person::delete(conn, inserted_person.id).unwrap();
622 Person::delete(conn, inserted_mod.id).unwrap();
624 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
625 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
626 assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
627 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
628 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
629 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
630 assert_eq!(expected_mod_ban, read_mod_ban);
631 assert_eq!(expected_mod_add_community, read_mod_add_community);
632 assert_eq!(expected_mod_add, read_mod_add);