From 9b7be1afb6390f2fbee4ce99ed3b218f4d2e7547 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 7 Aug 2020 15:15:44 +0200 Subject: [PATCH] Some apub fixes --- server/src/apub/fetcher.rs | 3 ++- server/src/apub/inbox/activities/announce.rs | 27 +++++++++----------- server/src/apub/inbox/activities/remove.rs | 6 ++--- server/src/apub/inbox/activities/undo.rs | 4 +-- server/src/apub/inbox/shared_inbox.rs | 20 ++++++--------- 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 5395dded7..b59e9e321 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -350,7 +350,7 @@ async fn fetch_remote_community( let outbox_items = outbox.items().unwrap().clone(); for o in outbox_items.many().unwrap() { let page = PageExt::from_any_base(o)?.unwrap(); - let post = PostForm::from_apub(&page, client, pool, Some(apub_id.to_owned())).await?; + let post = PostForm::from_apub(&page, client, pool, None).await?; let post_ap_id = post.ap_id.clone(); // Check whether the post already exists in the local db let existing = blocking(pool, move |conn| Post::read_from_apub_id(conn, &post_ap_id)).await?; @@ -358,6 +358,7 @@ async fn fetch_remote_community( Ok(e) => blocking(pool, move |conn| Post::update(conn, e.id, &post)).await??, Err(_) => blocking(pool, move |conn| Post::create(conn, &post)).await??, }; + // TODO: we need to send a websocket update here } Ok(community) diff --git a/server/src/apub/inbox/activities/announce.rs b/server/src/apub/inbox/activities/announce.rs index 95c4c8cd9..0ed04835f 100644 --- a/server/src/apub/inbox/activities/announce.rs +++ b/server/src/apub/inbox/activities/announce.rs @@ -1,18 +1,15 @@ use crate::{ - apub::{ - inbox::{ - activities::{ - create::receive_create, - delete::receive_delete, - dislike::receive_dislike, - like::receive_like, - remove::receive_remove, - undo::receive_undo, - update::receive_update, - }, - shared_inbox::{get_community_from_activity, receive_unhandled_activity}, + apub::inbox::{ + activities::{ + create::receive_create, + delete::receive_delete, + dislike::receive_dislike, + like::receive_like, + remove::receive_remove, + undo::receive_undo, + update::receive_update, }, - ActorType, + shared_inbox::{get_community_id_from_activity, receive_unhandled_activity}, }, routes::ChatServerParam, DbPool, @@ -34,8 +31,8 @@ pub async fn receive_announce( let announce = Announce::from_any_base(activity)?.unwrap(); // ensure that announce and community come from the same instance - let community = get_community_from_activity(&announce, client, pool).await?; - announce.id(community.actor_id()?.domain().unwrap())?; + let community = get_community_id_from_activity(&announce)?; + announce.id(community.domain().unwrap())?; let kind = announce.object().as_single_kind_str(); let object = announce.object(); diff --git a/server/src/apub/inbox/activities/remove.rs b/server/src/apub/inbox/activities/remove.rs index b81927af1..882954b68 100644 --- a/server/src/apub/inbox/activities/remove.rs +++ b/server/src/apub/inbox/activities/remove.rs @@ -4,7 +4,7 @@ use crate::{ fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, inbox::shared_inbox::{ announce_if_community_is_local, - get_community_from_activity, + get_community_id_from_activity, get_user_from_activity, receive_unhandled_activity, }, @@ -44,8 +44,8 @@ pub async fn receive_remove( ) -> Result { let remove = Remove::from_any_base(activity)?.unwrap(); let actor = get_user_from_activity(&remove, client, pool).await?; - let community = get_community_from_activity(&remove, client, pool).await?; - if actor.actor_id()?.domain() != community.actor_id()?.domain() { + let community = get_community_id_from_activity(&remove)?; + if actor.actor_id()?.domain() != community.domain() { return Err(anyhow!("Remove activities are only allowed on local objects").into()); } diff --git a/server/src/apub/inbox/activities/undo.rs b/server/src/apub/inbox/activities/undo.rs index 49e70830a..b2c91597f 100644 --- a/server/src/apub/inbox/activities/undo.rs +++ b/server/src/apub/inbox/activities/undo.rs @@ -209,7 +209,7 @@ async fn receive_undo_remove_comment( let mod_ = get_user_from_activity(remove, client, pool).await?; let note = Note::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap(); - let comment_ap_id = CommentForm::from_apub(¬e, client, pool, Some(mod_.actor_id()?)) + let comment_ap_id = CommentForm::from_apub(¬e, client, pool, None) .await? .get_ap_id()?; @@ -322,7 +322,7 @@ async fn receive_undo_remove_post( let mod_ = get_user_from_activity(remove, client, pool).await?; let page = PageExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap(); - let post_ap_id = PostForm::from_apub(&page, client, pool, Some(mod_.actor_id()?)) + let post_ap_id = PostForm::from_apub(&page, client, pool, None) .await? .get_ap_id()?; diff --git a/server/src/apub/inbox/shared_inbox.rs b/server/src/apub/inbox/shared_inbox.rs index d5ed9312d..8e1e3c444 100644 --- a/server/src/apub/inbox/shared_inbox.rs +++ b/server/src/apub/inbox/shared_inbox.rs @@ -31,7 +31,7 @@ use activitystreams::{ prelude::*, }; use actix_web::{client::Client, web, HttpRequest, HttpResponse}; -use lemmy_db::{community::Community, user::User_}; +use lemmy_db::user::User_; use log::debug; use serde::{Deserialize, Serialize}; use std::fmt::Debug; @@ -68,13 +68,12 @@ pub async fn shared_inbox( debug!("Shared inbox received activity: {}", json); let sender = &activity.actor()?.to_owned().single_xsd_any_uri().unwrap(); - // TODO: pass this actor in instead of using get_user_from_activity() - let actor = get_or_fetch_and_upsert_actor(sender, &client, &pool).await?; - - // TODO: i dont think this works for Announce/Undo activities - //let community = get_community_id_from_activity(&activity).await; + let community = get_community_id_from_activity(&activity)?; check_is_apub_id_valid(sender)?; + check_is_apub_id_valid(&community)?; + + let actor = get_or_fetch_and_upsert_actor(sender, &client, &pool).await?; verify(&request, actor.as_ref())?; let any_base = activity.clone().into_any_base()?; @@ -117,18 +116,15 @@ where get_or_fetch_and_upsert_user(&user_uri, client, pool).await } -pub(in crate::apub::inbox) async fn get_community_from_activity( +pub(in crate::apub::inbox) fn get_community_id_from_activity( activity: &T, - client: &Client, - pool: &DbPool, -) -> Result +) -> Result where T: AsBase + ActorAndObjectRef + AsObject, { let cc = activity.cc().unwrap(); let cc = cc.as_many().unwrap(); - let community_uri = cc.first().unwrap().as_xsd_any_uri().unwrap().to_owned(); - get_or_fetch_and_upsert_community(&community_uri, client, pool).await + Ok(cc.first().unwrap().as_xsd_any_uri().unwrap().to_owned()) } pub(in crate::apub::inbox) async fn announce_if_community_is_local(