]> Untitled Git - lemmy.git/blob - server/lemmy_db/src/moderator.rs
Merge remote-tracking branch 'weblate/main' into main
[lemmy.git] / server / lemmy_db / src / moderator.rs
1 use crate::{
2   schema::{
3     mod_add,
4     mod_add_community,
5     mod_ban,
6     mod_ban_from_community,
7     mod_lock_post,
8     mod_remove_comment,
9     mod_remove_community,
10     mod_remove_post,
11     mod_sticky_post,
12   },
13   Crud,
14 };
15 use diesel::{dsl::*, result::Error, *};
16 use serde::{Deserialize, Serialize};
17
18 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
19 #[table_name = "mod_remove_post"]
20 pub struct ModRemovePost {
21   pub id: i32,
22   pub mod_user_id: i32,
23   pub post_id: i32,
24   pub reason: Option<String>,
25   pub removed: Option<bool>,
26   pub when_: chrono::NaiveDateTime,
27 }
28
29 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
30 #[table_name = "mod_remove_post"]
31 pub struct ModRemovePostForm {
32   pub mod_user_id: i32,
33   pub post_id: i32,
34   pub reason: Option<String>,
35   pub removed: Option<bool>,
36 }
37
38 impl Crud<ModRemovePostForm> for ModRemovePost {
39   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
40     use crate::schema::mod_remove_post::dsl::*;
41     mod_remove_post.find(from_id).first::<Self>(conn)
42   }
43
44   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
45     use crate::schema::mod_remove_post::dsl::*;
46     diesel::delete(mod_remove_post.find(from_id)).execute(conn)
47   }
48
49   fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
50     use crate::schema::mod_remove_post::dsl::*;
51     insert_into(mod_remove_post)
52       .values(form)
53       .get_result::<Self>(conn)
54   }
55
56   fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
57     use crate::schema::mod_remove_post::dsl::*;
58     diesel::update(mod_remove_post.find(from_id))
59       .set(form)
60       .get_result::<Self>(conn)
61   }
62 }
63
64 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
65 #[table_name = "mod_lock_post"]
66 pub struct ModLockPost {
67   pub id: i32,
68   pub mod_user_id: i32,
69   pub post_id: i32,
70   pub locked: Option<bool>,
71   pub when_: chrono::NaiveDateTime,
72 }
73
74 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
75 #[table_name = "mod_lock_post"]
76 pub struct ModLockPostForm {
77   pub mod_user_id: i32,
78   pub post_id: i32,
79   pub locked: Option<bool>,
80 }
81
82 impl Crud<ModLockPostForm> for ModLockPost {
83   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
84     use crate::schema::mod_lock_post::dsl::*;
85     mod_lock_post.find(from_id).first::<Self>(conn)
86   }
87
88   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
89     use crate::schema::mod_lock_post::dsl::*;
90     diesel::delete(mod_lock_post.find(from_id)).execute(conn)
91   }
92
93   fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
94     use crate::schema::mod_lock_post::dsl::*;
95     insert_into(mod_lock_post)
96       .values(form)
97       .get_result::<Self>(conn)
98   }
99
100   fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
101     use crate::schema::mod_lock_post::dsl::*;
102     diesel::update(mod_lock_post.find(from_id))
103       .set(form)
104       .get_result::<Self>(conn)
105   }
106 }
107
108 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
109 #[table_name = "mod_sticky_post"]
110 pub struct ModStickyPost {
111   pub id: i32,
112   pub mod_user_id: i32,
113   pub post_id: i32,
114   pub stickied: Option<bool>,
115   pub when_: chrono::NaiveDateTime,
116 }
117
118 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
119 #[table_name = "mod_sticky_post"]
120 pub struct ModStickyPostForm {
121   pub mod_user_id: i32,
122   pub post_id: i32,
123   pub stickied: Option<bool>,
124 }
125
126 impl Crud<ModStickyPostForm> for ModStickyPost {
127   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
128     use crate::schema::mod_sticky_post::dsl::*;
129     mod_sticky_post.find(from_id).first::<Self>(conn)
130   }
131
132   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
133     use crate::schema::mod_sticky_post::dsl::*;
134     diesel::delete(mod_sticky_post.find(from_id)).execute(conn)
135   }
136
137   fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
138     use crate::schema::mod_sticky_post::dsl::*;
139     insert_into(mod_sticky_post)
140       .values(form)
141       .get_result::<Self>(conn)
142   }
143
144   fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> {
145     use crate::schema::mod_sticky_post::dsl::*;
146     diesel::update(mod_sticky_post.find(from_id))
147       .set(form)
148       .get_result::<Self>(conn)
149   }
150 }
151
152 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
153 #[table_name = "mod_remove_comment"]
154 pub struct ModRemoveComment {
155   pub id: i32,
156   pub mod_user_id: i32,
157   pub comment_id: i32,
158   pub reason: Option<String>,
159   pub removed: Option<bool>,
160   pub when_: chrono::NaiveDateTime,
161 }
162
163 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
164 #[table_name = "mod_remove_comment"]
165 pub struct ModRemoveCommentForm {
166   pub mod_user_id: i32,
167   pub comment_id: i32,
168   pub reason: Option<String>,
169   pub removed: Option<bool>,
170 }
171
172 impl Crud<ModRemoveCommentForm> for ModRemoveComment {
173   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
174     use crate::schema::mod_remove_comment::dsl::*;
175     mod_remove_comment.find(from_id).first::<Self>(conn)
176   }
177
178   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
179     use crate::schema::mod_remove_comment::dsl::*;
180     diesel::delete(mod_remove_comment.find(from_id)).execute(conn)
181   }
182
183   fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
184     use crate::schema::mod_remove_comment::dsl::*;
185     insert_into(mod_remove_comment)
186       .values(form)
187       .get_result::<Self>(conn)
188   }
189
190   fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
191     use crate::schema::mod_remove_comment::dsl::*;
192     diesel::update(mod_remove_comment.find(from_id))
193       .set(form)
194       .get_result::<Self>(conn)
195   }
196 }
197
198 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
199 #[table_name = "mod_remove_community"]
200 pub struct ModRemoveCommunity {
201   pub id: i32,
202   pub mod_user_id: i32,
203   pub community_id: i32,
204   pub reason: Option<String>,
205   pub removed: Option<bool>,
206   pub expires: Option<chrono::NaiveDateTime>,
207   pub when_: chrono::NaiveDateTime,
208 }
209
210 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
211 #[table_name = "mod_remove_community"]
212 pub struct ModRemoveCommunityForm {
213   pub mod_user_id: i32,
214   pub community_id: i32,
215   pub reason: Option<String>,
216   pub removed: Option<bool>,
217   pub expires: Option<chrono::NaiveDateTime>,
218 }
219
220 impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
221   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
222     use crate::schema::mod_remove_community::dsl::*;
223     mod_remove_community.find(from_id).first::<Self>(conn)
224   }
225
226   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
227     use crate::schema::mod_remove_community::dsl::*;
228     diesel::delete(mod_remove_community.find(from_id)).execute(conn)
229   }
230
231   fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
232     use crate::schema::mod_remove_community::dsl::*;
233     insert_into(mod_remove_community)
234       .values(form)
235       .get_result::<Self>(conn)
236   }
237
238   fn update(
239     conn: &PgConnection,
240     from_id: i32,
241     form: &ModRemoveCommunityForm,
242   ) -> Result<Self, Error> {
243     use crate::schema::mod_remove_community::dsl::*;
244     diesel::update(mod_remove_community.find(from_id))
245       .set(form)
246       .get_result::<Self>(conn)
247   }
248 }
249
250 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
251 #[table_name = "mod_ban_from_community"]
252 pub struct ModBanFromCommunity {
253   pub id: i32,
254   pub mod_user_id: i32,
255   pub other_user_id: i32,
256   pub community_id: i32,
257   pub reason: Option<String>,
258   pub banned: Option<bool>,
259   pub expires: Option<chrono::NaiveDateTime>,
260   pub when_: chrono::NaiveDateTime,
261 }
262
263 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
264 #[table_name = "mod_ban_from_community"]
265 pub struct ModBanFromCommunityForm {
266   pub mod_user_id: i32,
267   pub other_user_id: i32,
268   pub community_id: i32,
269   pub reason: Option<String>,
270   pub banned: Option<bool>,
271   pub expires: Option<chrono::NaiveDateTime>,
272 }
273
274 impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
275   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
276     use crate::schema::mod_ban_from_community::dsl::*;
277     mod_ban_from_community.find(from_id).first::<Self>(conn)
278   }
279
280   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
281     use crate::schema::mod_ban_from_community::dsl::*;
282     diesel::delete(mod_ban_from_community.find(from_id)).execute(conn)
283   }
284
285   fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
286     use crate::schema::mod_ban_from_community::dsl::*;
287     insert_into(mod_ban_from_community)
288       .values(form)
289       .get_result::<Self>(conn)
290   }
291
292   fn update(
293     conn: &PgConnection,
294     from_id: i32,
295     form: &ModBanFromCommunityForm,
296   ) -> Result<Self, Error> {
297     use crate::schema::mod_ban_from_community::dsl::*;
298     diesel::update(mod_ban_from_community.find(from_id))
299       .set(form)
300       .get_result::<Self>(conn)
301   }
302 }
303
304 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
305 #[table_name = "mod_ban"]
306 pub struct ModBan {
307   pub id: i32,
308   pub mod_user_id: i32,
309   pub other_user_id: i32,
310   pub reason: Option<String>,
311   pub banned: Option<bool>,
312   pub expires: Option<chrono::NaiveDateTime>,
313   pub when_: chrono::NaiveDateTime,
314 }
315
316 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
317 #[table_name = "mod_ban"]
318 pub struct ModBanForm {
319   pub mod_user_id: i32,
320   pub other_user_id: i32,
321   pub reason: Option<String>,
322   pub banned: Option<bool>,
323   pub expires: Option<chrono::NaiveDateTime>,
324 }
325
326 impl Crud<ModBanForm> for ModBan {
327   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
328     use crate::schema::mod_ban::dsl::*;
329     mod_ban.find(from_id).first::<Self>(conn)
330   }
331
332   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
333     use crate::schema::mod_ban::dsl::*;
334     diesel::delete(mod_ban.find(from_id)).execute(conn)
335   }
336
337   fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
338     use crate::schema::mod_ban::dsl::*;
339     insert_into(mod_ban).values(form).get_result::<Self>(conn)
340   }
341
342   fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
343     use crate::schema::mod_ban::dsl::*;
344     diesel::update(mod_ban.find(from_id))
345       .set(form)
346       .get_result::<Self>(conn)
347   }
348 }
349
350 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
351 #[table_name = "mod_add_community"]
352 pub struct ModAddCommunity {
353   pub id: i32,
354   pub mod_user_id: i32,
355   pub other_user_id: i32,
356   pub community_id: i32,
357   pub removed: Option<bool>,
358   pub when_: chrono::NaiveDateTime,
359 }
360
361 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
362 #[table_name = "mod_add_community"]
363 pub struct ModAddCommunityForm {
364   pub mod_user_id: i32,
365   pub other_user_id: i32,
366   pub community_id: i32,
367   pub removed: Option<bool>,
368 }
369
370 impl Crud<ModAddCommunityForm> for ModAddCommunity {
371   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
372     use crate::schema::mod_add_community::dsl::*;
373     mod_add_community.find(from_id).first::<Self>(conn)
374   }
375
376   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
377     use crate::schema::mod_add_community::dsl::*;
378     diesel::delete(mod_add_community.find(from_id)).execute(conn)
379   }
380
381   fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
382     use crate::schema::mod_add_community::dsl::*;
383     insert_into(mod_add_community)
384       .values(form)
385       .get_result::<Self>(conn)
386   }
387
388   fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
389     use crate::schema::mod_add_community::dsl::*;
390     diesel::update(mod_add_community.find(from_id))
391       .set(form)
392       .get_result::<Self>(conn)
393   }
394 }
395
396 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
397 #[table_name = "mod_add"]
398 pub struct ModAdd {
399   pub id: i32,
400   pub mod_user_id: i32,
401   pub other_user_id: i32,
402   pub removed: Option<bool>,
403   pub when_: chrono::NaiveDateTime,
404 }
405
406 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
407 #[table_name = "mod_add"]
408 pub struct ModAddForm {
409   pub mod_user_id: i32,
410   pub other_user_id: i32,
411   pub removed: Option<bool>,
412 }
413
414 impl Crud<ModAddForm> for ModAdd {
415   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
416     use crate::schema::mod_add::dsl::*;
417     mod_add.find(from_id).first::<Self>(conn)
418   }
419
420   fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
421     use crate::schema::mod_add::dsl::*;
422     diesel::delete(mod_add.find(from_id)).execute(conn)
423   }
424
425   fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
426     use crate::schema::mod_add::dsl::*;
427     insert_into(mod_add).values(form).get_result::<Self>(conn)
428   }
429
430   fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
431     use crate::schema::mod_add::dsl::*;
432     diesel::update(mod_add.find(from_id))
433       .set(form)
434       .get_result::<Self>(conn)
435   }
436 }
437
438 #[cfg(test)]
439 mod tests {
440   use crate::{
441     comment::*,
442     community::*,
443     moderator::*,
444     post::*,
445     tests::establish_unpooled_connection,
446     user::*,
447     ListingType,
448     SortType,
449   };
450
451   // use Crud;
452   #[test]
453   fn test_crud() {
454     let conn = establish_unpooled_connection();
455
456     let new_mod = UserForm {
457       name: "the mod".into(),
458       preferred_username: None,
459       password_encrypted: "nope".into(),
460       email: None,
461       matrix_user_id: None,
462       avatar: None,
463       banner: None,
464       admin: false,
465       banned: false,
466       updated: None,
467       show_nsfw: false,
468       theme: "darkly".into(),
469       default_sort_type: SortType::Hot as i16,
470       default_listing_type: ListingType::Subscribed as i16,
471       lang: "browser".into(),
472       show_avatars: true,
473       send_notifications_to_email: false,
474       actor_id: "changeme_829398".into(),
475       bio: None,
476       local: true,
477       private_key: None,
478       public_key: None,
479       last_refreshed_at: None,
480     };
481
482     let inserted_mod = User_::create(&conn, &new_mod).unwrap();
483
484     let new_user = UserForm {
485       name: "jim2".into(),
486       preferred_username: None,
487       password_encrypted: "nope".into(),
488       email: None,
489       matrix_user_id: None,
490       avatar: None,
491       banner: None,
492       admin: false,
493       banned: false,
494       updated: None,
495       show_nsfw: false,
496       theme: "darkly".into(),
497       default_sort_type: SortType::Hot as i16,
498       default_listing_type: ListingType::Subscribed as i16,
499       lang: "browser".into(),
500       show_avatars: true,
501       send_notifications_to_email: false,
502       actor_id: "changeme_82982738".into(),
503       bio: None,
504       local: true,
505       private_key: None,
506       public_key: None,
507       last_refreshed_at: None,
508     };
509
510     let inserted_user = User_::create(&conn, &new_user).unwrap();
511
512     let new_community = CommunityForm {
513       name: "mod_community".to_string(),
514       title: "nada".to_owned(),
515       description: None,
516       category_id: 1,
517       creator_id: inserted_user.id,
518       removed: None,
519       deleted: None,
520       updated: None,
521       nsfw: false,
522       actor_id: "changeme_283687".into(),
523       local: true,
524       private_key: None,
525       public_key: None,
526       last_refreshed_at: None,
527       published: None,
528       icon: None,
529       banner: None,
530     };
531
532     let inserted_community = Community::create(&conn, &new_community).unwrap();
533
534     let new_post = PostForm {
535       name: "A test post thweep".into(),
536       url: None,
537       body: None,
538       creator_id: inserted_user.id,
539       community_id: inserted_community.id,
540       removed: None,
541       deleted: None,
542       locked: None,
543       stickied: None,
544       updated: None,
545       nsfw: false,
546       embed_title: None,
547       embed_description: None,
548       embed_html: None,
549       thumbnail_url: None,
550       ap_id: "http://fake.com".into(),
551       local: true,
552       published: None,
553     };
554
555     let inserted_post = Post::create(&conn, &new_post).unwrap();
556
557     let comment_form = CommentForm {
558       content: "A test comment".into(),
559       creator_id: inserted_user.id,
560       post_id: inserted_post.id,
561       removed: None,
562       deleted: None,
563       read: None,
564       parent_id: None,
565       published: None,
566       updated: None,
567       ap_id: "http://fake.com".into(),
568       local: true,
569     };
570
571     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
572
573     // Now the actual tests
574
575     // remove post
576     let mod_remove_post_form = ModRemovePostForm {
577       mod_user_id: inserted_mod.id,
578       post_id: inserted_post.id,
579       reason: None,
580       removed: None,
581     };
582     let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
583     let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
584     let expected_mod_remove_post = ModRemovePost {
585       id: inserted_mod_remove_post.id,
586       post_id: inserted_post.id,
587       mod_user_id: inserted_mod.id,
588       reason: None,
589       removed: Some(true),
590       when_: inserted_mod_remove_post.when_,
591     };
592
593     // lock post
594
595     let mod_lock_post_form = ModLockPostForm {
596       mod_user_id: inserted_mod.id,
597       post_id: inserted_post.id,
598       locked: None,
599     };
600     let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
601     let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
602     let expected_mod_lock_post = ModLockPost {
603       id: inserted_mod_lock_post.id,
604       post_id: inserted_post.id,
605       mod_user_id: inserted_mod.id,
606       locked: Some(true),
607       when_: inserted_mod_lock_post.when_,
608     };
609
610     // sticky post
611
612     let mod_sticky_post_form = ModStickyPostForm {
613       mod_user_id: inserted_mod.id,
614       post_id: inserted_post.id,
615       stickied: None,
616     };
617     let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
618     let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
619     let expected_mod_sticky_post = ModStickyPost {
620       id: inserted_mod_sticky_post.id,
621       post_id: inserted_post.id,
622       mod_user_id: inserted_mod.id,
623       stickied: Some(true),
624       when_: inserted_mod_sticky_post.when_,
625     };
626
627     // comment
628
629     let mod_remove_comment_form = ModRemoveCommentForm {
630       mod_user_id: inserted_mod.id,
631       comment_id: inserted_comment.id,
632       reason: None,
633       removed: None,
634     };
635     let inserted_mod_remove_comment =
636       ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
637     let read_mod_remove_comment =
638       ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
639     let expected_mod_remove_comment = ModRemoveComment {
640       id: inserted_mod_remove_comment.id,
641       comment_id: inserted_comment.id,
642       mod_user_id: inserted_mod.id,
643       reason: None,
644       removed: Some(true),
645       when_: inserted_mod_remove_comment.when_,
646     };
647
648     // community
649
650     let mod_remove_community_form = ModRemoveCommunityForm {
651       mod_user_id: inserted_mod.id,
652       community_id: inserted_community.id,
653       reason: None,
654       removed: None,
655       expires: None,
656     };
657     let inserted_mod_remove_community =
658       ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
659     let read_mod_remove_community =
660       ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
661     let expected_mod_remove_community = ModRemoveCommunity {
662       id: inserted_mod_remove_community.id,
663       community_id: inserted_community.id,
664       mod_user_id: inserted_mod.id,
665       reason: None,
666       removed: Some(true),
667       expires: None,
668       when_: inserted_mod_remove_community.when_,
669     };
670
671     // ban from community
672
673     let mod_ban_from_community_form = ModBanFromCommunityForm {
674       mod_user_id: inserted_mod.id,
675       other_user_id: inserted_user.id,
676       community_id: inserted_community.id,
677       reason: None,
678       banned: None,
679       expires: None,
680     };
681     let inserted_mod_ban_from_community =
682       ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
683     let read_mod_ban_from_community =
684       ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
685     let expected_mod_ban_from_community = ModBanFromCommunity {
686       id: inserted_mod_ban_from_community.id,
687       community_id: inserted_community.id,
688       mod_user_id: inserted_mod.id,
689       other_user_id: inserted_user.id,
690       reason: None,
691       banned: Some(true),
692       expires: None,
693       when_: inserted_mod_ban_from_community.when_,
694     };
695
696     // ban
697
698     let mod_ban_form = ModBanForm {
699       mod_user_id: inserted_mod.id,
700       other_user_id: inserted_user.id,
701       reason: None,
702       banned: None,
703       expires: None,
704     };
705     let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
706     let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
707     let expected_mod_ban = ModBan {
708       id: inserted_mod_ban.id,
709       mod_user_id: inserted_mod.id,
710       other_user_id: inserted_user.id,
711       reason: None,
712       banned: Some(true),
713       expires: None,
714       when_: inserted_mod_ban.when_,
715     };
716
717     // mod add community
718
719     let mod_add_community_form = ModAddCommunityForm {
720       mod_user_id: inserted_mod.id,
721       other_user_id: inserted_user.id,
722       community_id: inserted_community.id,
723       removed: None,
724     };
725     let inserted_mod_add_community =
726       ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
727     let read_mod_add_community =
728       ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
729     let expected_mod_add_community = ModAddCommunity {
730       id: inserted_mod_add_community.id,
731       community_id: inserted_community.id,
732       mod_user_id: inserted_mod.id,
733       other_user_id: inserted_user.id,
734       removed: Some(false),
735       when_: inserted_mod_add_community.when_,
736     };
737
738     // mod add
739
740     let mod_add_form = ModAddForm {
741       mod_user_id: inserted_mod.id,
742       other_user_id: inserted_user.id,
743       removed: None,
744     };
745     let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
746     let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
747     let expected_mod_add = ModAdd {
748       id: inserted_mod_add.id,
749       mod_user_id: inserted_mod.id,
750       other_user_id: inserted_user.id,
751       removed: Some(false),
752       when_: inserted_mod_add.when_,
753     };
754
755     ModRemovePost::delete(&conn, inserted_mod_remove_post.id).unwrap();
756     ModLockPost::delete(&conn, inserted_mod_lock_post.id).unwrap();
757     ModStickyPost::delete(&conn, inserted_mod_sticky_post.id).unwrap();
758     ModRemoveComment::delete(&conn, inserted_mod_remove_comment.id).unwrap();
759     ModRemoveCommunity::delete(&conn, inserted_mod_remove_community.id).unwrap();
760     ModBanFromCommunity::delete(&conn, inserted_mod_ban_from_community.id).unwrap();
761     ModBan::delete(&conn, inserted_mod_ban.id).unwrap();
762     ModAddCommunity::delete(&conn, inserted_mod_add_community.id).unwrap();
763     ModAdd::delete(&conn, inserted_mod_add.id).unwrap();
764
765     Comment::delete(&conn, inserted_comment.id).unwrap();
766     Post::delete(&conn, inserted_post.id).unwrap();
767     Community::delete(&conn, inserted_community.id).unwrap();
768     User_::delete(&conn, inserted_user.id).unwrap();
769     User_::delete(&conn, inserted_mod.id).unwrap();
770
771     assert_eq!(expected_mod_remove_post, read_mod_remove_post);
772     assert_eq!(expected_mod_lock_post, read_mod_lock_post);
773     assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
774     assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
775     assert_eq!(expected_mod_remove_community, read_mod_remove_community);
776     assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
777     assert_eq!(expected_mod_ban, read_mod_ban);
778     assert_eq!(expected_mod_add_community, read_mod_add_community);
779     assert_eq!(expected_mod_add, read_mod_add);
780   }
781 }