1 use crate::structs::CommentReportView;
2 use diesel::{dsl::*, result::Error, *};
4 aggregates::structs::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},
27 utils::limit_and_offset,
30 type CommentReportViewTuple = (
38 Option<CommunityPersonBan>,
40 Option<PersonSafeAlias2>,
43 impl CommentReportView {
44 /// returns the CommentReportView for the provided report_id
46 /// * `report_id` - the report id to obtain
49 report_id: CommentReportId,
50 my_person_id: PersonId,
51 ) -> Result<Self, Error> {
60 creator_banned_from_community,
63 ) = comment_report::table
65 .inner_join(comment::table)
66 .inner_join(post::table.on(comment::post_id.eq(post::id)))
67 .inner_join(community::table.on(post::community_id.eq(community::id)))
68 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
69 .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
71 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
74 community_person_ban::table.on(
76 .eq(community_person_ban::community_id)
77 .and(community_person_ban::person_id.eq(comment::creator_id))
79 community_person_ban::expires
81 .or(community_person_ban::expires.gt(now)),
86 comment_like::table.on(
88 .eq(comment_like::comment_id)
89 .and(comment_like::person_id.eq(my_person_id)),
93 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
96 comment_report::all_columns,
99 Community::safe_columns_tuple(),
100 Person::safe_columns_tuple(),
101 PersonAlias1::safe_columns_tuple(),
102 comment_aggregates::all_columns,
103 community_person_ban::all_columns.nullable(),
104 comment_like::score.nullable(),
105 PersonAlias2::safe_columns_tuple().nullable(),
107 .first::<CommentReportViewTuple>(conn)?;
109 let my_vote = comment_like;
119 creator_banned_from_community: creator_banned_from_community.is_some(),
125 /// Returns the current unresolved post report count for the communities you mod
126 pub fn get_report_count(
128 my_person_id: PersonId,
130 community_id: Option<CommunityId>,
131 ) -> Result<i64, Error> {
134 let mut query = comment_report::table
135 .inner_join(comment::table)
136 .inner_join(post::table.on(comment::post_id.eq(post::id)))
137 .filter(comment_report::resolved.eq(false))
140 if let Some(community_id) = community_id {
141 query = query.filter(post::community_id.eq(community_id))
144 // If its not an admin, get only the ones you mod
148 community_moderator::table.on(
149 community_moderator::community_id
150 .eq(post::community_id)
151 .and(community_moderator::person_id.eq(my_person_id)),
154 .select(count(comment_report::id))
157 query.select(count(comment_report::id)).first::<i64>(conn)
162 pub struct CommentReportQueryBuilder<'a> {
163 conn: &'a PgConnection,
164 my_person_id: PersonId,
166 community_id: Option<CommunityId>,
169 unresolved_only: Option<bool>,
172 impl<'a> CommentReportQueryBuilder<'a> {
173 pub fn create(conn: &'a PgConnection, my_person_id: PersonId, admin: bool) -> Self {
174 CommentReportQueryBuilder {
181 unresolved_only: Some(true),
185 pub fn community_id<T: MaybeOptional<CommunityId>>(mut self, community_id: T) -> Self {
186 self.community_id = community_id.get_optional();
190 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
191 self.page = page.get_optional();
195 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
196 self.limit = limit.get_optional();
200 pub fn unresolved_only<T: MaybeOptional<bool>>(mut self, unresolved_only: T) -> Self {
201 self.unresolved_only = unresolved_only.get_optional();
205 pub fn list(self) -> Result<Vec<CommentReportView>, Error> {
206 let mut query = comment_report::table
207 .inner_join(comment::table)
208 .inner_join(post::table.on(comment::post_id.eq(post::id)))
209 .inner_join(community::table.on(post::community_id.eq(community::id)))
210 .inner_join(person::table.on(comment_report::creator_id.eq(person::id)))
211 .inner_join(person_alias_1::table.on(comment::creator_id.eq(person_alias_1::id)))
213 comment_aggregates::table.on(comment_report::comment_id.eq(comment_aggregates::comment_id)),
216 community_person_ban::table.on(
218 .eq(community_person_ban::community_id)
219 .and(community_person_ban::person_id.eq(comment::creator_id))
221 community_person_ban::expires
223 .or(community_person_ban::expires.gt(now)),
228 comment_like::table.on(
230 .eq(comment_like::comment_id)
231 .and(comment_like::person_id.eq(self.my_person_id)),
235 person_alias_2::table.on(comment_report::resolver_id.eq(person_alias_2::id.nullable())),
238 comment_report::all_columns,
239 comment::all_columns,
241 Community::safe_columns_tuple(),
242 Person::safe_columns_tuple(),
243 PersonAlias1::safe_columns_tuple(),
244 comment_aggregates::all_columns,
245 community_person_ban::all_columns.nullable(),
246 comment_like::score.nullable(),
247 PersonAlias2::safe_columns_tuple().nullable(),
251 if let Some(community_id) = self.community_id {
252 query = query.filter(post::community_id.eq(community_id));
255 if self.unresolved_only.unwrap_or(false) {
256 query = query.filter(comment_report::resolved.eq(false));
259 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
262 .order_by(comment_report::published.desc())
266 // If its not an admin, get only the ones you mod
267 let res = if !self.admin {
270 community_moderator::table.on(
271 community_moderator::community_id
272 .eq(post::community_id)
273 .and(community_moderator::person_id.eq(self.my_person_id)),
276 .load::<CommentReportViewTuple>(self.conn)?
278 query.load::<CommentReportViewTuple>(self.conn)?
281 Ok(CommentReportView::from_tuple_to_vec(res))
285 impl ViewToVec for CommentReportView {
286 type DbTuple = CommentReportViewTuple;
287 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
291 comment_report: a.0.to_owned(),
292 comment: a.1.to_owned(),
293 post: a.2.to_owned(),
294 community: a.3.to_owned(),
295 creator: a.4.to_owned(),
296 comment_creator: a.5.to_owned(),
297 counts: a.6.to_owned(),
298 creator_banned_from_community: a.7.is_some(),
300 resolver: a.9.to_owned(),
302 .collect::<Vec<Self>>()
308 use crate::comment_report_view::{CommentReportQueryBuilder, CommentReportView};
309 use lemmy_db_schema::{
310 aggregates::structs::CommentAggregates,
311 source::{comment::*, comment_report::*, community::*, person::*, post::*},
312 traits::{Crud, Joinable, Reportable},
313 utils::establish_unpooled_connection,
315 use serial_test::serial;
320 let conn = establish_unpooled_connection();
322 let new_person = PersonForm {
323 name: "timmy_crv".into(),
324 public_key: Some("pubkey".to_string()),
325 ..PersonForm::default()
328 let inserted_timmy = Person::create(&conn, &new_person).unwrap();
330 let new_person_2 = PersonForm {
331 name: "sara_crv".into(),
332 public_key: Some("pubkey".to_string()),
333 ..PersonForm::default()
336 let inserted_sara = Person::create(&conn, &new_person_2).unwrap();
338 // Add a third person, since new ppl can only report something once.
339 let new_person_3 = PersonForm {
340 name: "jessica_crv".into(),
341 public_key: Some("pubkey".to_string()),
342 ..PersonForm::default()
345 let inserted_jessica = Person::create(&conn, &new_person_3).unwrap();
347 let new_community = CommunityForm {
348 name: "test community crv".to_string(),
349 title: "nada".to_owned(),
350 public_key: Some("pubkey".to_string()),
351 ..CommunityForm::default()
354 let inserted_community = Community::create(&conn, &new_community).unwrap();
357 let timmy_moderator_form = CommunityModeratorForm {
358 community_id: inserted_community.id,
359 person_id: inserted_timmy.id,
362 let _inserted_moderator = CommunityModerator::join(&conn, &timmy_moderator_form).unwrap();
364 let new_post = PostForm {
365 name: "A test post crv".into(),
366 creator_id: inserted_timmy.id,
367 community_id: inserted_community.id,
368 ..PostForm::default()
371 let inserted_post = Post::create(&conn, &new_post).unwrap();
373 let comment_form = CommentForm {
374 content: "A test comment 32".into(),
375 creator_id: inserted_timmy.id,
376 post_id: inserted_post.id,
377 ..CommentForm::default()
380 let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
383 let sara_report_form = CommentReportForm {
384 creator_id: inserted_sara.id,
385 comment_id: inserted_comment.id,
386 original_comment_text: "this was it at time of creation".into(),
387 reason: "from sara".into(),
390 let inserted_sara_report = CommentReport::report(&conn, &sara_report_form).unwrap();
393 let jessica_report_form = CommentReportForm {
394 creator_id: inserted_jessica.id,
395 comment_id: inserted_comment.id,
396 original_comment_text: "this was it at time of creation".into(),
397 reason: "from jessica".into(),
400 let inserted_jessica_report = CommentReport::report(&conn, &jessica_report_form).unwrap();
402 let agg = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
404 let read_jessica_report_view =
405 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
406 let expected_jessica_report_view = CommentReportView {
407 comment_report: inserted_jessica_report.to_owned(),
408 comment: inserted_comment.to_owned(),
410 community: CommunitySafe {
411 id: inserted_community.id,
412 name: inserted_community.name,
417 actor_id: inserted_community.actor_id.to_owned(),
419 title: inserted_community.title,
424 posting_restricted_to_mods: false,
425 published: inserted_community.published,
427 creator: PersonSafe {
428 id: inserted_jessica.id,
429 name: inserted_jessica.name,
431 published: inserted_jessica.published,
433 actor_id: inserted_jessica.actor_id.to_owned(),
442 inbox_url: inserted_jessica.inbox_url.to_owned(),
443 shared_inbox_url: None,
444 matrix_user_id: None,
447 comment_creator: PersonSafeAlias1 {
448 id: inserted_timmy.id,
449 name: inserted_timmy.name.to_owned(),
451 published: inserted_timmy.published,
453 actor_id: inserted_timmy.actor_id.to_owned(),
462 inbox_url: inserted_timmy.inbox_url.to_owned(),
463 shared_inbox_url: None,
464 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,
505 // Do a batch read of timmys reports
506 let reports = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
513 expected_jessica_report_view.to_owned(),
514 expected_sara_report_view.to_owned()
518 // Make sure the counts are correct
520 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
521 assert_eq!(2, report_count);
523 // Try to resolve the report
524 CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
525 let read_jessica_report_view_after_resolve =
526 CommentReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
528 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
529 expected_jessica_report_view_after_resolve
532 expected_jessica_report_view_after_resolve
534 .resolver_id = Some(inserted_timmy.id);
535 expected_jessica_report_view_after_resolve
537 .updated = read_jessica_report_view_after_resolve
540 expected_jessica_report_view_after_resolve.resolver = Some(PersonSafeAlias2 {
541 id: inserted_timmy.id,
542 name: inserted_timmy.name.to_owned(),
544 published: inserted_timmy.published,
546 actor_id: inserted_timmy.actor_id.to_owned(),
555 inbox_url: inserted_timmy.inbox_url.to_owned(),
556 shared_inbox_url: None,
557 matrix_user_id: None,
562 read_jessica_report_view_after_resolve,
563 expected_jessica_report_view_after_resolve
566 // Do a batch read of timmys reports
567 // It should only show saras, which is unresolved
568 let reports_after_resolve = CommentReportQueryBuilder::create(&conn, inserted_timmy.id, false)
571 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
573 // Make sure the counts are correct
574 let report_count_after_resolved =
575 CommentReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
576 assert_eq!(1, report_count_after_resolved);
578 Person::delete(&conn, inserted_timmy.id).unwrap();
579 Person::delete(&conn, inserted_sara.id).unwrap();
580 Person::delete(&conn, inserted_jessica.id).unwrap();
581 Community::delete(&conn, inserted_community.id).unwrap();