1 use diesel::{result::Error, *};
3 aggregates::comment_aggregates::CommentAggregates,
5 newtypes::{CommentReportId, CommunityId, PersonId},
21 comment_report::CommentReport,
22 community::{Community, CommunityPersonBan, CommunitySafe},
23 person::{Person, PersonAlias1, PersonAlias2, PersonSafe, PersonSafeAlias1, PersonSafeAlias2},
26 traits::{MaybeOptional, ToSafe, ViewToVec},
28 use serde::{Deserialize, Serialize};
30 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
31 pub struct CommentReportView {
32 pub comment_report: CommentReport,
35 pub community: CommunitySafe,
36 pub creator: PersonSafe,
37 pub comment_creator: PersonSafeAlias1,
38 pub counts: CommentAggregates,
39 pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
40 pub my_vote: Option<i16>, // Left join to CommentLike
41 pub resolver: Option<PersonSafeAlias2>,
44 type CommentReportViewTuple = (
52 Option<CommunityPersonBan>,
54 Option<PersonSafeAlias2>,
57 impl CommentReportView {
58 /// returns the CommentReportView for the provided report_id
60 /// * `report_id` - the report id to obtain
63 report_id: CommentReportId,
64 my_person_id: PersonId,
65 ) -> Result<Self, Error> {
74 creator_banned_from_community,
77 ) = comment_report::table
79 .inner_join(comment::table)
80 .inner_join(post::table.on(comment::post_id.eq(post::id)))
81 .inner_join(community::table.on(post::community_id.eq(community::id)))
82 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
83 .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
85 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
88 community_person_ban::table.on(
90 .eq(community_person_ban::community_id)
91 .and(community_person_ban::person_id.eq(comment::creator_id)),
95 comment_like::table.on(
97 .eq(comment_like::comment_id)
98 .and(comment_like::person_id.eq(my_person_id)),
102 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
105 comment_report::all_columns,
106 comment::all_columns,
108 Community::safe_columns_tuple(),
109 Person::safe_columns_tuple(),
110 PersonAlias1::safe_columns_tuple(),
111 comment_aggregates::all_columns,
112 community_person_ban::all_columns.nullable(),
113 comment_like::score.nullable(),
114 PersonAlias2::safe_columns_tuple().nullable(),
116 .first::<CommentReportViewTuple>(conn)?;
118 let my_vote = if comment_like.is_none() {
132 creator_banned_from_community: creator_banned_from_community.is_some(),
138 /// Returns the current unresolved post report count for the communities you mod
139 pub fn get_report_count(
141 my_person_id: PersonId,
143 community_id: Option<CommunityId>,
144 ) -> Result<i64, Error> {
147 let mut query = comment_report::table
148 .inner_join(comment::table)
149 .inner_join(post::table.on(comment::post_id.eq(post::id)))
150 .filter(comment_report::resolved.eq(false))
153 if let Some(community_id) = community_id {
154 query = query.filter(post::community_id.eq(community_id))
157 // If its not an admin, get only the ones you mod
161 community_moderator::table.on(
162 community_moderator::community_id
163 .eq(post::community_id)
164 .and(community_moderator::person_id.eq(my_person_id)),
167 .select(count(comment_report::id))
170 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(comment::creator_id.eq(person_alias_1::id)))
226 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
229 community_person_ban::table.on(
231 .eq(community_person_ban::community_id)
232 .and(community_person_ban::person_id.eq(comment::creator_id)),
236 comment_like::table.on(
238 .eq(comment_like::comment_id)
239 .and(comment_like::person_id.eq(self.my_person_id)),
243 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
246 comment_report::all_columns,
247 comment::all_columns,
249 Community::safe_columns_tuple(),
250 Person::safe_columns_tuple(),
251 PersonAlias1::safe_columns_tuple(),
252 comment_aggregates::all_columns,
253 community_person_ban::all_columns.nullable(),
254 comment_like::score.nullable(),
255 PersonAlias2::safe_columns_tuple().nullable(),
259 if let Some(community_id) = self.community_id {
260 query = query.filter(post::community_id.eq(community_id));
263 if self.unresolved_only.unwrap_or(false) {
264 query = query.filter(comment_report::resolved.eq(false));
267 let (limit, offset) = limit_and_offset(self.page, self.limit);
270 .order_by(comment_report::published.desc())
274 // If its not an admin, get only the ones you mod
275 let res = if !self.admin {
278 community_moderator::table.on(
279 community_moderator::community_id
280 .eq(post::community_id)
281 .and(community_moderator::person_id.eq(self.my_person_id)),
284 .load::<CommentReportViewTuple>(self.conn)?
286 query.load::<CommentReportViewTuple>(self.conn)?
289 Ok(CommentReportView::from_tuple_to_vec(res))
293 impl ViewToVec for CommentReportView {
294 type DbTuple = CommentReportViewTuple;
295 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
299 comment_report: a.0.to_owned(),
300 comment: a.1.to_owned(),
301 post: a.2.to_owned(),
302 community: a.3.to_owned(),
303 creator: a.4.to_owned(),
304 comment_creator: a.5.to_owned(),
305 counts: a.6.to_owned(),
306 creator_banned_from_community: a.7.is_some(),
308 resolver: a.9.to_owned(),
310 .collect::<Vec<Self>>()
316 use crate::comment_report_view::{CommentReportQueryBuilder, CommentReportView};
317 use lemmy_db_schema::{
318 aggregates::comment_aggregates::CommentAggregates,
319 establish_unpooled_connection,
320 source::{comment::*, comment_report::*, community::*, person::*, post::*},
321 traits::{Crud, Joinable, Reportable},
323 use serial_test::serial;
328 let conn = establish_unpooled_connection();
330 let new_person = PersonForm {
331 name: "timmy_crv".into(),
332 ..PersonForm::default()
335 let inserted_timmy = Person::create(&conn, &new_person).unwrap();
337 let new_person_2 = PersonForm {
338 name: "sara_crv".into(),
339 ..PersonForm::default()
342 let inserted_sara = Person::create(&conn, &new_person_2).unwrap();
344 // Add a third person, since new ppl can only report something once.
345 let new_person_3 = PersonForm {
346 name: "jessica_crv".into(),
347 ..PersonForm::default()
350 let inserted_jessica = Person::create(&conn, &new_person_3).unwrap();
352 let new_community = CommunityForm {
353 name: "test community crv".to_string(),
354 title: "nada".to_owned(),
355 ..CommunityForm::default()
358 let inserted_community = Community::create(&conn, &new_community).unwrap();
361 let timmy_moderator_form = CommunityModeratorForm {
362 community_id: inserted_community.id,
363 person_id: inserted_timmy.id,
366 let _inserted_moderator = CommunityModerator::join(&conn, &timmy_moderator_form).unwrap();
368 let new_post = PostForm {
369 name: "A test post crv".into(),
370 creator_id: inserted_timmy.id,
371 community_id: inserted_community.id,
372 ..PostForm::default()
375 let inserted_post = Post::create(&conn, &new_post).unwrap();
377 let comment_form = CommentForm {
378 content: "A test comment 32".into(),
379 creator_id: inserted_timmy.id,
380 post_id: inserted_post.id,
381 ..CommentForm::default()
384 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
387 let sara_report_form = CommentReportForm {
388 creator_id: inserted_sara.id,
389 comment_id: inserted_comment.id,
390 original_comment_text: "this was it at time of creation".into(),
391 reason: "from sara".into(),
394 let inserted_sara_report = CommentReport::report(&conn, &sara_report_form).unwrap();
397 let jessica_report_form = CommentReportForm {
398 creator_id: inserted_jessica.id,
399 comment_id: inserted_comment.id,
400 original_comment_text: "this was it at time of creation".into(),
401 reason: "from jessica".into(),
404 let inserted_jessica_report = CommentReport::report(&conn, &jessica_report_form).unwrap();
406 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
408 let read_jessica_report_view =
409 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
410 let expected_jessica_report_view = CommentReportView {
411 comment_report: inserted_jessica_report.to_owned(),
412 comment: inserted_comment.to_owned(),
414 community: CommunitySafe {
415 id: inserted_community.id,
416 name: inserted_community.name,
421 actor_id: inserted_community.actor_id.to_owned(),
423 title: inserted_community.title,
427 published: inserted_community.published,
429 creator: PersonSafe {
430 id: inserted_jessica.id,
431 name: inserted_jessica.name,
433 published: inserted_jessica.published,
435 actor_id: inserted_jessica.actor_id.to_owned(),
444 inbox_url: inserted_jessica.inbox_url.to_owned(),
445 shared_inbox_url: None,
446 matrix_user_id: None,
448 comment_creator: PersonSafeAlias1 {
449 id: inserted_timmy.id,
450 name: inserted_timmy.name.to_owned(),
452 published: inserted_timmy.published,
454 actor_id: inserted_timmy.actor_id.to_owned(),
463 inbox_url: inserted_timmy.inbox_url.to_owned(),
464 shared_inbox_url: None,
465 matrix_user_id: None,
467 creator_banned_from_community: false,
468 counts: CommentAggregates {
470 comment_id: inserted_comment.id,
474 published: agg.published,
480 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
482 let mut expected_sara_report_view = expected_jessica_report_view.clone();
483 expected_sara_report_view.comment_report = inserted_sara_report;
484 expected_sara_report_view.creator = PersonSafe {
485 id: inserted_sara.id,
486 name: inserted_sara.name,
488 published: inserted_sara.published,
490 actor_id: inserted_sara.actor_id.to_owned(),
499 inbox_url: inserted_sara.inbox_url.to_owned(),
500 shared_inbox_url: None,
501 matrix_user_id: None,
504 // Do a batch read of timmys reports
505 let reports = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
512 expected_jessica_report_view.to_owned(),
513 expected_sara_report_view.to_owned()
517 // Make sure the counts are correct
519 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
520 assert_eq!(2, report_count);
522 // Try to resolve the report
523 CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
524 let read_jessica_report_view_after_resolve =
525 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
527 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
528 expected_jessica_report_view_after_resolve
531 expected_jessica_report_view_after_resolve
533 .resolver_id = Some(inserted_timmy.id);
534 expected_jessica_report_view_after_resolve
536 .updated = read_jessica_report_view_after_resolve
539 expected_jessica_report_view_after_resolve.resolver = Some(PersonSafeAlias2 {
540 id: inserted_timmy.id,
541 name: inserted_timmy.name.to_owned(),
543 published: inserted_timmy.published,
545 actor_id: inserted_timmy.actor_id.to_owned(),
554 inbox_url: inserted_timmy.inbox_url.to_owned(),
555 shared_inbox_url: None,
556 matrix_user_id: None,
560 read_jessica_report_view_after_resolve,
561 expected_jessica_report_view_after_resolve
564 // Do a batch read of timmys reports
565 // It should only show saras, which is unresolved
566 let reports_after_resolve = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
569 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
571 // Make sure the counts are correct
572 let report_count_after_resolved =
573 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
574 assert_eq!(1, report_count_after_resolved);
576 Person::delete(&conn, inserted_timmy.id).unwrap();
577 Person::delete(&conn, inserted_sara.id).unwrap();
578 Person::delete(&conn, inserted_jessica.id).unwrap();
579 Community::delete(&conn, inserted_community.id).unwrap();