]> Untitled Git - lemmy.git/blobdiff - lemmy_apub/src/fetcher.rs
Merge branch 'main' into move_views_to_diesel
[lemmy.git] / lemmy_apub / src / fetcher.rs
index 0eb33cb753daab8630fb033905619e21bb164866..7556580e60d09b5a19522f619802d900e8e71ec2 100644 (file)
@@ -1,7 +1,7 @@
 use crate::{
   check_is_apub_id_valid,
+  objects::FromApub,
   ActorType,
-  FromApub,
   GroupExt,
   NoteExt,
   PageExt,
@@ -13,15 +13,15 @@ use anyhow::{anyhow, Context};
 use chrono::NaiveDateTime;
 use diesel::result::Error::NotFound;
 use lemmy_db::{
-  comment::{Comment, CommentForm},
+  comment::Comment,
   comment_view::CommentView,
-  community::{Community, CommunityForm, CommunityModerator, CommunityModeratorForm},
+  community::{Community, CommunityModerator, CommunityModeratorForm},
   naive_now,
-  post::{Post, PostForm},
+  post::Post,
   post_view::PostView,
-  user::{UserForm, User_},
+  user::User_,
   views::{community_view::CommunityView, user_view::UserViewSafe},
-  Crud,
+  ApubObject,
   Joinable,
   SearchType,
 };
@@ -183,22 +183,16 @@ pub async fn search_by_apub_id(
       response
     }
     SearchAcceptedObjects::Page(p) => {
-      let post_form = PostForm::from_apub(&p, context, Some(query_url), recursion_counter).await?;
+      let p = Post::from_apub(&p, context, query_url, recursion_counter).await?;
 
-      let p = blocking(context.pool(), move |conn| Post::upsert(conn, &post_form)).await??;
       response.posts =
         vec![blocking(context.pool(), move |conn| PostView::read(conn, p.id, None)).await??];
 
       response
     }
     SearchAcceptedObjects::Comment(c) => {
-      let comment_form =
-        CommentForm::from_apub(&c, context, Some(query_url), recursion_counter).await?;
+      let c = Comment::from_apub(&c, context, query_url, recursion_counter).await?;
 
-      let c = blocking(context.pool(), move |conn| {
-        Comment::upsert(conn, &comment_form)
-      })
-      .await??;
       response.comments = vec![
         blocking(context.pool(), move |conn| {
           CommentView::read(conn, c.id, None)
@@ -242,7 +236,7 @@ pub(crate) async fn get_or_fetch_and_upsert_user(
 ) -> Result<User_, LemmyError> {
   let apub_id_owned = apub_id.to_owned();
   let user = blocking(context.pool(), move |conn| {
-    User_::read_from_actor_id(conn, apub_id_owned.as_ref())
+    User_::read_from_apub_id(conn, apub_id_owned.as_ref())
   })
   .await?;
 
@@ -257,15 +251,13 @@ pub(crate) async fn get_or_fetch_and_upsert_user(
         return Ok(u);
       }
 
-      let mut uf = UserForm::from_apub(
-        &person?,
-        context,
-        Some(apub_id.to_owned()),
-        recursion_counter,
-      )
-      .await?;
-      uf.last_refreshed_at = Some(naive_now());
-      let user = blocking(context.pool(), move |conn| User_::update(conn, u.id, &uf)).await??;
+      let user = User_::from_apub(&person?, context, apub_id.to_owned(), recursion_counter).await?;
+
+      let user_id = user.id;
+      blocking(context.pool(), move |conn| {
+        User_::mark_as_updated(conn, user_id)
+      })
+      .await??;
 
       Ok(user)
     }
@@ -275,14 +267,7 @@ pub(crate) async fn get_or_fetch_and_upsert_user(
       let person =
         fetch_remote_object::<PersonExt>(context.client(), apub_id, recursion_counter).await?;
 
-      let uf = UserForm::from_apub(
-        &person,
-        context,
-        Some(apub_id.to_owned()),
-        recursion_counter,
-      )
-      .await?;
-      let user = blocking(context.pool(), move |conn| User_::upsert(conn, &uf)).await??;
+      let user = User_::from_apub(&person, context, apub_id.to_owned(), recursion_counter).await?;
 
       Ok(user)
     }
@@ -317,7 +302,7 @@ pub(crate) async fn get_or_fetch_and_upsert_community(
 ) -> Result<Community, LemmyError> {
   let apub_id_owned = apub_id.to_owned();
   let community = blocking(context.pool(), move |conn| {
-    Community::read_from_actor_id(conn, apub_id_owned.as_str())
+    Community::read_from_apub_id(conn, apub_id_owned.as_str())
   })
   .await?;
 
@@ -353,9 +338,8 @@ async fn fetch_remote_community(
   }
 
   let group = group?;
-  let cf =
-    CommunityForm::from_apub(&group, context, Some(apub_id.to_owned()), recursion_counter).await?;
-  let community = blocking(context.pool(), move |conn| Community::upsert(conn, &cf)).await??;
+  let community =
+    Community::from_apub(&group, context, apub_id.to_owned(), recursion_counter).await?;
 
   // Also add the community moderators too
   let attributed_to = group.inner.attributed_to().context(location_info!())?;
@@ -405,23 +389,13 @@ async fn fetch_remote_community(
   }
   for o in outbox_items {
     let page = PageExt::from_any_base(o)?.context(location_info!())?;
+    let page_id = page.id_unchecked().context(location_info!())?;
 
-    // The post creator may be from a blocked instance,
-    // if it errors, then continue
-    let post = match PostForm::from_apub(&page, context, None, recursion_counter).await {
-      Ok(post) => post,
-      Err(_) => continue,
-    };
-    let post_ap_id = post.ap_id.as_ref().context(location_info!())?.clone();
-    // Check whether the post already exists in the local db
-    let existing = blocking(context.pool(), move |conn| {
-      Post::read_from_apub_id(conn, &post_ap_id)
-    })
-    .await?;
-    match existing {
-      Ok(e) => blocking(context.pool(), move |conn| Post::update(conn, e.id, &post)).await??,
-      Err(_) => blocking(context.pool(), move |conn| Post::upsert(conn, &post)).await??,
-    };
+    // The post creator may be from a blocked instance, if it errors, then skip it
+    if check_is_apub_id_valid(page_id).is_err() {
+      continue;
+    }
+    Post::from_apub(&page, context, page_id.to_owned(), recursion_counter).await?;
     // TODO: we need to send a websocket update here
   }
 
@@ -447,17 +421,9 @@ pub(crate) async fn get_or_fetch_and_insert_post(
     Ok(p) => Ok(p),
     Err(NotFound {}) => {
       debug!("Fetching and creating remote post: {}", post_ap_id);
-      let post =
+      let page =
         fetch_remote_object::<PageExt>(context.client(), post_ap_id, recursion_counter).await?;
-      let post_form = PostForm::from_apub(
-        &post,
-        context,
-        Some(post_ap_id.to_owned()),
-        recursion_counter,
-      )
-      .await?;
-
-      let post = blocking(context.pool(), move |conn| Post::upsert(conn, &post_form)).await??;
+      let post = Post::from_apub(&page, context, post_ap_id.to_owned(), recursion_counter).await?;
 
       Ok(post)
     }
@@ -489,25 +455,20 @@ pub(crate) async fn get_or_fetch_and_insert_comment(
       );
       let comment =
         fetch_remote_object::<NoteExt>(context.client(), comment_ap_id, recursion_counter).await?;
-      let comment_form = CommentForm::from_apub(
+      let comment = Comment::from_apub(
         &comment,
         context,
-        Some(comment_ap_id.to_owned()),
+        comment_ap_id.to_owned(),
         recursion_counter,
       )
       .await?;
 
-      let post_id = comment_form.post_id;
+      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());
       }
 
-      let comment = blocking(context.pool(), move |conn| {
-        Comment::upsert(conn, &comment_form)
-      })
-      .await??;
-
       Ok(comment)
     }
     Err(e) => Err(e.into()),