2 fetcher::fetch::fetch_remote_object,
3 objects::{post::Page, FromApub},
8 use diesel::result::Error::NotFound;
9 use lemmy_api_common::blocking;
10 use lemmy_db_queries::{ApubObject, Crud};
11 use lemmy_db_schema::source::{comment::Comment, post::Post};
12 use lemmy_utils::LemmyError;
13 use lemmy_websocket::LemmyContext;
17 /// Gets a post by its apub ID. If it exists locally, it is returned directly. Otherwise it is
18 /// pulled from its apub ID, inserted and returned.
20 /// The parent community is also pulled if necessary. Comments are not pulled.
21 pub async fn get_or_fetch_and_insert_post(
23 context: &LemmyContext,
24 recursion_counter: &mut i32,
25 ) -> Result<Post, LemmyError> {
26 let post_ap_id_owned = post_ap_id.to_owned();
27 let post = blocking(context.pool(), move |conn| {
28 Post::read_from_apub_id(conn, &post_ap_id_owned.into())
35 debug!("Fetching and creating remote post: {}", post_ap_id);
37 fetch_remote_object::<Page>(context.client(), post_ap_id, recursion_counter).await?;
38 let post = Post::from_apub(
41 post_ap_id.to_owned(),
49 Err(e) => Err(e.into()),
53 /// Gets a comment by its apub ID. If it exists locally, it is returned directly. Otherwise it is
54 /// pulled from its apub ID, inserted and returned.
56 /// The parent community, post and comment are also pulled if necessary.
57 pub async fn get_or_fetch_and_insert_comment(
59 context: &LemmyContext,
60 recursion_counter: &mut i32,
61 ) -> Result<Comment, LemmyError> {
62 let comment_ap_id_owned = comment_ap_id.to_owned();
63 let comment = blocking(context.pool(), move |conn| {
64 Comment::read_from_apub_id(conn, &comment_ap_id_owned.into())
72 "Fetching and creating remote comment and its parents: {}",
76 fetch_remote_object::<NoteExt>(context.client(), comment_ap_id, recursion_counter).await?;
77 let comment = Comment::from_apub(
80 comment_ap_id.to_owned(),
86 let post_id = comment.post_id;
87 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
89 return Err(anyhow!("Post is locked").into());
94 Err(e) => Err(e.into()),
98 pub async fn get_or_fetch_and_insert_post_or_comment(
100 context: &LemmyContext,
101 recursion_counter: &mut i32,
102 ) -> Result<PostOrComment, LemmyError> {
104 match get_or_fetch_and_insert_post(ap_id, context, recursion_counter).await {
105 Ok(p) => PostOrComment::Post(Box::new(p)),
107 let c = get_or_fetch_and_insert_comment(ap_id, context, recursion_counter).await?;
108 PostOrComment::Comment(Box::new(c))