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},
33 use typed_builder::TypedBuilder;
35 type PostReportViewTuple = (
41 Option<CommunityPersonBan>,
48 /// returns the PostReportView for the provided report_id
50 /// * `report_id` - the report id to obtain
53 report_id: PostReportId,
54 my_person_id: PersonId,
55 ) -> Result<Self, Error> {
56 let conn = &mut get_conn(pool).await?;
57 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
65 creator_banned_from_community,
69 ) = post_report::table
71 .inner_join(post::table)
72 .inner_join(community::table.on(post::community_id.eq(community::id)))
73 .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
74 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
76 community_person_ban::table.on(
78 .eq(community_person_ban::community_id)
79 .and(community_person_ban::person_id.eq(post::creator_id)),
85 .eq(post_like::post_id)
86 .and(post_like::person_id.eq(my_person_id)),
89 .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
91 person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
94 post_report::all_columns,
96 community::all_columns,
98 person_alias_1.fields(person::all_columns),
99 community_person_ban::all_columns.nullable(),
100 post_like::score.nullable(),
101 post_aggregates::all_columns,
102 person_alias_2.fields(person::all_columns.nullable()),
104 .first::<PostReportViewTuple>(conn)
107 let my_vote = post_like;
115 creator_banned_from_community: creator_banned_from_community.is_some(),
122 /// returns the current unresolved post report count for the communities you mod
123 pub async fn get_report_count(
125 my_person_id: PersonId,
127 community_id: Option<CommunityId>,
128 ) -> Result<i64, Error> {
129 use diesel::dsl::count;
130 let conn = &mut get_conn(pool).await?;
131 let mut query = post_report::table
132 .inner_join(post::table)
133 .filter(post_report::resolved.eq(false))
136 if let Some(community_id) = community_id {
137 query = query.filter(post::community_id.eq(community_id))
140 // If its not an admin, get only the ones you mod
144 community_moderator::table.on(
145 community_moderator::community_id
146 .eq(post::community_id)
147 .and(community_moderator::person_id.eq(my_person_id)),
150 .select(count(post_report::id))
155 .select(count(post_report::id))
162 #[derive(TypedBuilder)]
163 #[builder(field_defaults(default))]
164 pub struct PostReportQuery<'a> {
168 my_person_id: PersonId,
171 community_id: Option<CommunityId>,
174 unresolved_only: Option<bool>,
177 impl<'a> PostReportQuery<'a> {
178 pub async fn list(self) -> Result<Vec<PostReportView>, Error> {
179 let conn = &mut get_conn(self.pool).await?;
180 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
182 let mut query = post_report::table
183 .inner_join(post::table)
184 .inner_join(community::table.on(post::community_id.eq(community::id)))
185 .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
186 .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
188 community_person_ban::table.on(
190 .eq(community_person_ban::community_id)
191 .and(community_person_ban::person_id.eq(post::creator_id)),
197 .eq(post_like::post_id)
198 .and(post_like::person_id.eq(self.my_person_id)),
201 .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
203 person_alias_2.on(post_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
206 post_report::all_columns,
208 community::all_columns,
210 person_alias_1.fields(person::all_columns),
211 community_person_ban::all_columns.nullable(),
212 post_like::score.nullable(),
213 post_aggregates::all_columns,
214 person_alias_2.fields(person::all_columns.nullable()),
218 if let Some(community_id) = self.community_id {
219 query = query.filter(post::community_id.eq(community_id));
222 if self.unresolved_only.unwrap_or(false) {
223 query = query.filter(post_report::resolved.eq(false));
226 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
229 .order_by(post_report::published.desc())
233 // If its not an admin, get only the ones you mod
234 let res = if !self.admin {
237 community_moderator::table.on(
238 community_moderator::community_id
239 .eq(post::community_id)
240 .and(community_moderator::person_id.eq(self.my_person_id)),
243 .load::<PostReportViewTuple>(conn)
246 query.load::<PostReportViewTuple>(conn).await?
249 Ok(res.into_iter().map(PostReportView::from_tuple).collect())
253 impl JoinView for PostReportView {
254 type JoinTuple = PostReportViewTuple;
255 fn from_tuple(a: Self::JoinTuple) -> Self {
262 creator_banned_from_community: a.5.is_some(),
272 use crate::post_report_view::{PostReportQuery, PostReportView};
273 use lemmy_db_schema::{
274 aggregates::structs::PostAggregates,
276 community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
278 person::{Person, PersonInsertForm},
279 post::{Post, PostInsertForm},
280 post_report::{PostReport, PostReportForm},
282 traits::{Crud, Joinable, Reportable},
283 utils::build_db_pool_for_tests,
285 use serial_test::serial;
289 async fn test_crud() {
290 let pool = &build_db_pool_for_tests().await;
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,
477 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
479 let mut expected_sara_report_view = expected_jessica_report_view.clone();
480 expected_sara_report_view.post_report = inserted_sara_report;
481 expected_sara_report_view.my_vote = None;
482 expected_sara_report_view.creator = Person {
483 id: inserted_sara.id,
484 name: inserted_sara.name,
486 published: inserted_sara.published,
488 actor_id: inserted_sara.actor_id.clone(),
497 inbox_url: inserted_sara.inbox_url.clone(),
498 shared_inbox_url: None,
499 matrix_user_id: None,
501 instance_id: inserted_instance.id,
502 private_key: inserted_sara.private_key,
503 public_key: inserted_sara.public_key,
504 last_refreshed_at: inserted_sara.last_refreshed_at,
507 // Do a batch read of timmys reports
508 let reports = PostReportQuery::builder()
510 .my_person_id(inserted_timmy.id)
520 expected_jessica_report_view.clone(),
521 expected_sara_report_view.clone()
525 // Make sure the counts are correct
526 let report_count = PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
529 assert_eq!(2, report_count);
531 // Try to resolve the report
532 PostReport::resolve(pool, inserted_jessica_report.id, inserted_timmy.id)
535 let read_jessica_report_view_after_resolve =
536 PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
540 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
541 expected_jessica_report_view_after_resolve
544 expected_jessica_report_view_after_resolve
546 .resolver_id = Some(inserted_timmy.id);
547 expected_jessica_report_view_after_resolve
549 .updated = read_jessica_report_view_after_resolve.post_report.updated;
550 expected_jessica_report_view_after_resolve.resolver = Some(Person {
551 id: inserted_timmy.id,
552 name: inserted_timmy.name.clone(),
554 published: inserted_timmy.published,
556 actor_id: inserted_timmy.actor_id.clone(),
565 inbox_url: inserted_timmy.inbox_url.clone(),
566 shared_inbox_url: None,
567 matrix_user_id: None,
569 instance_id: inserted_instance.id,
570 private_key: inserted_timmy.private_key.clone(),
571 public_key: inserted_timmy.public_key.clone(),
572 last_refreshed_at: inserted_timmy.last_refreshed_at,
576 read_jessica_report_view_after_resolve,
577 expected_jessica_report_view_after_resolve
580 // Do a batch read of timmys reports
581 // It should only show saras, which is unresolved
582 let reports_after_resolve = PostReportQuery::builder()
584 .my_person_id(inserted_timmy.id)
586 .unresolved_only(Some(true))
591 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
593 // Make sure the counts are correct
594 let report_count_after_resolved =
595 PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
598 assert_eq!(1, report_count_after_resolved);
600 Person::delete(pool, inserted_timmy.id).await.unwrap();
601 Person::delete(pool, inserted_sara.id).await.unwrap();
602 Person::delete(pool, inserted_jessica.id).await.unwrap();
603 Community::delete(pool, inserted_community.id)
606 Instance::delete(pool, inserted_instance.id).await.unwrap();