1 use crate::structs::CommentReportView;
8 NullableExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
13 aggregates::structs::CommentAggregates,
14 newtypes::{CommentReportId, CommunityId, PersonId},
28 comment_report::CommentReport,
29 community::{Community, CommunityPersonBan},
34 utils::{get_conn, limit_and_offset, DbPool},
36 use typed_builder::TypedBuilder;
38 impl CommentReportView {
39 /// returns the CommentReportView for the provided report_id
41 /// * `report_id` - the report id to obtain
44 report_id: CommentReportId,
45 my_person_id: PersonId,
46 ) -> Result<Self, Error> {
47 let conn = &mut get_conn(pool).await?;
49 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
51 let res = comment_report::table
53 .inner_join(comment::table)
54 .inner_join(post::table.on(comment::post_id.eq(post::id)))
55 .inner_join(community::table.on(post::community_id.eq(community::id)))
56 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
57 .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id))))
59 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
62 community_person_ban::table.on(
64 .eq(community_person_ban::community_id)
65 .and(community_person_ban::person_id.eq(comment::creator_id))
67 community_person_ban::expires
69 .or(community_person_ban::expires.gt(now)),
74 comment_like::table.on(
76 .eq(comment_like::comment_id)
77 .and(comment_like::person_id.eq(my_person_id)),
82 .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
85 comment_report::all_columns,
88 community::all_columns,
90 person_alias_1.fields(person::all_columns),
91 comment_aggregates::all_columns,
92 community_person_ban::all_columns.nullable(),
93 comment_like::score.nullable(),
94 person_alias_2.fields(person::all_columns).nullable(),
96 .first::<<CommentReportView as JoinView>::JoinTuple>(conn)
99 Ok(Self::from_tuple(res))
102 /// Returns the current unresolved post report count for the communities you mod
103 pub async fn get_report_count(
105 my_person_id: PersonId,
107 community_id: Option<CommunityId>,
108 ) -> Result<i64, Error> {
109 use diesel::dsl::count;
111 let conn = &mut get_conn(pool).await?;
113 let mut query = comment_report::table
114 .inner_join(comment::table)
115 .inner_join(post::table.on(comment::post_id.eq(post::id)))
116 .filter(comment_report::resolved.eq(false))
119 if let Some(community_id) = community_id {
120 query = query.filter(post::community_id.eq(community_id))
123 // If its not an admin, get only the ones you mod
127 community_moderator::table.on(
128 community_moderator::community_id
129 .eq(post::community_id)
130 .and(community_moderator::person_id.eq(my_person_id)),
133 .select(count(comment_report::id))
138 .select(count(comment_report::id))
145 #[derive(TypedBuilder)]
146 #[builder(field_defaults(default))]
147 pub struct CommentReportQuery<'a> {
151 my_person_id: PersonId,
154 community_id: Option<CommunityId>,
157 unresolved_only: Option<bool>,
160 impl<'a> CommentReportQuery<'a> {
161 pub async fn list(self) -> Result<Vec<CommentReportView>, Error> {
162 let conn = &mut get_conn(self.pool).await?;
164 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
166 let mut query = comment_report::table
167 .inner_join(comment::table)
168 .inner_join(post::table.on(comment::post_id.eq(post::id)))
169 .inner_join(community::table.on(post::community_id.eq(community::id)))
170 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
171 .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id))))
173 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
176 community_person_ban::table.on(
178 .eq(community_person_ban::community_id)
179 .and(community_person_ban::person_id.eq(comment::creator_id))
181 community_person_ban::expires
183 .or(community_person_ban::expires.gt(now)),
188 comment_like::table.on(
190 .eq(comment_like::comment_id)
191 .and(comment_like::person_id.eq(self.my_person_id)),
196 .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
199 comment_report::all_columns,
200 comment::all_columns,
202 community::all_columns,
204 person_alias_1.fields(person::all_columns),
205 comment_aggregates::all_columns,
206 community_person_ban::all_columns.nullable(),
207 comment_like::score.nullable(),
208 person_alias_2.fields(person::all_columns).nullable(),
212 if let Some(community_id) = self.community_id {
213 query = query.filter(post::community_id.eq(community_id));
216 if self.unresolved_only.unwrap_or(true) {
217 query = query.filter(comment_report::resolved.eq(false));
220 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
223 .order_by(comment_report::published.desc())
227 // If its not an admin, get only the ones you mod
228 let res = if !self.admin {
231 community_moderator::table.on(
232 community_moderator::community_id
233 .eq(post::community_id)
234 .and(community_moderator::person_id.eq(self.my_person_id)),
237 .load::<<CommentReportView as JoinView>::JoinTuple>(conn)
241 .load::<<CommentReportView as JoinView>::JoinTuple>(conn)
245 Ok(res.into_iter().map(CommentReportView::from_tuple).collect())
249 impl JoinView for CommentReportView {
258 Option<CommunityPersonBan>,
263 fn from_tuple(a: Self::JoinTuple) -> Self {
270 comment_creator: a.5,
272 creator_banned_from_community: a.7.is_some(),
281 use crate::comment_report_view::{CommentReportQuery, CommentReportView};
282 use lemmy_db_schema::{
283 aggregates::structs::CommentAggregates,
285 comment::{Comment, CommentInsertForm},
286 comment_report::{CommentReport, CommentReportForm},
287 community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
289 person::{Person, PersonInsertForm},
290 post::{Post, PostInsertForm},
292 traits::{Crud, Joinable, Reportable},
293 utils::build_db_pool_for_tests,
295 use serial_test::serial;
299 async fn test_crud() {
300 let pool = &build_db_pool_for_tests().await;
302 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
306 let new_person = PersonInsertForm::builder()
307 .name("timmy_crv".into())
308 .public_key("pubkey".to_string())
309 .instance_id(inserted_instance.id)
312 let inserted_timmy = Person::create(pool, &new_person).await.unwrap();
314 let new_person_2 = PersonInsertForm::builder()
315 .name("sara_crv".into())
316 .public_key("pubkey".to_string())
317 .instance_id(inserted_instance.id)
320 let inserted_sara = Person::create(pool, &new_person_2).await.unwrap();
322 // Add a third person, since new ppl can only report something once.
323 let new_person_3 = PersonInsertForm::builder()
324 .name("jessica_crv".into())
325 .public_key("pubkey".to_string())
326 .instance_id(inserted_instance.id)
329 let inserted_jessica = Person::create(pool, &new_person_3).await.unwrap();
331 let new_community = CommunityInsertForm::builder()
332 .name("test community crv".to_string())
333 .title("nada".to_owned())
334 .public_key("pubkey".to_string())
335 .instance_id(inserted_instance.id)
338 let inserted_community = Community::create(pool, &new_community).await.unwrap();
341 let timmy_moderator_form = CommunityModeratorForm {
342 community_id: inserted_community.id,
343 person_id: inserted_timmy.id,
346 let _inserted_moderator = CommunityModerator::join(pool, &timmy_moderator_form)
350 let new_post = PostInsertForm::builder()
351 .name("A test post crv".into())
352 .creator_id(inserted_timmy.id)
353 .community_id(inserted_community.id)
356 let inserted_post = Post::create(pool, &new_post).await.unwrap();
358 let comment_form = CommentInsertForm::builder()
359 .content("A test comment 32".into())
360 .creator_id(inserted_timmy.id)
361 .post_id(inserted_post.id)
364 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
367 let sara_report_form = CommentReportForm {
368 creator_id: inserted_sara.id,
369 comment_id: inserted_comment.id,
370 original_comment_text: "this was it at time of creation".into(),
371 reason: "from sara".into(),
374 let inserted_sara_report = CommentReport::report(pool, &sara_report_form)
379 let jessica_report_form = CommentReportForm {
380 creator_id: inserted_jessica.id,
381 comment_id: inserted_comment.id,
382 original_comment_text: "this was it at time of creation".into(),
383 reason: "from jessica".into(),
386 let inserted_jessica_report = CommentReport::report(pool, &jessica_report_form)
390 let agg = CommentAggregates::read(pool, inserted_comment.id)
394 let read_jessica_report_view =
395 CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
398 let expected_jessica_report_view = CommentReportView {
399 comment_report: inserted_jessica_report.clone(),
400 comment: inserted_comment.clone(),
402 community: Community {
403 id: inserted_community.id,
404 name: inserted_community.name,
409 actor_id: inserted_community.actor_id.clone(),
411 title: inserted_community.title,
416 posting_restricted_to_mods: false,
417 published: inserted_community.published,
418 private_key: inserted_community.private_key,
419 public_key: inserted_community.public_key,
420 last_refreshed_at: inserted_community.last_refreshed_at,
421 followers_url: inserted_community.followers_url,
422 inbox_url: inserted_community.inbox_url,
423 shared_inbox_url: inserted_community.shared_inbox_url,
424 moderators_url: inserted_community.moderators_url,
425 featured_url: inserted_community.featured_url,
426 instance_id: inserted_instance.id,
429 id: inserted_jessica.id,
430 name: inserted_jessica.name,
432 published: inserted_jessica.published,
434 actor_id: inserted_jessica.actor_id.clone(),
443 inbox_url: inserted_jessica.inbox_url.clone(),
444 shared_inbox_url: None,
445 matrix_user_id: None,
447 instance_id: inserted_instance.id,
448 private_key: inserted_jessica.private_key,
449 public_key: inserted_jessica.public_key,
450 last_refreshed_at: inserted_jessica.last_refreshed_at,
452 comment_creator: Person {
453 id: inserted_timmy.id,
454 name: inserted_timmy.name.clone(),
456 published: inserted_timmy.published,
458 actor_id: inserted_timmy.actor_id.clone(),
467 inbox_url: inserted_timmy.inbox_url.clone(),
468 shared_inbox_url: None,
469 matrix_user_id: None,
471 instance_id: inserted_instance.id,
472 private_key: inserted_timmy.private_key.clone(),
473 public_key: inserted_timmy.public_key.clone(),
474 last_refreshed_at: inserted_timmy.last_refreshed_at,
476 creator_banned_from_community: false,
477 counts: CommentAggregates {
479 comment_id: inserted_comment.id,
483 published: agg.published,
490 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
492 let mut expected_sara_report_view = expected_jessica_report_view.clone();
493 expected_sara_report_view.comment_report = inserted_sara_report;
494 expected_sara_report_view.creator = Person {
495 id: inserted_sara.id,
496 name: inserted_sara.name,
498 published: inserted_sara.published,
500 actor_id: inserted_sara.actor_id.clone(),
509 inbox_url: inserted_sara.inbox_url.clone(),
510 shared_inbox_url: None,
511 matrix_user_id: None,
513 instance_id: inserted_instance.id,
514 private_key: inserted_sara.private_key,
515 public_key: inserted_sara.public_key,
516 last_refreshed_at: inserted_sara.last_refreshed_at,
519 // Do a batch read of timmys reports
520 let reports = CommentReportQuery::builder()
522 .my_person_id(inserted_timmy.id)
532 expected_jessica_report_view.clone(),
533 expected_sara_report_view.clone()
537 // Make sure the counts are correct
538 let report_count = CommentReportView::get_report_count(pool, inserted_timmy.id, false, None)
541 assert_eq!(2, report_count);
543 // Try to resolve the report
544 CommentReport::resolve(pool, inserted_jessica_report.id, inserted_timmy.id)
547 let read_jessica_report_view_after_resolve =
548 CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
552 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
553 expected_jessica_report_view_after_resolve
556 expected_jessica_report_view_after_resolve
558 .resolver_id = Some(inserted_timmy.id);
559 expected_jessica_report_view_after_resolve
561 .updated = read_jessica_report_view_after_resolve
564 expected_jessica_report_view_after_resolve.resolver = Some(Person {
565 id: inserted_timmy.id,
566 name: inserted_timmy.name.clone(),
568 published: inserted_timmy.published,
570 actor_id: inserted_timmy.actor_id.clone(),
579 inbox_url: inserted_timmy.inbox_url.clone(),
580 private_key: inserted_timmy.private_key.clone(),
581 public_key: inserted_timmy.public_key.clone(),
582 last_refreshed_at: inserted_timmy.last_refreshed_at,
583 shared_inbox_url: None,
584 matrix_user_id: None,
586 instance_id: inserted_instance.id,
590 read_jessica_report_view_after_resolve,
591 expected_jessica_report_view_after_resolve
594 // Do a batch read of timmys reports
595 // It should only show saras, which is unresolved
596 let reports_after_resolve = CommentReportQuery::builder()
598 .my_person_id(inserted_timmy.id)
604 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
605 assert_eq!(reports_after_resolve.len(), 1);
607 // Make sure the counts are correct
608 let report_count_after_resolved =
609 CommentReportView::get_report_count(pool, inserted_timmy.id, false, None)
612 assert_eq!(1, report_count_after_resolved);
614 Person::delete(pool, inserted_timmy.id).await.unwrap();
615 Person::delete(pool, inserted_sara.id).await.unwrap();
616 Person::delete(pool, inserted_jessica.id).await.unwrap();
617 Community::delete(pool, inserted_community.id)
620 Instance::delete(pool, inserted_instance.id).await.unwrap();