1 use diesel::{result::Error, *};
3 aggregates::post_aggregates::PostAggregates,
5 newtypes::{CommunityId, PersonId, PostReportId},
19 community::{Community, CommunityPersonBan, CommunitySafe},
20 person::{Person, PersonAlias1, PersonAlias2, PersonSafe, PersonSafeAlias1, PersonSafeAlias2},
22 post_report::PostReport,
24 traits::{MaybeOptional, ToSafe, ViewToVec},
26 use serde::{Deserialize, Serialize};
28 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
29 pub struct PostReportView {
30 pub post_report: PostReport,
32 pub community: CommunitySafe,
33 pub creator: PersonSafe,
34 pub post_creator: PersonSafeAlias1,
35 pub creator_banned_from_community: bool,
36 pub my_vote: Option<i16>,
37 pub counts: PostAggregates,
38 pub resolver: Option<PersonSafeAlias2>,
41 type PostReportViewTuple = (
47 Option<CommunityPersonBan>,
50 Option<PersonSafeAlias2>,
54 /// returns the PostReportView for the provided report_id
56 /// * `report_id` - the report id to obtain
59 report_id: PostReportId,
60 my_person_id: PersonId,
61 ) -> Result<Self, Error> {
68 creator_banned_from_community,
72 ) = post_report::table
74 .inner_join(post::table)
75 .inner_join(community::table.on(post::community_id.eq(community::id)))
76 .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
77 .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id)))
79 community_person_ban::table.on(
81 .eq(community_person_ban::community_id)
82 .and(community_person_ban::person_id.eq(post::creator_id)),
88 .eq(post_like::post_id)
89 .and(post_like::person_id.eq(my_person_id)),
92 .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
94 person_alias_2::table.on(post_report::resolver_id.eq(person_alias_2::id.nullable())),
97 post_report::all_columns,
99 Community::safe_columns_tuple(),
100 Person::safe_columns_tuple(),
101 PersonAlias1::safe_columns_tuple(),
102 community_person_ban::all_columns.nullable(),
103 post_like::score.nullable(),
104 post_aggregates::all_columns,
105 PersonAlias2::safe_columns_tuple().nullable(),
107 .first::<PostReportViewTuple>(conn)?;
109 let my_vote = if post_like.is_none() { None } else { post_like };
117 creator_banned_from_community: creator_banned_from_community.is_some(),
124 /// returns the current unresolved post report count for the communities you mod
125 pub fn get_report_count(
127 my_person_id: PersonId,
129 community_id: Option<CommunityId>,
130 ) -> Result<i64, Error> {
132 let mut query = post_report::table
133 .inner_join(post::table)
134 .filter(post_report::resolved.eq(false))
137 if let Some(community_id) = community_id {
138 query = query.filter(post::community_id.eq(community_id))
141 // If its not an admin, get only the ones you mod
145 community_moderator::table.on(
146 community_moderator::community_id
147 .eq(post::community_id)
148 .and(community_moderator::person_id.eq(my_person_id)),
151 .select(count(post_report::id))
154 query.select(count(post_report::id)).first::<i64>(conn)
159 pub struct PostReportQueryBuilder<'a> {
160 conn: &'a PgConnection,
161 my_person_id: PersonId,
163 community_id: Option<CommunityId>,
166 unresolved_only: Option<bool>,
169 impl<'a> PostReportQueryBuilder<'a> {
170 pub fn create(conn: &'a PgConnection, my_person_id: PersonId, admin: bool) -> Self {
171 PostReportQueryBuilder {
178 unresolved_only: Some(true),
182 pub fn community_id<T: MaybeOptional<CommunityId>>(mut self, community_id: T) -> Self {
183 self.community_id = community_id.get_optional();
187 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
188 self.page = page.get_optional();
192 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
193 self.limit = limit.get_optional();
197 pub fn unresolved_only<T: MaybeOptional<bool>>(mut self, unresolved_only: T) -> Self {
198 self.unresolved_only = unresolved_only.get_optional();
202 pub fn list(self) -> Result<Vec<PostReportView>, Error> {
203 let mut query = post_report::table
204 .inner_join(post::table)
205 .inner_join(community::table.on(post::community_id.eq(community::id)))
206 .inner_join(person::table.on(post_report::creator_id.eq(person::id)))
207 .inner_join(person_alias_1::table.on(post::creator_id.eq(person_alias_1::id)))
209 community_person_ban::table.on(
211 .eq(community_person_ban::community_id)
212 .and(community_person_ban::person_id.eq(post::creator_id)),
218 .eq(post_like::post_id)
219 .and(post_like::person_id.eq(self.my_person_id)),
222 .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
224 person_alias_2::table.on(post_report::resolver_id.eq(person_alias_2::id.nullable())),
227 post_report::all_columns,
229 Community::safe_columns_tuple(),
230 Person::safe_columns_tuple(),
231 PersonAlias1::safe_columns_tuple(),
232 community_person_ban::all_columns.nullable(),
233 post_like::score.nullable(),
234 post_aggregates::all_columns,
235 PersonAlias2::safe_columns_tuple().nullable(),
239 if let Some(community_id) = self.community_id {
240 query = query.filter(post::community_id.eq(community_id));
243 if self.unresolved_only.unwrap_or(false) {
244 query = query.filter(post_report::resolved.eq(false));
247 let (limit, offset) = limit_and_offset(self.page, self.limit);
250 .order_by(post_report::published.desc())
254 // If its not an admin, get only the ones you mod
255 let res = if !self.admin {
258 community_moderator::table.on(
259 community_moderator::community_id
260 .eq(post::community_id)
261 .and(community_moderator::person_id.eq(self.my_person_id)),
264 .load::<PostReportViewTuple>(self.conn)?
266 query.load::<PostReportViewTuple>(self.conn)?
269 Ok(PostReportView::from_tuple_to_vec(res))
273 impl ViewToVec for PostReportView {
274 type DbTuple = PostReportViewTuple;
275 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
279 post_report: a.0.to_owned(),
280 post: a.1.to_owned(),
281 community: a.2.to_owned(),
282 creator: a.3.to_owned(),
283 post_creator: a.4.to_owned(),
284 creator_banned_from_community: a.5.is_some(),
286 counts: a.7.to_owned(),
287 resolver: a.8.to_owned(),
289 .collect::<Vec<Self>>()
295 use crate::post_report_view::{PostReportQueryBuilder, PostReportView};
296 use lemmy_db_schema::{
297 aggregates::post_aggregates::PostAggregates,
298 establish_unpooled_connection,
303 post_report::{PostReport, PostReportForm},
305 traits::{Crud, Joinable, Reportable},
307 use serial_test::serial;
312 let conn = establish_unpooled_connection();
314 let new_person = PersonForm {
315 name: "timmy_prv".into(),
316 ..PersonForm::default()
319 let inserted_timmy = Person::create(&conn, &new_person).unwrap();
321 let new_person_2 = PersonForm {
322 name: "sara_prv".into(),
323 ..PersonForm::default()
326 let inserted_sara = Person::create(&conn, &new_person_2).unwrap();
328 // Add a third person, since new ppl can only report something once.
329 let new_person_3 = PersonForm {
330 name: "jessica_prv".into(),
331 ..PersonForm::default()
334 let inserted_jessica = Person::create(&conn, &new_person_3).unwrap();
336 let new_community = CommunityForm {
337 name: "test community prv".to_string(),
338 title: "nada".to_owned(),
339 ..CommunityForm::default()
342 let inserted_community = Community::create(&conn, &new_community).unwrap();
345 let timmy_moderator_form = CommunityModeratorForm {
346 community_id: inserted_community.id,
347 person_id: inserted_timmy.id,
350 let _inserted_moderator = CommunityModerator::join(&conn, &timmy_moderator_form).unwrap();
352 let new_post = PostForm {
353 name: "A test post crv".into(),
354 creator_id: inserted_timmy.id,
355 community_id: inserted_community.id,
356 ..PostForm::default()
359 let inserted_post = Post::create(&conn, &new_post).unwrap();
362 let sara_report_form = PostReportForm {
363 creator_id: inserted_sara.id,
364 post_id: inserted_post.id,
365 original_post_name: "Orig post".into(),
366 original_post_url: None,
367 original_post_body: None,
368 reason: "from sara".into(),
371 let inserted_sara_report = PostReport::report(&conn, &sara_report_form).unwrap();
374 let jessica_report_form = PostReportForm {
375 creator_id: inserted_jessica.id,
376 post_id: inserted_post.id,
377 original_post_name: "Orig post".into(),
378 original_post_url: None,
379 original_post_body: None,
380 reason: "from jessica".into(),
383 let inserted_jessica_report = PostReport::report(&conn, &jessica_report_form).unwrap();
385 let agg = PostAggregates::read(&conn, inserted_post.id).unwrap();
387 let read_jessica_report_view =
388 PostReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
389 let expected_jessica_report_view = PostReportView {
390 post_report: inserted_jessica_report.to_owned(),
391 post: inserted_post.to_owned(),
392 community: CommunitySafe {
393 id: inserted_community.id,
394 name: inserted_community.name,
399 actor_id: inserted_community.actor_id.to_owned(),
401 title: inserted_community.title,
405 published: inserted_community.published,
407 creator: PersonSafe {
408 id: inserted_jessica.id,
409 name: inserted_jessica.name,
411 published: inserted_jessica.published,
413 actor_id: inserted_jessica.actor_id.to_owned(),
422 inbox_url: inserted_jessica.inbox_url.to_owned(),
423 shared_inbox_url: None,
424 matrix_user_id: None,
426 post_creator: PersonSafeAlias1 {
427 id: inserted_timmy.id,
428 name: inserted_timmy.name.to_owned(),
430 published: inserted_timmy.published,
432 actor_id: inserted_timmy.actor_id.to_owned(),
441 inbox_url: inserted_timmy.inbox_url.to_owned(),
442 shared_inbox_url: None,
443 matrix_user_id: None,
445 creator_banned_from_community: false,
447 counts: PostAggregates {
449 post_id: inserted_post.id,
455 published: agg.published,
456 newest_comment_time_necro: inserted_post.published,
457 newest_comment_time: inserted_post.published,
462 assert_eq!(read_jessica_report_view, expected_jessica_report_view);
464 let mut expected_sara_report_view = expected_jessica_report_view.clone();
465 expected_sara_report_view.post_report = inserted_sara_report;
466 expected_sara_report_view.my_vote = None;
467 expected_sara_report_view.creator = PersonSafe {
468 id: inserted_sara.id,
469 name: inserted_sara.name,
471 published: inserted_sara.published,
473 actor_id: inserted_sara.actor_id.to_owned(),
482 inbox_url: inserted_sara.inbox_url.to_owned(),
483 shared_inbox_url: None,
484 matrix_user_id: None,
487 // Do a batch read of timmys reports
488 let reports = PostReportQueryBuilder::create(&conn, inserted_timmy.id, false)
495 expected_jessica_report_view.to_owned(),
496 expected_sara_report_view.to_owned()
500 // Make sure the counts are correct
502 PostReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
503 assert_eq!(2, report_count);
505 // Try to resolve the report
506 PostReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
507 let read_jessica_report_view_after_resolve =
508 PostReportView::read(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
510 let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;
511 expected_jessica_report_view_after_resolve
514 expected_jessica_report_view_after_resolve
516 .resolver_id = Some(inserted_timmy.id);
517 expected_jessica_report_view_after_resolve
519 .updated = read_jessica_report_view_after_resolve.post_report.updated;
520 expected_jessica_report_view_after_resolve.resolver = Some(PersonSafeAlias2 {
521 id: inserted_timmy.id,
522 name: inserted_timmy.name.to_owned(),
524 published: inserted_timmy.published,
526 actor_id: inserted_timmy.actor_id.to_owned(),
535 inbox_url: inserted_timmy.inbox_url.to_owned(),
536 shared_inbox_url: None,
537 matrix_user_id: None,
541 read_jessica_report_view_after_resolve,
542 expected_jessica_report_view_after_resolve
545 // Do a batch read of timmys reports
546 // It should only show saras, which is unresolved
547 let reports_after_resolve = PostReportQueryBuilder::create(&conn, inserted_timmy.id, false)
550 assert_eq!(reports_after_resolve[0], expected_sara_report_view);
552 // Make sure the counts are correct
553 let report_count_after_resolved =
554 PostReportView::get_report_count(&conn, inserted_timmy.id, false, None).unwrap();
555 assert_eq!(1, report_count_after_resolved);
557 Person::delete(&conn, inserted_timmy.id).unwrap();
558 Person::delete(&conn, inserted_sara.id).unwrap();
559 Person::delete(&conn, inserted_jessica.id).unwrap();
560 Community::delete(&conn, inserted_community.id).unwrap();