]> Untitled Git - lemmy.git/commitdiff
Some apub fixes
authorFelix Ableitner <me@nutomic.com>
Fri, 7 Aug 2020 13:15:44 +0000 (15:15 +0200)
committerFelix Ableitner <me@nutomic.com>
Fri, 7 Aug 2020 13:18:06 +0000 (15:18 +0200)
server/src/apub/fetcher.rs
server/src/apub/inbox/activities/announce.rs
server/src/apub/inbox/activities/remove.rs
server/src/apub/inbox/activities/undo.rs
server/src/apub/inbox/shared_inbox.rs

index 5395dded7380dc783fa5b3f268a80aa4a17a1855..b59e9e321de8a24dcba8b1496142ff8c17969ce6 100644 (file)
@@ -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)
index 95c4c8cd99c4880e482e690aa8bda741877a94f9..0ed04835f22b8f79ae845e8fe4e228e7ab8479b0 100644 (file)
@@ -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();
index b81927af199d2c9c37cf8739198fabad447dd089..882954b6882db34c0fcf9d9c5183f3f1997b8875 100644 (file)
@@ -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<HttpResponse, LemmyError> {
   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());
   }
 
index 49e70830ae9d3714eddd31b6888302764e718381..b2c91597f7a2abd4acc7ecde8f7cf52f31e70007 100644 (file)
@@ -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(&note, client, pool, Some(mod_.actor_id()?))
+  let comment_ap_id = CommentForm::from_apub(&note, 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()?;
 
index d5ed9312d862d3fecfdce48a7bd581d91a72ebff..8e1e3c44443ce250e6d53dcd449462f1542960dc 100644 (file)
@@ -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<T, A>(
+pub(in crate::apub::inbox) fn get_community_id_from_activity<T, A>(
   activity: &T,
-  client: &Client,
-  pool: &DbPool,
-) -> Result<Community, LemmyError>
+) -> Result<Url, LemmyError>
 where
   T: AsBase<A> + ActorAndObjectRef + AsObject<A>,
 {
   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<T, Kind>(