2021-11-03 17:33:51 +00:00
|
|
|
use crate::{
|
|
|
|
activities::{verify_mod_action, verify_person_in_community},
|
|
|
|
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
|
2021-11-16 00:58:15 +00:00
|
|
|
protocol::{
|
|
|
|
activities::deletion::{delete::Delete, undo_delete::UndoDelete},
|
|
|
|
objects::tombstone::Tombstone,
|
|
|
|
},
|
2021-11-03 17:33:51 +00:00
|
|
|
};
|
2021-08-17 18:04:58 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-11-16 00:58:15 +00:00
|
|
|
use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject, verify::verify_domains_match};
|
2021-10-18 21:36:44 +00:00
|
|
|
use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
|
2021-08-17 18:04:58 +00:00
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::{
|
|
|
|
send::{send_comment_ws_message_simple, send_community_ws_message, send_post_ws_message},
|
|
|
|
LemmyContext,
|
|
|
|
UserOperationCrud,
|
|
|
|
};
|
2021-11-03 17:33:51 +00:00
|
|
|
use url::Url;
|
2021-08-17 18:04:58 +00:00
|
|
|
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
pub mod delete;
|
|
|
|
pub mod undo_delete;
|
2021-08-17 18:04:58 +00:00
|
|
|
|
|
|
|
pub async fn send_apub_delete(
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
|
|
|
community: &ApubCommunity,
|
2021-11-16 00:58:15 +00:00
|
|
|
object: DeletableObjects,
|
2021-08-17 18:04:58 +00:00
|
|
|
deleted: bool,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
if deleted {
|
2021-11-16 00:58:15 +00:00
|
|
|
Delete::send(actor, community, object, None, context).await
|
2021-08-17 18:04:58 +00:00
|
|
|
} else {
|
2021-11-16 00:58:15 +00:00
|
|
|
UndoDelete::send(actor, community, object, None, context).await
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove reason is actually optional in lemmy. we set an empty string in that case, but its
|
|
|
|
// ugly
|
|
|
|
pub async fn send_apub_remove(
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
|
|
|
community: &ApubCommunity,
|
2021-11-16 00:58:15 +00:00
|
|
|
object: DeletableObjects,
|
2021-08-17 18:04:58 +00:00
|
|
|
reason: String,
|
|
|
|
removed: bool,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
if removed {
|
2021-11-16 00:58:15 +00:00
|
|
|
Delete::send(actor, community, object, Some(reason), context).await
|
2021-08-17 18:04:58 +00:00
|
|
|
} else {
|
2021-11-16 00:58:15 +00:00
|
|
|
UndoDelete::send(actor, community, object, Some(reason), context).await
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum DeletableObjects {
|
2021-11-09 18:16:37 +00:00
|
|
|
Community(Box<ApubCommunity>),
|
|
|
|
Comment(Box<ApubComment>),
|
|
|
|
Post(Box<ApubPost>),
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DeletableObjects {
|
|
|
|
pub(crate) async fn read_from_db(
|
|
|
|
ap_id: &Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<DeletableObjects, LemmyError> {
|
2021-10-18 21:36:44 +00:00
|
|
|
if let Some(c) = ApubCommunity::read_from_apub_id(ap_id.clone(), context).await? {
|
2021-11-09 18:16:37 +00:00
|
|
|
return Ok(DeletableObjects::Community(Box::new(c)));
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
2021-10-18 21:36:44 +00:00
|
|
|
if let Some(p) = ApubPost::read_from_apub_id(ap_id.clone(), context).await? {
|
2021-11-09 18:16:37 +00:00
|
|
|
return Ok(DeletableObjects::Post(Box::new(p)));
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
2021-10-18 21:36:44 +00:00
|
|
|
if let Some(c) = ApubComment::read_from_apub_id(ap_id.clone(), context).await? {
|
2021-11-09 18:16:37 +00:00
|
|
|
return Ok(DeletableObjects::Comment(Box::new(c)));
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
Err(diesel::NotFound.into())
|
|
|
|
}
|
2021-11-16 00:58:15 +00:00
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::activities) async fn verify_delete_activity(
|
|
|
|
object: &Url,
|
2021-11-03 17:33:51 +00:00
|
|
|
actor: &ObjectId<ApubPerson>,
|
2021-10-28 12:47:56 +00:00
|
|
|
community: &ApubCommunity,
|
2021-08-17 18:04:58 +00:00
|
|
|
is_mod_action: bool,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let object = DeletableObjects::read_from_db(object, context).await?;
|
|
|
|
match object {
|
2021-10-28 12:47:56 +00:00
|
|
|
DeletableObjects::Community(community) => {
|
|
|
|
if community.local {
|
2021-08-17 18:04:58 +00:00
|
|
|
// can only do this check for local community, in remote case it would try to fetch the
|
|
|
|
// deleted community (which fails)
|
2021-11-03 17:33:51 +00:00
|
|
|
verify_person_in_community(actor, &community, context, request_counter).await?;
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
// community deletion is always a mod (or admin) action
|
2021-11-03 17:33:51 +00:00
|
|
|
verify_mod_action(actor, &community, context, request_counter).await?;
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
DeletableObjects::Post(p) => {
|
|
|
|
verify_delete_activity_post_or_comment(
|
2021-11-03 17:33:51 +00:00
|
|
|
actor,
|
2021-10-18 21:36:44 +00:00
|
|
|
&p.ap_id.clone().into(),
|
2021-10-28 12:47:56 +00:00
|
|
|
community,
|
2021-08-17 18:04:58 +00:00
|
|
|
is_mod_action,
|
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
DeletableObjects::Comment(c) => {
|
|
|
|
verify_delete_activity_post_or_comment(
|
2021-11-03 17:33:51 +00:00
|
|
|
actor,
|
2021-10-18 21:36:44 +00:00
|
|
|
&c.ap_id.clone().into(),
|
2021-10-28 12:47:56 +00:00
|
|
|
community,
|
2021-08-17 18:04:58 +00:00
|
|
|
is_mod_action,
|
|
|
|
context,
|
|
|
|
request_counter,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn verify_delete_activity_post_or_comment(
|
2021-11-03 17:33:51 +00:00
|
|
|
actor: &ObjectId<ApubPerson>,
|
2021-08-17 18:04:58 +00:00
|
|
|
object_id: &Url,
|
2021-10-28 12:47:56 +00:00
|
|
|
community: &ApubCommunity,
|
2021-08-17 18:04:58 +00:00
|
|
|
is_mod_action: bool,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-11-03 17:33:51 +00:00
|
|
|
verify_person_in_community(actor, community, context, request_counter).await?;
|
2021-08-17 18:04:58 +00:00
|
|
|
if is_mod_action {
|
2021-11-03 17:33:51 +00:00
|
|
|
verify_mod_action(actor, community, context, request_counter).await?;
|
2021-08-17 18:04:58 +00:00
|
|
|
} else {
|
|
|
|
// domain of post ap_id and post.creator ap_id are identical, so we just check the former
|
2021-11-03 17:33:51 +00:00
|
|
|
verify_domains_match(actor.inner(), object_id)?;
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write deletion or restoring of an object to the database, and send websocket message.
|
|
|
|
/// TODO: we should do something similar for receive_remove_action(), but its much more complicated
|
|
|
|
/// because of the mod log
|
|
|
|
async fn receive_delete_action(
|
|
|
|
object: &Url,
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ObjectId<ApubPerson>,
|
2021-08-17 18:04:58 +00:00
|
|
|
deleted: bool,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
match DeletableObjects::read_from_db(object, context).await? {
|
|
|
|
DeletableObjects::Community(community) => {
|
|
|
|
if community.local {
|
2021-09-25 15:44:52 +00:00
|
|
|
let mod_ = actor.dereference(context, request_counter).await?;
|
2021-11-16 00:58:15 +00:00
|
|
|
let object = DeletableObjects::Community(community.clone());
|
2021-08-17 18:04:58 +00:00
|
|
|
send_apub_delete(&mod_, &community.clone(), object, true, context).await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::update_deleted(conn, community.id, deleted)
|
|
|
|
})
|
|
|
|
.await??;
|
2021-11-03 17:47:24 +00:00
|
|
|
send_community_ws_message(
|
|
|
|
community.id,
|
|
|
|
UserOperationCrud::DeleteCommunity,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
DeletableObjects::Post(post) => {
|
2021-11-03 17:47:24 +00:00
|
|
|
if deleted != post.deleted {
|
|
|
|
let deleted_post = blocking(context.pool(), move |conn| {
|
|
|
|
Post::update_deleted(conn, post.id, deleted)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
send_post_ws_message(
|
|
|
|
deleted_post.id,
|
|
|
|
UserOperationCrud::DeletePost,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
DeletableObjects::Comment(comment) => {
|
2021-11-03 17:47:24 +00:00
|
|
|
if deleted != comment.deleted {
|
|
|
|
let deleted_comment = blocking(context.pool(), move |conn| {
|
|
|
|
Comment::update_deleted(conn, comment.id, deleted)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
send_comment_ws_message_simple(
|
|
|
|
deleted_comment.id,
|
|
|
|
UserOperationCrud::DeleteComment,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
2021-08-17 18:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|