2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::source::moderator::*;
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)
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> 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> 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> 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> 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> 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> 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> 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> 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, ListingType, SortType};
201 use lemmy_db_schema::source::{comment::*, community::*, moderator::*, post::*, person::*};
206 let conn = establish_unpooled_connection();
208 let new_mod = PersonForm {
209 name: "the mod".into(),
210 preferred_username: None,
222 last_refreshed_at: None,
224 shared_inbox_url: None,
227 let inserted_mod = Person::create(&conn, &new_mod).unwrap();
229 let new_person = PersonForm {
231 preferred_username: None,
243 last_refreshed_at: None,
245 shared_inbox_url: None,
248 let inserted_person = Person::create(&conn, &new_person).unwrap();
250 let new_community = CommunityForm {
251 name: "mod_community".to_string(),
252 title: "nada".to_owned(),
254 creator_id: inserted_person.id,
263 last_refreshed_at: None,
269 shared_inbox_url: None,
272 let inserted_community = Community::create(&conn, &new_community).unwrap();
274 let new_post = PostForm {
275 name: "A test post thweep".into(),
278 creator_id: inserted_person.id,
279 community_id: inserted_community.id,
287 embed_description: None,
295 let inserted_post = Post::create(&conn, &new_post).unwrap();
297 let comment_form = CommentForm {
298 content: "A test comment".into(),
299 creator_id: inserted_person.id,
300 post_id: inserted_post.id,
311 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
313 // Now the actual tests
316 let mod_remove_post_form = ModRemovePostForm {
317 mod_person_id: inserted_mod.id,
318 post_id: inserted_post.id,
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,
330 when_: inserted_mod_remove_post.when_,
335 let mod_lock_post_form = ModLockPostForm {
336 mod_person_id: inserted_mod.id,
337 post_id: inserted_post.id,
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,
347 when_: inserted_mod_lock_post.when_,
352 let mod_sticky_post_form = ModStickyPostForm {
353 mod_person_id: inserted_mod.id,
354 post_id: inserted_post.id,
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_,
369 let mod_remove_comment_form = ModRemoveCommentForm {
370 mod_person_id: inserted_mod.id,
371 comment_id: inserted_comment.id,
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,
385 when_: inserted_mod_remove_comment.when_,
390 let mod_remove_community_form = ModRemoveCommunityForm {
391 mod_person_id: inserted_mod.id,
392 community_id: inserted_community.id,
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,
408 when_: inserted_mod_remove_community.when_,
411 // ban from community
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,
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,
433 when_: inserted_mod_ban_from_community.when_,
438 let mod_ban_form = ModBanForm {
439 mod_person_id: inserted_mod.id,
440 other_person_id: inserted_person.id,
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,
454 when_: inserted_mod_ban.when_,
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,
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_,
480 let mod_add_form = ModAddForm {
481 mod_person_id: inserted_mod.id,
482 other_person_id: inserted_person.id,
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_,
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();
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);