From 1588b2e46c78c27295d2b26f9896ab8253cd7539 Mon Sep 17 00:00:00 2001 From: aprilthepink Date: Mon, 17 Jun 2024 21:35:21 +0200 Subject: [PATCH] feat: Add db_post_from_url function The commit adds the `db_post_from_url` function to the `conversion.rs` file. This function retrieves a post from the database based on a given URL. If the post is not found in the database, it fetches the post from the URL, saves it to the database, and returns the post. This change enhances the functionality of the codebase by providing a convenient way to retrieve and store posts. --- src/lysand/conversion.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lysand/conversion.rs b/src/lysand/conversion.rs index 6a1941c..e236cd1 100644 --- a/src/lysand/conversion.rs +++ b/src/lysand/conversion.rs @@ -23,6 +23,23 @@ pub async fn option_content_format_text(opt: Option) -> Option anyhow::Result { + if !url.domain().eq(&Some(LYSAND_DOMAIN.as_str())) { + return Err(anyhow!("not lysands domain")); + } + let str_url = url.to_string(); + let post_res: Option = prelude::Post::find().filter(entities::post::Column::Url.eq(str_url.clone())).one(DB.get().unwrap()).await?; + + if let Some(post) = post_res { + Ok(post) + } else { + let post = fetch_note_from_url(url.clone()).await?; + receive_lysand_note(post, "1".to_string()).await?; + let post_res: Option = prelude::Post::find().filter(entities::post::Column::Url.eq(str_url)).one(DB.get().unwrap()).await?; + Ok(post_res.unwrap()) + } +} + pub async fn db_user_from_url(url: Url) -> anyhow::Result { if !url.domain().eq(&Some(LYSAND_DOMAIN.as_str())) { return Err(anyhow!("not lysands domain")); @@ -155,10 +172,10 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result "unlisted", }; if let Some(obj) = quote { - println!("Quoting: {}", obj.dereference(&data.to_request_data()).await.unwrap().url); + println!("Quoting: {}", db_post_from_url(note.replies_to.unwrap()).await?.url); } if let Some(obj) = reply { - println!("Replying to: {}", obj.dereference(&data.to_request_data()).await.unwrap().url); + println!("Replying to: {}", db_post_from_url(note.quotes.unwrap()).await?.url); } let post = entities::post::ActiveModel { id: Set(note.id.to_string()),