2021-11-03 17:33:51 +00:00
|
|
|
use crate::{
|
2022-06-02 14:33:41 +00:00
|
|
|
activities::{generate_activity_id, send_lemmy_activity, verify_person_in_community},
|
2023-03-21 15:03:05 +00:00
|
|
|
insert_activity,
|
2021-11-03 17:33:51 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
2022-12-01 20:52:49 +00:00
|
|
|
protocol::{activities::community::report::Report, InCommunity},
|
2021-11-03 17:33:51 +00:00
|
|
|
PostOrComment,
|
2022-11-28 14:29:33 +00:00
|
|
|
SendActivity,
|
2021-11-03 17:33:51 +00:00
|
|
|
};
|
2022-06-08 15:45:39 +00:00
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::activity::FlagType,
|
2022-06-08 15:45:39 +00:00
|
|
|
traits::{ActivityHandler, Actor},
|
|
|
|
};
|
2022-11-26 02:04:46 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
comment::{CommentReportResponse, CreateCommentReport},
|
|
|
|
context::LemmyContext,
|
|
|
|
post::{CreatePostReport, PostReportResponse},
|
|
|
|
utils::get_local_user_view_from_jwt,
|
2022-12-09 15:31:47 +00:00
|
|
|
websocket::UserOperation,
|
2022-11-26 02:04:46 +00:00
|
|
|
};
|
2021-10-15 14:37:33 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
comment_report::{CommentReport, CommentReportForm},
|
|
|
|
post_report::{PostReport, PostReportForm},
|
|
|
|
},
|
2021-10-18 21:36:44 +00:00
|
|
|
traits::Reportable,
|
2021-10-15 14:37:33 +00:00
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::structs::{CommentReportView, PostReportView};
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
use url::Url;
|
2021-10-15 14:37:33 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-11-28 14:29:33 +00:00
|
|
|
impl SendActivity for CreatePostReport {
|
|
|
|
type Response = PostReportResponse;
|
|
|
|
|
|
|
|
async fn send_activity(
|
|
|
|
request: &Self,
|
|
|
|
response: &Self::Response,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2022-11-28 14:29:33 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
|
|
|
|
Report::send(
|
2023-03-21 15:03:05 +00:00
|
|
|
ObjectId::from(response.post_report_view.post.ap_id.clone()),
|
2022-11-28 14:29:33 +00:00
|
|
|
&local_user_view.person.into(),
|
2023-03-21 15:03:05 +00:00
|
|
|
ObjectId::from(response.post_report_view.community.actor_id.clone()),
|
2022-11-28 14:29:33 +00:00
|
|
|
request.reason.to_string(),
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-11-28 14:29:33 +00:00
|
|
|
impl SendActivity for CreateCommentReport {
|
|
|
|
type Response = CommentReportResponse;
|
|
|
|
|
|
|
|
async fn send_activity(
|
|
|
|
request: &Self,
|
|
|
|
response: &Self::Response,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2022-11-28 14:29:33 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
|
|
|
|
Report::send(
|
2023-03-21 15:03:05 +00:00
|
|
|
ObjectId::from(response.comment_report_view.comment.ap_id.clone()),
|
2022-11-28 14:29:33 +00:00
|
|
|
&local_user_view.person.into(),
|
2023-03-21 15:03:05 +00:00
|
|
|
ObjectId::from(response.comment_report_view.community.actor_id.clone()),
|
2022-11-28 14:29:33 +00:00
|
|
|
request.reason.to_string(),
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 14:37:33 +00:00
|
|
|
impl Report {
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2022-11-28 14:29:33 +00:00
|
|
|
async fn send(
|
2021-10-15 14:37:33 +00:00
|
|
|
object_id: ObjectId<PostOrComment>,
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
|
|
|
community_id: ObjectId<ApubCommunity>,
|
2021-10-15 14:37:33 +00:00
|
|
|
reason: String,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2021-10-15 14:37:33 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2022-06-08 15:45:39 +00:00
|
|
|
let community = community_id.dereference_local(context).await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
let kind = FlagType::Flag;
|
|
|
|
let id = generate_activity_id(
|
|
|
|
kind.clone(),
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?;
|
|
|
|
let report = Report {
|
2023-03-21 15:03:05 +00:00
|
|
|
actor: actor.id().into(),
|
|
|
|
to: [community.id().into()],
|
2021-10-15 14:37:33 +00:00
|
|
|
object: object_id,
|
|
|
|
summary: reason,
|
|
|
|
kind,
|
|
|
|
id: id.clone(),
|
2023-03-21 15:03:05 +00:00
|
|
|
audience: Some(community.id().into()),
|
2021-10-15 14:37:33 +00:00
|
|
|
};
|
2022-06-08 15:45:39 +00:00
|
|
|
|
|
|
|
let inbox = vec![community.shared_inbox_or_inbox()];
|
|
|
|
send_lemmy_activity(context, report, actor, inbox, false).await
|
2021-10-15 14:37:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2021-10-15 14:37:33 +00:00
|
|
|
impl ActivityHandler for Report {
|
|
|
|
type DataType = LemmyContext;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
|
|
|
let community = self.community(context).await?;
|
|
|
|
verify_person_in_community(&self.actor, &community, context).await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn receive(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
|
|
|
insert_activity(&self.id, &self, false, true, context).await?;
|
|
|
|
let actor = self.actor.dereference(context).await?;
|
|
|
|
match self.object.dereference(context).await? {
|
2021-10-15 14:37:33 +00:00
|
|
|
PostOrComment::Post(post) => {
|
|
|
|
let report_form = PostReportForm {
|
|
|
|
creator_id: actor.id,
|
|
|
|
post_id: post.id,
|
2021-10-18 21:36:44 +00:00
|
|
|
original_post_name: post.name.clone(),
|
|
|
|
original_post_url: post.url.clone(),
|
2021-10-15 14:37:33 +00:00
|
|
|
reason: self.summary,
|
2021-10-18 21:36:44 +00:00
|
|
|
original_post_body: post.body.clone(),
|
2021-10-15 14:37:33 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let report = PostReport::report(context.pool(), &report_form).await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let post_report_view = PostReportView::read(context.pool(), report.id, actor.id).await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
|
2022-12-09 15:31:47 +00:00
|
|
|
context
|
|
|
|
.chat_server()
|
|
|
|
.send_mod_room_message(
|
|
|
|
UserOperation::CreateCommentReport,
|
|
|
|
&PostReportResponse { post_report_view },
|
|
|
|
post.community_id,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
}
|
|
|
|
PostOrComment::Comment(comment) => {
|
|
|
|
let report_form = CommentReportForm {
|
|
|
|
creator_id: actor.id,
|
|
|
|
comment_id: comment.id,
|
2021-10-18 21:36:44 +00:00
|
|
|
original_comment_text: comment.content.clone(),
|
2021-10-15 14:37:33 +00:00
|
|
|
reason: self.summary,
|
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let report = CommentReport::report(context.pool(), &report_form).await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let comment_report_view =
|
|
|
|
CommentReportView::read(context.pool(), report.id, actor.id).await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
let community_id = comment_report_view.community.id;
|
|
|
|
|
2022-12-09 15:31:47 +00:00
|
|
|
context
|
|
|
|
.chat_server()
|
|
|
|
.send_mod_room_message(
|
|
|
|
UserOperation::CreateCommentReport,
|
|
|
|
&CommentReportResponse {
|
|
|
|
comment_report_view,
|
|
|
|
},
|
|
|
|
community_id,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|