]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/moderator.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / db_schema / src / impls / moderator.rs
1 use crate::{source::moderator::*, traits::Crud};
2 use diesel::{dsl::*, result::Error, *};
3
4 impl Crud for ModRemovePost {
5   type Form = ModRemovePostForm;
6   type IdType = i32;
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)
10   }
11
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)
15       .values(form)
16       .get_result::<Self>(conn)
17   }
18
19   fn update(
20     conn: &mut PgConnection,
21     from_id: i32,
22     form: &ModRemovePostForm,
23   ) -> Result<Self, Error> {
24     use crate::schema::mod_remove_post::dsl::*;
25     diesel::update(mod_remove_post.find(from_id))
26       .set(form)
27       .get_result::<Self>(conn)
28   }
29 }
30
31 impl Crud for ModLockPost {
32   type Form = ModLockPostForm;
33   type IdType = i32;
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)
37   }
38
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)
42       .values(form)
43       .get_result::<Self>(conn)
44   }
45
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))
49       .set(form)
50       .get_result::<Self>(conn)
51   }
52 }
53
54 impl Crud for ModStickyPost {
55   type Form = ModStickyPostForm;
56   type IdType = i32;
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)
60   }
61
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)
65       .values(form)
66       .get_result::<Self>(conn)
67   }
68
69   fn update(
70     conn: &mut PgConnection,
71     from_id: i32,
72     form: &ModStickyPostForm,
73   ) -> Result<Self, Error> {
74     use crate::schema::mod_sticky_post::dsl::*;
75     diesel::update(mod_sticky_post.find(from_id))
76       .set(form)
77       .get_result::<Self>(conn)
78   }
79 }
80
81 impl Crud for ModRemoveComment {
82   type Form = ModRemoveCommentForm;
83   type IdType = i32;
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)
87   }
88
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)
92       .values(form)
93       .get_result::<Self>(conn)
94   }
95
96   fn update(
97     conn: &mut PgConnection,
98     from_id: i32,
99     form: &ModRemoveCommentForm,
100   ) -> Result<Self, Error> {
101     use crate::schema::mod_remove_comment::dsl::*;
102     diesel::update(mod_remove_comment.find(from_id))
103       .set(form)
104       .get_result::<Self>(conn)
105   }
106 }
107
108 impl Crud for ModRemoveCommunity {
109   type Form = ModRemoveCommunityForm;
110   type IdType = i32;
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)
114   }
115
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)
119       .values(form)
120       .get_result::<Self>(conn)
121   }
122
123   fn update(
124     conn: &mut PgConnection,
125     from_id: i32,
126     form: &ModRemoveCommunityForm,
127   ) -> Result<Self, Error> {
128     use crate::schema::mod_remove_community::dsl::*;
129     diesel::update(mod_remove_community.find(from_id))
130       .set(form)
131       .get_result::<Self>(conn)
132   }
133 }
134
135 impl Crud for ModBanFromCommunity {
136   type Form = ModBanFromCommunityForm;
137   type IdType = i32;
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)
141   }
142
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)
146       .values(form)
147       .get_result::<Self>(conn)
148   }
149
150   fn update(
151     conn: &mut PgConnection,
152     from_id: i32,
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))
157       .set(form)
158       .get_result::<Self>(conn)
159   }
160 }
161
162 impl Crud for ModBan {
163   type Form = ModBanForm;
164   type IdType = i32;
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)
168   }
169
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)
173   }
174
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))
178       .set(form)
179       .get_result::<Self>(conn)
180   }
181 }
182
183 impl Crud for ModHideCommunity {
184   type Form = ModHideCommunityForm;
185   type IdType = i32;
186
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)
190   }
191
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)
195       .values(form)
196       .get_result::<Self>(conn)
197   }
198
199   fn update(
200     conn: &mut PgConnection,
201     from_id: i32,
202     form: &ModHideCommunityForm,
203   ) -> Result<Self, Error> {
204     use crate::schema::mod_hide_community::dsl::*;
205     diesel::update(mod_hide_community.find(from_id))
206       .set(form)
207       .get_result::<Self>(conn)
208   }
209 }
210
211 impl Crud for ModAddCommunity {
212   type Form = ModAddCommunityForm;
213   type IdType = i32;
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)
217   }
218
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)
222       .values(form)
223       .get_result::<Self>(conn)
224   }
225
226   fn update(
227     conn: &mut PgConnection,
228     from_id: i32,
229     form: &ModAddCommunityForm,
230   ) -> Result<Self, Error> {
231     use crate::schema::mod_add_community::dsl::*;
232     diesel::update(mod_add_community.find(from_id))
233       .set(form)
234       .get_result::<Self>(conn)
235   }
236 }
237
238 impl Crud for ModTransferCommunity {
239   type Form = ModTransferCommunityForm;
240   type IdType = i32;
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)
244   }
245
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)
249       .values(form)
250       .get_result::<Self>(conn)
251   }
252
253   fn update(
254     conn: &mut PgConnection,
255     from_id: i32,
256     form: &ModTransferCommunityForm,
257   ) -> Result<Self, Error> {
258     use crate::schema::mod_transfer_community::dsl::*;
259     diesel::update(mod_transfer_community.find(from_id))
260       .set(form)
261       .get_result::<Self>(conn)
262   }
263 }
264
265 impl Crud for ModAdd {
266   type Form = ModAddForm;
267   type IdType = i32;
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)
271   }
272
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)
276   }
277
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))
281       .set(form)
282       .get_result::<Self>(conn)
283   }
284 }
285
286 impl Crud for AdminPurgePerson {
287   type Form = AdminPurgePersonForm;
288   type IdType = i32;
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)
292   }
293
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)
297       .values(form)
298       .get_result::<Self>(conn)
299   }
300
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))
304       .set(form)
305       .get_result::<Self>(conn)
306   }
307 }
308
309 impl Crud for AdminPurgeCommunity {
310   type Form = AdminPurgeCommunityForm;
311   type IdType = i32;
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)
315   }
316
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)
320       .values(form)
321       .get_result::<Self>(conn)
322   }
323
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))
327       .set(form)
328       .get_result::<Self>(conn)
329   }
330 }
331
332 impl Crud for AdminPurgePost {
333   type Form = AdminPurgePostForm;
334   type IdType = i32;
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)
338   }
339
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)
343       .values(form)
344       .get_result::<Self>(conn)
345   }
346
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))
350       .set(form)
351       .get_result::<Self>(conn)
352   }
353 }
354
355 impl Crud for AdminPurgeComment {
356   type Form = AdminPurgeCommentForm;
357   type IdType = i32;
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)
361   }
362
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)
366       .values(form)
367       .get_result::<Self>(conn)
368   }
369
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))
373       .set(form)
374       .get_result::<Self>(conn)
375   }
376 }
377
378 #[cfg(test)]
379 mod tests {
380   use crate::{
381     source::{comment::*, community::*, moderator::*, person::*, post::*},
382     traits::Crud,
383     utils::establish_unpooled_connection,
384   };
385   use serial_test::serial;
386
387   // use Crud;
388   #[test]
389   #[serial]
390   fn test_crud() {
391     let conn = &mut establish_unpooled_connection();
392
393     let new_mod = PersonForm {
394       name: "the mod".into(),
395       public_key: Some("pubkey".to_string()),
396       ..PersonForm::default()
397     };
398
399     let inserted_mod = Person::create(conn, &new_mod).unwrap();
400
401     let new_person = PersonForm {
402       name: "jim2".into(),
403       public_key: Some("pubkey".to_string()),
404       ..PersonForm::default()
405     };
406
407     let inserted_person = Person::create(conn, &new_person).unwrap();
408
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()
414     };
415
416     let inserted_community = Community::create(conn, &new_community).unwrap();
417
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()
423     };
424
425     let inserted_post = Post::create(conn, &new_post).unwrap();
426
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()
432     };
433
434     let inserted_comment = Comment::create(conn, &comment_form, None).unwrap();
435
436     // Now the actual tests
437
438     // remove post
439     let mod_remove_post_form = ModRemovePostForm {
440       mod_person_id: inserted_mod.id,
441       post_id: inserted_post.id,
442       reason: None,
443       removed: None,
444     };
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,
451       reason: None,
452       removed: Some(true),
453       when_: inserted_mod_remove_post.when_,
454     };
455
456     // lock post
457
458     let mod_lock_post_form = ModLockPostForm {
459       mod_person_id: inserted_mod.id,
460       post_id: inserted_post.id,
461       locked: None,
462     };
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,
469       locked: Some(true),
470       when_: inserted_mod_lock_post.when_,
471     };
472
473     // sticky post
474
475     let mod_sticky_post_form = ModStickyPostForm {
476       mod_person_id: inserted_mod.id,
477       post_id: inserted_post.id,
478       stickied: None,
479     };
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_,
488     };
489
490     // comment
491
492     let mod_remove_comment_form = ModRemoveCommentForm {
493       mod_person_id: inserted_mod.id,
494       comment_id: inserted_comment.id,
495       reason: None,
496       removed: None,
497     };
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,
506       reason: None,
507       removed: Some(true),
508       when_: inserted_mod_remove_comment.when_,
509     };
510
511     // community
512
513     let mod_remove_community_form = ModRemoveCommunityForm {
514       mod_person_id: inserted_mod.id,
515       community_id: inserted_community.id,
516       reason: None,
517       removed: None,
518       expires: None,
519     };
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,
528       reason: None,
529       removed: Some(true),
530       expires: None,
531       when_: inserted_mod_remove_community.when_,
532     };
533
534     // ban from community
535
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,
540       reason: None,
541       banned: None,
542       expires: None,
543     };
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,
553       reason: None,
554       banned: Some(true),
555       expires: None,
556       when_: inserted_mod_ban_from_community.when_,
557     };
558
559     // ban
560
561     let mod_ban_form = ModBanForm {
562       mod_person_id: inserted_mod.id,
563       other_person_id: inserted_person.id,
564       reason: None,
565       banned: None,
566       expires: None,
567     };
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,
574       reason: None,
575       banned: Some(true),
576       expires: None,
577       when_: inserted_mod_ban.when_,
578     };
579
580     // mod add community
581
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,
586       removed: None,
587     };
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_,
599     };
600
601     // mod add
602
603     let mod_add_form = ModAddForm {
604       mod_person_id: inserted_mod.id,
605       other_person_id: inserted_person.id,
606       removed: None,
607     };
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_,
616     };
617
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();
623
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);
633   }
634 }