From 5c1e753761564d6daf728f469de81a34bf8a6646 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 17 Nov 2023 12:23:14 +0100 Subject: [PATCH] use hashmap instead of vec for unique ids --- Cargo.lock | 402 +++++++++++++++++- Cargo.toml | 2 +- src/api.rs | 7 +- src/database.rs | 9 +- src/federation/activities/accept.rs | 4 +- src/federation/activities/follow.rs | 6 +- src/federation/mod.rs | 10 +- src/federation/objects/article.rs | 9 +- src/federation/objects/articles_collection.rs | 10 +- src/federation/objects/instance.rs | 22 +- tests/common.rs | 2 +- tests/test.rs | 9 +- 12 files changed, 450 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 00177fe..dd2628b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "activitypub_federation" -version = "0.4.7" +version = "0.5.0-beta.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41a0b760e573d41d9f6aec4280e4c0d273d4ce724eb00c3e4c9fb1b98d3ede47" +checksum = "9a122cf2c2adf45b164134946bc069659cd93083fab294839a3f1d794b707c17" dependencies = [ "activitystreams-kinds", "anyhow", @@ -18,6 +18,7 @@ dependencies = [ "derive_builder", "dyn-clone", "enum_delegate", + "futures", "futures-core", "http", "http-signature-normalization", @@ -25,6 +26,7 @@ dependencies = [ "httpdate", "hyper", "itertools", + "moka", "once_cell", "openssl", "pin-project-lite", @@ -96,6 +98,35 @@ version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" +[[package]] +name = "async-io" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af" +dependencies = [ + "async-lock", + "autocfg", + "cfg-if", + "concurrent-queue", + "futures-lite", + "log", + "parking", + "polling", + "rustix 0.37.27", + "slab", + "socket2 0.4.10", + "waker-fn", +] + +[[package]] +name = "async-lock" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" +dependencies = [ + "event-listener", +] + [[package]] name = "async-trait" version = "0.1.74" @@ -229,12 +260,49 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +[[package]] +name = "bytecount" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1e5f035d16fc623ae5f74981db80a439803888314e3a555fd6f04acd51a3205" + [[package]] name = "bytes" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +[[package]] +name = "camino" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-platform" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12024c4645c97566567129c204f65d5815a8c9aecf30fcbe682b2fe034996d36" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4acbb09d9ee8e23699b9634375c72795d095bf268439da88562cf9b501f181fa" +dependencies = [ + "camino", + "cargo-platform", + "semver", + "serde", + "serde_json", +] + [[package]] name = "cc" version = "1.0.83" @@ -265,6 +333,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "concurrent-queue" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f057a694a54f12365049b0958a1685bb52d567f5593b355fbf685838e873d400" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -290,6 +367,38 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -459,6 +568,30 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "error-chain" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" +dependencies = [ + "version_check", +] + +[[package]] +name = "event-listener" +version = "2.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" + +[[package]] +name = "fastrand" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +dependencies = [ + "instant", +] + [[package]] name = "fastrand" version = "2.0.1" @@ -567,6 +700,21 @@ version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" +[[package]] +name = "futures-lite" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce" +dependencies = [ + "fastrand 1.9.0", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite", + "waker-fn", +] + [[package]] name = "futures-macro" version = "0.3.29" @@ -635,6 +783,12 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "h2" version = "0.3.21" @@ -857,6 +1011,26 @@ dependencies = [ "hashbrown 0.14.2", ] +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "io-lifetimes" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys", +] + [[package]] name = "ipnet" version = "2.9.0" @@ -899,6 +1073,12 @@ version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +[[package]] +name = "linux-raw-sys" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" + [[package]] name = "linux-raw-sys" version = "0.4.11" @@ -921,6 +1101,15 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "mach2" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d0d1830bcd151a6fc4aea1369af235b36c1528fe976b8ff678683c9995eade8" +dependencies = [ + "libc", +] + [[package]] name = "matchit" version = "0.7.3" @@ -933,6 +1122,15 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -969,6 +1167,31 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "moka" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa6e72583bf6830c956235bff0d5afec8cf2952f579ebad18ae7821a917d950f" +dependencies = [ + "async-io", + "async-lock", + "crossbeam-channel", + "crossbeam-epoch", + "crossbeam-utils", + "futures-util", + "once_cell", + "parking_lot", + "quanta", + "rustc_version", + "scheduled-thread-pool", + "skeptic", + "smallvec", + "tagptr", + "thiserror", + "triomphe", + "uuid", +] + [[package]] name = "native-tls" version = "0.2.11" @@ -1065,6 +1288,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "parking" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" + [[package]] name = "parking_lot" version = "0.12.1" @@ -1132,6 +1361,22 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +[[package]] +name = "polling" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" +dependencies = [ + "autocfg", + "bitflags 1.3.2", + "cfg-if", + "concurrent-queue", + "libc", + "log", + "pin-project-lite", + "windows-sys", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1147,6 +1392,33 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pulldown-cmark" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a1a2f1f0a7ecff9c31abbe177637be0e97a0aef46cf8738ece09327985d998" +dependencies = [ + "bitflags 1.3.2", + "memchr", + "unicase", +] + +[[package]] +name = "quanta" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a17e662a7a8291a865152364c20c7abc5e60486ab2001e8ec10b24862de0b9ab" +dependencies = [ + "crossbeam-utils", + "libc", + "mach2", + "once_cell", + "raw-cpuid", + "wasi", + "web-sys", + "winapi", +] + [[package]] name = "quote" version = "1.0.33" @@ -1186,6 +1458,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "raw-cpuid" +version = "10.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c297679cb867470fa8c9f67dbba74a78d78e3e98d7cf2b08d6d71540f797332" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -1286,6 +1567,29 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "0.37.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2" +dependencies = [ + "bitflags 1.3.2", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys 0.3.8", + "windows-sys", +] + [[package]] name = "rustix" version = "0.38.23" @@ -1295,7 +1599,7 @@ dependencies = [ "bitflags 2.4.1", "errno", "libc", - "linux-raw-sys", + "linux-raw-sys 0.4.11", "windows-sys", ] @@ -1311,6 +1615,15 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "schannel" version = "0.1.22" @@ -1320,6 +1633,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "scheduled-thread-pool" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cbc66816425a074528352f5789333ecff06ca41b36b0b0efdfbb29edc391a19" +dependencies = [ + "parking_lot", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -1349,6 +1671,15 @@ dependencies = [ "libc", ] +[[package]] +name = "semver" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" +dependencies = [ + "serde", +] + [[package]] name = "serde" version = "1.0.192" @@ -1459,6 +1790,21 @@ dependencies = [ "libc", ] +[[package]] +name = "skeptic" +version = "0.13.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16d23b015676c90a0f01c197bfdc786c20342c73a0afdda9025adb0bc42940a8" +dependencies = [ + "bytecount", + "cargo_metadata", + "error-chain", + "glob", + "pulldown-cmark", + "tempfile", + "walkdir", +] + [[package]] name = "slab" version = "0.4.9" @@ -1549,6 +1895,12 @@ dependencies = [ "libc", ] +[[package]] +name = "tagptr" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" + [[package]] name = "task-local-extensions" version = "0.1.4" @@ -1565,9 +1917,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" dependencies = [ "cfg-if", - "fastrand", + "fastrand 2.0.1", "redox_syscall", - "rustix", + "rustix 0.38.23", "windows-sys", ] @@ -1720,6 +2072,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "triomphe" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee8098afad3fb0c54a9007aab6804558410503ad676d4633f9c2559a00ac0f" + [[package]] name = "try-lock" version = "0.2.4" @@ -1774,6 +2132,15 @@ dependencies = [ "serde", ] +[[package]] +name = "uuid" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ad59a7560b41a70d191093a945f0b87bc1deeda46fb237479708a1d6b6cdfc" +dependencies = [ + "getrandom", +] + [[package]] name = "vcpkg" version = "0.2.15" @@ -1786,6 +2153,22 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "waker-fn" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" + +[[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -1906,6 +2289,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 34cad1c..a914777 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -activitypub_federation = { version = "0.4.7", features = ["axum"], default-features = false } +activitypub_federation = { version = "0.5.0-beta.4", features = ["axum"], default-features = false } anyhow = "1.0.75" async-trait = "0.1.74" axum = "0.6.20" diff --git a/src/api.rs b/src/api.rs index ae5c646..949b23d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -43,7 +43,7 @@ async fn create_article( local: true, }; let mut articles = data.articles.lock().unwrap(); - articles.push(article.clone()); + articles.insert(article.ap_id.inner().clone(), article.clone()); Ok(Json(article)) } @@ -60,8 +60,9 @@ async fn get_article( let articles = data.articles.lock().unwrap(); let article = articles .iter() - .find(|a| a.title == query.title) + .find(|a| a.1.title == query.title) .ok_or(anyhow!("not found"))? + .1 .clone(); Ok(Json(article)) } @@ -78,7 +79,7 @@ async fn resolve_object( ) -> MyResult> { let instance: DbInstance = ObjectId::from(query.id).dereference(&data).await?; let mut instances = data.instances.lock().unwrap(); - instances.push(instance.clone()); + instances.insert(instance.ap_id.inner().clone(), instance.clone()); Ok(Json(instance)) } diff --git a/src/database.rs b/src/database.rs index 593424e..3c5e519 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,18 +1,19 @@ use crate::federation::objects::article::DbArticle; use crate::federation::objects::instance::DbInstance; - +use std::collections::HashMap; use std::sync::{Arc, Mutex}; +use url::Url; pub type DatabaseHandle = Arc; pub struct Database { - pub instances: Mutex>, - pub articles: Mutex>, + pub instances: Mutex>, + pub articles: Mutex>, } impl Database { pub fn local_instance(&self) -> DbInstance { let lock = self.instances.lock().unwrap(); - lock.iter().find(|i| i.local).unwrap().clone() + lock.iter().find(|i| i.1.local).unwrap().1.clone() } } diff --git a/src/federation/activities/accept.rs b/src/federation/activities/accept.rs index 894364b..487be2b 100644 --- a/src/federation/activities/accept.rs +++ b/src/federation/activities/accept.rs @@ -49,8 +49,8 @@ impl ActivityHandler for Accept { async fn receive(self, data: &Data) -> Result<(), Self::Error> { // add to follows - let mut instances = data.instances.lock().unwrap(); - let local_instance = instances.first_mut().unwrap(); + let mut lock = data.instances.lock().unwrap(); + let local_instance = lock.iter_mut().find(|i| i.1.local).unwrap().1; local_instance.follows.push(self.actor.inner().clone()); Ok(()) } diff --git a/src/federation/activities/follow.rs b/src/federation/activities/follow.rs index 07fc85f..123f1e7 100644 --- a/src/federation/activities/follow.rs +++ b/src/federation/activities/follow.rs @@ -50,13 +50,15 @@ impl ActivityHandler for Follow { } async fn receive(self, data: &Data) -> Result<(), Self::Error> { + dbg!(&self); // add to followers let local_instance = { - let mut instances = data.instances.lock().unwrap(); - let local_instance = instances.first_mut().unwrap(); + let mut lock = data.instances.lock().unwrap(); + let local_instance = lock.iter_mut().find(|i| i.1.local).unwrap().1; local_instance.followers.push(self.actor.inner().clone()); local_instance.clone() }; + dbg!(&local_instance.followers.len()); // send back an accept let follower = self.actor.dereference(data).await?; diff --git a/src/federation/mod.rs b/src/federation/mod.rs index 6b3c829..011db5c 100644 --- a/src/federation/mod.rs +++ b/src/federation/mod.rs @@ -4,6 +4,7 @@ use crate::federation::objects::instance::DbInstance; use activitypub_federation::config::FederationConfig; use activitypub_federation::http_signatures::generate_actor_keypair; use chrono::Local; +use std::collections::HashMap; use std::sync::{Arc, Mutex}; use url::Url; @@ -22,14 +23,17 @@ pub async fn federation_config(hostname: &str) -> Result) -> Result { - let post = DbArticle { + let article = DbArticle { title: json.name, text: json.content, ap_id: json.id, @@ -82,7 +83,7 @@ impl Object for DbArticle { }; let mut lock = data.articles.lock().unwrap(); - lock.push(post.clone()); - Ok(post) + lock.insert(article.ap_id.inner().clone(), article.clone()); + Ok(article) } } diff --git a/src/federation/objects/articles_collection.rs b/src/federation/objects/articles_collection.rs index ae9e023..d02c17d 100644 --- a/src/federation/objects/articles_collection.rs +++ b/src/federation/objects/articles_collection.rs @@ -40,6 +40,7 @@ impl Collection for DbArticleCollection { let articles = data.articles.lock().unwrap(); articles .iter() + .map(|a| a.1) .filter(|a| a.local) .clone() .cloned() @@ -52,7 +53,7 @@ impl Collection for DbArticleCollection { .collect::>(), ) .await?; - let ap_id = generate_object_id(data.local_instance().ap_id.inner())?.into(); + let ap_id = generate_object_id(data.local_instance().ap_id.inner())?; let collection = ArticleCollection { r#type: Default::default(), id: ap_id, @@ -78,15 +79,16 @@ impl Collection for DbArticleCollection { where Self: Sized, { - let mut articles = try_join_all( + let articles = try_join_all( apub.items .into_iter() .map(|i| DbArticle::from_json(i, data)), ) .await?; let mut lock = data.articles.lock().unwrap(); - // TODO: need to overwrite existing items - lock.append(&mut articles); + for a in &articles { + lock.insert(a.ap_id.inner().clone(), a.clone()); + } // TODO: return value propably not needed Ok(DbArticleCollection(articles)) } diff --git a/src/federation/objects/instance.rs b/src/federation/objects/instance.rs index 2745cbf..e17f7fd 100644 --- a/src/federation/objects/instance.rs +++ b/src/federation/objects/instance.rs @@ -1,16 +1,16 @@ use crate::error::Error; -use crate::federation::objects::articles_collection::{ArticleCollection, DbArticleCollection}; +use crate::federation::objects::articles_collection::DbArticleCollection; use crate::{database::DatabaseHandle, federation::activities::follow::Follow}; +use activitypub_federation::activity_sending::SendActivityTask; use activitypub_federation::fetch::collection_id::CollectionId; use activitypub_federation::kinds::actor::ServiceType; use activitypub_federation::{ - activity_queue::send_activity, config::Data, fetch::object_id::ObjectId, protocol::{context::WithContext, public_key::PublicKey, verification::verify_domains_match}, traits::{ActivityHandler, Actor, Object}, }; -use chrono::{Local, NaiveDateTime}; +use chrono::{DateTime, Local, Utc}; use serde::{Deserialize, Serialize}; use std::fmt::Debug; use url::Url; @@ -22,7 +22,7 @@ pub struct DbInstance { pub inbox: Url, pub(crate) public_key: String, pub(crate) private_key: Option, - pub(crate) last_refreshed_at: NaiveDateTime, + pub(crate) last_refreshed_at: DateTime, pub followers: Vec, pub follows: Vec, pub local: bool, @@ -70,7 +70,10 @@ impl DbInstance { ::Error: From + From, { let activity = WithContext::new_default(activity); - send_activity(activity, self, recipients, data).await?; + let sends = SendActivityTask::prepare(&activity, self, recipients, data).await?; + for send in sends { + send.sign_and_send(data).await?; + } Ok(()) } } @@ -81,7 +84,7 @@ impl Object for DbInstance { type Kind = Instance; type Error = Error; - fn last_refreshed_at(&self) -> Option { + fn last_refreshed_at(&self) -> Option> { Some(self.last_refreshed_at) } @@ -93,6 +96,7 @@ impl Object for DbInstance { let res = users .clone() .into_iter() + .map(|u| u.1) .find(|u| u.ap_id.inner() == &object_id); Ok(res) } @@ -123,15 +127,15 @@ impl Object for DbInstance { inbox: json.inbox, public_key: json.public_key.public_key_pem, private_key: None, - last_refreshed_at: Local::now().naive_local(), + last_refreshed_at: Local::now().into(), followers: vec![], follows: vec![], local: false, }; // TODO: very inefficient to sync all articles every time - instance.articles_id.dereference(&instance, &data).await?; + instance.articles_id.dereference(&instance, data).await?; let mut mutex = data.instances.lock().unwrap(); - mutex.push(instance.clone()); + mutex.insert(instance.ap_id.inner().clone(), instance.clone()); Ok(instance) } } diff --git a/tests/common.rs b/tests/common.rs index 50d5477..c868842 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -6,7 +6,7 @@ use serde::ser::Serialize; use std::sync::Once; use tracing::log::LevelFilter; -pub static CLIENT: Lazy = Lazy::new(|| Client::new()); +pub static CLIENT: Lazy = Lazy::new(Client::new); pub fn setup() { static INIT: Once = Once::new(); diff --git a/tests/test.rs b/tests/test.rs index b88d4b1..03ed58a 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -26,7 +26,8 @@ async fn test_create_and_read_article() -> MyResult<()> { title: "Manu_Chao".to_string(), }; let not_found = - get_query::(hostname, &format!("article"), Some(get_article.clone())).await; + get_query::(hostname, &"article".to_string(), Some(get_article.clone())) + .await; assert!(not_found.is_err()); // create article @@ -40,7 +41,7 @@ async fn test_create_and_read_article() -> MyResult<()> { // now article can be read let get_res: DbArticle = - get_query(hostname, &format!("article"), Some(get_article.clone())).await?; + get_query(hostname, &"article".to_string(), Some(get_article.clone())).await?; assert_eq!(create_article.title, get_res.title); assert_eq!(create_article.text, get_res.text); assert!(get_res.local); @@ -126,7 +127,7 @@ async fn test_synchronize_articles() -> MyResult<()> { }; let get_res = get_query::( hostname_beta, - &format!("article"), + &"article".to_string(), Some(get_article.clone()), ) .await; @@ -141,7 +142,7 @@ async fn test_synchronize_articles() -> MyResult<()> { // get the article and compare let get_res: DbArticle = get_query( hostname_beta, - &format!("article"), + &"article".to_string(), Some(get_article.clone()), ) .await?;