1 use crate::structs::PostReportView;
8 NullableExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
13 aggregates::structs::PostAggregates,
14 newtypes::{CommunityId, PersonId, PostReportId},
26 community::{Community, CommunityPersonBan, CommunitySafe},
27 person::{Person, PersonSafe},
29 post_report::PostReport,
31 traits::{ToSafe, ViewToVec},
32 utils::{get_conn, limit_and_offset, DbPool},
34 use typed_builder::TypedBuilder;
36 type PostReportViewTuple = (
42 Option<CommunityPersonBan>,
49 /// returns the PostReportView for the provided report_id
51 /// * `report_id` - the report id to obtain
54 report_id: PostReportId,
55 my_person_id: PersonId,
56 ) -> Result<Self, Error> {
57 let conn = &mut get_conn(pool).await?;
58 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
66 creator_banned_from_community,
70 ) = post_report::table
72 .inner_join(post::table)
73 .inner_join(community::table.on(post::community_id.eq(community::id)))
74 .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
75 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
77 community_person_ban::table.on(
79 .eq(community_person_ban::community_id)
80 .and(community_person_ban::person_id.eq(post::creator_id))
82 community_person_ban::expires
84 .or(community_person_ban::expires.gt(now)),
91 .eq(post_like::post_id)
92 .and(post_like::person_id.eq(my_person_id)),
95 .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
97 person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
100 post_report::all_columns,
102 Community::safe_columns_tuple(),
103 Person::safe_columns_tuple(),
104 person_alias_1.fields(Person::safe_columns_tuple()),
105 community_person_ban::all_columns.nullable(),
106 post_like::score.nullable(),
107 post_aggregates::all_columns,
108 person_alias_2.fields(Person::safe_columns_tuple().nullable()),
110 .first::<PostReportViewTuple>(conn)
113 let my_vote = post_like;
121 creator_banned_from_community: creator_banned_from_community.is_some(),
128 /// returns the current unresolved post report count for the communities you mod
129 pub async fn get_report_count(
131 my_person_id: PersonId,
133 community_id: Option<CommunityId>,
134 ) -> Result<i64, Error> {
136 let conn = &mut get_conn(pool).await?;
137 let mut query = post_report::table
138 .inner_join(post::table)
139 .filter(post_report::resolved.eq(false))
142 if let Some(community_id) = community_id {
143 query = query.filter(post::community_id.eq(community_id))
146 // If its not an admin, get only the ones you mod
150 community_moderator::table.on(
151 community_moderator::community_id
152 .eq(post::community_id)
153 .and(community_moderator::person_id.eq(my_person_id)),
156 .select(count(post_report::id))
161 .select(count(post_report::id))
168 #[derive(TypedBuilder)]
169 #[builder(field_defaults(default))]
170 pub struct PostReportQuery<'a> {
174 my_person_id: PersonId,
177 community_id: Option<CommunityId>,
180 unresolved_only: Option<bool>,
183 impl<'a> PostReportQuery<'a> {
184 pub async fn list(self) -> Result<Vec<PostReportView>, Error> {
185 let conn = &mut get_conn(self.pool).await?;
186 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
188 let mut query = post_report::table
189 .inner_join(post::table)
190 .inner_join(community::table.on(post::community_id.eq(community::id)))
191 .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
192 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
194 community_person_ban::table.on(
196 .eq(community_person_ban::community_id)
197 .and(community_person_ban::person_id.eq(post::creator_id))
199 community_person_ban::expires
201 .or(community_person_ban::expires.gt(now)),
208 .eq(post_like::post_id)
209 .and(post_like::person_id.eq(self.my_person_id)),
212 .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
214 person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
217 post_report::all_columns,
219 Community::safe_columns_tuple(),
220 Person::safe_columns_tuple(),
221 person_alias_1.fields(Person::safe_columns_tuple()),
222 community_person_ban::all_columns.nullable(),
223 post_like::score.nullable(),
224 post_aggregates::all_columns,
226 .fields(Person::safe_columns_tuple())
231 if let Some(community_id) = self.community_id {
232 query = query.filter(post::community_id.eq(community_id));
235 if self.unresolved_only.unwrap_or(true) {
236 query = query.filter(post_report::resolved.eq(false));
239 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
242 .order_by(post_report::published.desc())
246 // If its not an admin, get only the ones you mod
247 let res = if !self.admin {
250 community_moderator::table.on(
251 community_moderator::community_id
252 .eq(post::community_id)
253 .and(community_moderator::person_id.eq(self.my_person_id)),
256 .load::<PostReportViewTuple>(conn)
259 query.load::<PostReportViewTuple>(conn).await?
262 Ok(PostReportView::from_tuple_to_vec(res))
266 impl ViewToVec for PostReportView {
267 type DbTuple = PostReportViewTuple;
268 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
277 creator_banned_from_community: a.5.is_some(),
282 .collect::<Vec<Self>>()
288 use crate::post_report_view::{PostReportQuery, PostReportView};
289 use lemmy_db_schema::{
290 aggregates::structs::PostAggregates,
296 post_report::{PostReport, PostReportForm},
298 traits::{Crud, Joinable, Reportable},
299 utils::build_db_pool_for_tests,
301 use serial_test::serial;
305 async fn test_crud() {
306 let pool = &build_db_pool_for_tests().await;
308 let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
310 let new_person = PersonInsertForm::builder()
311 .name("timmy_prv".into())
312 .public_key("pubkey".to_string())
313 .instance_id(inserted_instance.id)
316 let inserted_timmy = Person::create(pool, &new_person).await.unwrap();
318 let new_person_2 = PersonInsertForm::builder()
319 .name("sara_prv".into())
320 .public_key("pubkey".to_string())
321 .instance_id(inserted_instance.id)
324 let inserted_sara = Person::create(pool, &new_person_2).await.unwrap();
326 // Add a third person, since new ppl can only report something once.
327 let new_person_3 = PersonInsertForm::builder()
328 .name("jessica_prv".into())
329 .public_key("pubkey".to_string())
330 .instance_id(inserted_instance.id)
333 let inserted_jessica = Person::create(pool, &new_person_3).await.unwrap();
335 let new_community = CommunityInsertForm::builder()
336 .name("test community prv".to_string())
337 .title("nada".to_owned())
338 .public_key("pubkey".to_string())
339 .instance_id(inserted_instance.id)
342 let inserted_community = Community::create(pool, &new_community).await.unwrap();
345 let timmy_moderator_form = CommunityModeratorForm {
346 community_id: inserted_community.id,
347 person_id: inserted_timmy.id,
350 let _inserted_moderator = CommunityModerator::join(pool, &timmy_moderator_form)
354 let new_post = PostInsertForm::builder()
355 .name("A test post crv".into())
356 .creator_id(inserted_timmy.id)
357 .community_id(inserted_community.id)
360 let inserted_post = Post::create(pool, &new_post).await.unwrap();
363 let sara_report_form = PostReportForm {
364 creator_id: inserted_sara.id,
365 post_id: inserted_post.id,
366 original_post_name: "Orig post".into(),
367 original_post_url: None,
368 original_post_body: None,
369 reason: "from sara".into(),
372 let inserted_sara_report = PostReport::report(pool, &sara_report_form).await.unwrap();
375 let jessica_report_form = PostReportForm {
376 creator_id: inserted_jessica.id,
377 post_id: inserted_post.id,
378 original_post_name: "Orig post".into(),
379 original_post_url: None,
380 original_post_body: None,
381 reason: "from jessica".into(),
384 let inserted_jessica_report = PostReport::report(pool, &jessica_report_form)
388 let agg = PostAggregates::read(pool, inserted_post.id).await.unwrap();
390 let read_jessica_report_view =
391 PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
394 let expected_jessica_report_view = PostReportView {
395 post_report: inserted_jessica_report.to_owned(),
396 post: inserted_post.to_owned(),
397 community: CommunitySafe {
398 id: inserted_community.id,
399 name: inserted_community.name,
404 actor_id: inserted_community.actor_id.to_owned(),
406 title: inserted_community.title,
411 posting_restricted_to_mods: false,
412 published: inserted_community.published,
413 instance_id: inserted_instance.id,
415 creator: PersonSafe {
416 id: inserted_jessica.id,
417 name: inserted_jessica.name,
419 published: inserted_jessica.published,
421 actor_id: inserted_jessica.actor_id.to_owned(),
430 inbox_url: inserted_jessica.inbox_url.to_owned(),
431 shared_inbox_url: None,
432 matrix_user_id: None,
434 instance_id: inserted_instance.id,
436 post_creator: PersonSafe {
437 id: inserted_timmy.id,
438 name: inserted_timmy.name.to_owned(),
440 published: inserted_timmy.published,
442 actor_id: inserted_timmy.actor_id.to_owned(),
451 inbox_url: inserted_timmy.inbox_url.to_owned(),
452 shared_inbox_url: None,
453 matrix_user_id: None,
455 instance_id: inserted_instance.id,
457 creator_banned_from_community: false,
459 counts: PostAggregates {
461 post_id: inserted_post.id,
467 published: agg.published,
468 newest_comment_time_necro: inserted_post.published,
469 newest_comment_time: inserted_post.published,
474 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
476 let mut expected_sara_report_view = expected_jessica_report_view.clone();
477 expected_sara_report_view.post_report = inserted_sara_report;
478 expected_sara_report_view.my_vote = None;
479 expected_sara_report_view.creator = PersonSafe {
480 id: inserted_sara.id,
481 name: inserted_sara.name,
483 published: inserted_sara.published,
485 actor_id: inserted_sara.actor_id.to_owned(),
494 inbox_url: inserted_sara.inbox_url.to_owned(),
495 shared_inbox_url: None,
496 matrix_user_id: None,
498 instance_id: inserted_instance.id,
501 // Do a batch read of timmys reports
502 let reports = PostReportQuery::builder()
504 .my_person_id(inserted_timmy.id)
514 expected_jessica_report_view.to_owned(),
515 expected_sara_report_view.to_owned()
519 // Make sure the counts are correct
520 let report_count = PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
523 assert_eq!(2, report_count);
525 // Try to resolve the report
526 PostReport::resolve(pool, inserted_jessica_report.id, inserted_timmy.id)
529 let read_jessica_report_view_after_resolve =
530 PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
534 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
535 expected_jessica_report_view_after_resolve
538 expected_jessica_report_view_after_resolve
540 .resolver_id = Some(inserted_timmy.id);
541 expected_jessica_report_view_after_resolve
543 .updated = read_jessica_report_view_after_resolve.post_report.updated;
544 expected_jessica_report_view_after_resolve.resolver = Some(PersonSafe {
545 id: inserted_timmy.id,
546 name: inserted_timmy.name.to_owned(),
548 published: inserted_timmy.published,
550 actor_id: inserted_timmy.actor_id.to_owned(),
559 inbox_url: inserted_timmy.inbox_url.to_owned(),
560 shared_inbox_url: None,
561 matrix_user_id: None,
563 instance_id: inserted_instance.id,
567 read_jessica_report_view_after_resolve,
568 expected_jessica_report_view_after_resolve
571 // Do a batch read of timmys reports
572 // It should only show saras, which is unresolved
573 let reports_after_resolve = PostReportQuery::builder()
575 .my_person_id(inserted_timmy.id)
581 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
583 // Make sure the counts are correct
584 let report_count_after_resolved =
585 PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
588 assert_eq!(1, report_count_after_resolved);
590 Person::delete(pool, inserted_timmy.id).await.unwrap();
591 Person::delete(pool, inserted_sara.id).await.unwrap();
592 Person::delete(pool, inserted_jessica.id).await.unwrap();
593 Community::delete(pool, inserted_community.id)
596 Instance::delete(pool, inserted_instance.id).await.unwrap();