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
|
|
|
use crate::{
|
|
|
|
fetcher::fetch::fetch_remote_object,
|
2021-07-31 14:57:37 +00:00
|
|
|
objects::{comment::Note, post::Page, FromApub},
|
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
|
|
|
PostOrComment,
|
|
|
|
};
|
2021-01-12 16:12:41 +00:00
|
|
|
use anyhow::anyhow;
|
|
|
|
use diesel::result::Error::NotFound;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-01-12 16:12:41 +00:00
|
|
|
use lemmy_db_queries::{ApubObject, Crud};
|
|
|
|
use lemmy_db_schema::source::{comment::Comment, post::Post};
|
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use log::debug;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
/// Gets a post by its apub ID. If it exists locally, it is returned directly. Otherwise it is
|
|
|
|
/// pulled from its apub ID, inserted and returned.
|
|
|
|
///
|
|
|
|
/// The parent community is also pulled if necessary. Comments are not pulled.
|
2021-03-30 20:23:50 +00:00
|
|
|
pub async fn get_or_fetch_and_insert_post(
|
2021-01-12 16:12:41 +00:00
|
|
|
post_ap_id: &Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
recursion_counter: &mut i32,
|
|
|
|
) -> Result<Post, LemmyError> {
|
|
|
|
let post_ap_id_owned = post_ap_id.to_owned();
|
|
|
|
let post = blocking(context.pool(), move |conn| {
|
2021-01-27 16:42:23 +00:00
|
|
|
Post::read_from_apub_id(conn, &post_ap_id_owned.into())
|
2021-01-12 16:12:41 +00:00
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
match post {
|
|
|
|
Ok(p) => Ok(p),
|
|
|
|
Err(NotFound {}) => {
|
|
|
|
debug!("Fetching and creating remote post: {}", post_ap_id);
|
|
|
|
let page =
|
2021-07-27 22:18:50 +00:00
|
|
|
fetch_remote_object::<Page>(context.client(), post_ap_id, recursion_counter).await?;
|
2021-08-12 12:48:09 +00:00
|
|
|
let post = Post::from_apub(&page, context, post_ap_id, recursion_counter).await?;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
|
|
|
Ok(post)
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a comment by its apub ID. If it exists locally, it is returned directly. Otherwise it is
|
|
|
|
/// pulled from its apub ID, inserted and returned.
|
|
|
|
///
|
|
|
|
/// The parent community, post and comment are also pulled if necessary.
|
2021-03-30 20:23:50 +00:00
|
|
|
pub async fn get_or_fetch_and_insert_comment(
|
2021-01-12 16:12:41 +00:00
|
|
|
comment_ap_id: &Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
recursion_counter: &mut i32,
|
|
|
|
) -> Result<Comment, LemmyError> {
|
|
|
|
let comment_ap_id_owned = comment_ap_id.to_owned();
|
|
|
|
let comment = blocking(context.pool(), move |conn| {
|
2021-01-27 16:42:23 +00:00
|
|
|
Comment::read_from_apub_id(conn, &comment_ap_id_owned.into())
|
2021-01-12 16:12:41 +00:00
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
match comment {
|
|
|
|
Ok(p) => Ok(p),
|
|
|
|
Err(NotFound {}) => {
|
|
|
|
debug!(
|
|
|
|
"Fetching and creating remote comment and its parents: {}",
|
|
|
|
comment_ap_id
|
|
|
|
);
|
|
|
|
let comment =
|
2021-07-31 14:57:37 +00:00
|
|
|
fetch_remote_object::<Note>(context.client(), comment_ap_id, recursion_counter).await?;
|
2021-08-12 12:48:09 +00:00
|
|
|
let comment = Comment::from_apub(&comment, context, comment_ap_id, recursion_counter).await?;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
|
|
|
let post_id = comment.post_id;
|
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
|
|
|
if post.locked {
|
|
|
|
return Err(anyhow!("Post is locked").into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(comment)
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}
|
|
|
|
}
|
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 async fn get_or_fetch_and_insert_post_or_comment(
|
|
|
|
ap_id: &Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
recursion_counter: &mut i32,
|
|
|
|
) -> Result<PostOrComment, LemmyError> {
|
|
|
|
Ok(
|
|
|
|
match get_or_fetch_and_insert_post(ap_id, context, recursion_counter).await {
|
|
|
|
Ok(p) => PostOrComment::Post(Box::new(p)),
|
|
|
|
Err(_) => {
|
|
|
|
let c = get_or_fetch_and_insert_comment(ap_id, context, recursion_counter).await?;
|
|
|
|
PostOrComment::Comment(Box::new(c))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|