2021-03-25 19:19:40 +00:00
|
|
|
use crate::Perform;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
|
|
|
check_community_ban,
|
|
|
|
get_local_user_view_from_jwt,
|
|
|
|
is_mod_or_admin,
|
|
|
|
post::{
|
|
|
|
CreatePostReport,
|
|
|
|
ListPostReports,
|
|
|
|
ListPostReportsResponse,
|
2021-09-28 10:36:17 +00:00
|
|
|
PostReportResponse,
|
2021-03-25 19:19:40 +00:00
|
|
|
ResolvePostReport,
|
|
|
|
},
|
|
|
|
};
|
2021-10-15 14:37:33 +00:00
|
|
|
use lemmy_apub::{activities::report::Report, fetcher::object_id::ObjectId};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::post_report::{PostReport, PostReportForm},
|
|
|
|
traits::Reportable,
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_db_views::{
|
|
|
|
post_report_view::{PostReportQueryBuilder, PostReportView},
|
|
|
|
post_view::PostView,
|
|
|
|
};
|
|
|
|
use lemmy_utils::{ApiError, ConnectionId, LemmyError};
|
2021-09-28 10:36:17 +00:00
|
|
|
use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
/// Creates a post report and notifies the moderators of the community
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for CreatePostReport {
|
2021-09-28 10:36:17 +00:00
|
|
|
type Response = PostReportResponse;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
2021-09-28 10:36:17 +00:00
|
|
|
) -> Result<PostReportResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &CreatePostReport = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// check size of report and check for whitespace
|
|
|
|
let reason = data.reason.trim();
|
|
|
|
if reason.is_empty() {
|
2021-10-13 19:50:21 +00:00
|
|
|
return Err(ApiError::err_plain("report_reason_required").into());
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
if reason.chars().count() > 1000 {
|
2021-10-13 19:50:21 +00:00
|
|
|
return Err(ApiError::err_plain("report_too_long").into());
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let post_id = data.post_id;
|
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2021-07-05 16:07:26 +00:00
|
|
|
PostView::read(conn, post_id, None)
|
2021-03-25 19:19:40 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
check_community_ban(person_id, post_view.community.id, context.pool()).await?;
|
|
|
|
|
|
|
|
let report_form = PostReportForm {
|
|
|
|
creator_id: person_id,
|
|
|
|
post_id,
|
|
|
|
original_post_name: post_view.post.name,
|
|
|
|
original_post_url: post_view.post.url,
|
|
|
|
original_post_body: post_view.post.body,
|
|
|
|
reason: data.reason.to_owned(),
|
|
|
|
};
|
|
|
|
|
2021-04-16 13:10:43 +00:00
|
|
|
let report = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
PostReport::report(conn, &report_form)
|
|
|
|
})
|
|
|
|
.await?
|
2021-10-13 19:50:21 +00:00
|
|
|
.map_err(|e| ApiError::err("couldnt_create_report", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-09-28 10:36:17 +00:00
|
|
|
let post_report_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostReportView::read(conn, report.id, person_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-09-28 10:36:17 +00:00
|
|
|
let res = PostReportResponse { post_report_view };
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
context.chat_server().do_send(SendModRoomMessage {
|
|
|
|
op: UserOperation::CreatePostReport,
|
2021-09-28 10:36:17 +00:00
|
|
|
response: res.clone(),
|
2021-03-25 19:19:40 +00:00
|
|
|
community_id: post_view.community.id,
|
|
|
|
websocket_id,
|
|
|
|
});
|
|
|
|
|
2021-10-15 14:37:33 +00:00
|
|
|
Report::send(
|
|
|
|
ObjectId::new(post_view.post.ap_id),
|
|
|
|
&local_user_view.person,
|
|
|
|
post_view.community.id,
|
|
|
|
reason.to_string(),
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolves or unresolves a post report and notifies the moderators of the community
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for ResolvePostReport {
|
2021-09-28 10:36:17 +00:00
|
|
|
type Response = PostReportResponse;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
2021-09-28 10:36:17 +00:00
|
|
|
) -> Result<PostReportResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &ResolvePostReport = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let report_id = data.report_id;
|
2021-09-28 10:36:17 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2021-03-25 19:19:40 +00:00
|
|
|
let report = blocking(context.pool(), move |conn| {
|
2021-09-28 10:36:17 +00:00
|
|
|
PostReportView::read(conn, report_id, person_id)
|
2021-03-25 19:19:40 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
|
|
|
|
|
|
|
|
let resolved = data.resolved;
|
|
|
|
let resolve_fun = move |conn: &'_ _| {
|
|
|
|
if resolved {
|
|
|
|
PostReport::resolve(conn, report_id, person_id)
|
|
|
|
} else {
|
|
|
|
PostReport::unresolve(conn, report_id, person_id)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), resolve_fun)
|
|
|
|
.await?
|
|
|
|
.map_err(|e| ApiError::err("couldnt_resolve_report", e))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-09-28 10:36:17 +00:00
|
|
|
let post_report_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostReportView::read(conn, report_id, person_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = PostReportResponse { post_report_view };
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
context.chat_server().do_send(SendModRoomMessage {
|
|
|
|
op: UserOperation::ResolvePostReport,
|
|
|
|
response: res.clone(),
|
|
|
|
community_id: report.community.id,
|
|
|
|
websocket_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lists post reports for a community if an id is supplied
|
|
|
|
/// or returns all post reports for communities a user moderates
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for ListPostReports {
|
|
|
|
type Response = ListPostReportsResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
2021-09-28 10:36:17 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2021-03-25 19:19:40 +00:00
|
|
|
) -> Result<ListPostReportsResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &ListPostReports = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let person_id = local_user_view.person.id;
|
2021-10-12 12:02:16 +00:00
|
|
|
let admin = local_user_view.person.admin;
|
2021-09-28 10:36:17 +00:00
|
|
|
let community_id = data.community_id;
|
|
|
|
let unresolved_only = data.unresolved_only;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
2021-09-28 10:36:17 +00:00
|
|
|
let post_reports = blocking(context.pool(), move |conn| {
|
2021-10-12 12:02:16 +00:00
|
|
|
PostReportQueryBuilder::create(conn, person_id, admin)
|
2021-09-28 10:36:17 +00:00
|
|
|
.community_id(community_id)
|
|
|
|
.unresolved_only(unresolved_only)
|
2021-03-25 19:19:40 +00:00
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-09-28 10:36:17 +00:00
|
|
|
let res = ListPostReportsResponse { post_reports };
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|