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)))
151 community_moderator::table.on(community_moderator::community_id.eq(post::community_id)),
153 .filter(comment_report::resolved.eq(false))
156 // If its not an admin, get only the ones you mod
158 query = query.filter(community_moderator::person_id.eq(my_person_id));
161 if let Some(community_id) = community_id {
162 query = query.filter(post::community_id.eq(community_id))
165 query.select(count(comment_report::id)).first::<i64>(conn)
169 pub struct CommentReportQueryBuilder<'a> {
170 conn: &'a PgConnection,
171 my_person_id: PersonId,
173 community_id: Option<CommunityId>,
176 unresolved_only: Option<bool>,
179 impl<'a> CommentReportQueryBuilder<'a> {
180 pub fn create(conn: &'a PgConnection, my_person_id: PersonId, admin: bool) -> Self {
181 CommentReportQueryBuilder {
188 unresolved_only: Some(true),
192 pub fn community_id<T: MaybeOptional<CommunityId>>(mut self, community_id: T) -> Self {
193 self.community_id = community_id.get_optional();
197 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
198 self.page = page.get_optional();
202 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
203 self.limit = limit.get_optional();
207 pub fn unresolved_only<T: MaybeOptional<bool>>(mut self, unresolved_only: T) -> Self {
208 self.unresolved_only = unresolved_only.get_optional();
212 pub fn list(self) -> Result<Vec<CommentReportView>, Error> {
213 let mut query = comment_report::table
214 .inner_join(comment::table)
215 .inner_join(post::table.on(comment::post_id.eq(post::id)))
216 .inner_join(community::table.on(post::community_id.eq(community::id)))
217 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
218 .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
221 community_moderator::table.on(community_moderator::community_id.eq(post::community_id)),
224 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
227 community_person_ban::table.on(
229 .eq(community_person_ban::community_id)
230 .and(community_person_ban::person_id.eq(comment::creator_id)),
234 comment_like::table.on(
236 .eq(comment_like::comment_id)
237 .and(comment_like::person_id.eq(self.my_person_id)),
241 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
244 comment_report::all_columns,
245 comment::all_columns,
247 Community::safe_columns_tuple(),
248 Person::safe_columns_tuple(),
249 PersonAlias1::safe_columns_tuple(),
250 comment_aggregates::all_columns,
251 community_person_ban::all_columns.nullable(),
252 comment_like::score.nullable(),
253 PersonAlias2::safe_columns_tuple().nullable(),
257 // If its not an admin, get only the ones you mod
259 query = query.filter(community_moderator::person_id.eq(self.my_person_id));
262 if let Some(community_id) = self.community_id {
263 query = query.filter(post::community_id.eq(community_id));
266 if self.unresolved_only.unwrap_or(false) {
267 query = query.filter(comment_report::resolved.eq(false));
270 let (limit, offset) = limit_and_offset(self.page, self.limit);
273 .order_by(comment_report::published.asc())
276 .load::<CommentReportViewTuple>(self.conn)?;
278 Ok(CommentReportView::from_tuple_to_vec(res))
282 impl ViewToVec for CommentReportView {
283 type DbTuple = CommentReportViewTuple;
284 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
288 comment_report: a.0.to_owned(),
289 comment: a.1.to_owned(),
290 post: a.2.to_owned(),
291 community: a.3.to_owned(),
292 creator: a.4.to_owned(),
293 comment_creator: a.5.to_owned(),
294 counts: a.6.to_owned(),
295 creator_banned_from_community: a.7.is_some(),
297 resolver: a.9.to_owned(),
299 .collect::<Vec<Self>>()
305 use crate::comment_report_view::{CommentReportQueryBuilder, CommentReportView};
306 use lemmy_db_schema::{
307 aggregates::comment_aggregates::CommentAggregates,
308 establish_unpooled_connection,
309 source::{comment::*, comment_report::*, community::*, person::*, post::*},
310 traits::{Crud, Joinable, Reportable},
312 use serial_test::serial;
317 let conn = establish_unpooled_connection();
319 let new_person = PersonForm {
320 name: "timmy_crv".into(),
321 ..PersonForm::default()
324 let inserted_timmy = Person::create(&conn, &new_person).unwrap();
326 let new_person_2 = PersonForm {
327 name: "sara_crv".into(),
328 ..PersonForm::default()
331 let inserted_sara = Person::create(&conn, &new_person_2).unwrap();
333 // Add a third person, since new ppl can only report something once.
334 let new_person_3 = PersonForm {
335 name: "jessica_crv".into(),
336 ..PersonForm::default()
339 let inserted_jessica = Person::create(&conn, &new_person_3).unwrap();
341 let new_community = CommunityForm {
342 name: "test community crv".to_string(),
343 title: "nada".to_owned(),
344 ..CommunityForm::default()
347 let inserted_community = Community::create(&conn, &new_community).unwrap();
350 let timmy_moderator_form = CommunityModeratorForm {
351 community_id: inserted_community.id,
352 person_id: inserted_timmy.id,
355 let _inserted_moderator = CommunityModerator::join(&conn, &timmy_moderator_form).unwrap();
357 let new_post = PostForm {
358 name: "A test post crv".into(),
359 creator_id: inserted_timmy.id,
360 community_id: inserted_community.id,
361 ..PostForm::default()
364 let inserted_post = Post::create(&conn, &new_post).unwrap();
366 let comment_form = CommentForm {
367 content: "A test comment 32".into(),
368 creator_id: inserted_timmy.id,
369 post_id: inserted_post.id,
370 ..CommentForm::default()
373 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
376 let sara_report_form = CommentReportForm {
377 creator_id: inserted_sara.id,
378 comment_id: inserted_comment.id,
379 original_comment_text: "this was it at time of creation".into(),
380 reason: "from sara".into(),
383 let inserted_sara_report = CommentReport::report(&conn, &sara_report_form).unwrap();
386 let jessica_report_form = CommentReportForm {
387 creator_id: inserted_jessica.id,
388 comment_id: inserted_comment.id,
389 original_comment_text: "this was it at time of creation".into(),
390 reason: "from jessica".into(),
393 let inserted_jessica_report = CommentReport::report(&conn, &jessica_report_form).unwrap();
395 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
397 let read_jessica_report_view =
398 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
399 let expected_jessica_report_view = CommentReportView {
400 comment_report: inserted_jessica_report.to_owned(),
401 comment: inserted_comment.to_owned(),
403 community: CommunitySafe {
404 id: inserted_community.id,
405 name: inserted_community.name,
410 actor_id: inserted_community.actor_id.to_owned(),
412 title: inserted_community.title,
416 published: inserted_community.published,
418 creator: PersonSafe {
419 id: inserted_jessica.id,
420 name: inserted_jessica.name,
422 published: inserted_jessica.published,
424 actor_id: inserted_jessica.actor_id.to_owned(),
433 inbox_url: inserted_jessica.inbox_url.to_owned(),
434 shared_inbox_url: None,
435 matrix_user_id: None,
437 comment_creator: PersonSafeAlias1 {
438 id: inserted_timmy.id,
439 name: inserted_timmy.name.to_owned(),
441 published: inserted_timmy.published,
443 actor_id: inserted_timmy.actor_id.to_owned(),
452 inbox_url: inserted_timmy.inbox_url.to_owned(),
453 shared_inbox_url: None,
454 matrix_user_id: None,
456 creator_banned_from_community: false,
457 counts: CommentAggregates {
459 comment_id: inserted_comment.id,
463 published: agg.published,
469 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
471 let mut expected_sara_report_view = expected_jessica_report_view.clone();
472 expected_sara_report_view.comment_report = inserted_sara_report;
473 expected_sara_report_view.creator = PersonSafe {
474 id: inserted_sara.id,
475 name: inserted_sara.name,
477 published: inserted_sara.published,
479 actor_id: inserted_sara.actor_id.to_owned(),
488 inbox_url: inserted_sara.inbox_url.to_owned(),
489 shared_inbox_url: None,
490 matrix_user_id: None,
493 // Do a batch read of timmys reports
494 let reports = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
501 expected_sara_report_view.to_owned(),
502 expected_jessica_report_view.to_owned()
506 // Make sure the counts are correct
508 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
509 assert_eq!(2, report_count);
511 // Try to resolve the report
512 CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
513 let read_jessica_report_view_after_resolve =
514 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
516 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
517 expected_jessica_report_view_after_resolve
520 expected_jessica_report_view_after_resolve
522 .resolver_id = Some(inserted_timmy.id);
523 expected_jessica_report_view_after_resolve
525 .updated = read_jessica_report_view_after_resolve
528 expected_jessica_report_view_after_resolve.resolver = Some(PersonSafeAlias2 {
529 id: inserted_timmy.id,
530 name: inserted_timmy.name.to_owned(),
532 published: inserted_timmy.published,
534 actor_id: inserted_timmy.actor_id.to_owned(),
543 inbox_url: inserted_timmy.inbox_url.to_owned(),
544 shared_inbox_url: None,
545 matrix_user_id: None,
549 read_jessica_report_view_after_resolve,
550 expected_jessica_report_view_after_resolve
553 // Do a batch read of timmys reports
554 // It should only show saras, which is unresolved
555 let reports_after_resolve = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
558 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
560 // Make sure the counts are correct
561 let report_count_after_resolved =
562 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
563 assert_eq!(1, report_count_after_resolved);
565 Person::delete(&conn, inserted_timmy.id).unwrap();
566 Person::delete(&conn, inserted_sara.id).unwrap();
567 Person::delete(&conn, inserted_jessica.id).unwrap();
568 Community::delete(&conn, inserted_community.id).unwrap();