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},
29 post_report::PostReport,
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::all_columns,
104 person_alias_1.fields(person::all_columns),
105 community_person_ban::all_columns.nullable(),
106 post_like::score.nullable(),
107 post_aggregates::all_columns,
108 person_alias_2.fields(person::all_columns.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> {
135 use diesel::dsl::count;
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::all_columns,
221 person_alias_1.fields(person::all_columns),
222 community_person_ban::all_columns.nullable(),
223 post_like::score.nullable(),
224 post_aggregates::all_columns,
225 person_alias_2.fields(person::all_columns.nullable()),
229 if let Some(community_id) = self.community_id {
230 query = query.filter(post::community_id.eq(community_id));
233 if self.unresolved_only.unwrap_or(true) {
234 query = query.filter(post_report::resolved.eq(false));
237 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
240 .order_by(post_report::published.desc())
244 // If its not an admin, get only the ones you mod
245 let res = if !self.admin {
248 community_moderator::table.on(
249 community_moderator::community_id
250 .eq(post::community_id)
251 .and(community_moderator::person_id.eq(self.my_person_id)),
254 .load::<PostReportViewTuple>(conn)
257 query.load::<PostReportViewTuple>(conn).await?
260 Ok(res.into_iter().map(PostReportView::from_tuple).collect())
264 impl JoinView for PostReportView {
265 type JoinTuple = PostReportViewTuple;
266 fn from_tuple(a: Self::JoinTuple) -> Self {
273 creator_banned_from_community: a.5.is_some(),
283 use crate::post_report_view::{PostReportQuery, PostReportView};
284 use lemmy_db_schema::{
285 aggregates::structs::PostAggregates,
287 community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
289 person::{Person, PersonInsertForm},
290 post::{Post, PostInsertForm},
291 post_report::{PostReport, PostReportForm},
293 traits::{Crud, Joinable, Reportable},
294 utils::build_db_pool_for_tests,
296 use serial_test::serial;
300 async fn test_crud() {
301 let pool = &build_db_pool_for_tests().await;
303 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
307 let new_person = PersonInsertForm::builder()
308 .name("timmy_prv".into())
309 .public_key("pubkey".to_string())
310 .instance_id(inserted_instance.id)
313 let inserted_timmy = Person::create(pool, &new_person).await.unwrap();
315 let new_person_2 = PersonInsertForm::builder()
316 .name("sara_prv".into())
317 .public_key("pubkey".to_string())
318 .instance_id(inserted_instance.id)
321 let inserted_sara = Person::create(pool, &new_person_2).await.unwrap();
323 // Add a third person, since new ppl can only report something once.
324 let new_person_3 = PersonInsertForm::builder()
325 .name("jessica_prv".into())
326 .public_key("pubkey".to_string())
327 .instance_id(inserted_instance.id)
330 let inserted_jessica = Person::create(pool, &new_person_3).await.unwrap();
332 let new_community = CommunityInsertForm::builder()
333 .name("test community prv".to_string())
334 .title("nada".to_owned())
335 .public_key("pubkey".to_string())
336 .instance_id(inserted_instance.id)
339 let inserted_community = Community::create(pool, &new_community).await.unwrap();
342 let timmy_moderator_form = CommunityModeratorForm {
343 community_id: inserted_community.id,
344 person_id: inserted_timmy.id,
347 let _inserted_moderator = CommunityModerator::join(pool, &timmy_moderator_form)
351 let new_post = PostInsertForm::builder()
352 .name("A test post crv".into())
353 .creator_id(inserted_timmy.id)
354 .community_id(inserted_community.id)
357 let inserted_post = Post::create(pool, &new_post).await.unwrap();
360 let sara_report_form = PostReportForm {
361 creator_id: inserted_sara.id,
362 post_id: inserted_post.id,
363 original_post_name: "Orig post".into(),
364 original_post_url: None,
365 original_post_body: None,
366 reason: "from sara".into(),
369 let inserted_sara_report = PostReport::report(pool, &sara_report_form).await.unwrap();
372 let jessica_report_form = PostReportForm {
373 creator_id: inserted_jessica.id,
374 post_id: inserted_post.id,
375 original_post_name: "Orig post".into(),
376 original_post_url: None,
377 original_post_body: None,
378 reason: "from jessica".into(),
381 let inserted_jessica_report = PostReport::report(pool, &jessica_report_form)
385 let agg = PostAggregates::read(pool, inserted_post.id).await.unwrap();
387 let read_jessica_report_view =
388 PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
391 let expected_jessica_report_view = PostReportView {
392 post_report: inserted_jessica_report.clone(),
393 post: inserted_post.clone(),
394 community: Community {
395 id: inserted_community.id,
396 name: inserted_community.name,
401 actor_id: inserted_community.actor_id.clone(),
403 title: inserted_community.title,
408 posting_restricted_to_mods: false,
409 published: inserted_community.published,
410 instance_id: inserted_instance.id,
411 private_key: inserted_community.private_key.clone(),
412 public_key: inserted_community.public_key.clone(),
413 last_refreshed_at: inserted_community.last_refreshed_at,
414 followers_url: inserted_community.followers_url.clone(),
415 inbox_url: inserted_community.inbox_url.clone(),
416 shared_inbox_url: inserted_community.shared_inbox_url.clone(),
417 moderators_url: inserted_community.moderators_url.clone(),
418 featured_url: inserted_community.featured_url.clone(),
421 id: inserted_jessica.id,
422 name: inserted_jessica.name,
424 published: inserted_jessica.published,
426 actor_id: inserted_jessica.actor_id.clone(),
435 inbox_url: inserted_jessica.inbox_url.clone(),
436 shared_inbox_url: None,
437 matrix_user_id: None,
439 instance_id: inserted_instance.id,
440 private_key: inserted_jessica.private_key,
441 public_key: inserted_jessica.public_key,
442 last_refreshed_at: inserted_jessica.last_refreshed_at,
444 post_creator: Person {
445 id: inserted_timmy.id,
446 name: inserted_timmy.name.clone(),
448 published: inserted_timmy.published,
450 actor_id: inserted_timmy.actor_id.clone(),
459 inbox_url: inserted_timmy.inbox_url.clone(),
460 shared_inbox_url: None,
461 matrix_user_id: None,
463 instance_id: inserted_instance.id,
464 private_key: inserted_timmy.private_key.clone(),
465 public_key: inserted_timmy.public_key.clone(),
466 last_refreshed_at: inserted_timmy.last_refreshed_at,
468 creator_banned_from_community: false,
470 counts: PostAggregates {
472 post_id: inserted_post.id,
477 published: agg.published,
478 newest_comment_time_necro: inserted_post.published,
479 newest_comment_time: inserted_post.published,
480 featured_community: false,
481 featured_local: false,
486 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
488 let mut expected_sara_report_view = expected_jessica_report_view.clone();
489 expected_sara_report_view.post_report = inserted_sara_report;
490 expected_sara_report_view.my_vote = None;
491 expected_sara_report_view.creator = Person {
492 id: inserted_sara.id,
493 name: inserted_sara.name,
495 published: inserted_sara.published,
497 actor_id: inserted_sara.actor_id.clone(),
506 inbox_url: inserted_sara.inbox_url.clone(),
507 shared_inbox_url: None,
508 matrix_user_id: None,
510 instance_id: inserted_instance.id,
511 private_key: inserted_sara.private_key,
512 public_key: inserted_sara.public_key,
513 last_refreshed_at: inserted_sara.last_refreshed_at,
516 // Do a batch read of timmys reports
517 let reports = PostReportQuery::builder()
519 .my_person_id(inserted_timmy.id)
529 expected_jessica_report_view.clone(),
530 expected_sara_report_view.clone()
534 // Make sure the counts are correct
535 let report_count = PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
538 assert_eq!(2, report_count);
540 // Try to resolve the report
541 PostReport::resolve(pool, inserted_jessica_report.id, inserted_timmy.id)
544 let read_jessica_report_view_after_resolve =
545 PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
549 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
550 expected_jessica_report_view_after_resolve
553 expected_jessica_report_view_after_resolve
555 .resolver_id = Some(inserted_timmy.id);
556 expected_jessica_report_view_after_resolve
558 .updated = read_jessica_report_view_after_resolve.post_report.updated;
559 expected_jessica_report_view_after_resolve.resolver = Some(Person {
560 id: inserted_timmy.id,
561 name: inserted_timmy.name.clone(),
563 published: inserted_timmy.published,
565 actor_id: inserted_timmy.actor_id.clone(),
574 inbox_url: inserted_timmy.inbox_url.clone(),
575 shared_inbox_url: None,
576 matrix_user_id: None,
578 instance_id: inserted_instance.id,
579 private_key: inserted_timmy.private_key.clone(),
580 public_key: inserted_timmy.public_key.clone(),
581 last_refreshed_at: inserted_timmy.last_refreshed_at,
585 read_jessica_report_view_after_resolve,
586 expected_jessica_report_view_after_resolve
589 // Do a batch read of timmys reports
590 // It should only show saras, which is unresolved
591 let reports_after_resolve = PostReportQuery::builder()
593 .my_person_id(inserted_timmy.id)
599 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
601 // Make sure the counts are correct
602 let report_count_after_resolved =
603 PostReportView::get_report_count(pool, inserted_timmy.id, false, None)
606 assert_eq!(1, report_count_after_resolved);
608 Person::delete(pool, inserted_timmy.id).await.unwrap();
609 Person::delete(pool, inserted_sara.id).await.unwrap();
610 Person::delete(pool, inserted_jessica.id).await.unwrap();
611 Community::delete(pool, inserted_community.id)
614 Instance::delete(pool, inserted_instance.id).await.unwrap();