2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::source::moderator::*;
5 impl Crud<ModRemovePostForm, i32> 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)
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)
15 .get_result::<Self>(conn)
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))
22 .get_result::<Self>(conn)
26 impl Crud<ModLockPostForm, i32> 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)
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)
36 .get_result::<Self>(conn)
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))
43 .get_result::<Self>(conn)
47 impl Crud<ModStickyPostForm, i32> 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)
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)
57 .get_result::<Self>(conn)
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))
64 .get_result::<Self>(conn)
68 impl Crud<ModRemoveCommentForm, i32> 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)
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)
78 .get_result::<Self>(conn)
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))
85 .get_result::<Self>(conn)
89 impl Crud<ModRemoveCommunityForm, i32> 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)
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)
99 .get_result::<Self>(conn)
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))
110 .get_result::<Self>(conn)
114 impl Crud<ModBanFromCommunityForm, i32> 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)
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)
124 .get_result::<Self>(conn)
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))
135 .get_result::<Self>(conn)
139 impl Crud<ModBanForm, i32> 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)
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)
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))
154 .get_result::<Self>(conn)
158 impl Crud<ModAddCommunityForm, i32> 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)
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)
168 .get_result::<Self>(conn)
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))
175 .get_result::<Self>(conn)
179 impl Crud<ModAddForm, i32> 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)
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)
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))
194 .get_result::<Self>(conn)
200 use crate::{establish_unpooled_connection, Crud};
201 use lemmy_db_schema::source::{comment::*, community::*, moderator::*, person::*, post::*};
202 use serial_test::serial;
208 let conn = establish_unpooled_connection();
210 let new_mod = PersonForm {
211 name: "the mod".into(),
212 preferred_username: None,
224 last_refreshed_at: None,
226 shared_inbox_url: None,
227 matrix_user_id: None,
230 let inserted_mod = Person::create(&conn, &new_mod).unwrap();
232 let new_person = PersonForm {
234 preferred_username: None,
246 last_refreshed_at: None,
248 shared_inbox_url: None,
249 matrix_user_id: None,
252 let inserted_person = Person::create(&conn, &new_person).unwrap();
254 let new_community = CommunityForm {
255 name: "mod_community".to_string(),
256 title: "nada".to_owned(),
258 creator_id: inserted_person.id,
267 last_refreshed_at: None,
273 shared_inbox_url: None,
276 let inserted_community = Community::create(&conn, &new_community).unwrap();
278 let new_post = PostForm {
279 name: "A test post thweep".into(),
282 creator_id: inserted_person.id,
283 community_id: inserted_community.id,
291 embed_description: None,
299 let inserted_post = Post::create(&conn, &new_post).unwrap();
301 let comment_form = CommentForm {
302 content: "A test comment".into(),
303 creator_id: inserted_person.id,
304 post_id: inserted_post.id,
315 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
317 // Now the actual tests
320 let mod_remove_post_form = ModRemovePostForm {
321 mod_person_id: inserted_mod.id,
322 post_id: inserted_post.id,
326 let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
327 let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
328 let expected_mod_remove_post = ModRemovePost {
329 id: inserted_mod_remove_post.id,
330 post_id: inserted_post.id,
331 mod_person_id: inserted_mod.id,
334 when_: inserted_mod_remove_post.when_,
339 let mod_lock_post_form = ModLockPostForm {
340 mod_person_id: inserted_mod.id,
341 post_id: inserted_post.id,
344 let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
345 let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
346 let expected_mod_lock_post = ModLockPost {
347 id: inserted_mod_lock_post.id,
348 post_id: inserted_post.id,
349 mod_person_id: inserted_mod.id,
351 when_: inserted_mod_lock_post.when_,
356 let mod_sticky_post_form = ModStickyPostForm {
357 mod_person_id: inserted_mod.id,
358 post_id: inserted_post.id,
361 let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
362 let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
363 let expected_mod_sticky_post = ModStickyPost {
364 id: inserted_mod_sticky_post.id,
365 post_id: inserted_post.id,
366 mod_person_id: inserted_mod.id,
367 stickied: Some(true),
368 when_: inserted_mod_sticky_post.when_,
373 let mod_remove_comment_form = ModRemoveCommentForm {
374 mod_person_id: inserted_mod.id,
375 comment_id: inserted_comment.id,
379 let inserted_mod_remove_comment =
380 ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
381 let read_mod_remove_comment =
382 ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
383 let expected_mod_remove_comment = ModRemoveComment {
384 id: inserted_mod_remove_comment.id,
385 comment_id: inserted_comment.id,
386 mod_person_id: inserted_mod.id,
389 when_: inserted_mod_remove_comment.when_,
394 let mod_remove_community_form = ModRemoveCommunityForm {
395 mod_person_id: inserted_mod.id,
396 community_id: inserted_community.id,
401 let inserted_mod_remove_community =
402 ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
403 let read_mod_remove_community =
404 ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
405 let expected_mod_remove_community = ModRemoveCommunity {
406 id: inserted_mod_remove_community.id,
407 community_id: inserted_community.id,
408 mod_person_id: inserted_mod.id,
412 when_: inserted_mod_remove_community.when_,
415 // ban from community
417 let mod_ban_from_community_form = ModBanFromCommunityForm {
418 mod_person_id: inserted_mod.id,
419 other_person_id: inserted_person.id,
420 community_id: inserted_community.id,
425 let inserted_mod_ban_from_community =
426 ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
427 let read_mod_ban_from_community =
428 ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
429 let expected_mod_ban_from_community = ModBanFromCommunity {
430 id: inserted_mod_ban_from_community.id,
431 community_id: inserted_community.id,
432 mod_person_id: inserted_mod.id,
433 other_person_id: inserted_person.id,
437 when_: inserted_mod_ban_from_community.when_,
442 let mod_ban_form = ModBanForm {
443 mod_person_id: inserted_mod.id,
444 other_person_id: inserted_person.id,
449 let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
450 let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
451 let expected_mod_ban = ModBan {
452 id: inserted_mod_ban.id,
453 mod_person_id: inserted_mod.id,
454 other_person_id: inserted_person.id,
458 when_: inserted_mod_ban.when_,
463 let mod_add_community_form = ModAddCommunityForm {
464 mod_person_id: inserted_mod.id,
465 other_person_id: inserted_person.id,
466 community_id: inserted_community.id,
469 let inserted_mod_add_community =
470 ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
471 let read_mod_add_community =
472 ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
473 let expected_mod_add_community = ModAddCommunity {
474 id: inserted_mod_add_community.id,
475 community_id: inserted_community.id,
476 mod_person_id: inserted_mod.id,
477 other_person_id: inserted_person.id,
478 removed: Some(false),
479 when_: inserted_mod_add_community.when_,
484 let mod_add_form = ModAddForm {
485 mod_person_id: inserted_mod.id,
486 other_person_id: inserted_person.id,
489 let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
490 let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
491 let expected_mod_add = ModAdd {
492 id: inserted_mod_add.id,
493 mod_person_id: inserted_mod.id,
494 other_person_id: inserted_person.id,
495 removed: Some(false),
496 when_: inserted_mod_add.when_,
499 Comment::delete(&conn, inserted_comment.id).unwrap();
500 Post::delete(&conn, inserted_post.id).unwrap();
501 Community::delete(&conn, inserted_community.id).unwrap();
502 Person::delete(&conn, inserted_person.id).unwrap();
503 Person::delete(&conn, inserted_mod.id).unwrap();
505 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
506 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
507 assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
508 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
509 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
510 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
511 assert_eq!(expected_mod_ban, read_mod_ban);
512 assert_eq!(expected_mod_add_community, read_mod_add_community);
513 assert_eq!(expected_mod_add, read_mod_add);