2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::source::moderator::*;
5 impl Crud for ModRemovePost {
6 type Form = ModRemovePostForm;
8 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
9 use lemmy_db_schema::schema::mod_remove_post::dsl::*;
10 mod_remove_post.find(from_id).first::<Self>(conn)
13 fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
14 use lemmy_db_schema::schema::mod_remove_post::dsl::*;
15 insert_into(mod_remove_post)
17 .get_result::<Self>(conn)
20 fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
21 use lemmy_db_schema::schema::mod_remove_post::dsl::*;
22 diesel::update(mod_remove_post.find(from_id))
24 .get_result::<Self>(conn)
28 impl Crud for ModLockPost {
29 type Form = ModLockPostForm;
31 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
32 use lemmy_db_schema::schema::mod_lock_post::dsl::*;
33 mod_lock_post.find(from_id).first::<Self>(conn)
36 fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
37 use lemmy_db_schema::schema::mod_lock_post::dsl::*;
38 insert_into(mod_lock_post)
40 .get_result::<Self>(conn)
43 fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
44 use lemmy_db_schema::schema::mod_lock_post::dsl::*;
45 diesel::update(mod_lock_post.find(from_id))
47 .get_result::<Self>(conn)
51 impl Crud for ModStickyPost {
52 type Form = ModStickyPostForm;
54 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
55 use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
56 mod_sticky_post.find(from_id).first::<Self>(conn)
59 fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
60 use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
61 insert_into(mod_sticky_post)
63 .get_result::<Self>(conn)
66 fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> {
67 use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
68 diesel::update(mod_sticky_post.find(from_id))
70 .get_result::<Self>(conn)
74 impl Crud for ModRemoveComment {
75 type Form = ModRemoveCommentForm;
77 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
78 use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
79 mod_remove_comment.find(from_id).first::<Self>(conn)
82 fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
83 use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
84 insert_into(mod_remove_comment)
86 .get_result::<Self>(conn)
89 fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
90 use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
91 diesel::update(mod_remove_comment.find(from_id))
93 .get_result::<Self>(conn)
97 impl Crud for ModRemoveCommunity {
98 type Form = ModRemoveCommunityForm;
100 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
101 use lemmy_db_schema::schema::mod_remove_community::dsl::*;
102 mod_remove_community.find(from_id).first::<Self>(conn)
105 fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
106 use lemmy_db_schema::schema::mod_remove_community::dsl::*;
107 insert_into(mod_remove_community)
109 .get_result::<Self>(conn)
115 form: &ModRemoveCommunityForm,
116 ) -> Result<Self, Error> {
117 use lemmy_db_schema::schema::mod_remove_community::dsl::*;
118 diesel::update(mod_remove_community.find(from_id))
120 .get_result::<Self>(conn)
124 impl Crud for ModBanFromCommunity {
125 type Form = ModBanFromCommunityForm;
127 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
128 use lemmy_db_schema::schema::mod_ban_from_community::dsl::*;
129 mod_ban_from_community.find(from_id).first::<Self>(conn)
132 fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
133 use lemmy_db_schema::schema::mod_ban_from_community::dsl::*;
134 insert_into(mod_ban_from_community)
136 .get_result::<Self>(conn)
142 form: &ModBanFromCommunityForm,
143 ) -> Result<Self, Error> {
144 use lemmy_db_schema::schema::mod_ban_from_community::dsl::*;
145 diesel::update(mod_ban_from_community.find(from_id))
147 .get_result::<Self>(conn)
151 impl Crud for ModBan {
152 type Form = ModBanForm;
154 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
155 use lemmy_db_schema::schema::mod_ban::dsl::*;
156 mod_ban.find(from_id).first::<Self>(conn)
159 fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
160 use lemmy_db_schema::schema::mod_ban::dsl::*;
161 insert_into(mod_ban).values(form).get_result::<Self>(conn)
164 fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
165 use lemmy_db_schema::schema::mod_ban::dsl::*;
166 diesel::update(mod_ban.find(from_id))
168 .get_result::<Self>(conn)
172 impl Crud for ModAddCommunity {
173 type Form = ModAddCommunityForm;
175 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
176 use lemmy_db_schema::schema::mod_add_community::dsl::*;
177 mod_add_community.find(from_id).first::<Self>(conn)
180 fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
181 use lemmy_db_schema::schema::mod_add_community::dsl::*;
182 insert_into(mod_add_community)
184 .get_result::<Self>(conn)
187 fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
188 use lemmy_db_schema::schema::mod_add_community::dsl::*;
189 diesel::update(mod_add_community.find(from_id))
191 .get_result::<Self>(conn)
195 impl Crud for ModTransferCommunity {
196 type Form = ModTransferCommunityForm;
198 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
199 use lemmy_db_schema::schema::mod_transfer_community::dsl::*;
200 mod_transfer_community.find(from_id).first::<Self>(conn)
203 fn create(conn: &PgConnection, form: &ModTransferCommunityForm) -> Result<Self, Error> {
204 use lemmy_db_schema::schema::mod_transfer_community::dsl::*;
205 insert_into(mod_transfer_community)
207 .get_result::<Self>(conn)
213 form: &ModTransferCommunityForm,
214 ) -> Result<Self, Error> {
215 use lemmy_db_schema::schema::mod_transfer_community::dsl::*;
216 diesel::update(mod_transfer_community.find(from_id))
218 .get_result::<Self>(conn)
222 impl Crud for ModAdd {
223 type Form = ModAddForm;
225 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
226 use lemmy_db_schema::schema::mod_add::dsl::*;
227 mod_add.find(from_id).first::<Self>(conn)
230 fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
231 use lemmy_db_schema::schema::mod_add::dsl::*;
232 insert_into(mod_add).values(form).get_result::<Self>(conn)
235 fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
236 use lemmy_db_schema::schema::mod_add::dsl::*;
237 diesel::update(mod_add.find(from_id))
239 .get_result::<Self>(conn)
245 use crate::{establish_unpooled_connection, Crud};
246 use lemmy_db_schema::source::{comment::*, community::*, moderator::*, person::*, post::*};
247 use serial_test::serial;
253 let conn = establish_unpooled_connection();
255 let new_mod = PersonForm {
256 name: "the mod".into(),
257 ..PersonForm::default()
260 let inserted_mod = Person::create(&conn, &new_mod).unwrap();
262 let new_person = PersonForm {
264 ..PersonForm::default()
267 let inserted_person = Person::create(&conn, &new_person).unwrap();
269 let new_community = CommunityForm {
270 name: "mod_community".to_string(),
271 title: "nada".to_owned(),
272 ..CommunityForm::default()
275 let inserted_community = Community::create(&conn, &new_community).unwrap();
277 let new_post = PostForm {
278 name: "A test post thweep".into(),
279 creator_id: inserted_person.id,
280 community_id: inserted_community.id,
281 ..PostForm::default()
284 let inserted_post = Post::create(&conn, &new_post).unwrap();
286 let comment_form = CommentForm {
287 content: "A test comment".into(),
288 creator_id: inserted_person.id,
289 post_id: inserted_post.id,
290 ..CommentForm::default()
293 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
295 // Now the actual tests
298 let mod_remove_post_form = ModRemovePostForm {
299 mod_person_id: inserted_mod.id,
300 post_id: inserted_post.id,
304 let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
305 let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
306 let expected_mod_remove_post = ModRemovePost {
307 id: inserted_mod_remove_post.id,
308 post_id: inserted_post.id,
309 mod_person_id: inserted_mod.id,
312 when_: inserted_mod_remove_post.when_,
317 let mod_lock_post_form = ModLockPostForm {
318 mod_person_id: inserted_mod.id,
319 post_id: inserted_post.id,
322 let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
323 let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
324 let expected_mod_lock_post = ModLockPost {
325 id: inserted_mod_lock_post.id,
326 post_id: inserted_post.id,
327 mod_person_id: inserted_mod.id,
329 when_: inserted_mod_lock_post.when_,
334 let mod_sticky_post_form = ModStickyPostForm {
335 mod_person_id: inserted_mod.id,
336 post_id: inserted_post.id,
339 let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
340 let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
341 let expected_mod_sticky_post = ModStickyPost {
342 id: inserted_mod_sticky_post.id,
343 post_id: inserted_post.id,
344 mod_person_id: inserted_mod.id,
345 stickied: Some(true),
346 when_: inserted_mod_sticky_post.when_,
351 let mod_remove_comment_form = ModRemoveCommentForm {
352 mod_person_id: inserted_mod.id,
353 comment_id: inserted_comment.id,
357 let inserted_mod_remove_comment =
358 ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
359 let read_mod_remove_comment =
360 ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
361 let expected_mod_remove_comment = ModRemoveComment {
362 id: inserted_mod_remove_comment.id,
363 comment_id: inserted_comment.id,
364 mod_person_id: inserted_mod.id,
367 when_: inserted_mod_remove_comment.when_,
372 let mod_remove_community_form = ModRemoveCommunityForm {
373 mod_person_id: inserted_mod.id,
374 community_id: inserted_community.id,
379 let inserted_mod_remove_community =
380 ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
381 let read_mod_remove_community =
382 ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
383 let expected_mod_remove_community = ModRemoveCommunity {
384 id: inserted_mod_remove_community.id,
385 community_id: inserted_community.id,
386 mod_person_id: inserted_mod.id,
390 when_: inserted_mod_remove_community.when_,
393 // ban from community
395 let mod_ban_from_community_form = ModBanFromCommunityForm {
396 mod_person_id: inserted_mod.id,
397 other_person_id: inserted_person.id,
398 community_id: inserted_community.id,
403 let inserted_mod_ban_from_community =
404 ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
405 let read_mod_ban_from_community =
406 ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
407 let expected_mod_ban_from_community = ModBanFromCommunity {
408 id: inserted_mod_ban_from_community.id,
409 community_id: inserted_community.id,
410 mod_person_id: inserted_mod.id,
411 other_person_id: inserted_person.id,
415 when_: inserted_mod_ban_from_community.when_,
420 let mod_ban_form = ModBanForm {
421 mod_person_id: inserted_mod.id,
422 other_person_id: inserted_person.id,
427 let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
428 let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
429 let expected_mod_ban = ModBan {
430 id: inserted_mod_ban.id,
431 mod_person_id: inserted_mod.id,
432 other_person_id: inserted_person.id,
436 when_: inserted_mod_ban.when_,
441 let mod_add_community_form = ModAddCommunityForm {
442 mod_person_id: inserted_mod.id,
443 other_person_id: inserted_person.id,
444 community_id: inserted_community.id,
447 let inserted_mod_add_community =
448 ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
449 let read_mod_add_community =
450 ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
451 let expected_mod_add_community = ModAddCommunity {
452 id: inserted_mod_add_community.id,
453 community_id: inserted_community.id,
454 mod_person_id: inserted_mod.id,
455 other_person_id: inserted_person.id,
456 removed: Some(false),
457 when_: inserted_mod_add_community.when_,
462 let mod_add_form = ModAddForm {
463 mod_person_id: inserted_mod.id,
464 other_person_id: inserted_person.id,
467 let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
468 let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
469 let expected_mod_add = ModAdd {
470 id: inserted_mod_add.id,
471 mod_person_id: inserted_mod.id,
472 other_person_id: inserted_person.id,
473 removed: Some(false),
474 when_: inserted_mod_add.when_,
477 Comment::delete(&conn, inserted_comment.id).unwrap();
478 Post::delete(&conn, inserted_post.id).unwrap();
479 Community::delete(&conn, inserted_community.id).unwrap();
480 Person::delete(&conn, inserted_person.id).unwrap();
481 Person::delete(&conn, inserted_mod.id).unwrap();
483 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
484 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
485 assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
486 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
487 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
488 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
489 assert_eq!(expected_mod_ban, read_mod_ban);
490 assert_eq!(expected_mod_add_community, read_mod_add_community);
491 assert_eq!(expected_mod_add, read_mod_add);