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