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 ModAdd {
196 type Form = ModAddForm;
198 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
199 use lemmy_db_schema::schema::mod_add::dsl::*;
200 mod_add.find(from_id).first::<Self>(conn)
203 fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
204 use lemmy_db_schema::schema::mod_add::dsl::*;
205 insert_into(mod_add).values(form).get_result::<Self>(conn)
208 fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
209 use lemmy_db_schema::schema::mod_add::dsl::*;
210 diesel::update(mod_add.find(from_id))
212 .get_result::<Self>(conn)
218 use crate::{establish_unpooled_connection, Crud};
219 use lemmy_db_schema::source::{comment::*, community::*, moderator::*, person::*, post::*};
220 use serial_test::serial;
226 let conn = establish_unpooled_connection();
228 let new_mod = PersonForm {
229 name: "the mod".into(),
230 ..PersonForm::default()
233 let inserted_mod = Person::create(&conn, &new_mod).unwrap();
235 let new_person = PersonForm {
237 ..PersonForm::default()
240 let inserted_person = Person::create(&conn, &new_person).unwrap();
242 let new_community = CommunityForm {
243 name: "mod_community".to_string(),
244 title: "nada".to_owned(),
245 ..CommunityForm::default()
248 let inserted_community = Community::create(&conn, &new_community).unwrap();
250 let new_post = PostForm {
251 name: "A test post thweep".into(),
252 creator_id: inserted_person.id,
253 community_id: inserted_community.id,
254 ..PostForm::default()
257 let inserted_post = Post::create(&conn, &new_post).unwrap();
259 let comment_form = CommentForm {
260 content: "A test comment".into(),
261 creator_id: inserted_person.id,
262 post_id: inserted_post.id,
263 ..CommentForm::default()
266 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
268 // Now the actual tests
271 let mod_remove_post_form = ModRemovePostForm {
272 mod_person_id: inserted_mod.id,
273 post_id: inserted_post.id,
277 let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
278 let read_mod_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
279 let expected_mod_remove_post = ModRemovePost {
280 id: inserted_mod_remove_post.id,
281 post_id: inserted_post.id,
282 mod_person_id: inserted_mod.id,
285 when_: inserted_mod_remove_post.when_,
290 let mod_lock_post_form = ModLockPostForm {
291 mod_person_id: inserted_mod.id,
292 post_id: inserted_post.id,
295 let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
296 let read_mod_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
297 let expected_mod_lock_post = ModLockPost {
298 id: inserted_mod_lock_post.id,
299 post_id: inserted_post.id,
300 mod_person_id: inserted_mod.id,
302 when_: inserted_mod_lock_post.when_,
307 let mod_sticky_post_form = ModStickyPostForm {
308 mod_person_id: inserted_mod.id,
309 post_id: inserted_post.id,
312 let inserted_mod_sticky_post = ModStickyPost::create(&conn, &mod_sticky_post_form).unwrap();
313 let read_mod_sticky_post = ModStickyPost::read(&conn, inserted_mod_sticky_post.id).unwrap();
314 let expected_mod_sticky_post = ModStickyPost {
315 id: inserted_mod_sticky_post.id,
316 post_id: inserted_post.id,
317 mod_person_id: inserted_mod.id,
318 stickied: Some(true),
319 when_: inserted_mod_sticky_post.when_,
324 let mod_remove_comment_form = ModRemoveCommentForm {
325 mod_person_id: inserted_mod.id,
326 comment_id: inserted_comment.id,
330 let inserted_mod_remove_comment =
331 ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
332 let read_mod_remove_comment =
333 ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
334 let expected_mod_remove_comment = ModRemoveComment {
335 id: inserted_mod_remove_comment.id,
336 comment_id: inserted_comment.id,
337 mod_person_id: inserted_mod.id,
340 when_: inserted_mod_remove_comment.when_,
345 let mod_remove_community_form = ModRemoveCommunityForm {
346 mod_person_id: inserted_mod.id,
347 community_id: inserted_community.id,
352 let inserted_mod_remove_community =
353 ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
354 let read_mod_remove_community =
355 ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
356 let expected_mod_remove_community = ModRemoveCommunity {
357 id: inserted_mod_remove_community.id,
358 community_id: inserted_community.id,
359 mod_person_id: inserted_mod.id,
363 when_: inserted_mod_remove_community.when_,
366 // ban from community
368 let mod_ban_from_community_form = ModBanFromCommunityForm {
369 mod_person_id: inserted_mod.id,
370 other_person_id: inserted_person.id,
371 community_id: inserted_community.id,
376 let inserted_mod_ban_from_community =
377 ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
378 let read_mod_ban_from_community =
379 ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
380 let expected_mod_ban_from_community = ModBanFromCommunity {
381 id: inserted_mod_ban_from_community.id,
382 community_id: inserted_community.id,
383 mod_person_id: inserted_mod.id,
384 other_person_id: inserted_person.id,
388 when_: inserted_mod_ban_from_community.when_,
393 let mod_ban_form = ModBanForm {
394 mod_person_id: inserted_mod.id,
395 other_person_id: inserted_person.id,
400 let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
401 let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
402 let expected_mod_ban = ModBan {
403 id: inserted_mod_ban.id,
404 mod_person_id: inserted_mod.id,
405 other_person_id: inserted_person.id,
409 when_: inserted_mod_ban.when_,
414 let mod_add_community_form = ModAddCommunityForm {
415 mod_person_id: inserted_mod.id,
416 other_person_id: inserted_person.id,
417 community_id: inserted_community.id,
420 let inserted_mod_add_community =
421 ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
422 let read_mod_add_community =
423 ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
424 let expected_mod_add_community = ModAddCommunity {
425 id: inserted_mod_add_community.id,
426 community_id: inserted_community.id,
427 mod_person_id: inserted_mod.id,
428 other_person_id: inserted_person.id,
429 removed: Some(false),
430 when_: inserted_mod_add_community.when_,
435 let mod_add_form = ModAddForm {
436 mod_person_id: inserted_mod.id,
437 other_person_id: inserted_person.id,
440 let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
441 let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
442 let expected_mod_add = ModAdd {
443 id: inserted_mod_add.id,
444 mod_person_id: inserted_mod.id,
445 other_person_id: inserted_person.id,
446 removed: Some(false),
447 when_: inserted_mod_add.when_,
450 Comment::delete(&conn, inserted_comment.id).unwrap();
451 Post::delete(&conn, inserted_post.id).unwrap();
452 Community::delete(&conn, inserted_community.id).unwrap();
453 Person::delete(&conn, inserted_person.id).unwrap();
454 Person::delete(&conn, inserted_mod.id).unwrap();
456 assert_eq!(expected_mod_remove_post, read_mod_remove_post);
457 assert_eq!(expected_mod_lock_post, read_mod_lock_post);
458 assert_eq!(expected_mod_sticky_post, read_mod_sticky_post);
459 assert_eq!(expected_mod_remove_comment, read_mod_remove_comment);
460 assert_eq!(expected_mod_remove_community, read_mod_remove_community);
461 assert_eq!(expected_mod_ban_from_community, read_mod_ban_from_community);
462 assert_eq!(expected_mod_ban, read_mod_ban);
463 assert_eq!(expected_mod_add_community, read_mod_add_community);
464 assert_eq!(expected_mod_add, read_mod_add);