1 use diesel::{dsl::*, 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))
93 community_person_ban::expires
95 .or(community_person_ban::expires.gt(now)),
100 comment_like::table.on(
102 .eq(comment_like::comment_id)
103 .and(comment_like::person_id.eq(my_person_id)),
107 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
110 comment_report::all_columns,
111 comment::all_columns,
113 Community::safe_columns_tuple(),
114 Person::safe_columns_tuple(),
115 PersonAlias1::safe_columns_tuple(),
116 comment_aggregates::all_columns,
117 community_person_ban::all_columns.nullable(),
118 comment_like::score.nullable(),
119 PersonAlias2::safe_columns_tuple().nullable(),
121 .first::<CommentReportViewTuple>(conn)?;
123 let my_vote = comment_like;
133 creator_banned_from_community: creator_banned_from_community.is_some(),
139 /// Returns the current unresolved post report count for the communities you mod
140 pub fn get_report_count(
142 my_person_id: PersonId,
144 community_id: Option<CommunityId>,
145 ) -> Result<i64, Error> {
148 let mut query = comment_report::table
149 .inner_join(comment::table)
150 .inner_join(post::table.on(comment::post_id.eq(post::id)))
151 .filter(comment_report::resolved.eq(false))
154 if let Some(community_id) = community_id {
155 query = query.filter(post::community_id.eq(community_id))
158 // If its not an admin, get only the ones you mod
162 community_moderator::table.on(
163 community_moderator::community_id
164 .eq(post::community_id)
165 .and(community_moderator::person_id.eq(my_person_id)),
168 .select(count(comment_report::id))
171 query.select(count(comment_report::id)).first::<i64>(conn)
176 pub struct CommentReportQueryBuilder<'a> {
177 conn: &'a PgConnection,
178 my_person_id: PersonId,
180 community_id: Option<CommunityId>,
183 unresolved_only: Option<bool>,
186 impl<'a> CommentReportQueryBuilder<'a> {
187 pub fn create(conn: &'a PgConnection, my_person_id: PersonId, admin: bool) -> Self {
188 CommentReportQueryBuilder {
195 unresolved_only: Some(true),
199 pub fn community_id<T: MaybeOptional<CommunityId>>(mut self, community_id: T) -> Self {
200 self.community_id = community_id.get_optional();
204 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
205 self.page = page.get_optional();
209 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
210 self.limit = limit.get_optional();
214 pub fn unresolved_only<T: MaybeOptional<bool>>(mut self, unresolved_only: T) -> Self {
215 self.unresolved_only = unresolved_only.get_optional();
219 pub fn list(self) -> Result<Vec<CommentReportView>, Error> {
220 let mut query = comment_report::table
221 .inner_join(comment::table)
222 .inner_join(post::table.on(comment::post_id.eq(post::id)))
223 .inner_join(community::table.on(post::community_id.eq(community::id)))
224 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
225 .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
227 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
230 community_person_ban::table.on(
232 .eq(community_person_ban::community_id)
233 .and(community_person_ban::person_id.eq(comment::creator_id))
235 community_person_ban::expires
237 .or(community_person_ban::expires.gt(now)),
242 comment_like::table.on(
244 .eq(comment_like::comment_id)
245 .and(comment_like::person_id.eq(self.my_person_id)),
249 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
252 comment_report::all_columns,
253 comment::all_columns,
255 Community::safe_columns_tuple(),
256 Person::safe_columns_tuple(),
257 PersonAlias1::safe_columns_tuple(),
258 comment_aggregates::all_columns,
259 community_person_ban::all_columns.nullable(),
260 comment_like::score.nullable(),
261 PersonAlias2::safe_columns_tuple().nullable(),
265 if let Some(community_id) = self.community_id {
266 query = query.filter(post::community_id.eq(community_id));
269 if self.unresolved_only.unwrap_or(false) {
270 query = query.filter(comment_report::resolved.eq(false));
273 let (limit, offset) = limit_and_offset(self.page, self.limit);
276 .order_by(comment_report::published.desc())
280 // If its not an admin, get only the ones you mod
281 let res = if !self.admin {
284 community_moderator::table.on(
285 community_moderator::community_id
286 .eq(post::community_id)
287 .and(community_moderator::person_id.eq(self.my_person_id)),
290 .load::<CommentReportViewTuple>(self.conn)?
292 query.load::<CommentReportViewTuple>(self.conn)?
295 Ok(CommentReportView::from_tuple_to_vec(res))
299 impl ViewToVec for CommentReportView {
300 type DbTuple = CommentReportViewTuple;
301 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
305 comment_report: a.0.to_owned(),
306 comment: a.1.to_owned(),
307 post: a.2.to_owned(),
308 community: a.3.to_owned(),
309 creator: a.4.to_owned(),
310 comment_creator: a.5.to_owned(),
311 counts: a.6.to_owned(),
312 creator_banned_from_community: a.7.is_some(),
314 resolver: a.9.to_owned(),
316 .collect::<Vec<Self>>()
322 use crate::comment_report_view::{CommentReportQueryBuilder, CommentReportView};
323 use lemmy_db_schema::{
324 aggregates::comment_aggregates::CommentAggregates,
325 establish_unpooled_connection,
326 source::{comment::*, comment_report::*, community::*, person::*, post::*},
327 traits::{Crud, Joinable, Reportable},
329 use serial_test::serial;
334 let conn = establish_unpooled_connection();
336 let new_person = PersonForm {
337 name: "timmy_crv".into(),
338 ..PersonForm::default()
341 let inserted_timmy = Person::create(&conn, &new_person).unwrap();
343 let new_person_2 = PersonForm {
344 name: "sara_crv".into(),
345 ..PersonForm::default()
348 let inserted_sara = Person::create(&conn, &new_person_2).unwrap();
350 // Add a third person, since new ppl can only report something once.
351 let new_person_3 = PersonForm {
352 name: "jessica_crv".into(),
353 ..PersonForm::default()
356 let inserted_jessica = Person::create(&conn, &new_person_3).unwrap();
358 let new_community = CommunityForm {
359 name: "test community crv".to_string(),
360 title: "nada".to_owned(),
361 ..CommunityForm::default()
364 let inserted_community = Community::create(&conn, &new_community).unwrap();
367 let timmy_moderator_form = CommunityModeratorForm {
368 community_id: inserted_community.id,
369 person_id: inserted_timmy.id,
372 let _inserted_moderator = CommunityModerator::join(&conn, &timmy_moderator_form).unwrap();
374 let new_post = PostForm {
375 name: "A test post crv".into(),
376 creator_id: inserted_timmy.id,
377 community_id: inserted_community.id,
378 ..PostForm::default()
381 let inserted_post = Post::create(&conn, &new_post).unwrap();
383 let comment_form = CommentForm {
384 content: "A test comment 32".into(),
385 creator_id: inserted_timmy.id,
386 post_id: inserted_post.id,
387 ..CommentForm::default()
390 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
393 let sara_report_form = CommentReportForm {
394 creator_id: inserted_sara.id,
395 comment_id: inserted_comment.id,
396 original_comment_text: "this was it at time of creation".into(),
397 reason: "from sara".into(),
400 let inserted_sara_report = CommentReport::report(&conn, &sara_report_form).unwrap();
403 let jessica_report_form = CommentReportForm {
404 creator_id: inserted_jessica.id,
405 comment_id: inserted_comment.id,
406 original_comment_text: "this was it at time of creation".into(),
407 reason: "from jessica".into(),
410 let inserted_jessica_report = CommentReport::report(&conn, &jessica_report_form).unwrap();
412 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
414 let read_jessica_report_view =
415 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
416 let expected_jessica_report_view = CommentReportView {
417 comment_report: inserted_jessica_report.to_owned(),
418 comment: inserted_comment.to_owned(),
420 community: CommunitySafe {
421 id: inserted_community.id,
422 name: inserted_community.name,
427 actor_id: inserted_community.actor_id.to_owned(),
429 title: inserted_community.title,
434 published: inserted_community.published,
436 creator: PersonSafe {
437 id: inserted_jessica.id,
438 name: inserted_jessica.name,
440 published: inserted_jessica.published,
442 actor_id: inserted_jessica.actor_id.to_owned(),
451 inbox_url: inserted_jessica.inbox_url.to_owned(),
452 shared_inbox_url: None,
453 matrix_user_id: None,
456 comment_creator: PersonSafeAlias1 {
457 id: inserted_timmy.id,
458 name: inserted_timmy.name.to_owned(),
460 published: inserted_timmy.published,
462 actor_id: inserted_timmy.actor_id.to_owned(),
471 inbox_url: inserted_timmy.inbox_url.to_owned(),
472 shared_inbox_url: None,
473 matrix_user_id: None,
476 creator_banned_from_community: false,
477 counts: CommentAggregates {
479 comment_id: inserted_comment.id,
483 published: agg.published,
489 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
491 let mut expected_sara_report_view = expected_jessica_report_view.clone();
492 expected_sara_report_view.comment_report = inserted_sara_report;
493 expected_sara_report_view.creator = PersonSafe {
494 id: inserted_sara.id,
495 name: inserted_sara.name,
497 published: inserted_sara.published,
499 actor_id: inserted_sara.actor_id.to_owned(),
508 inbox_url: inserted_sara.inbox_url.to_owned(),
509 shared_inbox_url: None,
510 matrix_user_id: None,
514 // Do a batch read of timmys reports
515 let reports = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
522 expected_jessica_report_view.to_owned(),
523 expected_sara_report_view.to_owned()
527 // Make sure the counts are correct
529 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
530 assert_eq!(2, report_count);
532 // Try to resolve the report
533 CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
534 let read_jessica_report_view_after_resolve =
535 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
537 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
538 expected_jessica_report_view_after_resolve
541 expected_jessica_report_view_after_resolve
543 .resolver_id = Some(inserted_timmy.id);
544 expected_jessica_report_view_after_resolve
546 .updated = read_jessica_report_view_after_resolve
549 expected_jessica_report_view_after_resolve.resolver = Some(PersonSafeAlias2 {
550 id: inserted_timmy.id,
551 name: inserted_timmy.name.to_owned(),
553 published: inserted_timmy.published,
555 actor_id: inserted_timmy.actor_id.to_owned(),
564 inbox_url: inserted_timmy.inbox_url.to_owned(),
565 shared_inbox_url: None,
566 matrix_user_id: None,
571 read_jessica_report_view_after_resolve,
572 expected_jessica_report_view_after_resolve
575 // Do a batch read of timmys reports
576 // It should only show saras, which is unresolved
577 let reports_after_resolve = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
580 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
582 // Make sure the counts are correct
583 let report_count_after_resolved =
584 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
585 assert_eq!(1, report_count_after_resolved);
587 Person::delete(&conn, inserted_timmy.id).unwrap();
588 Person::delete(&conn, inserted_sara.id).unwrap();
589 Person::delete(&conn, inserted_jessica.id).unwrap();
590 Community::delete(&conn, inserted_community.id).unwrap();