mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-02-27 08:06:42 +00:00
Simplify delete activity implementation
This commit is contained in:
parent
0670dc4565
commit
e65897a350
3 changed files with 161 additions and 170 deletions
|
@ -2,28 +2,18 @@ use crate::{
|
||||||
activities::{
|
activities::{
|
||||||
comment::send_websocket_message as send_comment_message,
|
comment::send_websocket_message as send_comment_message,
|
||||||
community::send_websocket_message as send_community_message,
|
community::send_websocket_message as send_community_message,
|
||||||
|
deletion::{verify_delete_activity, DeletableObjects},
|
||||||
post::send_websocket_message as send_post_message,
|
post::send_websocket_message as send_post_message,
|
||||||
verify_activity,
|
verify_activity,
|
||||||
verify_mod_action,
|
|
||||||
verify_person_in_community,
|
|
||||||
},
|
},
|
||||||
fetcher::{
|
fetcher::person::get_or_fetch_and_upsert_person,
|
||||||
community::get_or_fetch_and_upsert_community,
|
|
||||||
objects::get_or_fetch_and_insert_post_or_comment,
|
|
||||||
person::get_or_fetch_and_upsert_person,
|
|
||||||
},
|
|
||||||
ActorType,
|
|
||||||
CommunityType,
|
CommunityType,
|
||||||
PostOrComment,
|
|
||||||
};
|
};
|
||||||
use activitystreams::activity::kind::DeleteType;
|
use activitystreams::activity::kind::DeleteType;
|
||||||
use lemmy_api_common::blocking;
|
use lemmy_api_common::blocking;
|
||||||
use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
|
use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
|
||||||
use lemmy_db_queries::{
|
use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
|
||||||
source::{comment::Comment_, community::Community_, post::Post_},
|
use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
|
||||||
Crud,
|
|
||||||
};
|
|
||||||
use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
|
|
||||||
use lemmy_utils::LemmyError;
|
use lemmy_utils::LemmyError;
|
||||||
use lemmy_websocket::{LemmyContext, UserOperationCrud};
|
use lemmy_websocket::{LemmyContext, UserOperationCrud};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -56,20 +46,14 @@ impl ActivityHandler for DeletePostCommentOrCommunity {
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
verify_activity(self.common())?;
|
verify_activity(self.common())?;
|
||||||
let object_community =
|
verify_delete_activity(
|
||||||
get_or_fetch_and_upsert_community(&self.object, context, request_counter).await;
|
&self.object,
|
||||||
// deleting a community (set counter 0 to only fetch from local db)
|
&self.cc[0],
|
||||||
if object_community.is_ok() {
|
&self.common,
|
||||||
verify_mod_action(&self.common.actor, self.object.clone(), context).await?;
|
context,
|
||||||
}
|
request_counter,
|
||||||
// deleting a post or comment
|
)
|
||||||
else {
|
.await?;
|
||||||
verify_person_in_community(&self.common().actor, &self.cc[0], context, request_counter)
|
|
||||||
.await?;
|
|
||||||
let object_creator =
|
|
||||||
get_post_or_comment_actor_id(&self.object, context, request_counter).await?;
|
|
||||||
verify_urls_match(&self.common.actor, &object_creator)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,54 +62,34 @@ impl ActivityHandler for DeletePostCommentOrCommunity {
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
let object_community =
|
use UserOperationCrud::*;
|
||||||
get_or_fetch_and_upsert_community(&self.object, context, request_counter).await;
|
match DeletableObjects::read_from_db(&self.object, context).await? {
|
||||||
// deleting a community
|
DeletableObjects::Community(community) => {
|
||||||
if let Ok(community) = object_community {
|
if community.local {
|
||||||
if community.local {
|
let mod_ =
|
||||||
// repeat these checks just to be sure
|
get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
|
||||||
verify_person_in_community(&self.common().actor, &self.cc[0], context, request_counter)
|
community.send_delete(mod_, context).await?;
|
||||||
.await?;
|
}
|
||||||
verify_mod_action(&self.common.actor, self.object.clone(), context).await?;
|
|
||||||
let mod_ =
|
|
||||||
get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
|
|
||||||
community.send_delete(mod_, context).await?;
|
|
||||||
}
|
|
||||||
let deleted_community = blocking(context.pool(), move |conn| {
|
|
||||||
Community::update_deleted(conn, community.id, true)
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
send_community_message(
|
let deleted_community = blocking(context.pool(), move |conn| {
|
||||||
deleted_community.id,
|
Community::update_deleted(conn, community.id, true)
|
||||||
UserOperationCrud::DeleteCommunity,
|
})
|
||||||
context,
|
.await??;
|
||||||
)
|
send_community_message(deleted_community.id, DeleteCommunity, context).await
|
||||||
.await
|
}
|
||||||
}
|
DeletableObjects::Post(post) => {
|
||||||
// deleting a post or comment
|
let deleted_post = blocking(context.pool(), move |conn| {
|
||||||
else {
|
Post::update_deleted(conn, post.id, true)
|
||||||
match get_or_fetch_and_insert_post_or_comment(&self.object, context, request_counter).await? {
|
})
|
||||||
PostOrComment::Post(post) => {
|
.await??;
|
||||||
let deleted_post = blocking(context.pool(), move |conn| {
|
send_post_message(deleted_post.id, EditPost, context).await
|
||||||
Post::update_deleted(conn, post.id, true)
|
}
|
||||||
})
|
DeletableObjects::Comment(comment) => {
|
||||||
.await??;
|
let deleted_comment = blocking(context.pool(), move |conn| {
|
||||||
send_post_message(deleted_post.id, UserOperationCrud::EditPost, context).await
|
Comment::update_deleted(conn, comment.id, true)
|
||||||
}
|
})
|
||||||
PostOrComment::Comment(comment) => {
|
.await??;
|
||||||
let deleted_comment = blocking(context.pool(), move |conn| {
|
send_comment_message(deleted_comment.id, vec![], EditComment, context).await
|
||||||
Comment::update_deleted(conn, comment.id, true)
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
send_comment_message(
|
|
||||||
deleted_comment.id,
|
|
||||||
vec![],
|
|
||||||
UserOperationCrud::EditComment,
|
|
||||||
context,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,26 +98,3 @@ impl ActivityHandler for DeletePostCommentOrCommunity {
|
||||||
&self.common
|
&self.common
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_post_or_comment_actor_id(
|
|
||||||
object: &Url,
|
|
||||||
context: &LemmyContext,
|
|
||||||
request_counter: &mut i32,
|
|
||||||
) -> Result<Url, LemmyError> {
|
|
||||||
let actor_id =
|
|
||||||
match get_or_fetch_and_insert_post_or_comment(object, context, request_counter).await? {
|
|
||||||
PostOrComment::Post(post) => {
|
|
||||||
let creator_id = post.creator_id;
|
|
||||||
blocking(context.pool(), move |conn| Person::read(conn, creator_id))
|
|
||||||
.await??
|
|
||||||
.actor_id()
|
|
||||||
}
|
|
||||||
PostOrComment::Comment(comment) => {
|
|
||||||
let creator_id = comment.creator_id;
|
|
||||||
blocking(context.pool(), move |conn| Person::read(conn, creator_id))
|
|
||||||
.await??
|
|
||||||
.actor_id()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(actor_id)
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,2 +1,84 @@
|
||||||
|
use crate::{
|
||||||
|
activities::{verify_mod_action, verify_person_in_community},
|
||||||
|
ActorType,
|
||||||
|
};
|
||||||
|
use lemmy_api_common::blocking;
|
||||||
|
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields};
|
||||||
|
use lemmy_db_queries::ApubObject;
|
||||||
|
use lemmy_db_schema::{
|
||||||
|
source::{comment::Comment, community::Community, post::Post},
|
||||||
|
DbUrl,
|
||||||
|
};
|
||||||
|
use lemmy_utils::LemmyError;
|
||||||
|
use lemmy_websocket::LemmyContext;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
pub mod delete;
|
pub mod delete;
|
||||||
pub mod undo_delete;
|
pub mod undo_delete;
|
||||||
|
|
||||||
|
pub enum DeletableObjects {
|
||||||
|
Community(Box<Community>),
|
||||||
|
Comment(Box<Comment>),
|
||||||
|
Post(Box<Post>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeletableObjects {
|
||||||
|
pub(crate) async fn read_from_db(
|
||||||
|
ap_id: &Url,
|
||||||
|
context: &LemmyContext,
|
||||||
|
) -> Result<DeletableObjects, LemmyError> {
|
||||||
|
let id: DbUrl = ap_id.clone().into();
|
||||||
|
|
||||||
|
if let Some(c) = DeletableObjects::read_type_from_db::<Community>(id.clone(), context).await? {
|
||||||
|
return Ok(DeletableObjects::Community(Box::new(c)));
|
||||||
|
}
|
||||||
|
if let Some(p) = DeletableObjects::read_type_from_db::<Post>(id.clone(), context).await? {
|
||||||
|
return Ok(DeletableObjects::Post(Box::new(p)));
|
||||||
|
}
|
||||||
|
if let Some(c) = DeletableObjects::read_type_from_db::<Comment>(id.clone(), context).await? {
|
||||||
|
return Ok(DeletableObjects::Comment(Box::new(c)));
|
||||||
|
}
|
||||||
|
Err(diesel::NotFound.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: a method like this should be provided by fetcher module
|
||||||
|
async fn read_type_from_db<Type: ApubObject + Send + 'static>(
|
||||||
|
ap_id: DbUrl,
|
||||||
|
context: &LemmyContext,
|
||||||
|
) -> Result<Option<Type>, LemmyError> {
|
||||||
|
blocking(context.pool(), move |conn| {
|
||||||
|
Type::read_from_apub_id(conn, &ap_id).ok()
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(in crate::activities::deletion) async fn verify_delete_activity(
|
||||||
|
object: &Url,
|
||||||
|
cc: &Url,
|
||||||
|
common: &ActivityCommonFields,
|
||||||
|
context: &LemmyContext,
|
||||||
|
request_counter: &mut i32,
|
||||||
|
) -> Result<(), LemmyError> {
|
||||||
|
let object = DeletableObjects::read_from_db(object, context).await?;
|
||||||
|
match object {
|
||||||
|
DeletableObjects::Community(c) => {
|
||||||
|
if c.local {
|
||||||
|
// can only do this check for local community, in remote case it would try to fetch the
|
||||||
|
// deleted community (which fails)
|
||||||
|
verify_person_in_community(&common.actor, cc, context, request_counter).await?;
|
||||||
|
}
|
||||||
|
verify_mod_action(&common.actor, c.actor_id(), context).await?;
|
||||||
|
}
|
||||||
|
DeletableObjects::Post(p) => {
|
||||||
|
verify_person_in_community(&common.actor, cc, context, request_counter).await?;
|
||||||
|
// domain of post ap_id and post.creator ap_id are identical, so we just check the former
|
||||||
|
verify_domains_match(&common.actor, &p.ap_id.into())?;
|
||||||
|
}
|
||||||
|
DeletableObjects::Comment(c) => {
|
||||||
|
verify_person_in_community(&common.actor, cc, context, request_counter).await?;
|
||||||
|
verify_domains_match(&common.actor, &c.ap_id.into())?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
@ -2,23 +2,16 @@ use crate::{
|
||||||
activities::{
|
activities::{
|
||||||
comment::send_websocket_message as send_comment_message,
|
comment::send_websocket_message as send_comment_message,
|
||||||
community::send_websocket_message as send_community_message,
|
community::send_websocket_message as send_community_message,
|
||||||
deletion::delete::DeletePostCommentOrCommunity,
|
deletion::{delete::DeletePostCommentOrCommunity, verify_delete_activity, DeletableObjects},
|
||||||
post::send_websocket_message as send_post_message,
|
post::send_websocket_message as send_post_message,
|
||||||
verify_activity,
|
verify_activity,
|
||||||
verify_mod_action,
|
|
||||||
verify_person_in_community,
|
|
||||||
},
|
|
||||||
fetcher::{
|
|
||||||
community::get_or_fetch_and_upsert_community,
|
|
||||||
objects::get_or_fetch_and_insert_post_or_comment,
|
|
||||||
person::get_or_fetch_and_upsert_person,
|
|
||||||
},
|
},
|
||||||
|
fetcher::person::get_or_fetch_and_upsert_person,
|
||||||
CommunityType,
|
CommunityType,
|
||||||
PostOrComment,
|
|
||||||
};
|
};
|
||||||
use activitystreams::activity::kind::UndoType;
|
use activitystreams::activity::kind::UndoType;
|
||||||
use lemmy_api_common::blocking;
|
use lemmy_api_common::blocking;
|
||||||
use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
|
use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
|
||||||
use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
|
use lemmy_db_queries::source::{comment::Comment_, community::Community_, post::Post_};
|
||||||
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;
|
||||||
|
@ -46,18 +39,14 @@ impl ActivityHandler for UndoDeletePostCommentOrCommunity {
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
verify_activity(self.common())?;
|
verify_activity(self.common())?;
|
||||||
self.object.verify(context, request_counter).await?;
|
self.object.verify(context, request_counter).await?;
|
||||||
let object_community =
|
verify_delete_activity(
|
||||||
get_or_fetch_and_upsert_community(&self.object.object, context, request_counter).await;
|
&self.object.object,
|
||||||
// restoring a community
|
&self.cc[0],
|
||||||
if object_community.is_ok() {
|
&self.common,
|
||||||
verify_mod_action(&self.common.actor, self.object.object.clone(), context).await?;
|
context,
|
||||||
}
|
request_counter,
|
||||||
// restoring a post or comment
|
)
|
||||||
else {
|
.await?;
|
||||||
verify_person_in_community(&self.common().actor, &self.cc[0], context, request_counter)
|
|
||||||
.await?;
|
|
||||||
verify_urls_match(&self.common.actor, &self.object.common().actor)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,56 +55,35 @@ impl ActivityHandler for UndoDeletePostCommentOrCommunity {
|
||||||
context: &LemmyContext,
|
context: &LemmyContext,
|
||||||
request_counter: &mut i32,
|
request_counter: &mut i32,
|
||||||
) -> Result<(), LemmyError> {
|
) -> Result<(), LemmyError> {
|
||||||
let object_community =
|
use UserOperationCrud::*;
|
||||||
get_or_fetch_and_upsert_community(&self.object.object, context, request_counter).await;
|
let object = DeletableObjects::read_from_db(&self.object.object, context).await?;
|
||||||
// restoring a community
|
match object {
|
||||||
if let Ok(community) = object_community {
|
DeletableObjects::Community(community) => {
|
||||||
if community.local {
|
if community.local {
|
||||||
// repeat these checks just to be sure
|
let mod_ =
|
||||||
verify_person_in_community(&self.common().actor, &self.cc[0], context, request_counter)
|
get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
|
||||||
.await?;
|
community.send_undo_delete(mod_, context).await?;
|
||||||
verify_mod_action(&self.common.actor, self.object.object.clone(), context).await?;
|
}
|
||||||
let mod_ =
|
|
||||||
get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
|
|
||||||
community.send_undo_delete(mod_, context).await?;
|
|
||||||
}
|
|
||||||
let deleted_community = blocking(context.pool(), move |conn| {
|
|
||||||
Community::update_deleted(conn, community.id, false)
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
send_community_message(
|
let deleted_community = blocking(context.pool(), move |conn| {
|
||||||
deleted_community.id,
|
Community::update_deleted(conn, community.id, false)
|
||||||
UserOperationCrud::EditCommunity,
|
})
|
||||||
context,
|
.await??;
|
||||||
)
|
send_community_message(deleted_community.id, EditCommunity, context).await
|
||||||
.await
|
}
|
||||||
}
|
DeletableObjects::Post(post) => {
|
||||||
// restoring a post or comment
|
let deleted_post = blocking(context.pool(), move |conn| {
|
||||||
else {
|
Post::update_deleted(conn, post.id, false)
|
||||||
match get_or_fetch_and_insert_post_or_comment(&self.object.object, context, request_counter)
|
})
|
||||||
.await?
|
.await??;
|
||||||
{
|
send_post_message(deleted_post.id, EditPost, context).await
|
||||||
PostOrComment::Post(post) => {
|
}
|
||||||
let deleted_post = blocking(context.pool(), move |conn| {
|
DeletableObjects::Comment(comment) => {
|
||||||
Post::update_deleted(conn, post.id, false)
|
let deleted_comment = blocking(context.pool(), move |conn| {
|
||||||
})
|
Comment::update_deleted(conn, comment.id, false)
|
||||||
.await??;
|
})
|
||||||
send_post_message(deleted_post.id, UserOperationCrud::EditPost, context).await
|
.await??;
|
||||||
}
|
send_comment_message(deleted_comment.id, vec![], EditComment, context).await
|
||||||
PostOrComment::Comment(comment) => {
|
|
||||||
let deleted_comment = blocking(context.pool(), move |conn| {
|
|
||||||
Comment::update_deleted(conn, comment.id, false)
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
send_comment_message(
|
|
||||||
deleted_comment.id,
|
|
||||||
vec![],
|
|
||||||
UserOperationCrud::EditComment,
|
|
||||||
context,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue