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},
37 impl CommentReportView {
38 /// returns the CommentReportView for the provided report_id
40 /// * `report_id` - the report id to obtain
42 pool: &mut DbPool<'_>,
43 report_id: CommentReportId,
44 my_person_id: PersonId,
45 ) -> Result<Self, Error> {
46 let conn = &mut get_conn(pool).await?;
48 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
50 let res = comment_report::table
52 .inner_join(comment::table)
53 .inner_join(post::table.on(comment::post_id.eq(post::id)))
54 .inner_join(community::table.on(post::community_id.eq(community::id)))
55 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
56 .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id))))
58 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
61 community_person_ban::table.on(
63 .eq(community_person_ban::community_id)
64 .and(community_person_ban::person_id.eq(comment::creator_id)),
68 comment_like::table.on(
70 .eq(comment_like::comment_id)
71 .and(comment_like::person_id.eq(my_person_id)),
76 .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
79 comment_report::all_columns,
82 community::all_columns,
84 person_alias_1.fields(person::all_columns),
85 comment_aggregates::all_columns,
86 community_person_ban::all_columns.nullable(),
87 comment_like::score.nullable(),
88 person_alias_2.fields(person::all_columns).nullable(),
90 .first::<<CommentReportView as JoinView>::JoinTuple>(conn)
93 Ok(Self::from_tuple(res))
96 /// Returns the current unresolved post report count for the communities you mod
97 pub async fn get_report_count(
98 pool: &mut DbPool<'_>,
99 my_person_id: PersonId,
101 community_id: Option<CommunityId>,
102 ) -> Result<i64, Error> {
103 use diesel::dsl::count;
105 let conn = &mut get_conn(pool).await?;
107 let mut query = comment_report::table
108 .inner_join(comment::table)
109 .inner_join(post::table.on(comment::post_id.eq(post::id)))
110 .filter(comment_report::resolved.eq(false))
113 if let Some(community_id) = community_id {
114 query = query.filter(post::community_id.eq(community_id))
117 // If its not an admin, get only the ones you mod
121 community_moderator::table.on(
122 community_moderator::community_id
123 .eq(post::community_id)
124 .and(community_moderator::person_id.eq(my_person_id)),
127 .select(count(comment_report::id))
132 .select(count(comment_report::id))
140 pub struct CommentReportQuery {
141 pub community_id: Option<CommunityId>,
142 pub page: Option<i64>,
143 pub limit: Option<i64>,
144 pub unresolved_only: Option<bool>,
147 impl CommentReportQuery {
150 pool: &mut DbPool<'_>,
152 ) -> Result<Vec<CommentReportView>, Error> {
153 let conn = &mut get_conn(pool).await?;
155 let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
157 let mut query = comment_report::table
158 .inner_join(comment::table)
159 .inner_join(post::table.on(comment::post_id.eq(post::id)))
160 .inner_join(community::table.on(post::community_id.eq(community::id)))
161 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
162 .inner_join(person_alias_1.on(comment::creator_id.eq(person_alias_1.field(person::id))))
164 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
167 community_person_ban::table.on(
169 .eq(community_person_ban::community_id)
170 .and(community_person_ban::person_id.eq(comment::creator_id))
172 community_person_ban::expires
174 .or(community_person_ban::expires.gt(now)),
179 comment_like::table.on(
181 .eq(comment_like::comment_id)
182 .and(comment_like::person_id.eq(my_person.id)),
187 .on(comment_report::resolver_id.eq(person_alias_2.field(person::id).nullable())),
190 comment_report::all_columns,
191 comment::all_columns,
193 community::all_columns,
195 person_alias_1.fields(person::all_columns),
196 comment_aggregates::all_columns,
197 community_person_ban::all_columns.nullable(),
198 comment_like::score.nullable(),
199 person_alias_2.fields(person::all_columns).nullable(),
203 if let Some(community_id) = self.community_id {
204 query = query.filter(post::community_id.eq(community_id));
207 if self.unresolved_only.unwrap_or(false) {
208 query = query.filter(comment_report::resolved.eq(false));
211 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
214 .order_by(comment_report::published.desc())
218 // If its not an admin, get only the ones you mod
219 let res = if !my_person.admin {
222 community_moderator::table.on(
223 community_moderator::community_id
224 .eq(post::community_id)
225 .and(community_moderator::person_id.eq(my_person.id)),
228 .load::<<CommentReportView as JoinView>::JoinTuple>(conn)
232 .load::<<CommentReportView as JoinView>::JoinTuple>(conn)
236 Ok(res.into_iter().map(CommentReportView::from_tuple).collect())
240 impl JoinView for CommentReportView {
249 Option<CommunityPersonBan>,
254 fn from_tuple(a: Self::JoinTuple) -> Self {
261 comment_creator: a.5,
263 creator_banned_from_community: a.7.is_some(),
272 #![allow(clippy::unwrap_used)]
273 #![allow(clippy::indexing_slicing)]
275 use crate::comment_report_view::{CommentReportQuery, CommentReportView};
276 use lemmy_db_schema::{
277 aggregates::structs::CommentAggregates,
279 comment::{Comment, CommentInsertForm},
280 comment_report::{CommentReport, CommentReportForm},
281 community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
283 person::{Person, PersonInsertForm},
284 post::{Post, PostInsertForm},
286 traits::{Crud, Joinable, Reportable},
287 utils::build_db_pool_for_tests,
289 use serial_test::serial;
293 async fn test_crud() {
294 let pool = &build_db_pool_for_tests().await;
295 let pool = &mut pool.into();
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::default()
517 .list(pool, &inserted_timmy)
524 expected_jessica_report_view.clone(),
525 expected_sara_report_view.clone()
529 // Make sure the counts are correct
530 let report_count = CommentReportView::get_report_count(pool, inserted_timmy.id, false, None)
533 assert_eq!(2, report_count);
535 // Try to resolve the report
536 CommentReport::resolve(pool, inserted_jessica_report.id, inserted_timmy.id)
539 let read_jessica_report_view_after_resolve =
540 CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
544 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
545 expected_jessica_report_view_after_resolve
548 expected_jessica_report_view_after_resolve
550 .resolver_id = Some(inserted_timmy.id);
551 expected_jessica_report_view_after_resolve
553 .updated = read_jessica_report_view_after_resolve
556 expected_jessica_report_view_after_resolve.resolver = Some(Person {
557 id: inserted_timmy.id,
558 name: inserted_timmy.name.clone(),
560 published: inserted_timmy.published,
562 actor_id: inserted_timmy.actor_id.clone(),
571 inbox_url: inserted_timmy.inbox_url.clone(),
572 private_key: inserted_timmy.private_key.clone(),
573 public_key: inserted_timmy.public_key.clone(),
574 last_refreshed_at: inserted_timmy.last_refreshed_at,
575 shared_inbox_url: None,
576 matrix_user_id: None,
578 instance_id: inserted_instance.id,
582 read_jessica_report_view_after_resolve,
583 expected_jessica_report_view_after_resolve
586 // Do a batch read of timmys reports
587 // It should only show saras, which is unresolved
588 let reports_after_resolve = CommentReportQuery {
589 unresolved_only: (Some(true)),
592 .list(pool, &inserted_timmy)
595 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
596 assert_eq!(reports_after_resolve.len(), 1);
598 // Make sure the counts are correct
599 let report_count_after_resolved =
600 CommentReportView::get_report_count(pool, inserted_timmy.id, false, None)
603 assert_eq!(1, report_count_after_resolved);
605 Person::delete(pool, inserted_timmy.id).await.unwrap();
606 Person::delete(pool, inserted_sara.id).await.unwrap();
607 Person::delete(pool, inserted_jessica.id).await.unwrap();
608 Community::delete(pool, inserted_community.id)
611 Instance::delete(pool, inserted_instance.id).await.unwrap();