]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/moderator.rs
Adding admin purging of DB items and pictures. #904 #1331 (#1809)
[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: &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: &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(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))
22       .set(form)
23       .get_result::<Self>(conn)
24   }
25 }
26
27 impl Crud for ModLockPost {
28   type Form = ModLockPostForm;
29   type IdType = i32;
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)
33   }
34
35   fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
36     use crate::schema::mod_lock_post::dsl::*;
37     insert_into(mod_lock_post)
38       .values(form)
39       .get_result::<Self>(conn)
40   }
41
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))
45       .set(form)
46       .get_result::<Self>(conn)
47   }
48 }
49
50 impl Crud for ModStickyPost {
51   type Form = ModStickyPostForm;
52   type IdType = i32;
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)
56   }
57
58   fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
59     use crate::schema::mod_sticky_post::dsl::*;
60     insert_into(mod_sticky_post)
61       .values(form)
62       .get_result::<Self>(conn)
63   }
64
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))
68       .set(form)
69       .get_result::<Self>(conn)
70   }
71 }
72
73 impl Crud for ModRemoveComment {
74   type Form = ModRemoveCommentForm;
75   type IdType = i32;
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)
79   }
80
81   fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
82     use crate::schema::mod_remove_comment::dsl::*;
83     insert_into(mod_remove_comment)
84       .values(form)
85       .get_result::<Self>(conn)
86   }
87
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))
91       .set(form)
92       .get_result::<Self>(conn)
93   }
94 }
95
96 impl Crud for ModRemoveCommunity {
97   type Form = ModRemoveCommunityForm;
98   type IdType = i32;
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)
102   }
103
104   fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
105     use crate::schema::mod_remove_community::dsl::*;
106     insert_into(mod_remove_community)
107       .values(form)
108       .get_result::<Self>(conn)
109   }
110
111   fn update(
112     conn: &PgConnection,
113     from_id: i32,
114     form: &ModRemoveCommunityForm,
115   ) -> Result<Self, Error> {
116     use crate::schema::mod_remove_community::dsl::*;
117     diesel::update(mod_remove_community.find(from_id))
118       .set(form)
119       .get_result::<Self>(conn)
120   }
121 }
122
123 impl Crud for ModBanFromCommunity {
124   type Form = ModBanFromCommunityForm;
125   type IdType = i32;
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)
129   }
130
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)
134       .values(form)
135       .get_result::<Self>(conn)
136   }
137
138   fn update(
139     conn: &PgConnection,
140     from_id: i32,
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))
145       .set(form)
146       .get_result::<Self>(conn)
147   }
148 }
149
150 impl Crud for ModBan {
151   type Form = ModBanForm;
152   type IdType = i32;
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)
156   }
157
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)
161   }
162
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))
166       .set(form)
167       .get_result::<Self>(conn)
168   }
169 }
170
171 impl Crud for ModHideCommunity {
172   type Form = ModHideCommunityForm;
173   type IdType = i32;
174
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)
178   }
179
180   fn create(conn: &PgConnection, form: &ModHideCommunityForm) -> Result<Self, Error> {
181     use crate::schema::mod_hide_community::dsl::*;
182     insert_into(mod_hide_community)
183       .values(form)
184       .get_result::<Self>(conn)
185   }
186
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))
190       .set(form)
191       .get_result::<Self>(conn)
192   }
193 }
194
195 impl Crud for ModAddCommunity {
196   type Form = ModAddCommunityForm;
197   type IdType = i32;
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)
201   }
202
203   fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
204     use crate::schema::mod_add_community::dsl::*;
205     insert_into(mod_add_community)
206       .values(form)
207       .get_result::<Self>(conn)
208   }
209
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))
213       .set(form)
214       .get_result::<Self>(conn)
215   }
216 }
217
218 impl Crud for ModTransferCommunity {
219   type Form = ModTransferCommunityForm;
220   type IdType = i32;
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)
224   }
225
226   fn create(conn: &PgConnection, form: &ModTransferCommunityForm) -> Result<Self, Error> {
227     use crate::schema::mod_transfer_community::dsl::*;
228     insert_into(mod_transfer_community)
229       .values(form)
230       .get_result::<Self>(conn)
231   }
232
233   fn update(
234     conn: &PgConnection,
235     from_id: i32,
236     form: &ModTransferCommunityForm,
237   ) -> Result<Self, Error> {
238     use crate::schema::mod_transfer_community::dsl::*;
239     diesel::update(mod_transfer_community.find(from_id))
240       .set(form)
241       .get_result::<Self>(conn)
242   }
243 }
244
245 impl Crud for ModAdd {
246   type Form = ModAddForm;
247   type IdType = i32;
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)
251   }
252
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)
256   }
257
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))
261       .set(form)
262       .get_result::<Self>(conn)
263   }
264 }
265
266 impl Crud for AdminPurgePerson {
267   type Form = AdminPurgePersonForm;
268   type IdType = i32;
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)
272   }
273
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)
277       .values(form)
278       .get_result::<Self>(conn)
279   }
280
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))
284       .set(form)
285       .get_result::<Self>(conn)
286   }
287 }
288
289 impl Crud for AdminPurgeCommunity {
290   type Form = AdminPurgeCommunityForm;
291   type IdType = i32;
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)
295   }
296
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)
300       .values(form)
301       .get_result::<Self>(conn)
302   }
303
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))
307       .set(form)
308       .get_result::<Self>(conn)
309   }
310 }
311
312 impl Crud for AdminPurgePost {
313   type Form = AdminPurgePostForm;
314   type IdType = i32;
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)
318   }
319
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)
323       .values(form)
324       .get_result::<Self>(conn)
325   }
326
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))
330       .set(form)
331       .get_result::<Self>(conn)
332   }
333 }
334
335 impl Crud for AdminPurgeComment {
336   type Form = AdminPurgeCommentForm;
337   type IdType = i32;
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)
341   }
342
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)
346       .values(form)
347       .get_result::<Self>(conn)
348   }
349
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))
353       .set(form)
354       .get_result::<Self>(conn)
355   }
356 }
357
358 #[cfg(test)]
359 mod tests {
360   use crate::{
361     source::{comment::*, community::*, moderator::*, person::*, post::*},
362     traits::Crud,
363     utils::establish_unpooled_connection,
364   };
365   use serial_test::serial;
366
367   // use Crud;
368   #[test]
369   #[serial]
370   fn test_crud() {
371     let conn = establish_unpooled_connection();
372
373     let new_mod = PersonForm {
374       name: "the mod".into(),
375       ..PersonForm::default()
376     };
377
378     let inserted_mod = Person::create(&conn, &new_mod).unwrap();
379
380     let new_person = PersonForm {
381       name: "jim2".into(),
382       ..PersonForm::default()
383     };
384
385     let inserted_person = Person::create(&conn, &new_person).unwrap();
386
387     let new_community = CommunityForm {
388       name: "mod_community".to_string(),
389       title: "nada".to_owned(),
390       ..CommunityForm::default()
391     };
392
393     let inserted_community = Community::create(&conn, &new_community).unwrap();
394
395     let new_post = PostForm {
396       name: "A test post thweep".into(),
397       creator_id: inserted_person.id,
398       community_id: inserted_community.id,
399       ..PostForm::default()
400     };
401
402     let inserted_post = Post::create(&conn, &new_post).unwrap();
403
404     let comment_form = CommentForm {
405       content: "A test comment".into(),
406       creator_id: inserted_person.id,
407       post_id: inserted_post.id,
408       ..CommentForm::default()
409     };
410
411     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
412
413     // Now the actual tests
414
415     // remove post
416     let mod_remove_post_form = ModRemovePostForm {
417       mod_person_id: inserted_mod.id,
418       post_id: inserted_post.id,
419       reason: None,
420       removed: None,
421     };
422     let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
423     let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
424     let expected_mod_remove_post = ModRemovePost {
425       id: inserted_mod_remove_post.id,
426       post_id: inserted_post.id,
427       mod_person_id: inserted_mod.id,
428       reason: None,
429       removed: Some(true),
430       when_: inserted_mod_remove_post.when_,
431     };
432
433     // lock post
434
435     let mod_lock_post_form = ModLockPostForm {
436       mod_person_id: inserted_mod.id,
437       post_id: inserted_post.id,
438       locked: None,
439     };
440     let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
441     let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
442     let expected_mod_lock_post = ModLockPost {
443       id: inserted_mod_lock_post.id,
444       post_id: inserted_post.id,
445       mod_person_id: inserted_mod.id,
446       locked: Some(true),
447       when_: inserted_mod_lock_post.when_,
448     };
449
450     // sticky post
451
452     let mod_sticky_post_form = ModStickyPostForm {
453       mod_person_id: inserted_mod.id,
454       post_id: inserted_post.id,
455       stickied: None,
456     };
457     let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
458     let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
459     let expected_mod_sticky_post = ModStickyPost {
460       id: inserted_mod_sticky_post.id,
461       post_id: inserted_post.id,
462       mod_person_id: inserted_mod.id,
463       stickied: Some(true),
464       when_: inserted_mod_sticky_post.when_,
465     };
466
467     // comment
468
469     let mod_remove_comment_form = ModRemoveCommentForm {
470       mod_person_id: inserted_mod.id,
471       comment_id: inserted_comment.id,
472       reason: None,
473       removed: None,
474     };
475     let inserted_mod_remove_comment =
476       ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
477     let read_mod_remove_comment =
478       ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
479     let expected_mod_remove_comment = ModRemoveComment {
480       id: inserted_mod_remove_comment.id,
481       comment_id: inserted_comment.id,
482       mod_person_id: inserted_mod.id,
483       reason: None,
484       removed: Some(true),
485       when_: inserted_mod_remove_comment.when_,
486     };
487
488     // community
489
490     let mod_remove_community_form = ModRemoveCommunityForm {
491       mod_person_id: inserted_mod.id,
492       community_id: inserted_community.id,
493       reason: None,
494       removed: None,
495       expires: None,
496     };
497     let inserted_mod_remove_community =
498       ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
499     let read_mod_remove_community =
500       ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
501     let expected_mod_remove_community = ModRemoveCommunity {
502       id: inserted_mod_remove_community.id,
503       community_id: inserted_community.id,
504       mod_person_id: inserted_mod.id,
505       reason: None,
506       removed: Some(true),
507       expires: None,
508       when_: inserted_mod_remove_community.when_,
509     };
510
511     // ban from community
512
513     let mod_ban_from_community_form = ModBanFromCommunityForm {
514       mod_person_id: inserted_mod.id,
515       other_person_id: inserted_person.id,
516       community_id: inserted_community.id,
517       reason: None,
518       banned: None,
519       expires: None,
520     };
521     let inserted_mod_ban_from_community =
522       ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
523     let read_mod_ban_from_community =
524       ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
525     let expected_mod_ban_from_community = ModBanFromCommunity {
526       id: inserted_mod_ban_from_community.id,
527       community_id: inserted_community.id,
528       mod_person_id: inserted_mod.id,
529       other_person_id: inserted_person.id,
530       reason: None,
531       banned: Some(true),
532       expires: None,
533       when_: inserted_mod_ban_from_community.when_,
534     };
535
536     // ban
537
538     let mod_ban_form = ModBanForm {
539       mod_person_id: inserted_mod.id,
540       other_person_id: inserted_person.id,
541       reason: None,
542       banned: None,
543       expires: None,
544     };
545     let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
546     let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
547     let expected_mod_ban = ModBan {
548       id: inserted_mod_ban.id,
549       mod_person_id: inserted_mod.id,
550       other_person_id: inserted_person.id,
551       reason: None,
552       banned: Some(true),
553       expires: None,
554       when_: inserted_mod_ban.when_,
555     };
556
557     // mod add community
558
559     let mod_add_community_form = ModAddCommunityForm {
560       mod_person_id: inserted_mod.id,
561       other_person_id: inserted_person.id,
562       community_id: inserted_community.id,
563       removed: None,
564     };
565     let inserted_mod_add_community =
566       ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
567     let read_mod_add_community =
568       ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
569     let expected_mod_add_community = ModAddCommunity {
570       id: inserted_mod_add_community.id,
571       community_id: inserted_community.id,
572       mod_person_id: inserted_mod.id,
573       other_person_id: inserted_person.id,
574       removed: Some(false),
575       when_: inserted_mod_add_community.when_,
576     };
577
578     // mod add
579
580     let mod_add_form = ModAddForm {
581       mod_person_id: inserted_mod.id,
582       other_person_id: inserted_person.id,
583       removed: None,
584     };
585     let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
586     let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
587     let expected_mod_add = ModAdd {
588       id: inserted_mod_add.id,
589       mod_person_id: inserted_mod.id,
590       other_person_id: inserted_person.id,
591       removed: Some(false),
592       when_: inserted_mod_add.when_,
593     };
594
595     Comment::delete(&conn, inserted_comment.id).unwrap();
596     Post::delete(&conn, inserted_post.id).unwrap();
597     Community::delete(&conn, inserted_community.id).unwrap();
598     Person::delete(&conn, inserted_person.id).unwrap();
599     Person::delete(&conn, inserted_mod.id).unwrap();
600
601     assert_eq!(expected_mod_remove_post, read_mod_remove_post);
602     assert_eq!(expected_mod_lock_post, read_mod_lock_post);
603     assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
604     assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
605     assert_eq!(expected_mod_remove_community, read_mod_remove_community);
606     assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
607     assert_eq!(expected_mod_ban, read_mod_ban);
608     assert_eq!(expected_mod_add_community, read_mod_add_community);
609     assert_eq!(expected_mod_add, read_mod_add);
610   }
611 }