2 fetcher::fetch::fetch_remote_object,
9 use diesel::result::Error::NotFound;
10 use lemmy_api_common::blocking;
11 use lemmy_db_queries::{ApubObject, Crud};
12 use lemmy_db_schema::source::{comment::Comment, post::Post};
13 use lemmy_utils::LemmyError;
14 use lemmy_websocket::LemmyContext;
18 /// Gets a post by its apub ID. If it exists locally, it is returned directly. Otherwise it is
19 /// pulled from its apub ID, inserted and returned.
21 /// The parent community is also pulled if necessary. Comments are not pulled.
22 pub async fn get_or_fetch_and_insert_post(
24 context: &LemmyContext,
25 recursion_counter: &mut i32,
26 ) -> Result<Post, LemmyError> {
27 let post_ap_id_owned = post_ap_id.to_owned();
28 let post = blocking(context.pool(), move |conn| {
29 Post::read_from_apub_id(conn, &post_ap_id_owned.into())
36 debug!("Fetching and creating remote post: {}", post_ap_id);
38 fetch_remote_object::<PageExt>(context.client(), post_ap_id, recursion_counter).await?;
39 let post = Post::from_apub(
42 post_ap_id.to_owned(),
50 Err(e) => Err(e.into()),
54 /// Gets a comment by its apub ID. If it exists locally, it is returned directly. Otherwise it is
55 /// pulled from its apub ID, inserted and returned.
57 /// The parent community, post and comment are also pulled if necessary.
58 pub async fn get_or_fetch_and_insert_comment(
60 context: &LemmyContext,
61 recursion_counter: &mut i32,
62 ) -> Result<Comment, LemmyError> {
63 let comment_ap_id_owned = comment_ap_id.to_owned();
64 let comment = blocking(context.pool(), move |conn| {
65 Comment::read_from_apub_id(conn, &comment_ap_id_owned.into())
73 "Fetching and creating remote comment and its parents: {}",
77 fetch_remote_object::<NoteExt>(context.client(), comment_ap_id, recursion_counter).await?;
78 let comment = Comment::from_apub(
81 comment_ap_id.to_owned(),
87 let post_id = comment.post_id;
88 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
90 return Err(anyhow!("Post is locked").into());
95 Err(e) => Err(e.into()),
99 pub async fn get_or_fetch_and_insert_post_or_comment(
101 context: &LemmyContext,
102 recursion_counter: &mut i32,
103 ) -> Result<PostOrComment, LemmyError> {
105 match get_or_fetch_and_insert_post(ap_id, context, recursion_counter).await {
106 Ok(p) => PostOrComment::Post(Box::new(p)),
108 let c = get_or_fetch_and_insert_comment(ap_id, context, recursion_counter).await?;
109 PostOrComment::Comment(Box::new(c))