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, get_local_user_view_from_jwt,
is_mod_or_admin, 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::{ use lemmy_db_schema::{
source::{ source::{
comment::Comment, comment::Comment,
@ -69,20 +69,6 @@ impl PerformCrud for DeleteComment {
.await? .await?
.map_err(|e| ApiError::err("couldnt_update_comment", e))?; .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_id = updated_comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let recipient_ids = send_local_notifs( let recipient_ids = send_local_notifs(
@ -95,6 +81,20 @@ impl PerformCrud for DeleteComment {
) )
.await?; .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( send_comment_ws_message(
data.comment_id, data.comment_id,
UserOperationCrud::DeleteComment, UserOperationCrud::DeleteComment,
@ -162,21 +162,6 @@ impl PerformCrud for RemoveComment {
}) })
.await??; .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_id = updated_comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let recipient_ids = send_local_notifs( let recipient_ids = send_local_notifs(
@ -189,6 +174,21 @@ impl PerformCrud for RemoveComment {
) )
.await?; .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( send_comment_ws_message(
data.comment_id, data.comment_id,
UserOperationCrud::RemoveComment, UserOperationCrud::RemoveComment,

View File

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

View File

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

View File

@ -3,7 +3,10 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "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": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],

View File

@ -3,7 +3,10 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "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": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],

View File

@ -8,7 +8,10 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "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": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],

View File

@ -8,7 +8,10 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "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": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],

View File

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

View File

@ -1,14 +1,13 @@
use crate::{ use crate::{
activities::{verify_mod_action, verify_person_in_community}, activities::{verify_mod_action, verify_person_in_community},
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost}, 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_api_common::blocking;
use lemmy_apub_lib::{ use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject, verify::verify_domains_match};
object_id::ObjectId,
traits::{ActorType, ApubObject},
verify::verify_domains_match,
};
use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post}; use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{ use lemmy_websocket::{
@ -24,14 +23,14 @@ pub mod undo_delete;
pub async fn send_apub_delete( pub async fn send_apub_delete(
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity, community: &ApubCommunity,
object_id: Url, object: DeletableObjects,
deleted: bool, deleted: bool,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if deleted { if deleted {
Delete::send(actor, community, object_id, None, context).await Delete::send(actor, community, object, None, context).await
} else { } 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( pub async fn send_apub_remove(
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity, community: &ApubCommunity,
object_id: Url, object: DeletableObjects,
reason: String, reason: String,
removed: bool, removed: bool,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if removed { if removed {
Delete::send(actor, community, object_id, Some(reason), context).await Delete::send(actor, community, object, Some(reason), context).await
} else { } 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()) 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( pub(in crate::activities) async fn verify_delete_activity(
@ -153,7 +160,7 @@ async fn receive_delete_action(
DeletableObjects::Community(community) => { DeletableObjects::Community(community) => {
if community.local { if community.local {
let mod_ = actor.dereference(context, request_counter).await?; 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?; 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?; self.object.verify(context, request_counter).await?;
let community = self.get_community(context, request_counter).await?; let community = self.get_community(context, request_counter).await?;
verify_delete_activity( verify_delete_activity(
&self.object.object, &self.object.object.id,
&self.actor, &self.actor,
&community, &community,
self.object.summary.is_some(), self.object.summary.is_some(),
@ -57,10 +57,10 @@ impl ActivityHandler for UndoDelete {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if self.object.summary.is_some() { 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 { } else {
receive_delete_action( receive_delete_action(
&self.object.object, &self.object.object.id,
&self.actor, &self.actor,
false, false,
context, context,
@ -75,11 +75,11 @@ impl UndoDelete {
pub(in crate::activities::deletion) async fn send( pub(in crate::activities::deletion) async fn send(
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity, community: &ApubCommunity,
object_id: Url, object: DeletableObjects,
summary: Option<String>, summary: Option<String>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> 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( let id = generate_activity_id(
UndoType::Undo, 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> { 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()); return Err(anyhow!("Object is not public").into());
} }
Ok(()) Ok(())

View File

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

View File

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

View File

@ -128,10 +128,7 @@ impl ApubObject for ApubPost {
} }
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> { fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
Ok(Tombstone::new( Ok(Tombstone::new(self.ap_id.clone().into()))
PageType::Page,
self.updated.unwrap_or(self.published),
))
} }
async fn verify( 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 activitystreams::{activity::kind::DeleteType, unparsed::Unparsed};
use lemmy_apub_lib::object_id::ObjectId; use lemmy_apub_lib::object_id::ObjectId;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -11,8 +11,7 @@ use url::Url;
pub struct Delete { pub struct Delete {
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
pub(crate) to: Vec<Url>, pub(crate) to: Vec<Url>,
pub(crate) object: Url, pub(crate) object: Tombstone,
pub(crate) cc: Vec<Url>,
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: DeleteType, pub(crate) kind: DeleteType,
/// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user /// 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] #[actix_rt::test]
#[serial] #[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/remove_note.json");
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/delete_page.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 activitystreams::object::kind::TombstoneType;
use chrono::{DateTime, FixedOffset, NaiveDateTime};
use lemmy_utils::utils::convert_datetime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none; use serde_with::skip_serializing_none;
use url::Url;
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Tombstone { pub struct Tombstone {
pub(crate) id: Url,
#[serde(rename = "type")] #[serde(rename = "type")]
kind: TombstoneType, kind: TombstoneType,
former_type: String,
deleted: DateTime<FixedOffset>,
} }
impl Tombstone { impl Tombstone {
pub fn new<T: ToString>(former_type: T, updated_time: NaiveDateTime) -> Tombstone { pub fn new(id: Url) -> Tombstone {
Tombstone { Tombstone {
id,
kind: TombstoneType::Tombstone, kind: TombstoneType::Tombstone,
former_type: former_type.to_string(),
deleted: convert_datetime(updated_time),
} }
} }
} }