1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{
3 aggregates::comment_aggregates::CommentAggregates,
25 comment_report::CommentReport,
26 community::{Community, CommunityPersonBan, CommunitySafe},
27 person::{Person, PersonAlias1, PersonAlias2, PersonSafe, PersonSafeAlias1, PersonSafeAlias2},
36 #[derive(Debug, PartialEq, Serialize, Clone)]
37 pub struct CommentReportView {
38 pub comment_report: CommentReport,
41 pub community: CommunitySafe,
42 pub creator: PersonSafe,
43 pub comment_creator: PersonSafeAlias1,
44 pub counts: CommentAggregates,
45 pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
46 pub my_vote: Option<i16>, // Left join to CommentLike
47 pub resolver: Option<PersonSafeAlias2>,
50 type CommentReportViewTuple = (
58 Option<CommunityPersonBan>,
60 Option<PersonSafeAlias2>,
63 impl CommentReportView {
64 /// returns the CommentReportView for the provided report_id
66 /// * `report_id` - the report id to obtain
69 report_id: CommentReportId,
70 my_person_id: PersonId,
71 ) -> Result<Self, Error> {
80 creator_banned_from_community,
83 ) = comment_report::table
85 .inner_join(comment::table)
86 .inner_join(post::table.on(comment::post_id.eq(post::id)))
87 .inner_join(community::table.on(post::community_id.eq(community::id)))
88 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
89 .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id)))
91 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
94 community_person_ban::table.on(
96 .eq(community_person_ban::community_id)
97 .and(community_person_ban::person_id.eq(comment::creator_id)),
101 comment_like::table.on(
103 .eq(comment_like::comment_id)
104 .and(comment_like::person_id.eq(my_person_id)),
108 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
111 comment_report::all_columns,
112 comment::all_columns,
114 Community::safe_columns_tuple(),
115 Person::safe_columns_tuple(),
116 PersonAlias1::safe_columns_tuple(),
117 comment_aggregates::all_columns,
118 community_person_ban::all_columns.nullable(),
119 comment_like::score.nullable(),
120 PersonAlias2::safe_columns_tuple().nullable(),
122 .first::<CommentReportViewTuple>(conn)?;
124 let my_vote = if comment_like.is_none() {
138 creator_banned_from_community: creator_banned_from_community.is_some(),
144 /// Returns the current unresolved post report count for the communities you mod
145 pub fn get_report_count(
147 my_person_id: PersonId,
149 community_id: Option<CommunityId>,
150 ) -> Result<i64, Error> {
153 let mut query = comment_report::table
154 .inner_join(comment::table)
155 .inner_join(post::table.on(comment::post_id.eq(post::id)))
157 community_moderator::table.on(community_moderator::community_id.eq(post::community_id)),
159 .filter(comment_report::resolved.eq(false))
162 // If its not an admin, get only the ones you mod
164 query = query.filter(community_moderator::person_id.eq(my_person_id));
167 if let Some(community_id) = community_id {
168 query = query.filter(post::community_id.eq(community_id))
171 query.select(count(comment_report::id)).first::<i64>(conn)
175 pub struct CommentReportQueryBuilder<'a> {
176 conn: &'a PgConnection,
177 my_person_id: PersonId,
179 community_id: Option<CommunityId>,
182 unresolved_only: Option<bool>,
185 impl<'a> CommentReportQueryBuilder<'a> {
186 pub fn create(conn: &'a PgConnection, my_person_id: PersonId, admin: bool) -> Self {
187 CommentReportQueryBuilder {
194 unresolved_only: Some(true),
198 pub fn community_id<T: MaybeOptional<CommunityId>>(mut self, community_id: T) -> Self {
199 self.community_id = community_id.get_optional();
203 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
204 self.page = page.get_optional();
208 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
209 self.limit = limit.get_optional();
213 pub fn unresolved_only<T: MaybeOptional<bool>>(mut self, unresolved_only: T) -> Self {
214 self.unresolved_only = unresolved_only.get_optional();
218 pub fn list(self) -> Result<Vec<CommentReportView>, Error> {
219 let mut query = comment_report::table
220 .inner_join(comment::table)
221 .inner_join(post::table.on(comment::post_id.eq(post::id)))
222 .inner_join(community::table.on(post::community_id.eq(community::id)))
223 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
224 .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id)))
227 community_moderator::table.on(community_moderator::community_id.eq(post::community_id)),
230 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
233 community_person_ban::table.on(
235 .eq(community_person_ban::community_id)
236 .and(community_person_ban::person_id.eq(comment::creator_id)),
240 comment_like::table.on(
242 .eq(comment_like::comment_id)
243 .and(comment_like::person_id.eq(self.my_person_id)),
247 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
250 comment_report::all_columns,
251 comment::all_columns,
253 Community::safe_columns_tuple(),
254 Person::safe_columns_tuple(),
255 PersonAlias1::safe_columns_tuple(),
256 comment_aggregates::all_columns,
257 community_person_ban::all_columns.nullable(),
258 comment_like::score.nullable(),
259 PersonAlias2::safe_columns_tuple().nullable(),
263 // If its not an admin, get only the ones you mod
265 query = query.filter(community_moderator::person_id.eq(self.my_person_id));
268 if let Some(community_id) = self.community_id {
269 query = query.filter(post::community_id.eq(community_id));
272 if self.unresolved_only.unwrap_or(false) {
273 query = query.filter(comment_report::resolved.eq(false));
276 let (limit, offset) = limit_and_offset(self.page, self.limit);
279 .order_by(comment_report::published.asc())
282 .load::<CommentReportViewTuple>(self.conn)?;
284 Ok(CommentReportView::from_tuple_to_vec(res))
288 impl ViewToVec for CommentReportView {
289 type DbTuple = CommentReportViewTuple;
290 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
294 comment_report: a.0.to_owned(),
295 comment: a.1.to_owned(),
296 post: a.2.to_owned(),
297 community: a.3.to_owned(),
298 creator: a.4.to_owned(),
299 comment_creator: a.5.to_owned(),
300 counts: a.6.to_owned(),
301 creator_banned_from_community: a.7.is_some(),
303 resolver: a.9.to_owned(),
305 .collect::<Vec<Self>>()
311 use crate::comment_report_view::{CommentReportQueryBuilder, CommentReportView};
312 use lemmy_db_queries::{
313 aggregates::comment_aggregates::CommentAggregates,
314 establish_unpooled_connection,
319 use lemmy_db_schema::source::{comment::*, comment_report::*, community::*, person::*, post::*};
320 use serial_test::serial;
325 let conn = establish_unpooled_connection();
327 let new_person = PersonForm {
328 name: "timmy_crv".into(),
329 ..PersonForm::default()
332 let inserted_timmy = Person::create(&conn, &new_person).unwrap();
334 let new_person_2 = PersonForm {
335 name: "sara_crv".into(),
336 ..PersonForm::default()
339 let inserted_sara = Person::create(&conn, &new_person_2).unwrap();
341 // Add a third person, since new ppl can only report something once.
342 let new_person_3 = PersonForm {
343 name: "jessica_crv".into(),
344 ..PersonForm::default()
347 let inserted_jessica = Person::create(&conn, &new_person_3).unwrap();
349 let new_community = CommunityForm {
350 name: "test community crv".to_string(),
351 title: "nada".to_owned(),
352 ..CommunityForm::default()
355 let inserted_community = Community::create(&conn, &new_community).unwrap();
358 let timmy_moderator_form = CommunityModeratorForm {
359 community_id: inserted_community.id,
360 person_id: inserted_timmy.id,
363 let _inserted_moderator = CommunityModerator::join(&conn, &timmy_moderator_form).unwrap();
365 let new_post = PostForm {
366 name: "A test post crv".into(),
367 creator_id: inserted_timmy.id,
368 community_id: inserted_community.id,
369 ..PostForm::default()
372 let inserted_post = Post::create(&conn, &new_post).unwrap();
374 let comment_form = CommentForm {
375 content: "A test comment 32".into(),
376 creator_id: inserted_timmy.id,
377 post_id: inserted_post.id,
378 ..CommentForm::default()
381 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
384 let sara_report_form = CommentReportForm {
385 creator_id: inserted_sara.id,
386 comment_id: inserted_comment.id,
387 original_comment_text: "this was it at time of creation".into(),
388 reason: "from sara".into(),
391 let inserted_sara_report = CommentReport::report(&conn, &sara_report_form).unwrap();
394 let jessica_report_form = CommentReportForm {
395 creator_id: inserted_jessica.id,
396 comment_id: inserted_comment.id,
397 original_comment_text: "this was it at time of creation".into(),
398 reason: "from jessica".into(),
401 let inserted_jessica_report = CommentReport::report(&conn, &jessica_report_form).unwrap();
403 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
405 let read_jessica_report_view =
406 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
407 let expected_jessica_report_view = CommentReportView {
408 comment_report: inserted_jessica_report.to_owned(),
409 comment: inserted_comment.to_owned(),
411 community: CommunitySafe {
412 id: inserted_community.id,
413 name: inserted_community.name,
418 actor_id: inserted_community.actor_id.to_owned(),
420 title: inserted_community.title,
424 published: inserted_community.published,
426 creator: PersonSafe {
427 id: inserted_jessica.id,
428 name: inserted_jessica.name,
430 published: inserted_jessica.published,
432 actor_id: inserted_jessica.actor_id.to_owned(),
441 inbox_url: inserted_jessica.inbox_url.to_owned(),
442 shared_inbox_url: None,
443 matrix_user_id: None,
445 comment_creator: PersonSafeAlias1 {
446 id: inserted_timmy.id,
447 name: inserted_timmy.name.to_owned(),
449 published: inserted_timmy.published,
451 actor_id: inserted_timmy.actor_id.to_owned(),
460 inbox_url: inserted_timmy.inbox_url.to_owned(),
461 shared_inbox_url: None,
462 matrix_user_id: None,
464 creator_banned_from_community: false,
465 counts: CommentAggregates {
467 comment_id: inserted_comment.id,
471 published: agg.published,
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.comment_report = inserted_sara_report;
481 expected_sara_report_view.creator = PersonSafe {
482 id: inserted_sara.id,
483 name: inserted_sara.name,
485 published: inserted_sara.published,
487 actor_id: inserted_sara.actor_id.to_owned(),
496 inbox_url: inserted_sara.inbox_url.to_owned(),
497 shared_inbox_url: None,
498 matrix_user_id: None,
501 // Do a batch read of timmys reports
502 let reports = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
509 expected_sara_report_view.to_owned(),
510 expected_jessica_report_view.to_owned()
514 // Make sure the counts are correct
516 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
517 assert_eq!(2, report_count);
519 // Try to resolve the report
520 CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
521 let read_jessica_report_view_after_resolve =
522 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
524 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
525 expected_jessica_report_view_after_resolve
528 expected_jessica_report_view_after_resolve
530 .resolver_id = Some(inserted_timmy.id);
531 expected_jessica_report_view_after_resolve
533 .updated = read_jessica_report_view_after_resolve
536 expected_jessica_report_view_after_resolve.resolver = Some(PersonSafeAlias2 {
537 id: inserted_timmy.id,
538 name: inserted_timmy.name.to_owned(),
540 published: inserted_timmy.published,
542 actor_id: inserted_timmy.actor_id.to_owned(),
551 inbox_url: inserted_timmy.inbox_url.to_owned(),
552 shared_inbox_url: None,
553 matrix_user_id: None,
557 read_jessica_report_view_after_resolve,
558 expected_jessica_report_view_after_resolve
561 // Do a batch read of timmys reports
562 // It should only show saras, which is unresolved
563 let reports_after_resolve = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
566 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
568 // Make sure the counts are correct
569 let report_count_after_resolved =
570 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
571 assert_eq!(1, report_count_after_resolved);
573 Person::delete(&conn, inserted_timmy.id).unwrap();
574 Person::delete(&conn, inserted_sara.id).unwrap();
575 Person::delete(&conn, inserted_jessica.id).unwrap();
576 Community::delete(&conn, inserted_community.id).unwrap();