2022-05-06 23:53:33 +00:00
|
|
|
use crate::{
|
2022-12-01 20:52:49 +00:00
|
|
|
activities::{deletion::DeletableObjects, verify_community_matches},
|
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
|
|
|
protocol::{objects::tombstone::Tombstone, IdOrNestedObject, InCommunity},
|
2022-05-06 23:53:33 +00:00
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::activity::DeleteType,
|
|
|
|
protocol::helpers::deserialize_one_or_many,
|
|
|
|
};
|
2022-12-01 20:52:49 +00:00
|
|
|
use anyhow::anyhow;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2022-12-01 20:52:49 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{community::Community, post::Post},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2024-04-16 12:48:15 +00:00
|
|
|
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
|
2021-10-29 10:32:42 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_with::skip_serializing_none;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[skip_serializing_none]
|
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 Delete {
|
|
|
|
pub(crate) actor: ObjectId<ApubPerson>,
|
2022-06-02 14:33:41 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_one_or_many")]
|
2021-10-29 10:32:42 +00:00
|
|
|
pub(crate) to: Vec<Url>,
|
2022-05-06 23:53:33 +00:00
|
|
|
pub(crate) object: IdOrNestedObject<Tombstone>,
|
2022-02-17 22:04:01 +00:00
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub(crate) kind: DeleteType,
|
|
|
|
pub(crate) id: Url,
|
2022-12-01 20:52:49 +00:00
|
|
|
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
|
2022-02-17 22:04:01 +00:00
|
|
|
|
2022-06-02 14:33:41 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_one_or_many")]
|
2022-02-14 15:14:24 +00:00
|
|
|
#[serde(default)]
|
|
|
|
#[serde(skip_serializing_if = "Vec::is_empty")]
|
|
|
|
pub(crate) cc: Vec<Url>,
|
2021-10-29 10:32:42 +00:00
|
|
|
/// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
|
|
|
|
/// deleting their own content.
|
|
|
|
pub(crate) summary: Option<String>,
|
2023-11-16 13:22:40 +00:00
|
|
|
/// Nonstandard field, only valid if object refers to a Person. If present, all content from the
|
|
|
|
/// user should be deleted along with the account
|
|
|
|
pub(crate) remove_data: Option<bool>,
|
2021-10-29 10:32:42 +00:00
|
|
|
}
|
2022-12-01 20:52:49 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-12-01 20:52:49 +00:00
|
|
|
impl InCommunity for Delete {
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn community(&self, context: &Data<LemmyContext>) -> LemmyResult<ApubCommunity> {
|
2022-12-01 20:52:49 +00:00
|
|
|
let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? {
|
|
|
|
DeletableObjects::Community(c) => c.id,
|
|
|
|
DeletableObjects::Comment(c) => {
|
2024-04-16 12:48:15 +00:00
|
|
|
let post = Post::read(&mut context.pool(), c.post_id)
|
|
|
|
.await?
|
|
|
|
.ok_or(LemmyErrorType::CouldntFindPost)?;
|
2022-12-01 20:52:49 +00:00
|
|
|
post.community_id
|
|
|
|
}
|
|
|
|
DeletableObjects::Post(p) => p.community_id,
|
2023-11-16 13:22:40 +00:00
|
|
|
DeletableObjects::Person(_) => return Err(anyhow!("Person is not part of community").into()),
|
2022-12-01 20:52:49 +00:00
|
|
|
DeletableObjects::PrivateMessage(_) => {
|
|
|
|
return Err(anyhow!("Private message is not part of community").into())
|
|
|
|
}
|
|
|
|
};
|
2024-04-16 12:48:15 +00:00
|
|
|
let community = Community::read(&mut context.pool(), community_id)
|
|
|
|
.await?
|
|
|
|
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
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.into())
|
2022-12-01 20:52:49 +00:00
|
|
|
}
|
|
|
|
}
|