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