]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/moderator.rs
Support plain `cargo test` and disable unused doctests for speed
[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> 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> 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> 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> 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> 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> 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> 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> 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> 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, ListingType, SortType};
201   use lemmy_db_schema::source::{comment::*, community::*, moderator::*, post::*, user::*};
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 = UserForm {
211       name: "the mod".into(),
212       preferred_username: None,
213       password_encrypted: "nope".into(),
214       email: None,
215       matrix_user_id: None,
216       avatar: None,
217       banner: None,
218       admin: false,
219       banned: Some(false),
220       published: None,
221       updated: None,
222       show_nsfw: false,
223       theme: "browser".into(),
224       default_sort_type: SortType::Hot as i16,
225       default_listing_type: ListingType::Subscribed as i16,
226       lang: "browser".into(),
227       show_avatars: true,
228       send_notifications_to_email: false,
229       actor_id: None,
230       bio: None,
231       local: true,
232       private_key: None,
233       public_key: None,
234       last_refreshed_at: None,
235       inbox_url: None,
236       shared_inbox_url: None,
237     };
238
239     let inserted_mod = User_::create(&conn, &new_mod).unwrap();
240
241     let new_user = UserForm {
242       name: "jim2".into(),
243       preferred_username: None,
244       password_encrypted: "nope".into(),
245       email: None,
246       matrix_user_id: None,
247       avatar: None,
248       banner: None,
249       admin: false,
250       banned: Some(false),
251       published: None,
252       updated: None,
253       show_nsfw: false,
254       theme: "browser".into(),
255       default_sort_type: SortType::Hot as i16,
256       default_listing_type: ListingType::Subscribed as i16,
257       lang: "browser".into(),
258       show_avatars: true,
259       send_notifications_to_email: false,
260       actor_id: None,
261       bio: None,
262       local: true,
263       private_key: None,
264       public_key: None,
265       last_refreshed_at: None,
266       inbox_url: None,
267       shared_inbox_url: None,
268     };
269
270     let inserted_user = User_::create(&conn, &new_user).unwrap();
271
272     let new_community = CommunityForm {
273       name: "mod_community".to_string(),
274       title: "nada".to_owned(),
275       description: None,
276       creator_id: inserted_user.id,
277       removed: None,
278       deleted: None,
279       updated: None,
280       nsfw: false,
281       actor_id: None,
282       local: true,
283       private_key: None,
284       public_key: None,
285       last_refreshed_at: None,
286       published: None,
287       icon: None,
288       banner: None,
289       followers_url: None,
290       inbox_url: None,
291       shared_inbox_url: None,
292     };
293
294     let inserted_community = Community::create(&conn, &new_community).unwrap();
295
296     let new_post = PostForm {
297       name: "A test post thweep".into(),
298       url: None,
299       body: None,
300       creator_id: inserted_user.id,
301       community_id: inserted_community.id,
302       removed: None,
303       deleted: None,
304       locked: None,
305       stickied: None,
306       updated: None,
307       nsfw: false,
308       embed_title: None,
309       embed_description: None,
310       embed_html: None,
311       thumbnail_url: None,
312       ap_id: None,
313       local: true,
314       published: None,
315     };
316
317     let inserted_post = Post::create(&conn, &new_post).unwrap();
318
319     let comment_form = CommentForm {
320       content: "A test comment".into(),
321       creator_id: inserted_user.id,
322       post_id: inserted_post.id,
323       removed: None,
324       deleted: None,
325       read: None,
326       parent_id: None,
327       published: None,
328       updated: None,
329       ap_id: None,
330       local: true,
331     };
332
333     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
334
335     // Now the actual tests
336
337     // remove post
338     let mod_remove_post_form = ModRemovePostForm {
339       mod_user_id: inserted_mod.id,
340       post_id: inserted_post.id,
341       reason: None,
342       removed: None,
343     };
344     let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
345     let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
346     let expected_mod_remove_post = ModRemovePost {
347       id: inserted_mod_remove_post.id,
348       post_id: inserted_post.id,
349       mod_user_id: inserted_mod.id,
350       reason: None,
351       removed: Some(true),
352       when_: inserted_mod_remove_post.when_,
353     };
354
355     // lock post
356
357     let mod_lock_post_form = ModLockPostForm {
358       mod_user_id: inserted_mod.id,
359       post_id: inserted_post.id,
360       locked: None,
361     };
362     let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
363     let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
364     let expected_mod_lock_post = ModLockPost {
365       id: inserted_mod_lock_post.id,
366       post_id: inserted_post.id,
367       mod_user_id: inserted_mod.id,
368       locked: Some(true),
369       when_: inserted_mod_lock_post.when_,
370     };
371
372     // sticky post
373
374     let mod_sticky_post_form = ModStickyPostForm {
375       mod_user_id: inserted_mod.id,
376       post_id: inserted_post.id,
377       stickied: None,
378     };
379     let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
380     let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
381     let expected_mod_sticky_post = ModStickyPost {
382       id: inserted_mod_sticky_post.id,
383       post_id: inserted_post.id,
384       mod_user_id: inserted_mod.id,
385       stickied: Some(true),
386       when_: inserted_mod_sticky_post.when_,
387     };
388
389     // comment
390
391     let mod_remove_comment_form = ModRemoveCommentForm {
392       mod_user_id: inserted_mod.id,
393       comment_id: inserted_comment.id,
394       reason: None,
395       removed: None,
396     };
397     let inserted_mod_remove_comment =
398       ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
399     let read_mod_remove_comment =
400       ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
401     let expected_mod_remove_comment = ModRemoveComment {
402       id: inserted_mod_remove_comment.id,
403       comment_id: inserted_comment.id,
404       mod_user_id: inserted_mod.id,
405       reason: None,
406       removed: Some(true),
407       when_: inserted_mod_remove_comment.when_,
408     };
409
410     // community
411
412     let mod_remove_community_form = ModRemoveCommunityForm {
413       mod_user_id: inserted_mod.id,
414       community_id: inserted_community.id,
415       reason: None,
416       removed: None,
417       expires: None,
418     };
419     let inserted_mod_remove_community =
420       ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
421     let read_mod_remove_community =
422       ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
423     let expected_mod_remove_community = ModRemoveCommunity {
424       id: inserted_mod_remove_community.id,
425       community_id: inserted_community.id,
426       mod_user_id: inserted_mod.id,
427       reason: None,
428       removed: Some(true),
429       expires: None,
430       when_: inserted_mod_remove_community.when_,
431     };
432
433     // ban from community
434
435     let mod_ban_from_community_form = ModBanFromCommunityForm {
436       mod_user_id: inserted_mod.id,
437       other_user_id: inserted_user.id,
438       community_id: inserted_community.id,
439       reason: None,
440       banned: None,
441       expires: None,
442     };
443     let inserted_mod_ban_from_community =
444       ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
445     let read_mod_ban_from_community =
446       ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
447     let expected_mod_ban_from_community = ModBanFromCommunity {
448       id: inserted_mod_ban_from_community.id,
449       community_id: inserted_community.id,
450       mod_user_id: inserted_mod.id,
451       other_user_id: inserted_user.id,
452       reason: None,
453       banned: Some(true),
454       expires: None,
455       when_: inserted_mod_ban_from_community.when_,
456     };
457
458     // ban
459
460     let mod_ban_form = ModBanForm {
461       mod_user_id: inserted_mod.id,
462       other_user_id: inserted_user.id,
463       reason: None,
464       banned: None,
465       expires: None,
466     };
467     let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
468     let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
469     let expected_mod_ban = ModBan {
470       id: inserted_mod_ban.id,
471       mod_user_id: inserted_mod.id,
472       other_user_id: inserted_user.id,
473       reason: None,
474       banned: Some(true),
475       expires: None,
476       when_: inserted_mod_ban.when_,
477     };
478
479     // mod add community
480
481     let mod_add_community_form = ModAddCommunityForm {
482       mod_user_id: inserted_mod.id,
483       other_user_id: inserted_user.id,
484       community_id: inserted_community.id,
485       removed: None,
486     };
487     let inserted_mod_add_community =
488       ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
489     let read_mod_add_community =
490       ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
491     let expected_mod_add_community = ModAddCommunity {
492       id: inserted_mod_add_community.id,
493       community_id: inserted_community.id,
494       mod_user_id: inserted_mod.id,
495       other_user_id: inserted_user.id,
496       removed: Some(false),
497       when_: inserted_mod_add_community.when_,
498     };
499
500     // mod add
501
502     let mod_add_form = ModAddForm {
503       mod_user_id: inserted_mod.id,
504       other_user_id: inserted_user.id,
505       removed: None,
506     };
507     let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
508     let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
509     let expected_mod_add = ModAdd {
510       id: inserted_mod_add.id,
511       mod_user_id: inserted_mod.id,
512       other_user_id: inserted_user.id,
513       removed: Some(false),
514       when_: inserted_mod_add.when_,
515     };
516
517     Comment::delete(&conn, inserted_comment.id).unwrap();
518     Post::delete(&conn, inserted_post.id).unwrap();
519     Community::delete(&conn, inserted_community.id).unwrap();
520     User_::delete(&conn, inserted_user.id).unwrap();
521     User_::delete(&conn, inserted_mod.id).unwrap();
522
523     assert_eq!(expected_mod_remove_post, read_mod_remove_post);
524     assert_eq!(expected_mod_lock_post, read_mod_lock_post);
525     assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
526     assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
527     assert_eq!(expected_mod_remove_community, read_mod_remove_community);
528     assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
529     assert_eq!(expected_mod_ban, read_mod_ban);
530     assert_eq!(expected_mod_add_community, read_mod_add_community);
531     assert_eq!(expected_mod_add, read_mod_add);
532   }
533 }