From bb3e29e5c4abf1a5b572899fe2d2da4a4f865f2e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 13 Jul 2020 15:56:58 +0200 Subject: [PATCH] Make reads from activitypub objects immutable --- server/lemmy_db/src/lib.rs | 2 +- server/src/apub/comment.rs | 19 ++--- server/src/apub/community.rs | 44 +++--------- server/src/apub/fetcher.rs | 45 ++++++------ server/src/apub/mod.rs | 5 +- server/src/apub/post.rs | 16 ++--- server/src/apub/private_message.rs | 15 ++-- server/src/apub/shared_inbox.rs | 109 +++++++++++++---------------- server/src/apub/user.rs | 24 +++---- server/src/apub/user_inbox.rs | 25 +++---- 10 files changed, 118 insertions(+), 186 deletions(-) diff --git a/server/lemmy_db/src/lib.rs b/server/lemmy_db/src/lib.rs index b34919cdf..5cf20bb3c 100644 --- a/server/lemmy_db/src/lib.rs +++ b/server/lemmy_db/src/lib.rs @@ -181,7 +181,7 @@ mod tests { pub fn establish_unpooled_connection() -> PgConnection { let db_url = match get_database_url_from_env() { Ok(url) => url, - Err(_) => panic!("Failed to read database URL from env var LEMMY_DATABASE_URL"), + Err(e) => panic!("Failed to read database URL from env var LEMMY_DATABASE_URL: {}", e), }; PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url)) } diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index af3581cbb..84d11275d 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -1,25 +1,16 @@ use crate::{ apub::{ activities::{populate_object_props, send_activity_to_community}, - create_apub_response, - create_apub_tombstone_response, - create_tombstone, - fetch_webfinger_url, + create_apub_response, create_apub_tombstone_response, create_tombstone, fetch_webfinger_url, fetcher::{ - get_or_fetch_and_insert_remote_comment, - get_or_fetch_and_insert_remote_post, + get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post, get_or_fetch_and_upsert_remote_user, }, - ActorType, - ApubLikeableType, - ApubObjectType, - FromApub, - ToApub, + ActorType, ApubLikeableType, ApubObjectType, FromApub, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams::{ activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, @@ -121,7 +112,7 @@ impl FromApub for CommentForm { /// Parse an ActivityPub note received from another instance into a Lemmy comment async fn from_apub( - note: &mut Note, + note: &Note, client: &Client, pool: &DbPool, ) -> Result { diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 8b623e713..587977a33 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -1,28 +1,18 @@ use crate::{ apub::{ activities::{populate_object_props, send_activity}, - create_apub_response, - create_apub_tombstone_response, - create_tombstone, + create_apub_response, create_apub_tombstone_response, create_tombstone, extensions::group_extensions::GroupExtension, fetcher::get_or_fetch_and_upsert_remote_user, - get_shared_inbox, - insert_activity, - ActorType, - FromApub, - GroupExt, - ToApub, + get_shared_inbox, insert_activity, ActorType, FromApub, GroupExt, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams::{ activity::{Accept, Announce, Delete, Remove, Undo}, - Activity, - Base, - BaseBox, + Activity, Base, BaseBox, }; use activitystreams_ext::Ext2; use activitystreams_new::{ @@ -367,13 +357,8 @@ impl FromApub for CommunityForm { type ApubType = GroupExt; /// Parse an ActivityPub group received from another instance into a Lemmy community. - 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(); + async fn from_apub(group: &GroupExt, client: &Client, pool: &DbPool) -> Result { + let creator_and_moderator_uris = group.attributed_to().unwrap(); let creator_uri = creator_and_moderator_uris .as_many() .unwrap() @@ -386,27 +371,20 @@ impl FromApub for CommunityForm { let creator = get_or_fetch_and_upsert_remote_user(creator_uri.as_str(), client, pool).await?; Ok(CommunityForm { - name: group - .take_name() - .unwrap() - .as_single_xsd_string() - .unwrap() - .into(), - title: group.inner.take_preferred_username().unwrap(), + name: group.name().unwrap().as_single_xsd_string().unwrap().into(), + title: group.inner.preferred_username().unwrap().to_string(), // TODO: should be parsed as html and tags like