]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/moderator.rs
952bab40e767cba01327aad8557514d79a8b446b
[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 ModAddCommunity {
172   type Form = ModAddCommunityForm;
173   type IdType = i32;
174   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
175     use crate::schema::mod_add_community::dsl::*;
176     mod_add_community.find(from_id).first::<Self>(conn)
177   }
178
179   fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
180     use crate::schema::mod_add_community::dsl::*;
181     insert_into(mod_add_community)
182       .values(form)
183       .get_result::<Self>(conn)
184   }
185
186   fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
187     use crate::schema::mod_add_community::dsl::*;
188     diesel::update(mod_add_community.find(from_id))
189       .set(form)
190       .get_result::<Self>(conn)
191   }
192 }
193
194 impl Crud for ModTransferCommunity {
195   type Form = ModTransferCommunityForm;
196   type IdType = i32;
197   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
198     use crate::schema::mod_transfer_community::dsl::*;
199     mod_transfer_community.find(from_id).first::<Self>(conn)
200   }
201
202   fn create(conn: &PgConnection, form: &ModTransferCommunityForm) -> Result<Self, Error> {
203     use crate::schema::mod_transfer_community::dsl::*;
204     insert_into(mod_transfer_community)
205       .values(form)
206       .get_result::<Self>(conn)
207   }
208
209   fn update(
210     conn: &PgConnection,
211     from_id: i32,
212     form: &ModTransferCommunityForm,
213   ) -> Result<Self, Error> {
214     use crate::schema::mod_transfer_community::dsl::*;
215     diesel::update(mod_transfer_community.find(from_id))
216       .set(form)
217       .get_result::<Self>(conn)
218   }
219 }
220
221 impl Crud for ModAdd {
222   type Form = ModAddForm;
223   type IdType = i32;
224   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
225     use crate::schema::mod_add::dsl::*;
226     mod_add.find(from_id).first::<Self>(conn)
227   }
228
229   fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
230     use crate::schema::mod_add::dsl::*;
231     insert_into(mod_add).values(form).get_result::<Self>(conn)
232   }
233
234   fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
235     use crate::schema::mod_add::dsl::*;
236     diesel::update(mod_add.find(from_id))
237       .set(form)
238       .get_result::<Self>(conn)
239   }
240 }
241
242 #[cfg(test)]
243 mod tests {
244   use crate::{
245     establish_unpooled_connection,
246     source::{comment::*, community::*, moderator::*, person::*, post::*},
247     traits::Crud,
248   };
249   use serial_test::serial;
250
251   // use Crud;
252   #[test]
253   #[serial]
254   fn test_crud() {
255     let conn = establish_unpooled_connection();
256
257     let new_mod = PersonForm {
258       name: "the mod".into(),
259       ..PersonForm::default()
260     };
261
262     let inserted_mod = Person::create(&conn, &new_mod).unwrap();
263
264     let new_person = PersonForm {
265       name: "jim2".into(),
266       ..PersonForm::default()
267     };
268
269     let inserted_person = Person::create(&conn, &new_person).unwrap();
270
271     let new_community = CommunityForm {
272       name: "mod_community".to_string(),
273       title: "nada".to_owned(),
274       ..CommunityForm::default()
275     };
276
277     let inserted_community = Community::create(&conn, &new_community).unwrap();
278
279     let new_post = PostForm {
280       name: "A test post thweep".into(),
281       creator_id: inserted_person.id,
282       community_id: inserted_community.id,
283       ..PostForm::default()
284     };
285
286     let inserted_post = Post::create(&conn, &new_post).unwrap();
287
288     let comment_form = CommentForm {
289       content: "A test comment".into(),
290       creator_id: inserted_person.id,
291       post_id: inserted_post.id,
292       ..CommentForm::default()
293     };
294
295     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
296
297     // Now the actual tests
298
299     // remove post
300     let mod_remove_post_form = ModRemovePostForm {
301       mod_person_id: inserted_mod.id,
302       post_id: inserted_post.id,
303       reason: None,
304       removed: None,
305     };
306     let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
307     let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
308     let expected_mod_remove_post = ModRemovePost {
309       id: inserted_mod_remove_post.id,
310       post_id: inserted_post.id,
311       mod_person_id: inserted_mod.id,
312       reason: None,
313       removed: Some(true),
314       when_: inserted_mod_remove_post.when_,
315     };
316
317     // lock post
318
319     let mod_lock_post_form = ModLockPostForm {
320       mod_person_id: inserted_mod.id,
321       post_id: inserted_post.id,
322       locked: None,
323     };
324     let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
325     let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
326     let expected_mod_lock_post = ModLockPost {
327       id: inserted_mod_lock_post.id,
328       post_id: inserted_post.id,
329       mod_person_id: inserted_mod.id,
330       locked: Some(true),
331       when_: inserted_mod_lock_post.when_,
332     };
333
334     // sticky post
335
336     let mod_sticky_post_form = ModStickyPostForm {
337       mod_person_id: inserted_mod.id,
338       post_id: inserted_post.id,
339       stickied: None,
340     };
341     let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
342     let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
343     let expected_mod_sticky_post = ModStickyPost {
344       id: inserted_mod_sticky_post.id,
345       post_id: inserted_post.id,
346       mod_person_id: inserted_mod.id,
347       stickied: Some(true),
348       when_: inserted_mod_sticky_post.when_,
349     };
350
351     // comment
352
353     let mod_remove_comment_form = ModRemoveCommentForm {
354       mod_person_id: inserted_mod.id,
355       comment_id: inserted_comment.id,
356       reason: None,
357       removed: None,
358     };
359     let inserted_mod_remove_comment =
360       ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
361     let read_mod_remove_comment =
362       ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
363     let expected_mod_remove_comment = ModRemoveComment {
364       id: inserted_mod_remove_comment.id,
365       comment_id: inserted_comment.id,
366       mod_person_id: inserted_mod.id,
367       reason: None,
368       removed: Some(true),
369       when_: inserted_mod_remove_comment.when_,
370     };
371
372     // community
373
374     let mod_remove_community_form = ModRemoveCommunityForm {
375       mod_person_id: inserted_mod.id,
376       community_id: inserted_community.id,
377       reason: None,
378       removed: None,
379       expires: None,
380     };
381     let inserted_mod_remove_community =
382       ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
383     let read_mod_remove_community =
384       ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
385     let expected_mod_remove_community = ModRemoveCommunity {
386       id: inserted_mod_remove_community.id,
387       community_id: inserted_community.id,
388       mod_person_id: inserted_mod.id,
389       reason: None,
390       removed: Some(true),
391       expires: None,
392       when_: inserted_mod_remove_community.when_,
393     };
394
395     // ban from community
396
397     let mod_ban_from_community_form = ModBanFromCommunityForm {
398       mod_person_id: inserted_mod.id,
399       other_person_id: inserted_person.id,
400       community_id: inserted_community.id,
401       reason: None,
402       banned: None,
403       expires: None,
404     };
405     let inserted_mod_ban_from_community =
406       ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
407     let read_mod_ban_from_community =
408       ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
409     let expected_mod_ban_from_community = ModBanFromCommunity {
410       id: inserted_mod_ban_from_community.id,
411       community_id: inserted_community.id,
412       mod_person_id: inserted_mod.id,
413       other_person_id: inserted_person.id,
414       reason: None,
415       banned: Some(true),
416       expires: None,
417       when_: inserted_mod_ban_from_community.when_,
418     };
419
420     // ban
421
422     let mod_ban_form = ModBanForm {
423       mod_person_id: inserted_mod.id,
424       other_person_id: inserted_person.id,
425       reason: None,
426       banned: None,
427       expires: None,
428     };
429     let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
430     let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
431     let expected_mod_ban = ModBan {
432       id: inserted_mod_ban.id,
433       mod_person_id: inserted_mod.id,
434       other_person_id: inserted_person.id,
435       reason: None,
436       banned: Some(true),
437       expires: None,
438       when_: inserted_mod_ban.when_,
439     };
440
441     // mod add community
442
443     let mod_add_community_form = ModAddCommunityForm {
444       mod_person_id: inserted_mod.id,
445       other_person_id: inserted_person.id,
446       community_id: inserted_community.id,
447       removed: None,
448     };
449     let inserted_mod_add_community =
450       ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
451     let read_mod_add_community =
452       ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
453     let expected_mod_add_community = ModAddCommunity {
454       id: inserted_mod_add_community.id,
455       community_id: inserted_community.id,
456       mod_person_id: inserted_mod.id,
457       other_person_id: inserted_person.id,
458       removed: Some(false),
459       when_: inserted_mod_add_community.when_,
460     };
461
462     // mod add
463
464     let mod_add_form = ModAddForm {
465       mod_person_id: inserted_mod.id,
466       other_person_id: inserted_person.id,
467       removed: None,
468     };
469     let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
470     let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
471     let expected_mod_add = ModAdd {
472       id: inserted_mod_add.id,
473       mod_person_id: inserted_mod.id,
474       other_person_id: inserted_person.id,
475       removed: Some(false),
476       when_: inserted_mod_add.when_,
477     };
478
479     Comment::delete(&conn, inserted_comment.id).unwrap();
480     Post::delete(&conn, inserted_post.id).unwrap();
481     Community::delete(&conn, inserted_community.id).unwrap();
482     Person::delete(&conn, inserted_person.id).unwrap();
483     Person::delete(&conn, inserted_mod.id).unwrap();
484
485     assert_eq!(expected_mod_remove_post, read_mod_remove_post);
486     assert_eq!(expected_mod_lock_post, read_mod_lock_post);
487     assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
488     assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
489     assert_eq!(expected_mod_remove_community, read_mod_remove_community);
490     assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
491     assert_eq!(expected_mod_ban, read_mod_ban);
492     assert_eq!(expected_mod_add_community, read_mod_add_community);
493     assert_eq!(expected_mod_add, read_mod_add);
494   }
495 }