diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..422c1bd --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +DATABASE_URL="sqlite:///home/aprl/Documents/lysand-ap-layer/db.sqlite?mode=rwc" +LYSAND_DOMAIN="social.lysand.org" +API_DOMAIN="ap.lysand.org" +RUST_LOG="debug" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 39f22b2..03e16ed 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ /result-lib .direnv migration/target -db.sqlite \ No newline at end of file +db.sqlite +.env \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index eb62c0d..da608e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1027,6 +1027,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dotenvy" version = "0.15.7" @@ -1908,6 +1914,7 @@ dependencies = [ "async_once", "chrono", "clap", + "dotenv", "enum_delegate", "env_logger", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index dc6dad7..4b47ca8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ async_once = "0.2.6" reqwest = { version = "0.12.4", features = ["blocking", "json", "multipart"] } time = { version = "0.3.36", features = ["serde"] } serde_derive = "1.0.201" +dotenv = "0.15.0" [dependencies.sea-orm] version = "0.12.0" diff --git a/migration/Cargo.lock b/migration/Cargo.lock index f852531..bd6aa45 100644 --- a/migration/Cargo.lock +++ b/migration/Cargo.lock @@ -583,6 +583,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dotenvy" version = "0.15.7" @@ -1129,6 +1135,7 @@ version = "0.1.0" dependencies = [ "async-std", "chrono", + "dotenv", "sea-orm-migration", ] diff --git a/migration/Cargo.toml b/migration/Cargo.toml index edbcccf..579479a 100644 --- a/migration/Cargo.toml +++ b/migration/Cargo.toml @@ -11,6 +11,7 @@ path = "src/lib.rs" [dependencies] async-std = { version = "1", features = ["attributes", "tokio1"] } chrono = "0.4.38" +dotenv = "0.15.0" [dependencies.sea-orm-migration] version = "0.12.0" diff --git a/migration/src/main.rs b/migration/src/main.rs index c6b6e48..98a18fc 100644 --- a/migration/src/main.rs +++ b/migration/src/main.rs @@ -1,6 +1,8 @@ use sea_orm_migration::prelude::*; +use dotenv::dotenv; #[async_std::main] async fn main() { + dotenv().ok(); cli::run_cli(migration::Migrator).await; } diff --git a/nix-bootstrap.sh b/nix-bootstrap.sh new file mode 100644 index 0000000..65f6b25 --- /dev/null +++ b/nix-bootstrap.sh @@ -0,0 +1,2 @@ +nix run .#ls-ap-migration +nix run .#lysand-ap-layer \ No newline at end of file diff --git a/src/lysand/conversion.rs b/src/lysand/conversion.rs index fa3d46c..6a1941c 100644 --- a/src/lysand/conversion.rs +++ b/src/lysand/conversion.rs @@ -76,6 +76,7 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result = generate_object_id(data.domain(), ¬e.id.to_string())?.into(); let user_id = generate_user_id(data.domain(), &target.id.to_string())?; let user = fetch_user_from_url(note.author.clone()).await?; + let data = FEDERATION_CONFIG.get().unwrap(); let mut tag: Vec = Vec::new(); for l_tag in note.mentions.clone().unwrap_or_default() { tag.push(Mention { href: l_tag, //TODO convert to ap url @@ -94,7 +95,18 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result> = if let Some(rep) = note.replies_to.clone() { let note = fetch_note_from_url(rep).await?; let fake_rep_url = Url::parse(&format!( - "https://{}/lysand/apnote/{}", + "https://{}/apbridge/object/{}", + API_DOMAIN.to_string(), + ¬e.id.to_string() + ))?; + Some(fake_rep_url.into()) + } else { + None + }; + let quote: Option> = if let Some(rep) = note.quotes.clone() { + let note = fetch_note_from_url(rep).await?; + let fake_rep_url = Url::parse(&format!( + "https://{}/apbridge/object/{}", API_DOMAIN.to_string(), ¬e.id.to_string() ))?; @@ -105,7 +117,7 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result = if let Some(rep) = note.replies_to { let note = fetch_note_from_url(rep).await?; let fake_rep_url = Url::parse(&format!( - "https://{}/lysand/apnote/{}", + "https://{}/apbridge/object/{}", API_DOMAIN.to_string(), ¬e.id.to_string() ))?; @@ -116,7 +128,7 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result = if let Some(rep) = note.quotes.clone() { let note = fetch_note_from_url(rep).await?; let fake_rep_url = Url::parse(&format!( - "https://{}/lysand/apnote/{}", + "https://{}/apbridge/object/{}", API_DOMAIN.to_string(), ¬e.id.to_string() ))?; @@ -133,7 +145,7 @@ pub async fn receive_lysand_note(note: Note, db_id: String) -> anyhow::Result anyhow::Result "direct", super::objects::VisibilityType::Unlisted => "unlisted", }; - + if let Some(obj) = quote { + println!("Quoting: {}", obj.dereference(&data.to_request_data()).await.unwrap().url); + } + if let Some(obj) = reply { + println!("Replying to: {}", obj.dereference(&data.to_request_data()).await.unwrap().url); + } let post = entities::post::ActiveModel { id: Set(note.id.to_string()), creator: Set(lysand_author.id.clone()), diff --git a/src/lysand/http.rs b/src/lysand/http.rs index 229193d..8f08871 100644 --- a/src/lysand/http.rs +++ b/src/lysand/http.rs @@ -7,18 +7,21 @@ use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; use crate::{database::State, entities::{post::{self, Entity}, prelude}, error, Response, DB, FEDERATION_CONFIG}; #[get("/apbridge/object/{post}")] -async fn post_manually( - path: web::Path<(String, String)>, +async fn fetch_post( + path: web::Path, state: web::Data, ) -> actix_web::Result { let db = DB.get().unwrap(); let post = prelude::Post::find() - .filter(post::Column::Id.eq(path.0.as_str())) + .filter(post::Column::Id.eq(path.as_str())) .one(db) .await?; - let post = post.unwrap(); + let post = match post { + Some(post) => post, + None => return Ok(HttpResponse::NotFound().finish()), + }; Ok(HttpResponse::Ok().content_type(FEDERATION_CONTENT_TYPE).json(post.into_json(&FEDERATION_CONFIG.get().unwrap().to_request_data()).await?)) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0119c63..91eabef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ use crate::{ }; use crate::{activities::follow::Follow, entities::user}; use lazy_static::lazy_static; +use dotenv::dotenv; mod activities; mod database; @@ -152,6 +153,7 @@ static FEDERATION_CONFIG: OnceLock> = OnceLock::new(); #[actix_web::main] async fn main() -> actix_web::Result<(), anyhow::Error> { + dotenv().ok(); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); let ap_id = Url::parse(&format!( diff --git a/src/objects/post.rs b/src/objects/post.rs index 9b0ba09..6a1c3df 100644 --- a/src/objects/post.rs +++ b/src/objects/post.rs @@ -58,7 +58,7 @@ impl Object for post::Model { let post = crate::entities::prelude::Post::find() .filter(post::Column::Id.eq(object_id.to_string())) .one(data.app_data().database_connection.clone().as_ref()).await; - Ok(None) + Ok(post.unwrap()) } async fn into_json(self, _data: &Data) -> Result {