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},
|
2022-10-27 09:24:07 +00:00
|
|
|
check_apub_id_valid,
|
|
|
|
fetch_local_site_data,
|
2022-06-02 14:33:41 +00:00
|
|
|
local_instance,
|
2021-11-03 17:33:51 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
|
|
|
protocol::activities::community::report::Report,
|
2022-06-02 14:33:41 +00:00
|
|
|
ActorType,
|
2021-11-03 17:33:51 +00:00
|
|
|
PostOrComment,
|
|
|
|
};
|
2022-06-08 15:45:39 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
core::object_id::ObjectId,
|
|
|
|
data::Data,
|
|
|
|
traits::{ActivityHandler, Actor},
|
|
|
|
};
|
2021-11-19 17:47:06 +00:00
|
|
|
use activitystreams_kinds::activity::FlagType;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_api_common::{comment::CommentReportResponse, post::PostReportResponse, utils::blocking};
|
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;
|
2021-10-15 14:37:33 +00:00
|
|
|
use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
|
2022-06-02 14:33:41 +00:00
|
|
|
use url::Url;
|
2021-10-15 14:37:33 +00:00
|
|
|
|
|
|
|
impl Report {
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-15 14:37:33 +00:00
|
|
|
pub async fn send(
|
|
|
|
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,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> 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 {
|
|
|
|
actor: ObjectId::new(actor.actor_id()),
|
2022-06-08 15:45:39 +00:00
|
|
|
to: [ObjectId::new(community.actor_id())],
|
2021-10-15 14:37:33 +00:00
|
|
|
object: object_id,
|
|
|
|
summary: reason,
|
|
|
|
kind,
|
|
|
|
id: id.clone(),
|
|
|
|
unparsed: Default::default(),
|
|
|
|
};
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
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)]
|
2021-10-15 14:37:33 +00:00
|
|
|
async fn verify(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-10-27 09:24:07 +00:00
|
|
|
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
|
|
|
|
check_apub_id_valid(self.id(), &local_site_data, context.settings())
|
|
|
|
.map_err(LemmyError::from_message)?;
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
let community = self.to[0]
|
|
|
|
.dereference(context, local_instance(context), request_counter)
|
2021-12-06 22:54:34 +00:00
|
|
|
.await?;
|
2021-10-28 11:46:48 +00:00
|
|
|
verify_person_in_community(&self.actor, &community, context, request_counter).await?;
|
2021-10-15 14:37:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-15 14:37:33 +00:00
|
|
|
async fn receive(
|
|
|
|
self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-12-06 22:54:34 +00:00
|
|
|
let actor = self
|
|
|
|
.actor
|
2022-06-08 15:45:39 +00:00
|
|
|
.dereference(context, local_instance(context), request_counter)
|
2021-12-06 22:54:34 +00:00
|
|
|
.await?;
|
|
|
|
match self
|
|
|
|
.object
|
2022-06-08 15:45:39 +00:00
|
|
|
.dereference(context, local_instance(context), request_counter)
|
2021-12-06 22:54:34 +00:00
|
|
|
.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
|
|
|
};
|
|
|
|
|
|
|
|
let report = blocking(context.pool(), move |conn| {
|
|
|
|
PostReport::report(conn, &report_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let post_report_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostReportView::read(conn, report.id, actor.id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendModRoomMessage {
|
|
|
|
op: UserOperation::CreateCommentReport,
|
|
|
|
response: PostReportResponse { post_report_view },
|
|
|
|
community_id: post.community_id,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
let report = blocking(context.pool(), move |conn| {
|
|
|
|
CommentReport::report(conn, &report_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let comment_report_view = blocking(context.pool(), move |conn| {
|
|
|
|
CommentReportView::read(conn, report.id, actor.id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
let community_id = comment_report_view.community.id;
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendModRoomMessage {
|
|
|
|
op: UserOperation::CreateCommentReport,
|
|
|
|
response: CommentReportResponse {
|
|
|
|
comment_report_view,
|
|
|
|
},
|
|
|
|
community_id,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|