From a85873d294171bd6d32ebbdc44fc4ae2fcb231ad Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 29 Jul 2020 13:46:11 +0200 Subject: [PATCH] Take correct community uri in shared_inbox, rename fetch_remote* methods --- server/src/apub/activities.rs | 8 +++-- server/src/apub/comment.rs | 35 ++++++++++++++------ server/src/apub/community.rs | 25 ++++++++++---- server/src/apub/fetcher.rs | 35 ++++++++++++-------- server/src/apub/inbox/activities/announce.rs | 12 +++++-- server/src/apub/inbox/activities/create.rs | 11 ++++-- server/src/apub/inbox/activities/delete.rs | 18 ++++++---- server/src/apub/inbox/activities/dislike.rs | 17 ++++++---- server/src/apub/inbox/activities/like.rs | 17 ++++++---- server/src/apub/inbox/activities/remove.rs | 18 ++++++---- server/src/apub/inbox/activities/undo.rs | 29 ++++++++++------ server/src/apub/inbox/activities/update.rs | 17 ++++++---- server/src/apub/inbox/community_inbox.rs | 9 ++--- server/src/apub/inbox/shared_inbox.rs | 31 ++++++++++++----- server/src/apub/inbox/user_inbox.rs | 21 +++++++----- server/src/apub/mod.rs | 3 +- server/src/apub/post.rs | 29 +++++++++++----- server/src/apub/private_message.rs | 20 +++++++---- server/src/apub/user.rs | 13 ++++++-- 19 files changed, 250 insertions(+), 118 deletions(-) diff --git a/server/src/apub/activities.rs b/server/src/apub/activities.rs index 9cc20af8..a622c691 100644 --- a/server/src/apub/activities.rs +++ b/server/src/apub/activities.rs @@ -1,10 +1,14 @@ use crate::{ apub::{ - community::do_announce, extensions::signatures::sign, insert_activity, is_apub_id_valid, + community::do_announce, + extensions::signatures::sign, + insert_activity, + is_apub_id_valid, ActorType, }, request::retry_custom, - DbPool, LemmyError, + DbPool, + LemmyError, }; use activitystreams_new::base::AnyBase; use actix_web::client::Client; diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 61a1d15e..269e7fc2 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -1,21 +1,36 @@ use crate::{ apub::{ activities::{generate_activity_id, 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_upsert_remote_user, + get_or_fetch_and_insert_comment, + get_or_fetch_and_insert_post, + get_or_fetch_and_upsert_user, }, - ActorType, ApubLikeableType, ApubObjectType, FromApub, ToApub, + ActorType, + ApubLikeableType, + ApubObjectType, + FromApub, + ToApub, }, blocking, routes::DbPoolParam, - DbPool, LemmyError, + DbPool, + LemmyError, }; use activitystreams_new::{ activity::{ kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType}, - Create, Delete, Dislike, Like, Remove, Undo, Update, + Create, + Delete, + Dislike, + Like, + Remove, + Undo, + Update, }, base::AnyBase, context, @@ -124,7 +139,7 @@ impl FromApub for CommentForm { .as_single_xsd_any_uri() .unwrap(); - let creator = get_or_fetch_and_upsert_remote_user(creator_actor_id, client, pool).await?; + let creator = get_or_fetch_and_upsert_user(creator_actor_id, client, pool).await?; let mut in_reply_tos = note .in_reply_to() @@ -137,7 +152,7 @@ impl FromApub for CommentForm { let post_ap_id = in_reply_tos.next().unwrap(); // This post, or the parent comment might not yet exist on this server yet, fetch them. - let post = get_or_fetch_and_insert_remote_post(&post_ap_id, client, pool).await?; + let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?; // The 2nd item, if it exists, is the parent comment apub_id // For deeply nested comments, FromApub automatically gets called recursively @@ -145,7 +160,7 @@ impl FromApub for CommentForm { Some(parent_comment_uri) => { let parent_comment_ap_id = &parent_comment_uri; let parent_comment = - get_or_fetch_and_insert_remote_comment(&parent_comment_ap_id, client, pool).await?; + get_or_fetch_and_insert_comment(&parent_comment_ap_id, client, pool).await?; Some(parent_comment.id) } @@ -558,7 +573,7 @@ async fn collect_non_local_mentions_and_addresses( debug!("mention actor_id: {}", actor_id); addressed_ccs.push(actor_id.to_owned().to_string()); - let mention_user = get_or_fetch_and_upsert_remote_user(&actor_id, client, pool).await?; + let mention_user = get_or_fetch_and_upsert_user(&actor_id, client, pool).await?; let shared_inbox = mention_user.get_shared_inbox_url(); mention_inboxes.push(shared_inbox); diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 16d13711..b60059aa 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -1,20 +1,33 @@ use crate::{ apub::{ activities::{generate_activity_id, 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, + fetcher::get_or_fetch_and_upsert_user, + get_shared_inbox, + insert_activity, + ActorType, + FromApub, + GroupExt, + ToApub, }, blocking, routes::DbPoolParam, - DbPool, LemmyError, + DbPool, + LemmyError, }; use activitystreams_ext::Ext2; use activitystreams_new::{ activity::{ kind::{AcceptType, AnnounceType, DeleteType, LikeType, RemoveType, UndoType}, - Accept, Announce, Delete, Follow, Remove, Undo, + Accept, + Announce, + Delete, + Follow, + Remove, + Undo, }, actor::{kind::GroupType, ApActor, Endpoints, Group}, base::{AnyBase, BaseExt}, @@ -324,7 +337,7 @@ impl FromApub for CommunityForm { .as_xsd_any_uri() .unwrap(); - let creator = get_or_fetch_and_upsert_remote_user(creator_uri, client, pool).await?; + let creator = get_or_fetch_and_upsert_user(creator_uri, client, pool).await?; Ok(CommunityForm { name: group diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index cf198636..ff1ec11b 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -1,12 +1,19 @@ use crate::{ api::site::SearchResponse, apub::{ - is_apub_id_valid, ActorType, FromApub, GroupExt, PageExt, PersonExt, APUB_JSON_CONTENT_TYPE, + is_apub_id_valid, + ActorType, + FromApub, + GroupExt, + PageExt, + PersonExt, + APUB_JSON_CONTENT_TYPE, }, blocking, request::{retry, RecvError}, routes::nodeinfo::{NodeInfo, NodeInfoWellKnown}, - DbPool, LemmyError, + DbPool, + LemmyError, }; use activitystreams_new::{base::BaseExt, object::Note, prelude::*}; use actix_web::client::Client; @@ -22,7 +29,9 @@ use lemmy_db::{ post_view::PostView, user::{UserForm, User_}, user_view::UserView, - Crud, Joinable, SearchType, + Crud, + Joinable, + SearchType, }; use lemmy_utils::get_apub_protocol_string; use log::debug; @@ -140,7 +149,7 @@ pub async fn search_by_apub_id( SearchAcceptedObjects::Person(p) => { let user_uri = p.inner.id(domain)?.unwrap(); - let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; + let user = get_or_fetch_and_upsert_user(&user_uri, client, pool).await?; response.users = vec![blocking(pool, move |conn| UserView::read(conn, user.id)).await??]; @@ -149,7 +158,7 @@ pub async fn search_by_apub_id( SearchAcceptedObjects::Group(g) => { let community_uri = g.inner.id(domain)?.unwrap(); - let community = get_or_fetch_and_upsert_remote_community(community_uri, client, pool).await?; + let community = get_or_fetch_and_upsert_community(community_uri, client, pool).await?; // TODO Maybe at some point in the future, fetch all the history of a community // fetch_community_outbox(&c, conn)?; @@ -191,21 +200,21 @@ pub async fn search_by_apub_id( Ok(response) } -pub async fn get_or_fetch_and_upsert_remote_actor( +pub async fn get_or_fetch_and_upsert_actor( apub_id: &Url, client: &Client, pool: &DbPool, ) -> Result, LemmyError> { - let user = get_or_fetch_and_upsert_remote_user(apub_id, client, pool).await; + let user = get_or_fetch_and_upsert_user(apub_id, client, pool).await; let actor: Box = match user { Ok(u) => Box::new(u), - Err(_) => Box::new(get_or_fetch_and_upsert_remote_community(apub_id, client, pool).await?), + Err(_) => Box::new(get_or_fetch_and_upsert_community(apub_id, client, pool).await?), }; Ok(actor) } /// Check if a remote user exists, create if not found, if its too old update it.Fetch a user, insert/update it in the database and return the user. -pub async fn get_or_fetch_and_upsert_remote_user( +pub async fn get_or_fetch_and_upsert_user( apub_id: &Url, client: &Client, pool: &DbPool, @@ -257,7 +266,7 @@ fn should_refetch_actor(last_refreshed: NaiveDateTime) -> bool { } /// Check if a remote community exists, create if not found, if its too old update it.Fetch a community, insert/update it in the database and return the community. -pub async fn get_or_fetch_and_upsert_remote_community( +pub async fn get_or_fetch_and_upsert_community( apub_id: &Url, client: &Client, pool: &DbPool, @@ -299,7 +308,7 @@ pub async fn get_or_fetch_and_upsert_remote_community( let mut creator_and_moderators = Vec::new(); for uri in creator_and_moderator_uris { - let c_or_m = get_or_fetch_and_upsert_remote_user(uri, client, pool).await?; + let c_or_m = get_or_fetch_and_upsert_user(uri, client, pool).await?; creator_and_moderators.push(c_or_m); } @@ -333,7 +342,7 @@ fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result Result( @@ -118,8 +126,13 @@ where { let cc = activity.cc().unwrap(); let cc = cc.as_many().unwrap(); - let community_uri = cc.first().unwrap().as_xsd_any_uri().unwrap(); - let community = get_or_fetch_and_upsert_remote_community(&community_uri, client, pool).await?; + let community_followers_uri = cc.first().unwrap().as_xsd_any_uri().unwrap(); + // TODO: this is hacky but seems to be the only way to get the community ID + let community_uri = community_followers_uri + .to_string() + .replace("/followers", ""); + let community = + get_or_fetch_and_upsert_community(&Url::parse(&community_uri)?, client, pool).await?; if community.local { do_announce(activity.into_any_base()?, &community, &user, client, pool).await?; diff --git a/server/src/apub/inbox/user_inbox.rs b/server/src/apub/inbox/user_inbox.rs index 73916ee2..13d974e9 100644 --- a/server/src/apub/inbox/user_inbox.rs +++ b/server/src/apub/inbox/user_inbox.rs @@ -2,13 +2,15 @@ use crate::{ api::user::PrivateMessageResponse, apub::{ extensions::signatures::verify, - fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, - insert_activity, FromApub, + fetcher::{get_or_fetch_and_upsert_community, get_or_fetch_and_upsert_user}, + insert_activity, + FromApub, }, blocking, routes::{ChatServerParam, DbPoolParam}, websocket::{server::SendUserRoomMessage, UserOperation}, - DbPool, LemmyError, + DbPool, + LemmyError, }; use activitystreams_new::{ activity::{Accept, Create, Delete, Undo, Update}, @@ -22,7 +24,8 @@ use lemmy_db::{ private_message::{PrivateMessage, PrivateMessageForm}, private_message_view::PrivateMessageView, user::User_, - Crud, Followable, + Crud, + Followable, }; use log::debug; use serde::Deserialize; @@ -79,7 +82,7 @@ async fn receive_accept( ) -> Result { let community_uri = accept.actor()?.to_owned().single_xsd_any_uri().unwrap(); - let community = get_or_fetch_and_upsert_remote_community(&community_uri, client, pool).await?; + let community = get_or_fetch_and_upsert_community(&community_uri, client, pool).await?; verify(request, &community)?; let username = username.to_owned(); @@ -113,7 +116,7 @@ async fn receive_create_private_message( let user_uri = &create.actor()?.to_owned().single_xsd_any_uri().unwrap(); let note = Note::from_any_base(create.object().as_one().unwrap().to_owned())?.unwrap(); - let user = get_or_fetch_and_upsert_remote_user(user_uri, client, pool).await?; + let user = get_or_fetch_and_upsert_user(user_uri, client, pool).await?; verify(request, &user)?; insert_activity(user.id, create, false, pool).await?; @@ -154,7 +157,7 @@ async fn receive_update_private_message( let user_uri = &update.actor()?.to_owned().single_xsd_any_uri().unwrap(); let note = Note::from_any_base(update.object().as_one().unwrap().to_owned())?.unwrap(); - let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; + let user = get_or_fetch_and_upsert_user(&user_uri, client, pool).await?; verify(request, &user)?; insert_activity(user.id, update, false, pool).await?; @@ -203,7 +206,7 @@ async fn receive_delete_private_message( let user_uri = &delete.actor()?.to_owned().single_xsd_any_uri().unwrap(); let note = Note::from_any_base(delete.object().as_one().unwrap().to_owned())?.unwrap(); - let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; + let user = get_or_fetch_and_upsert_user(&user_uri, client, pool).await?; verify(request, &user)?; insert_activity(user.id, delete, false, pool).await?; @@ -265,7 +268,7 @@ async fn receive_undo_delete_private_message( let note = Note::from_any_base(delete.object().as_one().unwrap().to_owned())?.unwrap(); let user_uri = &delete.actor()?.to_owned().single_xsd_any_uri().unwrap(); - let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; + let user = get_or_fetch_and_upsert_user(&user_uri, client, pool).await?; verify(request, &user)?; insert_activity(user.id, delete, false, pool).await?; diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index a5d8984c..47f01e31 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -17,7 +17,8 @@ use crate::{ blocking, request::{retry, RecvError}, routes::webfinger::WebFingerResponse, - DbPool, LemmyError, + DbPool, + LemmyError, }; use activitystreams_ext::{Ext1, Ext2}; use activitystreams_new::{ diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 932ff8ce..a4cf8e4c 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -1,20 +1,34 @@ use crate::{ apub::{ activities::{generate_activity_id, send_activity_to_community}, - create_apub_response, create_apub_tombstone_response, create_tombstone, + create_apub_response, + create_apub_tombstone_response, + create_tombstone, extensions::page_extension::PageExtension, - fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, - ActorType, ApubLikeableType, ApubObjectType, FromApub, PageExt, ToApub, + fetcher::{get_or_fetch_and_upsert_community, get_or_fetch_and_upsert_user}, + ActorType, + ApubLikeableType, + ApubObjectType, + FromApub, + PageExt, + ToApub, }, blocking, routes::DbPoolParam, - DbPool, LemmyError, + DbPool, + LemmyError, }; use activitystreams_ext::Ext1; use activitystreams_new::{ activity::{ kind::{CreateType, DeleteType, DislikeType, LikeType, RemoveType, UndoType, UpdateType}, - Create, Delete, Dislike, Like, Remove, Undo, Update, + Create, + Delete, + Dislike, + Like, + Remove, + Undo, + Update, }, context, object::{kind::PageType, Image, Page, Tombstone}, @@ -151,7 +165,7 @@ impl FromApub for PostForm { .as_single_xsd_any_uri() .unwrap(); - let creator = get_or_fetch_and_upsert_remote_user(creator_actor_id, client, pool).await?; + let creator = get_or_fetch_and_upsert_user(creator_actor_id, client, pool).await?; let community_actor_id = page .inner @@ -161,8 +175,7 @@ impl FromApub for PostForm { .as_single_xsd_any_uri() .unwrap(); - let community = - get_or_fetch_and_upsert_remote_community(community_actor_id, client, pool).await?; + let community = get_or_fetch_and_upsert_community(community_actor_id, client, pool).await?; let thumbnail_url = match &page.inner.image() { Some(any_image) => Image::from_any_base(any_image.to_owned().as_one().unwrap().to_owned())? diff --git a/server/src/apub/private_message.rs b/server/src/apub/private_message.rs index b10f87b5..5f8abca8 100644 --- a/server/src/apub/private_message.rs +++ b/server/src/apub/private_message.rs @@ -2,15 +2,23 @@ use crate::{ apub::{ activities::{generate_activity_id, send_activity}, create_tombstone, - fetcher::get_or_fetch_and_upsert_remote_user, - insert_activity, ApubObjectType, FromApub, ToApub, + fetcher::get_or_fetch_and_upsert_user, + insert_activity, + ApubObjectType, + FromApub, + ToApub, }, - blocking, DbPool, LemmyError, + blocking, + DbPool, + LemmyError, }; use activitystreams_new::{ activity::{ kind::{CreateType, DeleteType, UndoType, UpdateType}, - Create, Delete, Undo, Update, + Create, + Delete, + Undo, + Update, }, context, object::{kind::NoteType, Note, Tombstone}, @@ -76,11 +84,11 @@ impl FromApub for PrivateMessageForm { .single_xsd_any_uri() .unwrap(); - let creator = get_or_fetch_and_upsert_remote_user(&creator_actor_id, client, pool).await?; + let creator = get_or_fetch_and_upsert_user(&creator_actor_id, client, pool).await?; let recipient_actor_id = note.to().unwrap().clone().single_xsd_any_uri().unwrap(); - let recipient = get_or_fetch_and_upsert_remote_user(&recipient_actor_id, client, pool).await?; + let recipient = get_or_fetch_and_upsert_user(&recipient_actor_id, client, pool).await?; Ok(PrivateMessageForm { creator_id: creator.id, diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 8ec972da..9d8c9167 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -1,17 +1,24 @@ use crate::{ apub::{ activities::{generate_activity_id, send_activity}, - create_apub_response, insert_activity, ActorType, FromApub, PersonExt, ToApub, + create_apub_response, + insert_activity, + ActorType, + FromApub, + PersonExt, + ToApub, }, blocking, routes::DbPoolParam, - DbPool, LemmyError, + DbPool, + LemmyError, }; use activitystreams_ext::Ext1; use activitystreams_new::{ activity::{ kind::{FollowType, UndoType}, - Follow, Undo, + Follow, + Undo, }, actor::{ApActor, Endpoints, Person}, context, -- 2.44.1