]> Untitled Git - lemmy.git/commitdiff
Adding back in post fetching.
authorDessalines <tyhou13@gmx.com>
Sat, 25 Apr 2020 15:49:15 +0000 (11:49 -0400)
committerDessalines <tyhou13@gmx.com>
Sat, 25 Apr 2020 15:49:15 +0000 (11:49 -0400)
server/src/apub/fetcher.rs
server/src/apub/mod.rs

index b8aaeaaf985075da4af39f8dcf18108683da89a4..e07e410bac3ebcd4b16536e8769fbf52c366665b 100644 (file)
@@ -39,6 +39,7 @@ where
 pub enum SearchAcceptedObjects {
   Person(Box<PersonExt>),
   Group(Box<GroupExt>),
+  Page(Box<Page>),
 }
 
 /// Attempt to parse the query as URL, and fetch an ActivityPub object from it.
@@ -69,6 +70,10 @@ pub fn search_by_apub_id(query: &str, conn: &PgConnection) -> Result<SearchRespo
       // fetch_community_outbox(&c, conn)?;
       response.communities = vec![CommunityView::read(conn, community.id, None)?];
     }
+    SearchAcceptedObjects::Page(p) => {
+      let p = upsert_post(&PostForm::from_apub(&p, conn)?, conn)?;
+      response.posts = vec![PostView::read(conn, p.id, None)?];
+    }
   }
   Ok(response)
 }
@@ -137,15 +142,14 @@ pub fn get_or_fetch_and_upsert_remote_community(
   }
 }
 
-// TODO Maybe add post, comment searching / caching at a later time
-// fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result<Post, Error> {
-//   let existing = Post::read_from_apub_id(conn, &post_form.ap_id);
-//   match existing {
-//     Err(NotFound {}) => Ok(Post::create(conn, &post_form)?),
-//     Ok(p) => Ok(Post::update(conn, p.id, &post_form)?),
-//     Err(e) => Err(Error::from(e)),
-//   }
-// }
+fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result<Post, Error> {
+  let existing = Post::read_from_apub_id(conn, &post_form.ap_id);
+  match existing {
+    Err(NotFound {}) => Ok(Post::create(conn, &post_form)?),
+    Ok(p) => Ok(Post::update(conn, p.id, &post_form)?),
+    Err(e) => Err(Error::from(e)),
+  }
+}
 
 // TODO It should not be fetching data from a community outbox.
 // All posts, comments, comment likes, etc should be posts to our community_inbox
index 9c02d1071fb20a2b18b05221ff02aaec989b7e8c..671f8c5ac9fb1933595eccc98359a48ac5001e98 100644 (file)
@@ -39,6 +39,7 @@ use crate::api::site::SearchResponse;
 use crate::db::community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm};
 use crate::db::community_view::{CommunityFollowerView, CommunityView};
 use crate::db::post::{Post, PostForm};
+use crate::db::post_view::PostView;
 use crate::db::user::{UserForm, User_};
 use crate::db::user_view::UserView;
 use crate::db::{Crud, Followable, SearchType};