]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/moderator.rs
Moving matrix_user_id to person table. #1438
[lemmy.git] / crates / db_queries / src / source / moderator.rs
1 use crate::Crud;
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::source::moderator::*;
4
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)
9   }
10
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)
14       .values(form)
15       .get_result::<Self>(conn)
16   }
17
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))
21       .set(form)
22       .get_result::<Self>(conn)
23   }
24 }
25
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)
30   }
31
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)
35       .values(form)
36       .get_result::<Self>(conn)
37   }
38
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))
42       .set(form)
43       .get_result::<Self>(conn)
44   }
45 }
46
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)
51   }
52
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)
56       .values(form)
57       .get_result::<Self>(conn)
58   }
59
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))
63       .set(form)
64       .get_result::<Self>(conn)
65   }
66 }
67
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)
72   }
73
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)
77       .values(form)
78       .get_result::<Self>(conn)
79   }
80
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))
84       .set(form)
85       .get_result::<Self>(conn)
86   }
87 }
88
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)
93   }
94
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)
98       .values(form)
99       .get_result::<Self>(conn)
100   }
101
102   fn update(
103     conn: &PgConnection,
104     from_id: i32,
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))
109       .set(form)
110       .get_result::<Self>(conn)
111   }
112 }
113
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)
118   }
119
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)
123       .values(form)
124       .get_result::<Self>(conn)
125   }
126
127   fn update(
128     conn: &PgConnection,
129     from_id: i32,
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))
134       .set(form)
135       .get_result::<Self>(conn)
136   }
137 }
138
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)
143   }
144
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)
148   }
149
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))
153       .set(form)
154       .get_result::<Self>(conn)
155   }
156 }
157
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)
162   }
163
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)
167       .values(form)
168       .get_result::<Self>(conn)
169   }
170
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))
174       .set(form)
175       .get_result::<Self>(conn)
176   }
177 }
178
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)
183   }
184
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)
188   }
189
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))
193       .set(form)
194       .get_result::<Self>(conn)
195   }
196 }
197
198 #[cfg(test)]
199 mod tests {
200   use crate::{establish_unpooled_connection, Crud};
201   use lemmy_db_schema::source::{comment::*, community::*, moderator::*, person::*, post::*};
202   use serial_test::serial;
203
204   // use Crud;
205   #[test]
206   #[serial]
207   fn test_crud() {
208     let conn = establish_unpooled_connection();
209
210     let new_mod = PersonForm {
211       name: "the mod".into(),
212       preferred_username: None,
213       avatar: None,
214       banner: None,
215       banned: None,
216       deleted: None,
217       published: None,
218       updated: None,
219       actor_id: None,
220       bio: None,
221       local: None,
222       private_key: None,
223       public_key: None,
224       last_refreshed_at: None,
225       inbox_url: None,
226       shared_inbox_url: None,
227       matrix_user_id: None,
228     };
229
230     let inserted_mod = Person::create(&conn, &new_mod).unwrap();
231
232     let new_person = PersonForm {
233       name: "jim2".into(),
234       preferred_username: None,
235       avatar: None,
236       banner: None,
237       banned: None,
238       deleted: None,
239       published: None,
240       updated: None,
241       actor_id: None,
242       bio: None,
243       local: None,
244       private_key: None,
245       public_key: None,
246       last_refreshed_at: None,
247       inbox_url: None,
248       shared_inbox_url: None,
249       matrix_user_id: None,
250     };
251
252     let inserted_person = Person::create(&conn, &new_person).unwrap();
253
254     let new_community = CommunityForm {
255       name: "mod_community".to_string(),
256       title: "nada".to_owned(),
257       description: None,
258       creator_id: inserted_person.id,
259       removed: None,
260       deleted: None,
261       updated: None,
262       nsfw: false,
263       actor_id: None,
264       local: true,
265       private_key: None,
266       public_key: None,
267       last_refreshed_at: None,
268       published: None,
269       icon: None,
270       banner: None,
271       followers_url: None,
272       inbox_url: None,
273       shared_inbox_url: None,
274     };
275
276     let inserted_community = Community::create(&conn, &new_community).unwrap();
277
278     let new_post = PostForm {
279       name: "A test post thweep".into(),
280       url: None,
281       body: None,
282       creator_id: inserted_person.id,
283       community_id: inserted_community.id,
284       removed: None,
285       deleted: None,
286       locked: None,
287       stickied: None,
288       updated: None,
289       nsfw: false,
290       embed_title: None,
291       embed_description: None,
292       embed_html: None,
293       thumbnail_url: None,
294       ap_id: None,
295       local: true,
296       published: None,
297     };
298
299     let inserted_post = Post::create(&conn, &new_post).unwrap();
300
301     let comment_form = CommentForm {
302       content: "A test comment".into(),
303       creator_id: inserted_person.id,
304       post_id: inserted_post.id,
305       removed: None,
306       deleted: None,
307       read: None,
308       parent_id: None,
309       published: None,
310       updated: None,
311       ap_id: None,
312       local: true,
313     };
314
315     let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
316
317     // Now the actual tests
318
319     // remove post
320     let mod_remove_post_form = ModRemovePostForm {
321       mod_person_id: inserted_mod.id,
322       post_id: inserted_post.id,
323       reason: None,
324       removed: None,
325     };
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,
332       reason: None,
333       removed: Some(true),
334       when_: inserted_mod_remove_post.when_,
335     };
336
337     // lock post
338
339     let mod_lock_post_form = ModLockPostForm {
340       mod_person_id: inserted_mod.id,
341       post_id: inserted_post.id,
342       locked: None,
343     };
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,
350       locked: Some(true),
351       when_: inserted_mod_lock_post.when_,
352     };
353
354     // sticky post
355
356     let mod_sticky_post_form = ModStickyPostForm {
357       mod_person_id: inserted_mod.id,
358       post_id: inserted_post.id,
359       stickied: None,
360     };
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_,
369     };
370
371     // comment
372
373     let mod_remove_comment_form = ModRemoveCommentForm {
374       mod_person_id: inserted_mod.id,
375       comment_id: inserted_comment.id,
376       reason: None,
377       removed: None,
378     };
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,
387       reason: None,
388       removed: Some(true),
389       when_: inserted_mod_remove_comment.when_,
390     };
391
392     // community
393
394     let mod_remove_community_form = ModRemoveCommunityForm {
395       mod_person_id: inserted_mod.id,
396       community_id: inserted_community.id,
397       reason: None,
398       removed: None,
399       expires: None,
400     };
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,
409       reason: None,
410       removed: Some(true),
411       expires: None,
412       when_: inserted_mod_remove_community.when_,
413     };
414
415     // ban from community
416
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,
421       reason: None,
422       banned: None,
423       expires: None,
424     };
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,
434       reason: None,
435       banned: Some(true),
436       expires: None,
437       when_: inserted_mod_ban_from_community.when_,
438     };
439
440     // ban
441
442     let mod_ban_form = ModBanForm {
443       mod_person_id: inserted_mod.id,
444       other_person_id: inserted_person.id,
445       reason: None,
446       banned: None,
447       expires: None,
448     };
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,
455       reason: None,
456       banned: Some(true),
457       expires: None,
458       when_: inserted_mod_ban.when_,
459     };
460
461     // mod add community
462
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,
467       removed: None,
468     };
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_,
480     };
481
482     // mod add
483
484     let mod_add_form = ModAddForm {
485       mod_person_id: inserted_mod.id,
486       other_person_id: inserted_person.id,
487       removed: None,
488     };
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_,
497     };
498
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();
504
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);
514   }
515 }