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: &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: &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)
19 fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
20 use crate::schema::mod_remove_post::dsl::*;
21 diesel::update(mod_remove_post.find(from_id))
23 .get_result::<Self>(conn)
27 impl Crud for ModLockPost {
28 type Form = ModLockPostForm;
30 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
31 use crate::schema::mod_lock_post::dsl::*;
32 mod_lock_post.find(from_id).first::<Self>(conn)
35 fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
36 use crate::schema::mod_lock_post::dsl::*;
37 insert_into(mod_lock_post)
39 .get_result::<Self>(conn)
42 fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
43 use crate::schema::mod_lock_post::dsl::*;
44 diesel::update(mod_lock_post.find(from_id))
46 .get_result::<Self>(conn)
50 impl Crud for ModStickyPost {
51 type Form = ModStickyPostForm;
53 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
54 use crate::schema::mod_sticky_post::dsl::*;
55 mod_sticky_post.find(from_id).first::<Self>(conn)
58 fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
59 use crate::schema::mod_sticky_post::dsl::*;
60 insert_into(mod_sticky_post)
62 .get_result::<Self>(conn)
65 fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> {
66 use crate::schema::mod_sticky_post::dsl::*;
67 diesel::update(mod_sticky_post.find(from_id))
69 .get_result::<Self>(conn)
73 impl Crud for ModRemoveComment {
74 type Form = ModRemoveCommentForm;
76 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
77 use crate::schema::mod_remove_comment::dsl::*;
78 mod_remove_comment.find(from_id).first::<Self>(conn)
81 fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
82 use crate::schema::mod_remove_comment::dsl::*;
83 insert_into(mod_remove_comment)
85 .get_result::<Self>(conn)
88 fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
89 use crate::schema::mod_remove_comment::dsl::*;
90 diesel::update(mod_remove_comment.find(from_id))
92 .get_result::<Self>(conn)
96 impl Crud for ModRemoveCommunity {
97 type Form = ModRemoveCommunityForm;
99 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
100 use crate::schema::mod_remove_community::dsl::*;
101 mod_remove_community.find(from_id).first::<Self>(conn)
104 fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
105 use crate::schema::mod_remove_community::dsl::*;
106 insert_into(mod_remove_community)
108 .get_result::<Self>(conn)
114 form: &ModRemoveCommunityForm,
115 ) -> Result<Self, Error> {
116 use crate::schema::mod_remove_community::dsl::*;
117 diesel::update(mod_remove_community.find(from_id))
119 .get_result::<Self>(conn)
123 impl Crud for ModBanFromCommunity {
124 type Form = ModBanFromCommunityForm;
126 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
127 use crate::schema::mod_ban_from_community::dsl::*;
128 mod_ban_from_community.find(from_id).first::<Self>(conn)
131 fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
132 use crate::schema::mod_ban_from_community::dsl::*;
133 insert_into(mod_ban_from_community)
135 .get_result::<Self>(conn)
141 form: &ModBanFromCommunityForm,
142 ) -> Result<Self, Error> {
143 use crate::schema::mod_ban_from_community::dsl::*;
144 diesel::update(mod_ban_from_community.find(from_id))
146 .get_result::<Self>(conn)
150 impl Crud for ModBan {
151 type Form = ModBanForm;
153 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
154 use crate::schema::mod_ban::dsl::*;
155 mod_ban.find(from_id).first::<Self>(conn)
158 fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
159 use crate::schema::mod_ban::dsl::*;
160 insert_into(mod_ban).values(form).get_result::<Self>(conn)
163 fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
164 use crate::schema::mod_ban::dsl::*;
165 diesel::update(mod_ban.find(from_id))
167 .get_result::<Self>(conn)
171 impl Crud for ModHideCommunity {
172 type Form = ModHideCommunityForm;
175 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
176 use crate::schema::mod_hide_community::dsl::*;
177 mod_hide_community.find(from_id).first::<Self>(conn)
180 fn create(conn: &PgConnection, form: &ModHideCommunityForm) -> Result<Self, Error> {
181 use crate::schema::mod_hide_community::dsl::*;
182 insert_into(mod_hide_community)
184 .get_result::<Self>(conn)
187 fn update(conn: &PgConnection, from_id: i32, form: &ModHideCommunityForm) -> Result<Self, Error> {
188 use crate::schema::mod_hide_community::dsl::*;
189 diesel::update(mod_hide_community.find(from_id))
191 .get_result::<Self>(conn)
195 impl Crud for ModAddCommunity {
196 type Form = ModAddCommunityForm;
198 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
199 use crate::schema::mod_add_community::dsl::*;
200 mod_add_community.find(from_id).first::<Self>(conn)
203 fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
204 use crate::schema::mod_add_community::dsl::*;
205 insert_into(mod_add_community)
207 .get_result::<Self>(conn)
210 fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
211 use crate::schema::mod_add_community::dsl::*;
212 diesel::update(mod_add_community.find(from_id))
214 .get_result::<Self>(conn)
218 impl Crud for ModTransferCommunity {
219 type Form = ModTransferCommunityForm;
221 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
222 use crate::schema::mod_transfer_community::dsl::*;
223 mod_transfer_community.find(from_id).first::<Self>(conn)
226 fn create(conn: &PgConnection, form: &ModTransferCommunityForm) -> Result<Self, Error> {
227 use crate::schema::mod_transfer_community::dsl::*;
228 insert_into(mod_transfer_community)
230 .get_result::<Self>(conn)
236 form: &ModTransferCommunityForm,
237 ) -> Result<Self, Error> {
238 use crate::schema::mod_transfer_community::dsl::*;
239 diesel::update(mod_transfer_community.find(from_id))
241 .get_result::<Self>(conn)
245 impl Crud for ModAdd {
246 type Form = ModAddForm;
248 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
249 use crate::schema::mod_add::dsl::*;
250 mod_add.find(from_id).first::<Self>(conn)
253 fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
254 use crate::schema::mod_add::dsl::*;
255 insert_into(mod_add).values(form).get_result::<Self>(conn)
258 fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
259 use crate::schema::mod_add::dsl::*;
260 diesel::update(mod_add.find(from_id))
262 .get_result::<Self>(conn)
266 impl Crud for AdminPurgePerson {
267 type Form = AdminPurgePersonForm;
269 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
270 use crate::schema::admin_purge_person::dsl::*;
271 admin_purge_person.find(from_id).first::<Self>(conn)
274 fn create(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error> {
275 use crate::schema::admin_purge_person::dsl::*;
276 insert_into(admin_purge_person)
278 .get_result::<Self>(conn)
281 fn update(conn: &PgConnection, from_id: i32, form: &Self::Form) -> Result<Self, Error> {
282 use crate::schema::admin_purge_person::dsl::*;
283 diesel::update(admin_purge_person.find(from_id))
285 .get_result::<Self>(conn)
289 impl Crud for AdminPurgeCommunity {
290 type Form = AdminPurgeCommunityForm;
292 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
293 use crate::schema::admin_purge_community::dsl::*;
294 admin_purge_community.find(from_id).first::<Self>(conn)
297 fn create(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error> {
298 use crate::schema::admin_purge_community::dsl::*;
299 insert_into(admin_purge_community)
301 .get_result::<Self>(conn)
304 fn update(conn: &PgConnection, from_id: i32, form: &Self::Form) -> Result<Self, Error> {
305 use crate::schema::admin_purge_community::dsl::*;
306 diesel::update(admin_purge_community.find(from_id))
308 .get_result::<Self>(conn)
312 impl Crud for AdminPurgePost {
313 type Form = AdminPurgePostForm;
315 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
316 use crate::schema::admin_purge_post::dsl::*;
317 admin_purge_post.find(from_id).first::<Self>(conn)
320 fn create(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error> {
321 use crate::schema::admin_purge_post::dsl::*;
322 insert_into(admin_purge_post)
324 .get_result::<Self>(conn)
327 fn update(conn: &PgConnection, from_id: i32, form: &Self::Form) -> Result<Self, Error> {
328 use crate::schema::admin_purge_post::dsl::*;
329 diesel::update(admin_purge_post.find(from_id))
331 .get_result::<Self>(conn)
335 impl Crud for AdminPurgeComment {
336 type Form = AdminPurgeCommentForm;
338 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
339 use crate::schema::admin_purge_comment::dsl::*;
340 admin_purge_comment.find(from_id).first::<Self>(conn)
343 fn create(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error> {
344 use crate::schema::admin_purge_comment::dsl::*;
345 insert_into(admin_purge_comment)
347 .get_result::<Self>(conn)
350 fn update(conn: &PgConnection, from_id: i32, form: &Self::Form) -> Result<Self, Error> {
351 use crate::schema::admin_purge_comment::dsl::*;
352 diesel::update(admin_purge_comment.find(from_id))
354 .get_result::<Self>(conn)
361 source::{comment::*, community::*, moderator::*, person::*, post::*},
363 utils::establish_unpooled_connection,
365 use serial_test::serial;
371 let conn = establish_unpooled_connection();
373 let new_mod = PersonForm {
374 name: "the mod".into(),
375 public_key: Some("pubkey".to_string()),
376 ..PersonForm::default()
379 let inserted_mod = Person::create(&conn, &new_mod).unwrap();
381 let new_person = PersonForm {
383 public_key: Some("pubkey".to_string()),
384 ..PersonForm::default()
387 let inserted_person = Person::create(&conn, &new_person).unwrap();
389 let new_community = CommunityForm {
390 name: "mod_community".to_string(),
391 title: "nada".to_owned(),
392 public_key: Some("pubkey".to_string()),
393 ..CommunityForm::default()
396 let inserted_community = Community::create(&conn, &new_community).unwrap();
398 let new_post = PostForm {
399 name: "A test post thweep".into(),
400 creator_id: inserted_person.id,
401 community_id: inserted_community.id,
402 ..PostForm::default()
405 let inserted_post = Post::create(&conn, &new_post).unwrap();
407 let comment_form = CommentForm {
408 content: "A test comment".into(),
409 creator_id: inserted_person.id,
410 post_id: inserted_post.id,
411 ..CommentForm::default()
414 let inserted_comment = Comment::create(&conn, &comment_form, None).unwrap();
416 // Now the actual tests
419 let mod_remove_post_form = ModRemovePostForm {
420 mod_person_id: inserted_mod.id,
421 post_id: inserted_post.id,
425 let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
426 let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
427 let expected_mod_remove_post = ModRemovePost {
428 id: inserted_mod_remove_post.id,
429 post_id: inserted_post.id,
430 mod_person_id: inserted_mod.id,
433 when_: inserted_mod_remove_post.when_,
438 let mod_lock_post_form = ModLockPostForm {
439 mod_person_id: inserted_mod.id,
440 post_id: inserted_post.id,
443 let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
444 let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
445 let expected_mod_lock_post = ModLockPost {
446 id: inserted_mod_lock_post.id,
447 post_id: inserted_post.id,
448 mod_person_id: inserted_mod.id,
450 when_: inserted_mod_lock_post.when_,
455 let mod_sticky_post_form = ModStickyPostForm {
456 mod_person_id: inserted_mod.id,
457 post_id: inserted_post.id,
460 let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
461 let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
462 let expected_mod_sticky_post = ModStickyPost {
463 id: inserted_mod_sticky_post.id,
464 post_id: inserted_post.id,
465 mod_person_id: inserted_mod.id,
466 stickied: Some(true),
467 when_: inserted_mod_sticky_post.when_,
472 let mod_remove_comment_form = ModRemoveCommentForm {
473 mod_person_id: inserted_mod.id,
474 comment_id: inserted_comment.id,
478 let inserted_mod_remove_comment =
479 ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
480 let read_mod_remove_comment =
481 ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
482 let expected_mod_remove_comment = ModRemoveComment {
483 id: inserted_mod_remove_comment.id,
484 comment_id: inserted_comment.id,
485 mod_person_id: inserted_mod.id,
488 when_: inserted_mod_remove_comment.when_,
493 let mod_remove_community_form = ModRemoveCommunityForm {
494 mod_person_id: inserted_mod.id,
495 community_id: inserted_community.id,
500 let inserted_mod_remove_community =
501 ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
502 let read_mod_remove_community =
503 ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
504 let expected_mod_remove_community = ModRemoveCommunity {
505 id: inserted_mod_remove_community.id,
506 community_id: inserted_community.id,
507 mod_person_id: inserted_mod.id,
511 when_: inserted_mod_remove_community.when_,
514 // ban from community
516 let mod_ban_from_community_form = ModBanFromCommunityForm {
517 mod_person_id: inserted_mod.id,
518 other_person_id: inserted_person.id,
519 community_id: inserted_community.id,
524 let inserted_mod_ban_from_community =
525 ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
526 let read_mod_ban_from_community =
527 ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
528 let expected_mod_ban_from_community = ModBanFromCommunity {
529 id: inserted_mod_ban_from_community.id,
530 community_id: inserted_community.id,
531 mod_person_id: inserted_mod.id,
532 other_person_id: inserted_person.id,
536 when_: inserted_mod_ban_from_community.when_,
541 let mod_ban_form = ModBanForm {
542 mod_person_id: inserted_mod.id,
543 other_person_id: inserted_person.id,
548 let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
549 let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
550 let expected_mod_ban = ModBan {
551 id: inserted_mod_ban.id,
552 mod_person_id: inserted_mod.id,
553 other_person_id: inserted_person.id,
557 when_: inserted_mod_ban.when_,
562 let mod_add_community_form = ModAddCommunityForm {
563 mod_person_id: inserted_mod.id,
564 other_person_id: inserted_person.id,
565 community_id: inserted_community.id,
568 let inserted_mod_add_community =
569 ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
570 let read_mod_add_community =
571 ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
572 let expected_mod_add_community = ModAddCommunity {
573 id: inserted_mod_add_community.id,
574 community_id: inserted_community.id,
575 mod_person_id: inserted_mod.id,
576 other_person_id: inserted_person.id,
577 removed: Some(false),
578 when_: inserted_mod_add_community.when_,
583 let mod_add_form = ModAddForm {
584 mod_person_id: inserted_mod.id,
585 other_person_id: inserted_person.id,
588 let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
589 let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
590 let expected_mod_add = ModAdd {
591 id: inserted_mod_add.id,
592 mod_person_id: inserted_mod.id,
593 other_person_id: inserted_person.id,
594 removed: Some(false),
595 when_: inserted_mod_add.when_,
598 Comment::delete(&conn, inserted_comment.id).unwrap();
599 Post::delete(&conn, inserted_post.id).unwrap();
600 Community::delete(&conn, inserted_community.id).unwrap();
601 Person::delete(&conn, inserted_person.id).unwrap();
602 Person::delete(&conn, inserted_mod.id).unwrap();
604 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
605 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
606 assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
607 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
608 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
609 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
610 assert_eq!(expected_mod_ban, read_mod_ban);
611 assert_eq!(expected_mod_add_community, read_mod_add_community);
612 assert_eq!(expected_mod_add, read_mod_add);