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)),
69 comment_like::table.on(
71 .eq(comment_like::comment_id)
72 .and(comment_like::person_id.eq(my_person_id)),
77 .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
80 comment_report::all_columns,
83 community::all_columns,
85 person_alias_1.fields(person::all_columns),
86 comment_aggregates::all_columns,
87 community_person_ban::all_columns.nullable(),
88 comment_like::score.nullable(),
89 person_alias_2.fields(person::all_columns).nullable(),
91 .first::<<CommentReportView as JoinView>::JoinTuple>(conn)
94 Ok(Self::from_tuple(res))
97 /// Returns the current unresolved post report count for the communities you mod
98 pub async fn get_report_count(
100 my_person_id: PersonId,
102 community_id: Option<CommunityId>,
103 ) -> Result<i64, Error> {
104 use diesel::dsl::count;
106 let conn = &mut get_conn(pool).await?;
108 let mut query = comment_report::table
109 .inner_join(comment::table)
110 .inner_join(post::table.on(comment::post_id.eq(post::id)))
111 .filter(comment_report::resolved.eq(false))
114 if let Some(community_id) = community_id {
115 query = query.filter(post::community_id.eq(community_id))
118 // If its not an admin, get only the ones you mod
122 community_moderator::table.on(
123 community_moderator::community_id
124 .eq(post::community_id)
125 .and(community_moderator::person_id.eq(my_person_id)),
128 .select(count(comment_report::id))
133 .select(count(comment_report::id))
140 #[derive(TypedBuilder)]
141 #[builder(field_defaults(default))]
142 pub struct CommentReportQuery<'a> {
146 my_person_id: PersonId,
149 community_id: Option<CommunityId>,
152 unresolved_only: Option<bool>,
155 impl<'a> CommentReportQuery<'a> {
156 pub async fn list(self) -> Result<Vec<CommentReportView>, Error> {
157 let conn = &mut get_conn(self.pool).await?;
159 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
161 let mut query = comment_report::table
162 .inner_join(comment::table)
163 .inner_join(post::table.on(comment::post_id.eq(post::id)))
164 .inner_join(community::table.on(post::community_id.eq(community::id)))
165 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
166 .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id))))
168 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
171 community_person_ban::table.on(
173 .eq(community_person_ban::community_id)
174 .and(community_person_ban::person_id.eq(comment::creator_id))
176 community_person_ban::expires
178 .or(community_person_ban::expires.gt(now)),
183 comment_like::table.on(
185 .eq(comment_like::comment_id)
186 .and(comment_like::person_id.eq(self.my_person_id)),
191 .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
194 comment_report::all_columns,
195 comment::all_columns,
197 community::all_columns,
199 person_alias_1.fields(person::all_columns),
200 comment_aggregates::all_columns,
201 community_person_ban::all_columns.nullable(),
202 comment_like::score.nullable(),
203 person_alias_2.fields(person::all_columns).nullable(),
207 if let Some(community_id) = self.community_id {
208 query = query.filter(post::community_id.eq(community_id));
211 if self.unresolved_only.unwrap_or(false) {
212 query = query.filter(comment_report::resolved.eq(false));
215 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
218 .order_by(comment_report::published.desc())
222 // If its not an admin, get only the ones you mod
223 let res = if !self.admin {
226 community_moderator::table.on(
227 community_moderator::community_id
228 .eq(post::community_id)
229 .and(community_moderator::person_id.eq(self.my_person_id)),
232 .load::<<CommentReportView as JoinView>::JoinTuple>(conn)
236 .load::<<CommentReportView as JoinView>::JoinTuple>(conn)
240 Ok(res.into_iter().map(CommentReportView::from_tuple).collect())
244 impl JoinView for CommentReportView {
253 Option<CommunityPersonBan>,
258 fn from_tuple(a: Self::JoinTuple) -> Self {
265 comment_creator: a.5,
267 creator_banned_from_community: a.7.is_some(),
276 use crate::comment_report_view::{CommentReportQuery, CommentReportView};
277 use lemmy_db_schema::{
278 aggregates::structs::CommentAggregates,
280 comment::{Comment, CommentInsertForm},
281 comment_report::{CommentReport, CommentReportForm},
282 community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
284 person::{Person, PersonInsertForm},
285 post::{Post, PostInsertForm},
287 traits::{Crud, Joinable, Reportable},
288 utils::build_db_pool_for_tests,
290 use serial_test::serial;
294 async fn test_crud() {
295 let pool = &build_db_pool_for_tests().await;
297 let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
301 let new_person = PersonInsertForm::builder()
302 .name("timmy_crv".into())
303 .public_key("pubkey".to_string())
304 .instance_id(inserted_instance.id)
307 let inserted_timmy = Person::create(pool, &new_person).await.unwrap();
309 let new_person_2 = PersonInsertForm::builder()
310 .name("sara_crv".into())
311 .public_key("pubkey".to_string())
312 .instance_id(inserted_instance.id)
315 let inserted_sara = Person::create(pool, &new_person_2).await.unwrap();
317 // Add a third person, since new ppl can only report something once.
318 let new_person_3 = PersonInsertForm::builder()
319 .name("jessica_crv".into())
320 .public_key("pubkey".to_string())
321 .instance_id(inserted_instance.id)
324 let inserted_jessica = Person::create(pool, &new_person_3).await.unwrap();
326 let new_community = CommunityInsertForm::builder()
327 .name("test community crv".to_string())
328 .title("nada".to_owned())
329 .public_key("pubkey".to_string())
330 .instance_id(inserted_instance.id)
333 let inserted_community = Community::create(pool, &new_community).await.unwrap();
336 let timmy_moderator_form = CommunityModeratorForm {
337 community_id: inserted_community.id,
338 person_id: inserted_timmy.id,
341 let _inserted_moderator = CommunityModerator::join(pool, &timmy_moderator_form)
345 let new_post = PostInsertForm::builder()
346 .name("A test post crv".into())
347 .creator_id(inserted_timmy.id)
348 .community_id(inserted_community.id)
351 let inserted_post = Post::create(pool, &new_post).await.unwrap();
353 let comment_form = CommentInsertForm::builder()
354 .content("A test comment 32".into())
355 .creator_id(inserted_timmy.id)
356 .post_id(inserted_post.id)
359 let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap();
362 let sara_report_form = CommentReportForm {
363 creator_id: inserted_sara.id,
364 comment_id: inserted_comment.id,
365 original_comment_text: "this was it at time of creation".into(),
366 reason: "from sara".into(),
369 let inserted_sara_report = CommentReport::report(pool, &sara_report_form)
374 let jessica_report_form = CommentReportForm {
375 creator_id: inserted_jessica.id,
376 comment_id: inserted_comment.id,
377 original_comment_text: "this was it at time of creation".into(),
378 reason: "from jessica".into(),
381 let inserted_jessica_report = CommentReport::report(pool, &jessica_report_form)
385 let agg = CommentAggregates::read(pool, inserted_comment.id)
389 let read_jessica_report_view =
390 CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
393 let expected_jessica_report_view = CommentReportView {
394 comment_report: inserted_jessica_report.clone(),
395 comment: inserted_comment.clone(),
397 community: Community {
398 id: inserted_community.id,
399 name: inserted_community.name,
404 actor_id: inserted_community.actor_id.clone(),
406 title: inserted_community.title,
411 posting_restricted_to_mods: false,
412 published: inserted_community.published,
413 private_key: inserted_community.private_key,
414 public_key: inserted_community.public_key,
415 last_refreshed_at: inserted_community.last_refreshed_at,
416 followers_url: inserted_community.followers_url,
417 inbox_url: inserted_community.inbox_url,
418 shared_inbox_url: inserted_community.shared_inbox_url,
419 moderators_url: inserted_community.moderators_url,
420 featured_url: inserted_community.featured_url,
421 instance_id: inserted_instance.id,
424 id: inserted_jessica.id,
425 name: inserted_jessica.name,
427 published: inserted_jessica.published,
429 actor_id: inserted_jessica.actor_id.clone(),
438 inbox_url: inserted_jessica.inbox_url.clone(),
439 shared_inbox_url: None,
440 matrix_user_id: None,
442 instance_id: inserted_instance.id,
443 private_key: inserted_jessica.private_key,
444 public_key: inserted_jessica.public_key,
445 last_refreshed_at: inserted_jessica.last_refreshed_at,
447 comment_creator: Person {
448 id: inserted_timmy.id,
449 name: inserted_timmy.name.clone(),
451 published: inserted_timmy.published,
453 actor_id: inserted_timmy.actor_id.clone(),
462 inbox_url: inserted_timmy.inbox_url.clone(),
463 shared_inbox_url: None,
464 matrix_user_id: None,
466 instance_id: inserted_instance.id,
467 private_key: inserted_timmy.private_key.clone(),
468 public_key: inserted_timmy.public_key.clone(),
469 last_refreshed_at: inserted_timmy.last_refreshed_at,
471 creator_banned_from_community: false,
472 counts: CommentAggregates {
474 comment_id: inserted_comment.id,
478 published: agg.published,
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.comment_report = inserted_sara_report;
490 expected_sara_report_view.creator = Person {
491 id: inserted_sara.id,
492 name: inserted_sara.name,
494 published: inserted_sara.published,
496 actor_id: inserted_sara.actor_id.clone(),
505 inbox_url: inserted_sara.inbox_url.clone(),
506 shared_inbox_url: None,
507 matrix_user_id: None,
509 instance_id: inserted_instance.id,
510 private_key: inserted_sara.private_key,
511 public_key: inserted_sara.public_key,
512 last_refreshed_at: inserted_sara.last_refreshed_at,
515 // Do a batch read of timmys reports
516 let reports = CommentReportQuery::builder()
518 .my_person_id(inserted_timmy.id)
528 expected_jessica_report_view.clone(),
529 expected_sara_report_view.clone()
533 // Make sure the counts are correct
534 let report_count = CommentReportView::get_report_count(pool, inserted_timmy.id, false, None)
537 assert_eq!(2, report_count);
539 // Try to resolve the report
540 CommentReport::resolve(pool, inserted_jessica_report.id, inserted_timmy.id)
543 let read_jessica_report_view_after_resolve =
544 CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
548 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
549 expected_jessica_report_view_after_resolve
552 expected_jessica_report_view_after_resolve
554 .resolver_id = Some(inserted_timmy.id);
555 expected_jessica_report_view_after_resolve
557 .updated = read_jessica_report_view_after_resolve
560 expected_jessica_report_view_after_resolve.resolver = Some(Person {
561 id: inserted_timmy.id,
562 name: inserted_timmy.name.clone(),
564 published: inserted_timmy.published,
566 actor_id: inserted_timmy.actor_id.clone(),
575 inbox_url: inserted_timmy.inbox_url.clone(),
576 private_key: inserted_timmy.private_key.clone(),
577 public_key: inserted_timmy.public_key.clone(),
578 last_refreshed_at: inserted_timmy.last_refreshed_at,
579 shared_inbox_url: None,
580 matrix_user_id: None,
582 instance_id: inserted_instance.id,
586 read_jessica_report_view_after_resolve,
587 expected_jessica_report_view_after_resolve
590 // Do a batch read of timmys reports
591 // It should only show saras, which is unresolved
592 let reports_after_resolve = CommentReportQuery::builder()
594 .my_person_id(inserted_timmy.id)
596 .unresolved_only(Some(true))
601 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
602 assert_eq!(reports_after_resolve.len(), 1);
604 // Make sure the counts are correct
605 let report_count_after_resolved =
606 CommentReportView::get_report_count(pool, inserted_timmy.id, false, None)
609 assert_eq!(1, report_count_after_resolved);
611 Person::delete(pool, inserted_timmy.id).await.unwrap();
612 Person::delete(pool, inserted_sara.id).await.unwrap();
613 Person::delete(pool, inserted_jessica.id).await.unwrap();
614 Community::delete(pool, inserted_community.id)
617 Instance::delete(pool, inserted_instance.id).await.unwrap();