Support mastodon deletes

This commit is contained in:
Aode (lion) 2021-11-15 18:58:15 -06:00 committed by Felix Ableitner
parent 7f4a773b88
commit 20cddf5e81
17 changed files with 104 additions and 93 deletions

View file

@ -7,7 +7,7 @@ use lemmy_api_common::{
get_local_user_view_from_jwt,
is_mod_or_admin,
};
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove, DeletableObjects};
use lemmy_db_schema::{
source::{
comment::Comment,
@ -69,20 +69,6 @@ impl PerformCrud for DeleteComment {
.await?
.map_err(|e| ApiError::err("couldnt_update_comment", e))?;
// Send the apub message
let community = blocking(context.pool(), move |conn| {
Community::read(conn, orig_comment.post.community_id)
})
.await??;
send_apub_delete(
&local_user_view.person.clone().into(),
&community.clone().into(),
updated_comment.ap_id.clone().into(),
deleted,
context,
)
.await?;
let post_id = updated_comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let recipient_ids = send_local_notifs(
@ -95,6 +81,20 @@ impl PerformCrud for DeleteComment {
)
.await?;
// Send the apub message
let community = blocking(context.pool(), move |conn| {
Community::read(conn, orig_comment.post.community_id)
})
.await??;
send_apub_delete(
&local_user_view.person.clone().into(),
&community.clone().into(),
DeletableObjects::Comment(Box::new(updated_comment.into())),
deleted,
context,
)
.await?;
send_comment_ws_message(
data.comment_id,
UserOperationCrud::DeleteComment,
@ -162,21 +162,6 @@ impl PerformCrud for RemoveComment {
})
.await??;
// Send the apub message
let community = blocking(context.pool(), move |conn| {
Community::read(conn, orig_comment.post.community_id)
})
.await??;
send_apub_remove(
&local_user_view.person.clone().into(),
&community.into(),
updated_comment.ap_id.clone().into(),
data.reason.clone().unwrap_or_else(|| "".to_string()),
removed,
context,
)
.await?;
let post_id = updated_comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let recipient_ids = send_local_notifs(
@ -189,6 +174,21 @@ impl PerformCrud for RemoveComment {
)
.await?;
// Send the apub message
let community = blocking(context.pool(), move |conn| {
Community::read(conn, orig_comment.post.community_id)
})
.await??;
send_apub_remove(
&local_user_view.person.clone().into(),
&community.into(),
DeletableObjects::Comment(Box::new(updated_comment.into())),
data.reason.clone().unwrap_or_else(|| "".to_string()),
removed,
context,
)
.await?;
send_comment_ws_message(
data.comment_id,
UserOperationCrud::RemoveComment,

View file

@ -1,7 +1,7 @@
use crate::PerformCrud;
use actix_web::web::Data;
use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt, is_admin};
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove, DeletableObjects};
use lemmy_db_schema::{
source::{
community::Community,
@ -51,7 +51,7 @@ impl PerformCrud for DeleteCommunity {
send_apub_delete(
&local_user_view.person.clone().into(),
&updated_community.clone().into(),
updated_community.actor_id.clone().into(),
DeletableObjects::Community(Box::new(updated_community.into())),
deleted,
context,
)
@ -111,7 +111,7 @@ impl PerformCrud for RemoveCommunity {
send_apub_remove(
&local_user_view.person.clone().into(),
&updated_community.clone().into(),
updated_community.actor_id.clone().into(),
DeletableObjects::Community(Box::new(updated_community.into())),
data.reason.clone().unwrap_or_else(|| "".to_string()),
removed,
context,

View file

@ -8,7 +8,7 @@ use lemmy_api_common::{
is_mod_or_admin,
post::*,
};
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove};
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove, DeletableObjects};
use lemmy_db_schema::{
source::{
community::Community,
@ -70,7 +70,7 @@ impl PerformCrud for DeletePost {
send_apub_delete(
&local_user_view.person.clone().into(),
&community.into(),
updated_post.ap_id.into(),
DeletableObjects::Post(Box::new(updated_post.into())),
deleted,
context,
)
@ -146,7 +146,7 @@ impl PerformCrud for RemovePost {
send_apub_remove(
&local_user_view.person.clone().into(),
&community.into(),
updated_post.ap_id.into(),
DeletableObjects::Post(Box::new(updated_post.into())),
data.reason.clone().unwrap_or_else(|| "".to_string()),
removed,
context,

View file

@ -3,7 +3,10 @@
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"object": "http://ds9.lemmy.ml/post/1",
"object": {
"id": "http://ds9.lemmy.ml/post/1",
"type": "Tombstone"
},
"cc": [
"http://enterprise.lemmy.ml/c/main"
],

View file

@ -3,7 +3,10 @@
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"object": "http://ds9.lemmy.ml/comment/1",
"object": {
"id": "http://ds9.lemmy.ml/comment/1",
"type": "Tombstone"
},
"cc": [
"http://enterprise.lemmy.ml/c/main"
],

View file

@ -8,7 +8,10 @@
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"object": "http://ds9.lemmy.ml/post/1",
"object": {
"id": "http://ds9.lemmy.ml/post/1",
"type": "Tombstone"
},
"cc": [
"http://enterprise.lemmy.ml/c/main"
],

View file

@ -8,7 +8,10 @@
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"object": "http://ds9.lemmy.ml/comment/1",
"object": {
"id": "http://ds9.lemmy.ml/comment/1",
"type": "Tombstone"
},
"cc": [
"http://enterprise.lemmy.ml/c/main"
],

View file

@ -50,11 +50,11 @@ impl ActivityHandler for Delete {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_is_public(&self.to, &self.cc)?;
verify_is_public(&self.to, &[])?;
verify_activity(&self.id, self.actor.inner(), &context.settings())?;
let community = self.get_community(context, request_counter).await?;
verify_delete_activity(
&self.object,
&self.object.id,
&self.actor,
&community,
self.summary.is_some(),
@ -78,9 +78,16 @@ impl ActivityHandler for Delete {
} else {
Some(reason)
};
receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await
receive_remove_action(
&self.actor,
&self.object.id,
reason,
context,
request_counter,
)
.await
} else {
receive_delete_action(&self.object, &self.actor, true, context, request_counter).await
receive_delete_action(&self.object.id, &self.actor, true, context, request_counter).await
}
}
}
@ -88,16 +95,14 @@ impl ActivityHandler for Delete {
impl Delete {
pub(in crate::activities::deletion) fn new(
actor: &ApubPerson,
community: &ApubCommunity,
object_id: Url,
object: DeletableObjects,
summary: Option<String>,
context: &LemmyContext,
) -> Result<Delete, LemmyError> {
Ok(Delete {
actor: ObjectId::new(actor.actor_id()),
to: vec![public()],
object: object_id,
cc: vec![community.actor_id()],
object: object.to_tombstone()?,
kind: DeleteType::Delete,
summary,
id: generate_activity_id(
@ -110,11 +115,11 @@ impl Delete {
pub(in crate::activities::deletion) async fn send(
actor: &ApubPerson,
community: &ApubCommunity,
object_id: Url,
object: DeletableObjects,
summary: Option<String>,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let delete = Delete::new(actor, community, object_id, summary, context)?;
let delete = Delete::new(actor, object, summary, context)?;
let delete_id = delete.id.clone();
let activity = AnnouncableActivities::Delete(delete);
@ -201,7 +206,7 @@ impl GetCommunity for Delete {
context: &LemmyContext,
_request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> {
let community_id = match DeletableObjects::read_from_db(&self.object, context).await? {
let community_id = match DeletableObjects::read_from_db(&self.object.id, context).await? {
DeletableObjects::Community(c) => c.id,
DeletableObjects::Comment(c) => {
let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;

View file

@ -1,14 +1,13 @@
use crate::{
activities::{verify_mod_action, verify_person_in_community},
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
protocol::{
activities::deletion::{delete::Delete, undo_delete::UndoDelete},
objects::tombstone::Tombstone,
},
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
object_id::ObjectId,
traits::{ActorType, ApubObject},
verify::verify_domains_match,
};
use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject, verify::verify_domains_match};
use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
use lemmy_utils::LemmyError;
use lemmy_websocket::{
@ -24,14 +23,14 @@ pub mod undo_delete;
pub async fn send_apub_delete(
actor: &ApubPerson,
community: &ApubCommunity,
object_id: Url,
object: DeletableObjects,
deleted: bool,
context: &LemmyContext,
) -> Result<(), LemmyError> {
if deleted {
Delete::send(actor, community, object_id, None, context).await
Delete::send(actor, community, object, None, context).await
} else {
UndoDelete::send(actor, community, object_id, None, context).await
UndoDelete::send(actor, community, object, None, context).await
}
}
@ -40,15 +39,15 @@ pub async fn send_apub_delete(
pub async fn send_apub_remove(
actor: &ApubPerson,
community: &ApubCommunity,
object_id: Url,
object: DeletableObjects,
reason: String,
removed: bool,
context: &LemmyContext,
) -> Result<(), LemmyError> {
if removed {
Delete::send(actor, community, object_id, Some(reason), context).await
Delete::send(actor, community, object, Some(reason), context).await
} else {
UndoDelete::send(actor, community, object_id, Some(reason), context).await
UndoDelete::send(actor, community, object, Some(reason), context).await
}
}
@ -74,6 +73,14 @@ impl DeletableObjects {
}
Err(diesel::NotFound.into())
}
pub(crate) fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
match self {
DeletableObjects::Community(c) => c.to_tombstone(),
DeletableObjects::Comment(c) => c.to_tombstone(),
DeletableObjects::Post(p) => p.to_tombstone(),
}
}
}
pub(in crate::activities) async fn verify_delete_activity(
@ -153,7 +160,7 @@ async fn receive_delete_action(
DeletableObjects::Community(community) => {
if community.local {
let mod_ = actor.dereference(context, request_counter).await?;
let object = community.actor_id();
let object = DeletableObjects::Community(community.clone());
send_apub_delete(&mod_, &community.clone(), object, true, context).await?;
}

View file

@ -40,7 +40,7 @@ impl ActivityHandler for UndoDelete {
self.object.verify(context, request_counter).await?;
let community = self.get_community(context, request_counter).await?;
verify_delete_activity(
&self.object.object,
&self.object.object.id,
&self.actor,
&community,
self.object.summary.is_some(),
@ -57,10 +57,10 @@ impl ActivityHandler for UndoDelete {
request_counter: &mut i32,
) -> Result<(), LemmyError> {
if self.object.summary.is_some() {
UndoDelete::receive_undo_remove_action(&self.object.object, context).await
UndoDelete::receive_undo_remove_action(&self.object.object.id, context).await
} else {
receive_delete_action(
&self.object.object,
&self.object.object.id,
&self.actor,
false,
context,
@ -75,11 +75,11 @@ impl UndoDelete {
pub(in crate::activities::deletion) async fn send(
actor: &ApubPerson,
community: &ApubCommunity,
object_id: Url,
object: DeletableObjects,
summary: Option<String>,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let object = Delete::new(actor, community, object_id, summary, context)?;
let object = Delete::new(actor, object, summary, context)?;
let id = generate_activity_id(
UndoType::Undo,

View file

@ -117,7 +117,7 @@ fn verify_add_remove_moderator_target(
}
pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
if !to.contains(&public()) && !cc.contains(&public()) {
if ![to, cc].iter().any(|set| set.contains(&public())) {
return Err(anyhow!("Object is not public").into());
}
Ok(())

View file

@ -132,10 +132,7 @@ impl ApubObject for ApubComment {
}
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
Ok(Tombstone::new(
NoteType::Note,
self.updated.unwrap_or(self.published),
))
Ok(Tombstone::new(self.ap_id.clone().into()))
}
async fn verify(

View file

@ -118,10 +118,7 @@ impl ApubObject for ApubCommunity {
}
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
Ok(Tombstone::new(
GroupType::Group,
self.updated.unwrap_or(self.published),
))
Ok(Tombstone::new(self.actor_id()))
}
async fn verify(

View file

@ -128,10 +128,7 @@ impl ApubObject for ApubPost {
}
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
Ok(Tombstone::new(
PageType::Page,
self.updated.unwrap_or(self.published),
))
Ok(Tombstone::new(self.ap_id.clone().into()))
}
async fn verify(

View file

@ -1,4 +1,4 @@
use crate::objects::person::ApubPerson;
use crate::{objects::person::ApubPerson, protocol::objects::tombstone::Tombstone};
use activitystreams::{activity::kind::DeleteType, unparsed::Unparsed};
use lemmy_apub_lib::object_id::ObjectId;
use serde::{Deserialize, Serialize};
@ -11,8 +11,7 @@ use url::Url;
pub struct Delete {
pub(crate) actor: ObjectId<ApubPerson>,
pub(crate) to: Vec<Url>,
pub(crate) object: Url,
pub(crate) cc: Vec<Url>,
pub(crate) object: Tombstone,
#[serde(rename = "type")]
pub(crate) kind: DeleteType,
/// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user

View file

@ -11,7 +11,7 @@ mod tests {
#[actix_rt::test]
#[serial]
async fn test_parse_lemmy_voting() {
async fn test_parse_lemmy_deletion() {
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/remove_note.json");
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/delete_page.json");

View file

@ -1,25 +1,22 @@
use activitystreams::object::kind::TombstoneType;
use chrono::{DateTime, FixedOffset, NaiveDateTime};
use lemmy_utils::utils::convert_datetime;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use url::Url;
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Tombstone {
pub(crate) id: Url,
#[serde(rename = "type")]
kind: TombstoneType,
former_type: String,
deleted: DateTime<FixedOffset>,
}
impl Tombstone {
pub fn new<T: ToString>(former_type: T, updated_time: NaiveDateTime) -> Tombstone {
pub fn new(id: Url) -> Tombstone {
Tombstone {
id,
kind: TombstoneType::Tombstone,
former_type: former_type.to_string(),
deleted: convert_datetime(updated_time),
}
}
}