2021-10-29 10:32:42 +00:00
|
|
|
use crate::{
|
2022-12-01 20:52:49 +00:00
|
|
|
activities::verify_community_matches,
|
2021-11-05 00:24:10 +00:00
|
|
|
fetcher::post_or_comment::PostOrComment,
|
2021-10-29 10:32:42 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
2022-12-01 20:52:49 +00:00
|
|
|
protocol::InCommunity,
|
2021-10-29 10:32:42 +00:00
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::activity::FlagType,
|
|
|
|
protocol::helpers::deserialize_one,
|
|
|
|
};
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2024-01-05 16:03:13 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorType, LemmyResult};
|
2021-10-29 10:32:42 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use url::Url;
|
|
|
|
|
2021-11-03 17:33:51 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2021-10-29 10:32:42 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Report {
|
|
|
|
pub(crate) actor: ObjectId<ApubPerson>,
|
2022-06-02 14:33:41 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_one")]
|
2022-06-08 15:45:39 +00:00
|
|
|
pub(crate) to: [ObjectId<ApubCommunity>; 1],
|
2024-01-05 16:03:13 +00:00
|
|
|
pub(crate) object: ReportObject,
|
|
|
|
/// Report reason as sent by Lemmy
|
|
|
|
pub(crate) summary: Option<String>,
|
|
|
|
/// Report reason as sent by Mastodon
|
|
|
|
pub(crate) content: Option<String>,
|
2021-10-29 10:32:42 +00:00
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub(crate) kind: FlagType,
|
|
|
|
pub(crate) id: Url,
|
2022-12-01 20:52:49 +00:00
|
|
|
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:03:13 +00:00
|
|
|
impl Report {
|
|
|
|
pub fn reason(&self) -> LemmyResult<String> {
|
|
|
|
self
|
|
|
|
.summary
|
|
|
|
.clone()
|
|
|
|
.or(self.content.clone())
|
|
|
|
.ok_or(LemmyErrorType::CouldntFindObject.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub(crate) enum ReportObject {
|
|
|
|
Lemmy(ObjectId<PostOrComment>),
|
|
|
|
/// Mastodon sends an array containing user id and one or more post ids
|
|
|
|
Mastodon(Vec<Url>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReportObject {
|
|
|
|
pub async fn dereference(self, context: &Data<LemmyContext>) -> LemmyResult<PostOrComment> {
|
|
|
|
match self {
|
|
|
|
ReportObject::Lemmy(l) => l.dereference(context).await,
|
|
|
|
ReportObject::Mastodon(objects) => {
|
|
|
|
for o in objects {
|
|
|
|
// Find the first reported item which can be dereferenced as post or comment (Lemmy can
|
|
|
|
// only handle one item per report).
|
|
|
|
let deref = ObjectId::from(o).dereference(context).await;
|
|
|
|
if deref.is_ok() {
|
|
|
|
return deref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(LemmyErrorType::CouldntFindObject.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-12-01 20:52:49 +00:00
|
|
|
impl InCommunity for Report {
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn community(&self, context: &Data<LemmyContext>) -> Result<ApubCommunity, LemmyError> {
|
|
|
|
let community = self.to[0].dereference(context).await?;
|
2022-12-01 20:52:49 +00:00
|
|
|
if let Some(audience) = &self.audience {
|
2023-02-18 14:50:28 +00:00
|
|
|
verify_community_matches(audience, community.actor_id.clone())?;
|
2022-12-01 20:52:49 +00:00
|
|
|
}
|
2023-02-18 14:50:28 +00:00
|
|
|
Ok(community)
|
2022-12-01 20:52:49 +00:00
|
|
|
}
|
2021-10-29 10:32:42 +00:00
|
|
|
}
|