Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding report_combined table. #5231

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ use std::io::Cursor;
use totp_rs::{Secret, TOTP};

pub mod comment;
pub mod comment_report;
pub mod community;
pub mod local_user;
pub mod post;
pub mod post_report;
pub mod private_message;
pub mod private_message_report;
pub mod reports;
pub mod site;
pub mod sitemap;

Expand Down
36 changes: 8 additions & 28 deletions crates/api/src/local_user/report_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ use lemmy_api_common::{
person::{GetReportCount, GetReportCountResponse},
utils::check_community_mod_of_any_or_admin_action,
};
use lemmy_db_views::structs::{
CommentReportView,
LocalUserView,
PostReportView,
PrivateMessageReportView,
};
use lemmy_db_views::structs::{LocalUserView, ReportCombinedViewInternal};
use lemmy_utils::error::LemmyResult;

#[tracing::instrument(skip(context))]
Expand All @@ -18,29 +13,14 @@ pub async fn report_count(
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> LemmyResult<Json<GetReportCountResponse>> {
let person_id = local_user_view.person.id;
let admin = local_user_view.local_user.admin;
let community_id = data.community_id;

check_community_mod_of_any_or_admin_action(&local_user_view, &mut context.pool()).await?;

let comment_reports =
CommentReportView::get_report_count(&mut context.pool(), person_id, admin, community_id)
.await?;

let post_reports =
PostReportView::get_report_count(&mut context.pool(), person_id, admin, community_id).await?;

let private_message_reports = if admin && community_id.is_none() {
Some(PrivateMessageReportView::get_report_count(&mut context.pool()).await?)
} else {
None
};
let count = ReportCombinedViewInternal::get_report_count(
&mut context.pool(),
&local_user_view,
data.community_id,
)
.await?;

Ok(Json(GetReportCountResponse {
community_id,
comment_reports,
post_reports,
private_message_reports,
}))
Ok(Json(GetReportCountResponse { count }))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::check_report_reason;
use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{
comment::{CommentReportResponse, CreateCommentReport},
context::LemmyContext,
reports::comment::{CommentReportResponse, CreateCommentReport},
send_activity::{ActivityChannel, SendActivityData},
utils::{
check_comment_deleted_or_removed,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
comment::{ListCommentReports, ListCommentReportsResponse},
context::LemmyContext,
reports::comment::{ListCommentReports, ListCommentReportsResponse},
utils::check_community_mod_of_any_or_admin_action,
};
use lemmy_db_views::{comment_report_view::CommentReportQuery, structs::LocalUserView};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::web::{Data, Json};
use lemmy_api_common::{
comment::{CommentReportResponse, ResolveCommentReport},
context::LemmyContext,
reports::comment::{CommentReportResponse, ResolveCommentReport},
utils::check_community_mod_action,
};
use lemmy_db_schema::{source::comment_report::CommentReport, traits::Reportable};
Expand Down
4 changes: 4 additions & 0 deletions crates/api/src/reports/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod comment_report;
pub mod post_report;
pub mod private_message_report;
pub mod report_combined;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use activitypub_federation::config::Data;
use actix_web::web::Json;
use lemmy_api_common::{
context::LemmyContext,
post::{CreatePostReport, PostReportResponse},
reports::post::{CreatePostReport, PostReportResponse},
send_activity::{ActivityChannel, SendActivityData},
utils::{
check_community_user_action,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
context::LemmyContext,
post::{ListPostReports, ListPostReportsResponse},
reports::post::{ListPostReports, ListPostReportsResponse},
utils::check_community_mod_of_any_or_admin_action,
};
use lemmy_db_views::{post_report_view::PostReportQuery, structs::LocalUserView};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::web::{Data, Json};
use lemmy_api_common::{
context::LemmyContext,
post::{PostReportResponse, ResolvePostReport},
reports::post::{PostReportResponse, ResolvePostReport},
utils::check_community_mod_action,
};
use lemmy_db_schema::{source::post_report::PostReport, traits::Reportable};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::check_report_reason;
use actix_web::web::{Data, Json};
use lemmy_api_common::{
context::LemmyContext,
private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse},
reports::private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse},
utils::send_new_report_email_to_admins,
};
use lemmy_db_schema::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
context::LemmyContext,
private_message::{ListPrivateMessageReports, ListPrivateMessageReportsResponse},
reports::private_message::{ListPrivateMessageReports, ListPrivateMessageReportsResponse},
utils::is_admin,
};
use lemmy_db_views::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::web::{Data, Json};
use lemmy_api_common::{
context::LemmyContext,
private_message::{PrivateMessageReportResponse, ResolvePrivateMessageReport},
reports::private_message::{PrivateMessageReportResponse, ResolvePrivateMessageReport},
utils::is_admin,
};
use lemmy_db_schema::{source::private_message_report::PrivateMessageReport, traits::Reportable};
Expand Down
35 changes: 35 additions & 0 deletions crates/api/src/reports/report_combined/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
context::LemmyContext,
reports::combined::{ListReports, ListReportsResponse},
utils::check_community_mod_of_any_or_admin_action,
};
use lemmy_db_views::{report_combined_view::ReportCombinedQuery, structs::LocalUserView};
use lemmy_utils::error::LemmyResult;

/// Lists reports for a community if an id is supplied
/// or returns all reports for communities a user moderates
#[tracing::instrument(skip(context))]
pub async fn list_reports(
data: Query<ListReports>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> LemmyResult<Json<ListReportsResponse>> {
let community_id = data.community_id;
let unresolved_only = data.unresolved_only.unwrap_or_default();

check_community_mod_of_any_or_admin_action(&local_user_view, &mut context.pool()).await?;

let page = data.page;
let limit = data.limit;
let reports = ReportCombinedQuery {
community_id,
unresolved_only,
page,
limit,
}
.list(&mut context.pool(), &local_user_view)
.await?;

Ok(Json(ListReportsResponse { reports }))
}
1 change: 1 addition & 0 deletions crates/api/src/reports/report_combined/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod list;
58 changes: 2 additions & 56 deletions crates/api_common/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use lemmy_db_schema::{
newtypes::{CommentId, CommentReportId, CommunityId, LanguageId, LocalUserId, PostId},
newtypes::{CommentId, CommunityId, LanguageId, LocalUserId, PostId},
CommentSortType,
ListingType,
};
use lemmy_db_views::structs::{CommentReportView, CommentView, VoteView};
use lemmy_db_views::structs::{CommentView, VoteView};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[cfg(feature = "full")]
Expand Down Expand Up @@ -146,60 +146,6 @@ pub struct GetCommentsResponse {
pub comments: Vec<CommentView>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Report a comment.
pub struct CreateCommentReport {
pub comment_id: CommentId,
pub reason: String,
}
Comment on lines -149 to -156
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moved these to a reports specific mod.


#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The comment report response.
pub struct CommentReportResponse {
pub comment_report_view: CommentReportView,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Resolve a comment report (only doable by mods).
pub struct ResolveCommentReport {
pub report_id: CommentReportId,
pub resolved: bool,
}

#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// List comment reports.
pub struct ListCommentReports {
#[cfg_attr(feature = "full", ts(optional))]
pub comment_id: Option<CommentId>,
#[cfg_attr(feature = "full", ts(optional))]
pub page: Option<i64>,
#[cfg_attr(feature = "full", ts(optional))]
pub limit: Option<i64>,
/// Only shows the unresolved reports
#[cfg_attr(feature = "full", ts(optional))]
pub unresolved_only: Option<bool>,
/// if no community is given, it returns reports for all communities moderated by the auth user
#[cfg_attr(feature = "full", ts(optional))]
pub community_id: Option<CommunityId>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The comment report list response.
pub struct ListCommentReportsResponse {
pub comment_reports: Vec<CommentReportView>,
}

#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
Expand Down
1 change: 1 addition & 0 deletions crates/api_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod oauth_provider;
pub mod person;
pub mod post;
pub mod private_message;
pub mod reports;
#[cfg(feature = "full")]
pub mod request;
#[cfg(feature = "full")]
Expand Down
7 changes: 1 addition & 6 deletions crates/api_common/src/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,7 @@ pub struct GetReportCount {
#[cfg_attr(feature = "full", ts(export))]
/// A response for the number of reports.
pub struct GetReportCountResponse {
#[cfg_attr(feature = "full", ts(optional))]
pub community_id: Option<CommunityId>,
pub comment_reports: i64,
pub post_reports: i64,
#[cfg_attr(feature = "full", ts(optional))]
pub private_message_reports: Option<i64>,
pub count: i64,
Comment on lines 450 to +451
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we now have a ReportsCombinedInternal::get_report_count(), it seems pointless to get each specific count, and make the front-ends add them, but I can revert this back if necessary.

}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
59 changes: 2 additions & 57 deletions crates/api_common/src/post.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId, PostReportId},
newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId},
ListingType,
PostFeatureType,
PostSortType,
};
use lemmy_db_views::structs::{PaginationCursor, PostReportView, PostView, VoteView};
use lemmy_db_views::structs::{PaginationCursor, PostView, VoteView};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
Expand Down Expand Up @@ -247,61 +247,6 @@ pub struct SavePost {
pub save: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Create a post report.
pub struct CreatePostReport {
pub post_id: PostId,
pub reason: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The post report response.
pub struct PostReportResponse {
pub post_report_view: PostReportView,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Resolve a post report (mods only).
pub struct ResolvePostReport {
pub report_id: PostReportId,
pub resolved: bool,
}

#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// List post reports.
pub struct ListPostReports {
#[cfg_attr(feature = "full", ts(optional))]
pub page: Option<i64>,
#[cfg_attr(feature = "full", ts(optional))]
pub limit: Option<i64>,
/// Only shows the unresolved reports
#[cfg_attr(feature = "full", ts(optional))]
pub unresolved_only: Option<bool>,
// TODO make into tagged enum at some point
/// if no community is given, it returns reports for all communities moderated by the auth user
#[cfg_attr(feature = "full", ts(optional))]
pub community_id: Option<CommunityId>,
#[cfg_attr(feature = "full", ts(optional))]
pub post_id: Option<PostId>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The post reports response.
pub struct ListPostReportsResponse {
pub post_reports: Vec<PostReportView>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
Expand Down
Loading