From c9338027f26170dffc95a052ecfc3def1bf3a729 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 3 Jul 2020 14:20:28 +0200 Subject: [PATCH] Migrate user and group to new activitystreams library --- docker/federation-test/run-tests.sh | 4 + docker/federation/docker-compose.yml | 1 + server/Cargo.lock | 145 +++++++++++++++++++-------- server/src/apub/comment.rs | 2 +- server/src/apub/community.rs | 136 +++++++++++++------------ server/src/apub/fetcher.rs | 91 +++++++++-------- server/src/apub/mod.rs | 18 ++-- server/src/apub/post.rs | 2 +- server/src/apub/private_message.rs | 2 +- server/src/apub/shared_inbox.rs | 88 ++++++++-------- server/src/apub/user.rs | 118 ++++++++++------------ server/src/apub/user_inbox.rs | 16 +-- 12 files changed, 346 insertions(+), 277 deletions(-) diff --git a/docker/federation-test/run-tests.sh b/docker/federation-test/run-tests.sh index 57c6cc8f..3848414b 100755 --- a/docker/federation-test/run-tests.sh +++ b/docker/federation-test/run-tests.sh @@ -1,6 +1,10 @@ #!/bin/bash set -e +# make sure there are no old containers or old data around +sudo docker-compose --file ../federation/docker-compose.yml --project-directory . down +sudo rm -rf volumes + pushd ../../server/ cargo build popd diff --git a/docker/federation/docker-compose.yml b/docker/federation/docker-compose.yml index cbc648e6..c552d18f 100644 --- a/docker/federation/docker-compose.yml +++ b/docker/federation/docker-compose.yml @@ -110,3 +110,4 @@ services: image: dogbin/iframely:latest volumes: - ../iframely.config.local.js:/iframely/config.local.js:ro + restart: always diff --git a/server/Cargo.lock b/server/Cargo.lock index c54419e1..6d6364d4 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -63,7 +63,7 @@ dependencies = [ "futures-util", "log", "once_cell", - "parking_lot", + "parking_lot 0.10.2", "pin-project", "smallvec", "tokio", @@ -269,7 +269,7 @@ dependencies = [ "lazy_static", "log", "num_cpus", - "parking_lot", + "parking_lot 0.10.2", "threadpool", ] @@ -397,6 +397,12 @@ dependencies = [ "gimli", ] +[[package]] +name = "adler" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccc9a9dd069569f212bc4330af9f17c4afb5e8ce185e83dbb14f1349dda18b10" + [[package]] name = "adler32" version = "1.1.0" @@ -506,7 +512,7 @@ dependencies = [ "addr2line", "cfg-if", "libc", - "miniz_oxide", + "miniz_oxide 0.3.7", "object", "rustc-demangle", ] @@ -680,9 +686,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.55" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1be3409f94d7bdceeb5f5fac551039d9b3f00e25da7a74fc4d33400a0d96368" +checksum = "0fde55d2a2bfaa4c9668bbc63f531fbdeee3ffe188f4662511ce2c22b3eedebe" [[package]] name = "cfg-if" @@ -692,9 +698,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "chrono" -version = "0.4.11" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" +checksum = "f0fee792e164f78f5fe0c296cc2eb3688a2ca2b70cdff33040922d298203f0c4" dependencies = [ "num-integer", "num-traits 0.2.12", @@ -726,6 +732,15 @@ dependencies = [ "bitflags", ] +[[package]] +name = "cloudabi" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4344512281c643ae7638bbabc3af17a11307803ec8f0fcad9fae512a8bf36467" +dependencies = [ + "bitflags", +] + [[package]] name = "comrak" version = "0.7.0" @@ -1120,14 +1135,14 @@ dependencies = [ [[package]] name = "flate2" -version = "1.0.14" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cfff41391129e0a856d6d822600b8d71179d46879e310417eb9c762eb178b42" +checksum = "68c90b0fc46cf89d227cc78b40e494ff81287a92dd07631e5af0d06fe3cf885e" dependencies = [ "cfg-if", "crc32fast", "libc", - "miniz_oxide", + "miniz_oxide 0.4.0", ] [[package]] @@ -1470,6 +1485,12 @@ dependencies = [ "autocfg 1.0.0", ] +[[package]] +name = "instant" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69da7ce1490173c2bf4d26bc8be429aaeeaf4cce6c4b970b7949651fa17655fe" + [[package]] name = "iovec" version = "0.1.4" @@ -1508,18 +1529,18 @@ checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" [[package]] name = "js-sys" -version = "0.3.40" +version = "0.3.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce10c23ad2ea25ceca0093bd3192229da4c5b3c0f2de499c1ecac0d98d452177" +checksum = "c4b9172132a62451e56142bff9afc91c8e4a4500aa5b847da36815b63bfda916" dependencies = [ "wasm-bindgen", ] [[package]] name = "jsonwebtoken" -version = "7.1.2" +version = "7.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f325ae57ddcf609f02d891486ce740f5bbd0cc3e93f9bffaacdf6594b21404" +checksum = "afabcc15e437a6484fc4f12d0fd63068fe457bf93f1c148d3d9649c60b103f32" dependencies = [ "base64 0.12.3", "pem", @@ -1676,6 +1697,15 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "lock_api" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de302ce1fe7482db13738fbaf2e21cfb06a986b89c0bf38d88abf16681aada4e" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.8" @@ -1781,6 +1811,15 @@ dependencies = [ "adler32", ] +[[package]] +name = "miniz_oxide" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be0f75932c1f6cfae3c04000e40114adf955636e19040f9c0a2c380702aa1c7f" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.6.22" @@ -1985,8 +2024,19 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" dependencies = [ - "lock_api", - "parking_lot_core", + "lock_api 0.3.4", + "parking_lot_core 0.7.2", +] + +[[package]] +name = "parking_lot" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4893845fa2ca272e647da5d0e46660a314ead9c2fdd9a883aabc32e481a8733" +dependencies = [ + "instant", + "lock_api 0.4.0", + "parking_lot_core 0.8.0", ] [[package]] @@ -1996,7 +2046,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" dependencies = [ "cfg-if", - "cloudabi", + "cloudabi 0.0.3", + "libc", + "redox_syscall", + "smallvec", + "winapi 0.3.9", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c361aa727dd08437f2f1447be8b59a33b0edd15e0fcee698f935613d9efbca9b" +dependencies = [ + "cfg-if", + "cloudabi 0.1.0", + "instant", "libc", "redox_syscall", "smallvec", @@ -2164,12 +2229,12 @@ dependencies = [ [[package]] name = "r2d2" -version = "0.8.8" +version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1497e40855348e4a8a40767d8e55174bce1e445a3ac9254ad44ad468ee0485af" +checksum = "545c5bc2b880973c9c10e4067418407a0ccaa3091781d1671d46eb35107cb26f" dependencies = [ "log", - "parking_lot", + "parking_lot 0.11.0", "scheduled-thread-pool", ] @@ -2306,7 +2371,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" dependencies = [ - "cloudabi", + "cloudabi 0.0.3", "fuchsia-cprng", "libc", "rand_core 0.4.2", @@ -2462,11 +2527,11 @@ dependencies = [ [[package]] name = "scheduled-thread-pool" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0988d7fdf88d5e5fcf5923a0f1e8ab345f3e98ab4bc6bc45a2d5ff7f7458fbf6" +checksum = "dc6f74fd1204073fa02d5d5d68bec8021be4c38690b61264b2fdb48083d0e7d7" dependencies = [ - "parking_lot", + "parking_lot 0.11.0", ] [[package]] @@ -2570,9 +2635,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.55" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec2c5d7e739bc07a3e73381a39d61fdb5f671c60c1df26a130690665803d8226" +checksum = "3433e879a558dde8b5e8feb2a04899cf34fdde1fafb894687e52105fc1162ac3" dependencies = [ "indexmap", "itoa", @@ -3106,9 +3171,9 @@ checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" [[package]] name = "unicode-width" -version = "0.1.7" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" [[package]] name = "unicode-xid" @@ -3222,9 +3287,9 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" [[package]] name = "wasm-bindgen" -version = "0.2.63" +version = "0.2.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c2dc4aa152834bc334f506c1a06b866416a8b6697d5c9f75b9a689c8486def0" +checksum = "6a634620115e4a229108b71bde263bb4220c483b3f07f5ba514ee8d15064c4c2" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -3232,9 +3297,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.63" +version = "0.2.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ded84f06e0ed21499f6184df0e0cb3494727b0c5da89534e0fcc55c51d812101" +checksum = "3e53963b583d18a5aa3aaae4b4c1cb535218246131ba22a71f05b518098571df" dependencies = [ "bumpalo", "lazy_static", @@ -3247,9 +3312,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.63" +version = "0.2.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "838e423688dac18d73e31edce74ddfac468e37b1506ad163ffaf0a46f703ffe3" +checksum = "3fcfd5ef6eec85623b4c6e844293d4516470d8f19cd72d0d12246017eb9060b8" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -3257,9 +3322,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.63" +version = "0.2.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3156052d8ec77142051a533cdd686cba889537b213f948cd1d20869926e68e92" +checksum = "9adff9ee0e94b926ca81b57f57f86d5545cdcb1d259e21ec9bdd95b901754c75" dependencies = [ "proc-macro2", "quote", @@ -3270,15 +3335,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.63" +version = "0.2.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9ba19973a58daf4db6f352eda73dc0e289493cd29fb2632eb172085b6521acd" +checksum = "7f7b90ea6c632dd06fd765d44542e234d5e63d9bb917ecd64d79778a13bd79ae" [[package]] name = "web-sys" -version = "0.3.40" +version = "0.3.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b72fe77fd39e4bd3eaa4412fd299a0be6b3dfe9d2597e2f1c20beb968f41d17" +checksum = "863539788676619aac1a23e2df3655e96b32b0e05eb72ca34ba045ad573c625d" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index a42a52c2..dbc15909 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -123,7 +123,7 @@ impl FromApub for CommentForm { /// Parse an ActivityPub note received from another instance into a Lemmy comment async fn from_apub( - note: &Note, + note: &mut Note, client: &Client, pool: &DbPool, ) -> Result { diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index f866511c..bfc896af 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -4,7 +4,7 @@ use crate::{ create_apub_response, create_apub_tombstone_response, create_tombstone, - extensions::{group_extensions::GroupExtension, signatures::PublicKey}, + extensions::group_extensions::GroupExtension, fetcher::get_or_fetch_and_upsert_remote_user, get_shared_inbox, ActorType, @@ -27,21 +27,25 @@ use crate::{ }; use activitystreams::{ activity::{Accept, Announce, Delete, Remove, Undo}, - actor::{kind::GroupType, properties::ApActorProperties, Group}, - collection::UnorderedCollection, - context, - endpoint::EndpointProperties, - object::properties::ObjectProperties, Activity, Base, BaseBox, }; -use activitystreams_ext::Ext3; -use activitystreams_new::{activity::Follow, object::Tombstone}; +use activitystreams_ext::Ext2; +use activitystreams_new::{ + activity::Follow, + actor::{kind::GroupType, ApActor, Endpoints, Group}, + base::BaseExt, + collection::UnorderedCollection, + context, + object::Tombstone, + prelude::*, + primitives::{XsdAnyUri, XsdDateTime}, +}; use actix_web::{body::Body, client::Client, web, HttpResponse}; use itertools::Itertools; use serde::{Deserialize, Serialize}; -use std::fmt::Debug; +use std::{fmt::Debug, str::FromStr}; #[derive(Deserialize)] pub struct CommunityQuery { @@ -54,9 +58,6 @@ impl ToApub for Community { // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network. async fn to_apub(&self, pool: &DbPool) -> Result { - let mut group = Group::default(); - let oprops: &mut ObjectProperties = group.as_mut(); - // The attributed to, is an ordered vector with the creator actor_ids first, // then the rest of the moderators // TODO Technically the instance admins can mod the community, but lets @@ -66,36 +67,36 @@ impl ToApub for Community { CommunityModeratorView::for_community(&conn, id) }) .await??; - let moderators = moderators.into_iter().map(|m| m.user_actor_id).collect(); + let moderators: Vec = moderators.into_iter().map(|m| m.user_actor_id).collect(); - oprops - .set_context_xsd_any_uri(context())? - .set_id(self.actor_id.to_owned())? - .set_name_xsd_string(self.name.to_owned())? - .set_published(convert_datetime(self.published))? - .set_many_attributed_to_xsd_any_uris(moderators)?; + let mut group = Group::new(); + group + .set_context(context()) + .set_id(XsdAnyUri::from_str(&self.actor_id)?) + .set_name(self.name.to_owned()) + .set_published(XsdDateTime::from(convert_datetime(self.published))) + .set_many_attributed_tos(moderators); if let Some(u) = self.updated.to_owned() { - oprops.set_updated(convert_datetime(u))?; + group.set_updated(XsdDateTime::from(convert_datetime(u))); } if let Some(d) = self.description.to_owned() { // TODO: this should be html, also add source field with raw markdown // -> same for post.content and others - oprops.set_content_xsd_string(d)?; + group.set_content(d); } - let mut endpoint_props = EndpointProperties::default(); - - endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?; - - let mut actor_props = ApActorProperties::default(); - - actor_props - .set_preferred_username(self.title.to_owned())? - .set_inbox(self.get_inbox_url())? - .set_outbox(self.get_outbox_url())? - .set_endpoints(endpoint_props)? - .set_followers(self.get_followers_url())?; + let mut ap_actor = ApActor::new(self.get_inbox_url().parse()?, group); + ap_actor + .set_preferred_username(self.title.to_owned()) + .set_outbox(self.get_outbox_url().parse()?) + .set_followers(self.get_followers_url().parse()?) + .set_following(self.get_following_url().parse()?) + .set_liked(self.get_liked_url().parse()?) + .set_endpoints(Endpoints { + shared_inbox: Some(self.get_shared_inbox_url().parse()?), + ..Default::default() + }); let nsfw = self.nsfw; let category_id = self.category_id; @@ -104,10 +105,9 @@ impl ToApub for Community { }) .await??; - Ok(Ext3::new( - group, + Ok(Ext2::new( + ap_actor, group_extension, - actor_props, self.get_public_key_ext(), )) } @@ -367,38 +367,52 @@ impl FromApub for CommunityForm { type ApubType = GroupExt; /// Parse an ActivityPub group received from another instance into a Lemmy community. - async fn from_apub(group: &GroupExt, client: &Client, pool: &DbPool) -> Result { - let group_extensions: &GroupExtension = &group.ext_one; - let oprops = &group.inner.object_props; - let aprops = &group.ext_two; - let public_key: &PublicKey = &group.ext_three.public_key; - - let mut creator_and_moderator_uris = oprops.get_many_attributed_to_xsd_any_uris().unwrap(); - let creator_uri = creator_and_moderator_uris.next().unwrap(); + async fn from_apub( + group: &mut GroupExt, + client: &Client, + pool: &DbPool, + ) -> Result { + // TODO: this is probably gonna cause problems cause fetcher:292 also calls take_attributed_to() + let creator_and_moderator_uris = group.clone().take_attributed_to().unwrap(); + let creator_uri = creator_and_moderator_uris + .as_many() + .unwrap() + .iter() + .next() + .unwrap() + .as_xsd_any_uri() + .unwrap(); let creator = get_or_fetch_and_upsert_remote_user(creator_uri.as_str(), client, pool).await?; Ok(CommunityForm { - name: oprops.get_name_xsd_string().unwrap().to_string(), - title: aprops.get_preferred_username().unwrap().to_string(), + name: group + .take_name() + .unwrap() + .as_single_xsd_string() + .unwrap() + .into(), + title: group.inner.take_preferred_username().unwrap(), // TODO: should be parsed as html and tags like