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