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