summaryrefslogtreecommitdiffstats
path: root/server/src/apub/fetcher.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/apub/fetcher.rs')
-rw-r--r--server/src/apub/fetcher.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs
index c7467c5b..05b7492e 100644
--- a/server/src/apub/fetcher.rs
+++ b/server/src/apub/fetcher.rs
@@ -254,6 +254,22 @@ fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result<Post, Error>
}
}
+pub fn get_or_fetch_and_insert_remote_post(
+ post_ap_id: &str,
+ conn: &PgConnection,
+) -> Result<Post, Error> {
+ match Post::read_from_apub_id(conn, post_ap_id) {
+ Ok(p) => Ok(p),
+ Err(NotFound {}) => {
+ debug!("Fetching and creating remote post: {}", post_ap_id);
+ let post = fetch_remote_object::<PageExt>(&Url::parse(post_ap_id)?)?;
+ let post_form = PostForm::from_apub(&post, conn)?;
+ Ok(Post::create(conn, &post_form)?)
+ }
+ Err(e) => Err(Error::from(e)),
+ }
+}
+
fn upsert_comment(comment_form: &CommentForm, conn: &PgConnection) -> Result<Comment, Error> {
let existing = Comment::read_from_apub_id(conn, &comment_form.ap_id);
match existing {
@@ -263,6 +279,25 @@ fn upsert_comment(comment_form: &CommentForm, conn: &PgConnection) -> Result<Com
}
}
+pub fn get_or_fetch_and_insert_remote_comment(
+ comment_ap_id: &str,
+ conn: &PgConnection,
+) -> Result<Comment, Error> {
+ match Comment::read_from_apub_id(conn, comment_ap_id) {
+ Ok(p) => Ok(p),
+ Err(NotFound {}) => {
+ debug!(
+ "Fetching and creating remote comment and its parents: {}",
+ comment_ap_id
+ );
+ let comment = fetch_remote_object::<Note>(&Url::parse(comment_ap_id)?)?;
+ let comment_form = CommentForm::from_apub(&comment, conn)?;
+ Ok(Comment::create(conn, &comment_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
// The only data we should be periodically fetching (if it hasn't been fetched in the last day