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