]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/moderator.rs
14bb07f1c8ec4bd84ddc78d8ba8d56cca54eabc8
[lemmy.git] / crates / db_queries / src / source / moderator.rs
1 use crate::Crud;
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::source::moderator::*;
4
5 impl Crud<ModRemovePostForm, i32> for ModRemovePost {
6   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
7     use lemmy_db_schema::schema::mod_remove_post::dsl::*;
8     mod_remove_post.find(from_id).first::<Self>(conn)
9   }
10
11   fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
12     use lemmy_db_schema::schema::mod_remove_post::dsl::*;
13     insert_into(mod_remove_post)
14       .values(form)
15       .get_result::<Self>(conn)
16   }
17
18   fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
19     use lemmy_db_schema::schema::mod_remove_post::dsl::*;
20     diesel::update(mod_remove_post.find(from_id))
21       .set(form)
22       .get_result::<Self>(conn)
23   }
24 }
25
26 impl Crud<ModLockPostForm, i32> for ModLockPost {
27   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
28     use lemmy_db_schema::schema::mod_lock_post::dsl::*;
29     mod_lock_post.find(from_id).first::<Self>(conn)
30   }
31
32   fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
33     use lemmy_db_schema::schema::mod_lock_post::dsl::*;
34     insert_into(mod_lock_post)
35       .values(form)
36       .get_result::<Self>(conn)
37   }
38
39   fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
40     use lemmy_db_schema::schema::mod_lock_post::dsl::*;
41     diesel::update(mod_lock_post.find(from_id))
42       .set(form)
43       .get_result::<Self>(conn)
44   }
45 }
46
47 impl Crud<ModStickyPostForm, i32> for ModStickyPost {
48   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
49     use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
50     mod_sticky_post.find(from_id).first::<Self>(conn)
51   }
52
53   fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
54     use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
55     insert_into(mod_sticky_post)
56       .values(form)
57       .get_result::<Self>(conn)
58   }
59
60   fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> {
61     use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
62     diesel::update(mod_sticky_post.find(from_id))
63       .set(form)
64       .get_result::<Self>(conn)
65   }
66 }
67
68 impl Crud<ModRemoveCommentForm, i32> for ModRemoveComment {
69   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
70     use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
71     mod_remove_comment.find(from_id).first::<Self>(conn)
72   }
73
74   fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
75     use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
76     insert_into(mod_remove_comment)
77       .values(form)
78       .get_result::<Self>(conn)
79   }
80
81   fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
82     use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
83     diesel::update(mod_remove_comment.find(from_id))
84       .set(form)
85       .get_result::<Self>(conn)
86   }
87 }
88
89 impl Crud<ModRemoveCommunityForm, i32> for ModRemoveCommunity {
90   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
91     use lemmy_db_schema::schema::mod_remove_community::dsl::*;
92     mod_remove_community.find(from_id).first::<Self>(conn)
93   }
94
95   fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
96     use lemmy_db_schema::schema::mod_remove_community::dsl::*;
97     insert_into(mod_remove_community)
98       .values(form)
99       .get_result::<Self>(conn)
100   }
101
102   fn update(
103     conn: &PgConnection,
104     from_id: i32,
105     form: &ModRemoveCommunityForm,
106   ) -> Result<Self, Error> {
107     use lemmy_db_schema::schema::mod_remove_community::dsl::*;
108     diesel::update(mod_remove_community.find(from_id))
109       .set(form)
110       .get_result::<Self>(conn)
111   }
112 }
113
114 impl Crud<ModBanFromCommunityForm, i32> for ModBanFromCommunity {
115   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
116     use lemmy_db_schema::schema::mod_ban_from_community::dsl::*;
117     mod_ban_from_community.find(from_id).first::<Self>(conn)
118   }
119
120   fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
121     use lemmy_db_schema::schema::mod_ban_from_community::dsl::*;
122     insert_into(mod_ban_from_community)
123       .values(form)
124       .get_result::<Self>(conn)
125   }
126
127   fn update(
128     conn: &PgConnection,
129     from_id: i32,
130     form: &ModBanFromCommunityForm,
131   ) -> Result<Self, Error> {
132     use lemmy_db_schema::schema::mod_ban_from_community::dsl::*;
133     diesel::update(mod_ban_from_community.find(from_id))
134       .set(form)
135       .get_result::<Self>(conn)
136   }
137 }
138
139 impl Crud<ModBanForm, i32> for ModBan {
140   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
141     use lemmy_db_schema::schema::mod_ban::dsl::*;
142     mod_ban.find(from_id).first::<Self>(conn)
143   }
144
145   fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
146     use lemmy_db_schema::schema::mod_ban::dsl::*;
147     insert_into(mod_ban).values(form).get_result::<Self>(conn)
148   }
149
150   fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
151     use lemmy_db_schema::schema::mod_ban::dsl::*;
152     diesel::update(mod_ban.find(from_id))
153       .set(form)
154       .get_result::<Self>(conn)
155   }
156 }
157
158 impl Crud<ModAddCommunityForm, i32> for ModAddCommunity {
159   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
160     use lemmy_db_schema::schema::mod_add_community::dsl::*;
161     mod_add_community.find(from_id).first::<Self>(conn)
162   }
163
164   fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
165     use lemmy_db_schema::schema::mod_add_community::dsl::*;
166     insert_into(mod_add_community)
167       .values(form)
168       .get_result::<Self>(conn)
169   }
170
171   fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
172     use lemmy_db_schema::schema::mod_add_community::dsl::*;
173     diesel::update(mod_add_community.find(from_id))
174       .set(form)
175       .get_result::<Self>(conn)
176   }
177 }
178
179 impl Crud<ModAddForm, i32> for ModAdd {
180   fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
181     use lemmy_db_schema::schema::mod_add::dsl::*;
182     mod_add.find(from_id).first::<Self>(conn)
183   }
184
185   fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
186     use lemmy_db_schema::schema::mod_add::dsl::*;
187     insert_into(mod_add).values(form).get_result::<Self>(conn)
188   }
189
190   fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
191     use lemmy_db_schema::schema::mod_add::dsl::*;
192     diesel::update(mod_add.find(from_id))
193       .set(form)
194       .get_result::<Self>(conn)
195   }
196 }
197
198 #[cfg(test)]
199 mod tests {
200   use crate::{establish_unpooled_connection, Crud};
201   use lemmy_db_schema::source::{comment::*, community::*, moderator::*, person::*, post::*};
202   use serial_test::serial;
203
204   // use Crud;
205   #[test]
206   #[serial]
207   fn test_crud() {
208     let conn = establish_unpooled_connection();
209
210     let new_mod = PersonForm {
211       name: "the mod".into(),
212       ..PersonForm::default()
213     };
214
215     let inserted_mod = Person::create(&conn, &new_mod).unwrap();
216
217     let new_person = PersonForm {
218       name: "jim2".into(),
219       ..PersonForm::default()
220     };
221
222     let inserted_person = Person::create(&conn, &new_person).unwrap();
223
224     let new_community = CommunityForm {
225       name: "mod_community".to_string(),
226       title: "nada".to_owned(),
227       ..CommunityForm::default()
228     };
229
230     let inserted_community = Community::create(&conn, &new_community).unwrap();
231
232     let new_post = PostForm {
233       name: "A test post thweep".into(),
234       creator_id: inserted_person.id,
235       community_id: inserted_community.id,
236       ..PostForm::default()
237     };
238
239     let inserted_post = Post::create(&conn, &new_post).unwrap();
240
241     let comment_form = CommentForm {
242       content: "A test comment".into(),
243       creator_id: inserted_person.id,
244       post_id: inserted_post.id,
245       ..CommentForm::default()
246     };
247
248     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
249
250     // Now the actual tests
251
252     // remove post
253     let mod_remove_post_form = ModRemovePostForm {
254       mod_person_id: inserted_mod.id,
255       post_id: inserted_post.id,
256       reason: None,
257       removed: None,
258     };
259     let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
260     let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
261     let expected_mod_remove_post = ModRemovePost {
262       id: inserted_mod_remove_post.id,
263       post_id: inserted_post.id,
264       mod_person_id: inserted_mod.id,
265       reason: None,
266       removed: Some(true),
267       when_: inserted_mod_remove_post.when_,
268     };
269
270     // lock post
271
272     let mod_lock_post_form = ModLockPostForm {
273       mod_person_id: inserted_mod.id,
274       post_id: inserted_post.id,
275       locked: None,
276     };
277     let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
278     let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
279     let expected_mod_lock_post = ModLockPost {
280       id: inserted_mod_lock_post.id,
281       post_id: inserted_post.id,
282       mod_person_id: inserted_mod.id,
283       locked: Some(true),
284       when_: inserted_mod_lock_post.when_,
285     };
286
287     // sticky post
288
289     let mod_sticky_post_form = ModStickyPostForm {
290       mod_person_id: inserted_mod.id,
291       post_id: inserted_post.id,
292       stickied: None,
293     };
294     let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
295     let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
296     let expected_mod_sticky_post = ModStickyPost {
297       id: inserted_mod_sticky_post.id,
298       post_id: inserted_post.id,
299       mod_person_id: inserted_mod.id,
300       stickied: Some(true),
301       when_: inserted_mod_sticky_post.when_,
302     };
303
304     // comment
305
306     let mod_remove_comment_form = ModRemoveCommentForm {
307       mod_person_id: inserted_mod.id,
308       comment_id: inserted_comment.id,
309       reason: None,
310       removed: None,
311     };
312     let inserted_mod_remove_comment =
313       ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
314     let read_mod_remove_comment =
315       ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
316     let expected_mod_remove_comment = ModRemoveComment {
317       id: inserted_mod_remove_comment.id,
318       comment_id: inserted_comment.id,
319       mod_person_id: inserted_mod.id,
320       reason: None,
321       removed: Some(true),
322       when_: inserted_mod_remove_comment.when_,
323     };
324
325     // community
326
327     let mod_remove_community_form = ModRemoveCommunityForm {
328       mod_person_id: inserted_mod.id,
329       community_id: inserted_community.id,
330       reason: None,
331       removed: None,
332       expires: None,
333     };
334     let inserted_mod_remove_community =
335       ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
336     let read_mod_remove_community =
337       ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
338     let expected_mod_remove_community = ModRemoveCommunity {
339       id: inserted_mod_remove_community.id,
340       community_id: inserted_community.id,
341       mod_person_id: inserted_mod.id,
342       reason: None,
343       removed: Some(true),
344       expires: None,
345       when_: inserted_mod_remove_community.when_,
346     };
347
348     // ban from community
349
350     let mod_ban_from_community_form = ModBanFromCommunityForm {
351       mod_person_id: inserted_mod.id,
352       other_person_id: inserted_person.id,
353       community_id: inserted_community.id,
354       reason: None,
355       banned: None,
356       expires: None,
357     };
358     let inserted_mod_ban_from_community =
359       ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
360     let read_mod_ban_from_community =
361       ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
362     let expected_mod_ban_from_community = ModBanFromCommunity {
363       id: inserted_mod_ban_from_community.id,
364       community_id: inserted_community.id,
365       mod_person_id: inserted_mod.id,
366       other_person_id: inserted_person.id,
367       reason: None,
368       banned: Some(true),
369       expires: None,
370       when_: inserted_mod_ban_from_community.when_,
371     };
372
373     // ban
374
375     let mod_ban_form = ModBanForm {
376       mod_person_id: inserted_mod.id,
377       other_person_id: inserted_person.id,
378       reason: None,
379       banned: None,
380       expires: None,
381     };
382     let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
383     let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
384     let expected_mod_ban = ModBan {
385       id: inserted_mod_ban.id,
386       mod_person_id: inserted_mod.id,
387       other_person_id: inserted_person.id,
388       reason: None,
389       banned: Some(true),
390       expires: None,
391       when_: inserted_mod_ban.when_,
392     };
393
394     // mod add community
395
396     let mod_add_community_form = ModAddCommunityForm {
397       mod_person_id: inserted_mod.id,
398       other_person_id: inserted_person.id,
399       community_id: inserted_community.id,
400       removed: None,
401     };
402     let inserted_mod_add_community =
403       ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
404     let read_mod_add_community =
405       ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
406     let expected_mod_add_community = ModAddCommunity {
407       id: inserted_mod_add_community.id,
408       community_id: inserted_community.id,
409       mod_person_id: inserted_mod.id,
410       other_person_id: inserted_person.id,
411       removed: Some(false),
412       when_: inserted_mod_add_community.when_,
413     };
414
415     // mod add
416
417     let mod_add_form = ModAddForm {
418       mod_person_id: inserted_mod.id,
419       other_person_id: inserted_person.id,
420       removed: None,
421     };
422     let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
423     let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
424     let expected_mod_add = ModAdd {
425       id: inserted_mod_add.id,
426       mod_person_id: inserted_mod.id,
427       other_person_id: inserted_person.id,
428       removed: Some(false),
429       when_: inserted_mod_add.when_,
430     };
431
432     Comment::delete(&conn, inserted_comment.id).unwrap();
433     Post::delete(&conn, inserted_post.id).unwrap();
434     Community::delete(&conn, inserted_community.id).unwrap();
435     Person::delete(&conn, inserted_person.id).unwrap();
436     Person::delete(&conn, inserted_mod.id).unwrap();
437
438     assert_eq!(expected_mod_remove_post, read_mod_remove_post);
439     assert_eq!(expected_mod_lock_post, read_mod_lock_post);
440     assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
441     assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
442     assert_eq!(expected_mod_remove_community, read_mod_remove_community);
443     assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
444     assert_eq!(expected_mod_ban, read_mod_ban);
445     assert_eq!(expected_mod_add_community, read_mod_add_community);
446     assert_eq!(expected_mod_add, read_mod_add);
447   }
448 }