1 use crate::schema::{mod_remove_post, mod_lock_post, mod_remove_comment, mod_remove_community, mod_ban_from_community, mod_ban, mod_add_community, mod_add};
4 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
5 #[table_name="mod_remove_post"]
6 pub struct ModRemovePost {
10 pub reason: Option<String>,
11 pub removed: Option<bool>,
12 pub when_: chrono::NaiveDateTime,
15 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
16 #[table_name="mod_remove_post"]
17 pub struct ModRemovePostForm {
20 pub reason: Option<String>,
21 pub removed: Option<bool>,
24 impl Crud<ModRemovePostForm> for ModRemovePost {
25 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
26 use crate::schema::mod_remove_post::dsl::*;
27 mod_remove_post.find(from_id)
31 fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
32 use crate::schema::mod_remove_post::dsl::*;
33 diesel::delete(mod_remove_post.find(from_id))
37 fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
38 use crate::schema::mod_remove_post::dsl::*;
39 insert_into(mod_remove_post)
41 .get_result::<Self>(conn)
44 fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
45 use crate::schema::mod_remove_post::dsl::*;
46 diesel::update(mod_remove_post.find(from_id))
48 .get_result::<Self>(conn)
54 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
55 #[table_name="mod_lock_post"]
56 pub struct ModLockPost {
60 pub locked: Option<bool>,
61 pub when_: chrono::NaiveDateTime,
64 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
65 #[table_name="mod_lock_post"]
66 pub struct ModLockPostForm {
69 pub locked: Option<bool>,
72 impl Crud<ModLockPostForm> for ModLockPost {
73 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
74 use crate::schema::mod_lock_post::dsl::*;
75 mod_lock_post.find(from_id)
79 fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
80 use crate::schema::mod_lock_post::dsl::*;
81 diesel::delete(mod_lock_post.find(from_id))
85 fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
86 use crate::schema::mod_lock_post::dsl::*;
87 insert_into(mod_lock_post)
89 .get_result::<Self>(conn)
92 fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
93 use crate::schema::mod_lock_post::dsl::*;
94 diesel::update(mod_lock_post.find(from_id))
96 .get_result::<Self>(conn)
100 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
101 #[table_name="mod_remove_comment"]
102 pub struct ModRemoveComment {
104 pub mod_user_id: i32,
106 pub reason: Option<String>,
107 pub removed: Option<bool>,
108 pub when_: chrono::NaiveDateTime,
111 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
112 #[table_name="mod_remove_comment"]
113 pub struct ModRemoveCommentForm {
114 pub mod_user_id: i32,
116 pub reason: Option<String>,
117 pub removed: Option<bool>,
120 impl Crud<ModRemoveCommentForm> for ModRemoveComment {
121 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
122 use crate::schema::mod_remove_comment::dsl::*;
123 mod_remove_comment.find(from_id)
127 fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
128 use crate::schema::mod_remove_comment::dsl::*;
129 diesel::delete(mod_remove_comment.find(from_id))
133 fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
134 use crate::schema::mod_remove_comment::dsl::*;
135 insert_into(mod_remove_comment)
137 .get_result::<Self>(conn)
140 fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
141 use crate::schema::mod_remove_comment::dsl::*;
142 diesel::update(mod_remove_comment.find(from_id))
144 .get_result::<Self>(conn)
148 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
149 #[table_name="mod_remove_community"]
150 pub struct ModRemoveCommunity {
152 pub mod_user_id: i32,
153 pub community_id: i32,
154 pub reason: Option<String>,
155 pub removed: Option<bool>,
156 pub expires: Option<chrono::NaiveDateTime>,
157 pub when_: chrono::NaiveDateTime,
160 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
161 #[table_name="mod_remove_community"]
162 pub struct ModRemoveCommunityForm {
163 pub mod_user_id: i32,
164 pub community_id: i32,
165 pub reason: Option<String>,
166 pub removed: Option<bool>,
167 pub expires: Option<chrono::NaiveDateTime>,
170 impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
171 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
172 use crate::schema::mod_remove_community::dsl::*;
173 mod_remove_community.find(from_id)
177 fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
178 use crate::schema::mod_remove_community::dsl::*;
179 diesel::delete(mod_remove_community.find(from_id))
183 fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
184 use crate::schema::mod_remove_community::dsl::*;
185 insert_into(mod_remove_community)
187 .get_result::<Self>(conn)
190 fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
191 use crate::schema::mod_remove_community::dsl::*;
192 diesel::update(mod_remove_community.find(from_id))
194 .get_result::<Self>(conn)
198 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
199 #[table_name="mod_ban_from_community"]
200 pub struct ModBanFromCommunity {
202 pub mod_user_id: i32,
203 pub other_user_id: i32,
204 pub community_id: i32,
205 pub reason: Option<String>,
206 pub banned: Option<bool>,
207 pub expires: Option<chrono::NaiveDateTime>,
208 pub when_: chrono::NaiveDateTime,
211 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
212 #[table_name="mod_ban_from_community"]
213 pub struct ModBanFromCommunityForm {
214 pub mod_user_id: i32,
215 pub other_user_id: i32,
216 pub community_id: i32,
217 pub reason: Option<String>,
218 pub banned: Option<bool>,
219 pub expires: Option<chrono::NaiveDateTime>,
222 impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
223 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
224 use crate::schema::mod_ban_from_community::dsl::*;
225 mod_ban_from_community.find(from_id)
229 fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
230 use crate::schema::mod_ban_from_community::dsl::*;
231 diesel::delete(mod_ban_from_community.find(from_id))
235 fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
236 use crate::schema::mod_ban_from_community::dsl::*;
237 insert_into(mod_ban_from_community)
239 .get_result::<Self>(conn)
242 fn update(conn: &PgConnection, from_id: i32, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
243 use crate::schema::mod_ban_from_community::dsl::*;
244 diesel::update(mod_ban_from_community.find(from_id))
246 .get_result::<Self>(conn)
251 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
252 #[table_name="mod_ban"]
255 pub mod_user_id: i32,
256 pub other_user_id: i32,
257 pub reason: Option<String>,
258 pub banned: Option<bool>,
259 pub expires: Option<chrono::NaiveDateTime>,
260 pub when_: chrono::NaiveDateTime,
263 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
264 #[table_name="mod_ban"]
265 pub struct ModBanForm {
266 pub mod_user_id: i32,
267 pub other_user_id: i32,
268 pub reason: Option<String>,
269 pub banned: Option<bool>,
270 pub expires: Option<chrono::NaiveDateTime>,
273 impl Crud<ModBanForm> for ModBan {
274 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
275 use crate::schema::mod_ban::dsl::*;
276 mod_ban.find(from_id)
280 fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
281 use crate::schema::mod_ban::dsl::*;
282 diesel::delete(mod_ban.find(from_id))
286 fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
287 use crate::schema::mod_ban::dsl::*;
290 .get_result::<Self>(conn)
293 fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
294 use crate::schema::mod_ban::dsl::*;
295 diesel::update(mod_ban.find(from_id))
297 .get_result::<Self>(conn)
301 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
302 #[table_name="mod_add_community"]
303 pub struct ModAddCommunity {
305 pub mod_user_id: i32,
306 pub other_user_id: i32,
307 pub community_id: i32,
308 pub removed: Option<bool>,
309 pub when_: chrono::NaiveDateTime,
312 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
313 #[table_name="mod_add_community"]
314 pub struct ModAddCommunityForm {
315 pub mod_user_id: i32,
316 pub other_user_id: i32,
317 pub community_id: i32,
318 pub removed: Option<bool>,
321 impl Crud<ModAddCommunityForm> for ModAddCommunity {
322 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
323 use crate::schema::mod_add_community::dsl::*;
324 mod_add_community.find(from_id)
328 fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
329 use crate::schema::mod_add_community::dsl::*;
330 diesel::delete(mod_add_community.find(from_id))
334 fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
335 use crate::schema::mod_add_community::dsl::*;
336 insert_into(mod_add_community)
338 .get_result::<Self>(conn)
341 fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
342 use crate::schema::mod_add_community::dsl::*;
343 diesel::update(mod_add_community.find(from_id))
345 .get_result::<Self>(conn)
349 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
350 #[table_name="mod_add"]
353 pub mod_user_id: i32,
354 pub other_user_id: i32,
355 pub removed: Option<bool>,
356 pub when_: chrono::NaiveDateTime,
359 #[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
360 #[table_name="mod_add"]
361 pub struct ModAddForm {
362 pub mod_user_id: i32,
363 pub other_user_id: i32,
364 pub removed: Option<bool>,
367 impl Crud<ModAddForm> for ModAdd {
368 fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
369 use crate::schema::mod_add::dsl::*;
370 mod_add.find(from_id)
374 fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
375 use crate::schema::mod_add::dsl::*;
376 diesel::delete(mod_add.find(from_id))
380 fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
381 use crate::schema::mod_add::dsl::*;
384 .get_result::<Self>(conn)
387 fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
388 use crate::schema::mod_add::dsl::*;
389 diesel::update(mod_add.find(from_id))
391 .get_result::<Self>(conn)
398 use super::super::user::*;
399 use super::super::post::*;
400 use super::super::community::*;
401 use super::super::comment::*;
405 let conn = establish_connection();
407 let new_mod = UserForm {
408 name: "the mod".into(),
409 fedi_name: "rrf".into(),
410 preferred_username: None,
411 password_encrypted: "nope".into(),
419 let inserted_mod = User_::create(&conn, &new_mod).unwrap();
421 let new_user = UserForm {
423 fedi_name: "rrf".into(),
424 preferred_username: None,
425 password_encrypted: "nope".into(),
433 let inserted_user = User_::create(&conn, &new_user).unwrap();
435 let new_community = CommunityForm {
436 name: "mod_community".to_string(),
437 title: "nada".to_owned(),
440 creator_id: inserted_user.id,
447 let inserted_community = Community::create(&conn, &new_community).unwrap();
449 let new_post = PostForm {
450 name: "A test post thweep".into(),
453 creator_id: inserted_user.id,
454 community_id: inserted_community.id,
462 let inserted_post = Post::create(&conn, &new_post).unwrap();
464 let comment_form = CommentForm {
465 content: "A test comment".into(),
466 creator_id: inserted_user.id,
467 post_id: inserted_post.id,
475 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
477 // Now the actual tests
480 let mod_remove_post_form = ModRemovePostForm {
481 mod_user_id: inserted_mod.id,
482 post_id: inserted_post.id,
486 let inserted_mod_remove_post = ModRemovePost::create(&conn, &mod_remove_post_form).unwrap();
487 let read_moderator_remove_post = ModRemovePost::read(&conn, inserted_mod_remove_post.id).unwrap();
488 let expected_moderator_remove_post = ModRemovePost {
489 id: inserted_mod_remove_post.id,
490 post_id: inserted_post.id,
491 mod_user_id: inserted_mod.id,
494 when_: inserted_mod_remove_post.when_,
499 let mod_lock_post_form = ModLockPostForm {
500 mod_user_id: inserted_mod.id,
501 post_id: inserted_post.id,
504 let inserted_mod_lock_post = ModLockPost::create(&conn, &mod_lock_post_form).unwrap();
505 let read_moderator_lock_post = ModLockPost::read(&conn, inserted_mod_lock_post.id).unwrap();
506 let expected_moderator_lock_post = ModLockPost {
507 id: inserted_mod_lock_post.id,
508 post_id: inserted_post.id,
509 mod_user_id: inserted_mod.id,
511 when_: inserted_mod_lock_post.when_,
516 let mod_remove_comment_form = ModRemoveCommentForm {
517 mod_user_id: inserted_mod.id,
518 comment_id: inserted_comment.id,
522 let inserted_mod_remove_comment = ModRemoveComment::create(&conn, &mod_remove_comment_form).unwrap();
523 let read_moderator_remove_comment = ModRemoveComment::read(&conn, inserted_mod_remove_comment.id).unwrap();
524 let expected_moderator_remove_comment = ModRemoveComment {
525 id: inserted_mod_remove_comment.id,
526 comment_id: inserted_comment.id,
527 mod_user_id: inserted_mod.id,
530 when_: inserted_mod_remove_comment.when_,
535 let mod_remove_community_form = ModRemoveCommunityForm {
536 mod_user_id: inserted_mod.id,
537 community_id: inserted_community.id,
542 let inserted_mod_remove_community = ModRemoveCommunity::create(&conn, &mod_remove_community_form).unwrap();
543 let read_moderator_remove_community = ModRemoveCommunity::read(&conn, inserted_mod_remove_community.id).unwrap();
544 let expected_moderator_remove_community = ModRemoveCommunity {
545 id: inserted_mod_remove_community.id,
546 community_id: inserted_community.id,
547 mod_user_id: inserted_mod.id,
551 when_: inserted_mod_remove_community.when_,
554 // ban from community
556 let mod_ban_from_community_form = ModBanFromCommunityForm {
557 mod_user_id: inserted_mod.id,
558 other_user_id: inserted_user.id,
559 community_id: inserted_community.id,
564 let inserted_mod_ban_from_community = ModBanFromCommunity::create(&conn, &mod_ban_from_community_form).unwrap();
565 let read_moderator_ban_from_community = ModBanFromCommunity::read(&conn, inserted_mod_ban_from_community.id).unwrap();
566 let expected_moderator_ban_from_community = ModBanFromCommunity {
567 id: inserted_mod_ban_from_community.id,
568 community_id: inserted_community.id,
569 mod_user_id: inserted_mod.id,
570 other_user_id: inserted_user.id,
574 when_: inserted_mod_ban_from_community.when_,
579 let mod_ban_form = ModBanForm {
580 mod_user_id: inserted_mod.id,
581 other_user_id: inserted_user.id,
586 let inserted_mod_ban = ModBan::create(&conn, &mod_ban_form).unwrap();
587 let read_moderator_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
588 let expected_moderator_ban = ModBan {
589 id: inserted_mod_ban.id,
590 mod_user_id: inserted_mod.id,
591 other_user_id: inserted_user.id,
595 when_: inserted_mod_ban.when_,
600 let mod_add_community_form = ModAddCommunityForm {
601 mod_user_id: inserted_mod.id,
602 other_user_id: inserted_user.id,
603 community_id: inserted_community.id,
606 let inserted_mod_add_community = ModAddCommunity::create(&conn, &mod_add_community_form).unwrap();
607 let read_moderator_add_community = ModAddCommunity::read(&conn, inserted_mod_add_community.id).unwrap();
608 let expected_moderator_add_community = ModAddCommunity {
609 id: inserted_mod_add_community.id,
610 community_id: inserted_community.id,
611 mod_user_id: inserted_mod.id,
612 other_user_id: inserted_user.id,
613 removed: Some(false),
614 when_: inserted_mod_add_community.when_,
619 let mod_add_form = ModAddForm {
620 mod_user_id: inserted_mod.id,
621 other_user_id: inserted_user.id,
624 let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
625 let read_moderator_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
626 let expected_moderator_add = ModAdd {
627 id: inserted_mod_add.id,
628 mod_user_id: inserted_mod.id,
629 other_user_id: inserted_user.id,
630 removed: Some(false),
631 when_: inserted_mod_add.when_,
634 ModRemovePost::delete(&conn, inserted_mod_remove_post.id).unwrap();
635 ModLockPost::delete(&conn, inserted_mod_lock_post.id).unwrap();
636 ModRemoveComment::delete(&conn, inserted_mod_remove_comment.id).unwrap();
637 ModRemoveCommunity::delete(&conn, inserted_mod_remove_community.id).unwrap();
638 ModBanFromCommunity::delete(&conn, inserted_mod_ban_from_community.id).unwrap();
639 ModBan::delete(&conn, inserted_mod_ban.id).unwrap();
640 ModAddCommunity::delete(&conn, inserted_mod_add_community.id).unwrap();
641 ModAdd::delete(&conn, inserted_mod_add.id).unwrap();
643 Comment::delete(&conn, inserted_comment.id).unwrap();
644 Post::delete(&conn, inserted_post.id).unwrap();
645 Community::delete(&conn, inserted_community.id).unwrap();
646 User_::delete(&conn, inserted_user.id).unwrap();
647 User_::delete(&conn, inserted_mod.id).unwrap();
649 assert_eq!(expected_moderator_remove_post, read_moderator_remove_post);
650 assert_eq!(expected_moderator_lock_post, read_moderator_lock_post);
651 assert_eq!(expected_moderator_remove_comment, read_moderator_remove_comment);
652 assert_eq!(expected_moderator_remove_community, read_moderator_remove_community);
653 assert_eq!(expected_moderator_ban_from_community, read_moderator_ban_from_community);
654 assert_eq!(expected_moderator_ban, read_moderator_ban);
655 assert_eq!(expected_moderator_add_community, read_moderator_add_community);
656 assert_eq!(expected_moderator_add, read_moderator_add);