1 use crate::structs::PostReportView;
7 NullableExpressionMethods,
10 use diesel_async::RunQueryDsl;
11 use lemmy_db_schema::{
12 aggregates::structs::PostAggregates,
13 newtypes::{CommunityId, PersonId, PostReportId},
25 community::{Community, CommunityPersonBan},
28 post_report::PostReport,
31 utils::{get_conn, limit_and_offset, DbPool},
34 type PostReportViewTuple = (
40 Option<CommunityPersonBan>,
47 /// returns the PostReportView for the provided report_id
49 /// * `report_id` - the report id to obtain
51 pool: &mut DbPool<'_>,
52 report_id: PostReportId,
53 my_person_id: PersonId,
54 ) -> Result<Self, Error> {
55 let conn = &mut get_conn(pool).await?;
56 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
64 creator_banned_from_community,
68 ) = post_report::table
70 .inner_join(post::table)
71 .inner_join(community::table.on(post::community_id.eq(community::id)))
72 .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
73 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
75 community_person_ban::table.on(
77 .eq(community_person_ban::community_id)
78 .and(community_person_ban::person_id.eq(post::creator_id)),
84 .eq(post_like::post_id)
85 .and(post_like::person_id.eq(my_person_id)),
88 .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
90 person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
93 post_report::all_columns,
95 community::all_columns,
97 person_alias_1.fields(person::all_columns),
98 community_person_ban::all_columns.nullable(),
99 post_like::score.nullable(),
100 post_aggregates::all_columns,
101 person_alias_2.fields(person::all_columns.nullable()),
103 .first::<PostReportViewTuple>(conn)
106 let my_vote = post_like;
114 creator_banned_from_community: creator_banned_from_community.is_some(),
121 /// returns the current unresolved post report count for the communities you mod
122 pub async fn get_report_count(
123 pool: &mut DbPool<'_>,
124 my_person_id: PersonId,
126 community_id: Option<CommunityId>,
127 ) -> Result<i64, Error> {
128 use diesel::dsl::count;
129 let conn = &mut get_conn(pool).await?;
130 let mut query = post_report::table
131 .inner_join(post::table)
132 .filter(post_report::resolved.eq(false))
135 if let Some(community_id) = community_id {
136 query = query.filter(post::community_id.eq(community_id))
139 // If its not an admin, get only the ones you mod
143 community_moderator::table.on(
144 community_moderator::community_id
145 .eq(post::community_id)
146 .and(community_moderator::person_id.eq(my_person_id)),
149 .select(count(post_report::id))
154 .select(count(post_report::id))
162 pub struct PostReportQuery {
163 pub community_id: Option<CommunityId>,
164 pub page: Option<i64>,
165 pub limit: Option<i64>,
166 pub unresolved_only: Option<bool>,
169 impl PostReportQuery {
172 pool: &mut DbPool<'_>,
174 ) -> Result<Vec<PostReportView>, Error> {
175 let conn = &mut get_conn(pool).await?;
176 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
178 let mut query = post_report::table
179 .inner_join(post::table)
180 .inner_join(community::table.on(post::community_id.eq(community::id)))
181 .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
182 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
184 community_person_ban::table.on(
186 .eq(community_person_ban::community_id)
187 .and(community_person_ban::person_id.eq(post::creator_id)),
193 .eq(post_like::post_id)
194 .and(post_like::person_id.eq(my_person.id)),
197 .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
199 person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
202 post_report::all_columns,
204 community::all_columns,
206 person_alias_1.fields(person::all_columns),
207 community_person_ban::all_columns.nullable(),
208 post_like::score.nullable(),
209 post_aggregates::all_columns,
210 person_alias_2.fields(person::all_columns.nullable()),
214 if let Some(community_id) = self.community_id {
215 query = query.filter(post::community_id.eq(community_id));
218 if self.unresolved_only.unwrap_or(false) {
219 query = query.filter(post_report::resolved.eq(false));
222 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
225 .order_by(post_report::published.desc())
229 // If its not an admin, get only the ones you mod
230 let res = if !my_person.admin {
233 community_moderator::table.on(
234 community_moderator::community_id
235 .eq(post::community_id)
236 .and(community_moderator::person_id.eq(my_person.id)),
239 .load::<PostReportViewTuple>(conn)
242 query.load::<PostReportViewTuple>(conn).await?
245 Ok(res.into_iter().map(PostReportView::from_tuple).collect())
249 impl JoinView for PostReportView {
250 type JoinTuple = PostReportViewTuple;
251 fn from_tuple(a: Self::JoinTuple) -> Self {
258 creator_banned_from_community: a.5.is_some(),
268 #![allow(clippy::unwrap_used)]
269 #![allow(clippy::indexing_slicing)]
271 use crate::post_report_view::{PostReportQuery, PostReportView};
272 use lemmy_db_schema::{
273 aggregates::structs::PostAggregates,
275 community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
277 person::{Person, PersonInsertForm},
278 post::{Post, PostInsertForm},
279 post_report::{PostReport, PostReportForm},
281 traits::{Crud, Joinable, Reportable},
282 utils::build_db_pool_for_tests,
284 use serial_test::serial;
288 async fn test_crud() {
289 let pool = &build_db_pool_for_tests().await;
290 let pool = &mut pool.into();
292 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
296 let new_person = PersonInsertForm::builder()
297 .name("timmy_prv".into())
298 .public_key("pubkey".to_string())
299 .instance_id(inserted_instance.id)
302 let inserted_timmy = Person::create(pool, &new_person).await.unwrap();
304 let new_person_2 = PersonInsertForm::builder()
305 .name("sara_prv".into())
306 .public_key("pubkey".to_string())
307 .instance_id(inserted_instance.id)
310 let inserted_sara = Person::create(pool, &new_person_2).await.unwrap();
312 // Add a third person, since new ppl can only report something once.
313 let new_person_3 = PersonInsertForm::builder()
314 .name("jessica_prv".into())
315 .public_key("pubkey".to_string())
316 .instance_id(inserted_instance.id)
319 let inserted_jessica = Person::create(pool, &new_person_3).await.unwrap();
321 let new_community = CommunityInsertForm::builder()
322 .name("test community prv".to_string())
323 .title("nada".to_owned())
324 .public_key("pubkey".to_string())
325 .instance_id(inserted_instance.id)
328 let inserted_community = Community::create(pool, &new_community).await.unwrap();
331 let timmy_moderator_form = CommunityModeratorForm {
332 community_id: inserted_community.id,
333 person_id: inserted_timmy.id,
336 let _inserted_moderator = CommunityModerator::join(pool, &timmy_moderator_form)
340 let new_post = PostInsertForm::builder()
341 .name("A test post crv".into())
342 .creator_id(inserted_timmy.id)
343 .community_id(inserted_community.id)
346 let inserted_post = Post::create(pool, &new_post).await.unwrap();
349 let sara_report_form = PostReportForm {
350 creator_id: inserted_sara.id,
351 post_id: inserted_post.id,
352 original_post_name: "Orig post".into(),
353 original_post_url: None,
354 original_post_body: None,
355 reason: "from sara".into(),
358 let inserted_sara_report = PostReport::report(pool, &sara_report_form).await.unwrap();
361 let jessica_report_form = PostReportForm {
362 creator_id: inserted_jessica.id,
363 post_id: inserted_post.id,
364 original_post_name: "Orig post".into(),
365 original_post_url: None,
366 original_post_body: None,
367 reason: "from jessica".into(),
370 let inserted_jessica_report = PostReport::report(pool, &jessica_report_form)
374 let agg = PostAggregates::read(pool, inserted_post.id).await.unwrap();
376 let read_jessica_report_view =
377 PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
380 let expected_jessica_report_view = PostReportView {
381 post_report: inserted_jessica_report.clone(),
382 post: inserted_post.clone(),
383 community: Community {
384 id: inserted_community.id,
385 name: inserted_community.name,
390 actor_id: inserted_community.actor_id.clone(),
392 title: inserted_community.title,
397 posting_restricted_to_mods: false,
398 published: inserted_community.published,
399 instance_id: inserted_instance.id,
400 private_key: inserted_community.private_key.clone(),
401 public_key: inserted_community.public_key.clone(),
402 last_refreshed_at: inserted_community.last_refreshed_at,
403 followers_url: inserted_community.followers_url.clone(),
404 inbox_url: inserted_community.inbox_url.clone(),
405 shared_inbox_url: inserted_community.shared_inbox_url.clone(),
406 moderators_url: inserted_community.moderators_url.clone(),
407 featured_url: inserted_community.featured_url.clone(),
410 id: inserted_jessica.id,
411 name: inserted_jessica.name,
413 published: inserted_jessica.published,
415 actor_id: inserted_jessica.actor_id.clone(),
424 inbox_url: inserted_jessica.inbox_url.clone(),
425 shared_inbox_url: None,
426 matrix_user_id: None,
428 instance_id: inserted_instance.id,
429 private_key: inserted_jessica.private_key,
430 public_key: inserted_jessica.public_key,
431 last_refreshed_at: inserted_jessica.last_refreshed_at,
433 post_creator: Person {
434 id: inserted_timmy.id,
435 name: inserted_timmy.name.clone(),
437 published: inserted_timmy.published,
439 actor_id: inserted_timmy.actor_id.clone(),
448 inbox_url: inserted_timmy.inbox_url.clone(),
449 shared_inbox_url: None,
450 matrix_user_id: None,
452 instance_id: inserted_instance.id,
453 private_key: inserted_timmy.private_key.clone(),
454 public_key: inserted_timmy.public_key.clone(),
455 last_refreshed_at: inserted_timmy.last_refreshed_at,
457 creator_banned_from_community: false,
459 counts: PostAggregates {
461 post_id: inserted_post.id,
466 published: agg.published,
467 newest_comment_time_necro: inserted_post.published,
468 newest_comment_time: inserted_post.published,
469 featured_community: false,
470 featured_local: false,
472 hot_rank_active: 1728,
473 community_id: inserted_post.community_id,
474 creator_id: inserted_post.creator_id,
479 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
481 let mut expected_sara_report_view = expected_jessica_report_view.clone();
482 expected_sara_report_view.post_report = inserted_sara_report;
483 expected_sara_report_view.my_vote = None;
484 expected_sara_report_view.creator = Person {
485 id: inserted_sara.id,
486 name: inserted_sara.name,
488 published: inserted_sara.published,
490 actor_id: inserted_sara.actor_id.clone(),
499 inbox_url: inserted_sara.inbox_url.clone(),
500 shared_inbox_url: None,
501 matrix_user_id: None,
503 instance_id: inserted_instance.id,
504 private_key: inserted_sara.private_key,
505 public_key: inserted_sara.public_key,
506 last_refreshed_at: inserted_sara.last_refreshed_at,
509 // Do a batch read of timmys reports
510 let reports = PostReportQuery::default()
511 .list(pool, &inserted_timmy)
518 expected_jessica_report_view.clone(),
519 expected_sara_report_view.clone()
523 // Make sure the counts are correct
524 let report_count = PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
527 assert_eq!(2, report_count);
529 // Try to resolve the report
530 PostReport::resolve(pool, inserted_jessica_report.id, inserted_timmy.id)
533 let read_jessica_report_view_after_resolve =
534 PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
538 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
539 expected_jessica_report_view_after_resolve
542 expected_jessica_report_view_after_resolve
544 .resolver_id = Some(inserted_timmy.id);
545 expected_jessica_report_view_after_resolve
547 .updated = read_jessica_report_view_after_resolve.post_report.updated;
548 expected_jessica_report_view_after_resolve.resolver = Some(Person {
549 id: inserted_timmy.id,
550 name: inserted_timmy.name.clone(),
552 published: inserted_timmy.published,
554 actor_id: inserted_timmy.actor_id.clone(),
563 inbox_url: inserted_timmy.inbox_url.clone(),
564 shared_inbox_url: None,
565 matrix_user_id: None,
567 instance_id: inserted_instance.id,
568 private_key: inserted_timmy.private_key.clone(),
569 public_key: inserted_timmy.public_key.clone(),
570 last_refreshed_at: inserted_timmy.last_refreshed_at,
574 read_jessica_report_view_after_resolve,
575 expected_jessica_report_view_after_resolve
578 // Do a batch read of timmys reports
579 // It should only show saras, which is unresolved
580 let reports_after_resolve = PostReportQuery {
581 unresolved_only: (Some(true)),
584 .list(pool, &inserted_timmy)
587 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
589 // Make sure the counts are correct
590 let report_count_after_resolved =
591 PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
594 assert_eq!(1, report_count_after_resolved);
596 Person::delete(pool, inserted_timmy.id).await.unwrap();
597 Person::delete(pool, inserted_sara.id).await.unwrap();
598 Person::delete(pool, inserted_jessica.id).await.unwrap();
599 Community::delete(pool, inserted_community.id)
602 Instance::delete(pool, inserted_instance.id).await.unwrap();