]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/fetcher/objects.rs
Merge pull request #1785 from LemmyNet/add_cardano_donation_link
[lemmy.git] / crates / apub / src / fetcher / objects.rs
index 269b27ef8438f466661b24ae07201a3595747244..e538982db45c64e6c3ee1f910ace33f5d8500610 100644 (file)
@@ -1,9 +1,13 @@
-use crate::{fetcher::fetch::fetch_remote_object, objects::FromApub, NoteExt, PageExt};
+use crate::{
+  fetcher::fetch::fetch_remote_object,
+  objects::{comment::Note, post::Page, FromApub},
+  PostOrComment,
+};
 use anyhow::anyhow;
 use diesel::result::Error::NotFound;
+use lemmy_api_common::blocking;
 use lemmy_db_queries::{ApubObject, Crud};
 use lemmy_db_schema::source::{comment::Comment, post::Post};
-use lemmy_structs::blocking;
 use lemmy_utils::LemmyError;
 use lemmy_websocket::LemmyContext;
 use log::debug;
@@ -20,7 +24,7 @@ pub(crate) async fn get_or_fetch_and_insert_post(
 ) -> Result<Post, LemmyError> {
   let post_ap_id_owned = post_ap_id.to_owned();
   let post = blocking(context.pool(), move |conn| {
-    Post::read_from_apub_id(conn, post_ap_id_owned.as_str())
+    Post::read_from_apub_id(conn, &post_ap_id_owned.into())
   })
   .await?;
 
@@ -29,8 +33,8 @@ pub(crate) async fn get_or_fetch_and_insert_post(
     Err(NotFound {}) => {
       debug!("Fetching and creating remote post: {}", post_ap_id);
       let page =
-        fetch_remote_object::<PageExt>(context.client(), post_ap_id, recursion_counter).await?;
-      let post = Post::from_apub(&page, context, post_ap_id.to_owned(), recursion_counter).await?;
+        fetch_remote_object::<Page>(context.client(), post_ap_id, recursion_counter).await?;
+      let post = Post::from_apub(&page, context, post_ap_id, recursion_counter).await?;
 
       Ok(post)
     }
@@ -49,7 +53,7 @@ pub(crate) async fn get_or_fetch_and_insert_comment(
 ) -> Result<Comment, LemmyError> {
   let comment_ap_id_owned = comment_ap_id.to_owned();
   let comment = blocking(context.pool(), move |conn| {
-    Comment::read_from_apub_id(conn, comment_ap_id_owned.as_str())
+    Comment::read_from_apub_id(conn, &comment_ap_id_owned.into())
   })
   .await?;
 
@@ -61,14 +65,8 @@ pub(crate) async fn get_or_fetch_and_insert_comment(
         comment_ap_id
       );
       let comment =
-        fetch_remote_object::<NoteExt>(context.client(), comment_ap_id, recursion_counter).await?;
-      let comment = Comment::from_apub(
-        &comment,
-        context,
-        comment_ap_id.to_owned(),
-        recursion_counter,
-      )
-      .await?;
+        fetch_remote_object::<Note>(context.client(), comment_ap_id, recursion_counter).await?;
+      let comment = Comment::from_apub(&comment, context, comment_ap_id, recursion_counter).await?;
 
       let post_id = comment.post_id;
       let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@@ -81,3 +79,19 @@ pub(crate) async fn get_or_fetch_and_insert_comment(
     Err(e) => Err(e.into()),
   }
 }
+
+pub(crate) 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))
+      }
+    },
+  )
+}