1 use crate::{fetcher::fetch::fetch_remote_object, objects::FromApub, NoteExt, PageExt};
3 use diesel::result::Error::NotFound;
4 use lemmy_api_structs::blocking;
5 use lemmy_db_queries::{ApubObject, Crud};
6 use lemmy_db_schema::source::{comment::Comment, post::Post};
7 use lemmy_utils::LemmyError;
8 use lemmy_websocket::LemmyContext;
12 /// Gets a post by its apub ID. If it exists locally, it is returned directly. Otherwise it is
13 /// pulled from its apub ID, inserted and returned.
15 /// The parent community is also pulled if necessary. Comments are not pulled.
16 pub(crate) async fn get_or_fetch_and_insert_post(
18 context: &LemmyContext,
19 recursion_counter: &mut i32,
20 ) -> Result<Post, LemmyError> {
21 let post_ap_id_owned = post_ap_id.to_owned();
22 let post = blocking(context.pool(), move |conn| {
23 Post::read_from_apub_id(conn, &post_ap_id_owned.into())
30 debug!("Fetching and creating remote post: {}", post_ap_id);
32 fetch_remote_object::<PageExt>(context.client(), post_ap_id, recursion_counter).await?;
33 let post = Post::from_apub(&page, context, post_ap_id.to_owned(), recursion_counter).await?;
37 Err(e) => Err(e.into()),
41 /// Gets a comment by its apub ID. If it exists locally, it is returned directly. Otherwise it is
42 /// pulled from its apub ID, inserted and returned.
44 /// The parent community, post and comment are also pulled if necessary.
45 pub(crate) async fn get_or_fetch_and_insert_comment(
47 context: &LemmyContext,
48 recursion_counter: &mut i32,
49 ) -> Result<Comment, LemmyError> {
50 let comment_ap_id_owned = comment_ap_id.to_owned();
51 let comment = blocking(context.pool(), move |conn| {
52 Comment::read_from_apub_id(conn, &comment_ap_id_owned.into())
60 "Fetching and creating remote comment and its parents: {}",
64 fetch_remote_object::<NoteExt>(context.client(), comment_ap_id, recursion_counter).await?;
65 let comment = Comment::from_apub(
68 comment_ap_id.to_owned(),
73 let post_id = comment.post_id;
74 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
76 return Err(anyhow!("Post is locked").into());
81 Err(e) => Err(e.into()),