2 use actix_web::web::Data;
3 use lemmy_api_common::{
7 get_local_user_view_from_jwt,
10 use lemmy_apub::protocol::activities::community::report::Report;
11 use lemmy_apub_lib::object_id::ObjectId;
12 use lemmy_db_schema::{source::comment_report::*, traits::Reportable};
14 comment_report_view::{CommentReportQueryBuilder, CommentReportView},
15 comment_view::CommentView,
17 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
18 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
20 /// Creates a comment report and notifies the moderators of the community
21 #[async_trait::async_trait(?Send)]
22 impl Perform for CreateCommentReport {
23 type Response = CommentReportResponse;
27 context: &Data<LemmyContext>,
28 websocket_id: Option<ConnectionId>,
29 ) -> Result<CommentReportResponse, LemmyError> {
30 let data: &CreateCommentReport = self;
32 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 // check size of report and check for whitespace
35 let reason = data.reason.trim();
36 if reason.is_empty() {
37 return Err(ApiError::err_plain("report_reason_required").into());
39 if reason.chars().count() > 1000 {
40 return Err(ApiError::err_plain("report_too_long").into());
43 let person_id = local_user_view.person.id;
44 let comment_id = data.comment_id;
45 let comment_view = blocking(context.pool(), move |conn| {
46 CommentView::read(conn, comment_id, None)
50 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
52 let report_form = CommentReportForm {
53 creator_id: person_id,
55 original_comment_text: comment_view.comment.content,
56 reason: data.reason.to_owned(),
59 let report = blocking(context.pool(), move |conn| {
60 CommentReport::report(conn, &report_form)
63 .map_err(|e| ApiError::err("couldnt_create_report", e))?;
65 let comment_report_view = blocking(context.pool(), move |conn| {
66 CommentReportView::read(conn, report.id, person_id)
70 let res = CommentReportResponse {
74 context.chat_server().do_send(SendModRoomMessage {
75 op: UserOperation::CreateCommentReport,
76 response: res.clone(),
77 community_id: comment_view.community.id,
82 ObjectId::new(comment_view.comment.ap_id),
83 &local_user_view.person.into(),
84 ObjectId::new(comment_view.community.actor_id),
94 /// Resolves or unresolves a comment report and notifies the moderators of the community
95 #[async_trait::async_trait(?Send)]
96 impl Perform for ResolveCommentReport {
97 type Response = CommentReportResponse;
101 context: &Data<LemmyContext>,
102 websocket_id: Option<ConnectionId>,
103 ) -> Result<CommentReportResponse, LemmyError> {
104 let data: &ResolveCommentReport = self;
105 let local_user_view =
106 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
108 let report_id = data.report_id;
109 let person_id = local_user_view.person.id;
110 let report = blocking(context.pool(), move |conn| {
111 CommentReportView::read(conn, report_id, person_id)
115 let person_id = local_user_view.person.id;
116 is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
118 let resolved = data.resolved;
119 let resolve_fun = move |conn: &'_ _| {
121 CommentReport::resolve(conn, report_id, person_id)
123 CommentReport::unresolve(conn, report_id, person_id)
127 blocking(context.pool(), resolve_fun)
129 .map_err(|e| ApiError::err("couldnt_resolve_report", e))?;
131 let report_id = data.report_id;
132 let comment_report_view = blocking(context.pool(), move |conn| {
133 CommentReportView::read(conn, report_id, person_id)
137 let res = CommentReportResponse {
141 context.chat_server().do_send(SendModRoomMessage {
142 op: UserOperation::ResolveCommentReport,
143 response: res.clone(),
144 community_id: report.community.id,
152 /// Lists comment reports for a community if an id is supplied
153 /// or returns all comment reports for communities a user moderates
154 #[async_trait::async_trait(?Send)]
155 impl Perform for ListCommentReports {
156 type Response = ListCommentReportsResponse;
160 context: &Data<LemmyContext>,
161 _websocket_id: Option<ConnectionId>,
162 ) -> Result<ListCommentReportsResponse, LemmyError> {
163 let data: &ListCommentReports = self;
164 let local_user_view =
165 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
167 let person_id = local_user_view.person.id;
168 let admin = local_user_view.person.admin;
169 let community_id = data.community_id;
170 let unresolved_only = data.unresolved_only;
172 let page = data.page;
173 let limit = data.limit;
174 let comment_reports = blocking(context.pool(), move |conn| {
175 CommentReportQueryBuilder::create(conn, person_id, admin)
176 .community_id(community_id)
177 .unresolved_only(unresolved_only)
184 let res = ListCommentReportsResponse { comment_reports };