]> Untitled Git - lemmy.git/blobdiff - server/src/apub/inbox/activities/undo.rs
Merge branch 'main' into federation-authorisation
[lemmy.git] / server / src / apub / inbox / activities / undo.rs
index ad62a8f6efe4f0d7304e6cf79eeea6e8ff9005d2..34e9e2109b15ebad44ca76438901df0c6a2efaf3 100644 (file)
@@ -1,11 +1,16 @@
 use crate::{
   api::{comment::CommentResponse, community::CommunityResponse, post::PostResponse},
   apub::{
-    fetcher::{get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post},
+    fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
     inbox::shared_inbox::{
-      announce_if_community_is_local, get_user_from_activity, receive_unhandled_activity,
+      announce_if_community_is_local,
+      get_user_from_activity,
+      receive_unhandled_activity,
     },
-    ActorType, FromApub, GroupExt, PageExt,
+    ActorType,
+    FromApub,
+    GroupExt,
+    PageExt,
   },
   blocking,
   routes::ChatServerParam,
@@ -13,10 +18,17 @@ use crate::{
     server::{SendComment, SendCommunityRoomMessage, SendPost},
     UserOperation,
   },
-  DbPool, LemmyError,
+  DbPool,
+  LemmyError,
+};
+use activitystreams::{
+  activity::*,
+  base::{AnyBase, AsBase},
+  object::Note,
+  prelude::*,
 };
-use activitystreams_new::{activity::*, base::AnyBase, object::Note, prelude::*};
 use actix_web::{client::Client, HttpResponse};
+use anyhow::anyhow;
 use lemmy_db::{
   comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
   comment_view::CommentView,
@@ -25,7 +37,8 @@ use lemmy_db::{
   naive_now,
   post::{Post, PostForm, PostLike, PostLikeForm},
   post_view::PostView,
-  Crud, Likeable,
+  Crud,
+  Likeable,
 };
 
 pub async fn receive_undo(
@@ -40,11 +53,27 @@ pub async fn receive_undo(
     Some("Remove") => receive_undo_remove(undo, client, pool, chat_server).await,
     Some("Like") => receive_undo_like(undo, client, pool, chat_server).await,
     Some("Dislike") => receive_undo_dislike(undo, client, pool, chat_server).await,
-    // TODO: handle undo_dislike?
     _ => receive_unhandled_activity(undo),
   }
 }
 
+fn check_is_undo_valid<T, A>(outer_activity: &Undo, inner_activity: &T) -> Result<(), LemmyError>
+where
+  T: AsBase<A> + ActorAndObjectRef,
+{
+  let outer_actor = outer_activity.actor()?;
+  let outer_actor_uri = outer_actor.as_single_xsd_any_uri().unwrap();
+
+  let inner_actor = inner_activity.actor()?;
+  let inner_actor_uri = inner_actor.as_single_xsd_any_uri().unwrap();
+
+  if outer_actor_uri.domain() != inner_actor_uri.domain() {
+    Err(anyhow!("Cant undo activities from a different instance").into())
+  } else {
+    Ok(())
+  }
+}
+
 async fn receive_undo_delete(
   undo: Undo,
   client: &Client,
@@ -52,12 +81,13 @@ async fn receive_undo_delete(
   chat_server: ChatServerParam,
 ) -> Result<HttpResponse, LemmyError> {
   let delete = Delete::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
+  check_is_undo_valid(&undo, &delete)?;
   let type_ = delete.object().as_single_kind_str().unwrap();
   match type_ {
     "Note" => receive_undo_delete_comment(undo, &delete, client, pool, chat_server).await,
     "Page" => receive_undo_delete_post(undo, &delete, client, pool, chat_server).await,
     "Group" => receive_undo_delete_community(undo, &delete, client, pool, chat_server).await,
-    d => Err(format_err!("Undo Delete type {} not supported", d).into()),
+    d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
   }
 }
 
@@ -68,13 +98,14 @@ async fn receive_undo_remove(
   chat_server: ChatServerParam,
 ) -> Result<HttpResponse, LemmyError> {
   let remove = Remove::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
+  check_is_undo_valid(&undo, &remove)?;
 
   let type_ = remove.object().as_single_kind_str().unwrap();
   match type_ {
     "Note" => receive_undo_remove_comment(undo, &remove, client, pool, chat_server).await,
     "Page" => receive_undo_remove_post(undo, &remove, client, pool, chat_server).await,
     "Group" => receive_undo_remove_community(undo, &remove, client, pool, chat_server).await,
-    d => Err(format_err!("Undo Delete type {} not supported", d).into()),
+    d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
   }
 }
 
@@ -85,12 +116,13 @@ async fn receive_undo_like(
   chat_server: ChatServerParam,
 ) -> Result<HttpResponse, LemmyError> {
   let like = Like::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
+  check_is_undo_valid(&undo, &like)?;
 
   let type_ = like.object().as_single_kind_str().unwrap();
   match type_ {
     "Note" => receive_undo_like_comment(undo, &like, client, pool, chat_server).await,
     "Page" => receive_undo_like_post(undo, &like, client, pool, chat_server).await,
-    d => Err(format_err!("Undo Delete type {} not supported", d).into()),
+    d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
   }
 }
 
@@ -101,9 +133,12 @@ async fn receive_undo_dislike(
   _chat_server: ChatServerParam,
 ) -> Result<HttpResponse, LemmyError> {
   let dislike = Dislike::from_any_base(undo.object().to_owned().one().unwrap())?.unwrap();
+  check_is_undo_valid(&undo, &dislike)?;
+
+  // TODO: need to implement Undo<Dislike>
 
   let type_ = dislike.object().as_single_kind_str().unwrap();
-  Err(format_err!("Undo Delete type {} not supported", type_).into())
+  Err(anyhow!("Undo Delete type {} not supported", type_).into())
 }
 
 async fn receive_undo_delete_comment(
@@ -116,11 +151,11 @@ async fn receive_undo_delete_comment(
   let user = get_user_from_activity(delete, client, pool).await?;
   let note = Note::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
 
-  let comment_ap_id = CommentForm::from_apub(&note, client, pool, &user.actor_id()?)
+  let comment_ap_id = CommentForm::from_apub(&note, client, pool, Some(user.actor_id()?))
     .await?
     .get_ap_id()?;
 
-  let comment = get_or_fetch_and_insert_remote_comment(&comment_ap_id, client, pool).await?;
+  let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
 
   let comment_form = CommentForm {
     content: comment.content.to_owned(),
@@ -174,11 +209,11 @@ 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, &mod_.actor_id()?)
+  let comment_ap_id = CommentForm::from_apub(&note, client, pool, None)
     .await?
     .get_ap_id()?;
 
-  let comment = get_or_fetch_and_insert_remote_comment(&comment_ap_id, client, pool).await?;
+  let comment = get_or_fetch_and_insert_comment(&comment_ap_id, client, pool).await?;
 
   let comment_form = CommentForm {
     content: comment.content.to_owned(),
@@ -232,11 +267,11 @@ async fn receive_undo_delete_post(
   let user = get_user_from_activity(delete, client, pool).await?;
   let page = PageExt::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
 
-  let post_ap_id = PostForm::from_apub(&page, client, pool, &user.actor_id()?)
+  let post_ap_id = PostForm::from_apub(&page, client, pool, Some(user.actor_id()?))
     .await?
     .get_ap_id()?;
 
-  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?;
 
   let post_form = PostForm {
     name: post.name.to_owned(),
@@ -287,11 +322,11 @@ 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, &mod_.actor_id()?)
+  let post_ap_id = PostForm::from_apub(&page, client, pool, None)
     .await?
     .get_ap_id()?;
 
-  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?;
 
   let post_form = PostForm {
     name: post.name.to_owned(),
@@ -342,7 +377,7 @@ async fn receive_undo_delete_community(
   let user = get_user_from_activity(delete, client, pool).await?;
   let group = GroupExt::from_any_base(delete.object().to_owned().one().unwrap())?.unwrap();
 
-  let community_actor_id = CommunityForm::from_apub(&group, client, pool, &user.actor_id()?)
+  let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(user.actor_id()?))
     .await?
     .actor_id;
 
@@ -367,6 +402,8 @@ async fn receive_undo_delete_community(
     private_key: community.private_key,
     public_key: community.public_key,
     last_refreshed_at: None,
+    icon: Some(community.icon.to_owned()),
+    banner: Some(community.banner.to_owned()),
   };
 
   let community_id = community.id;
@@ -406,7 +443,7 @@ async fn receive_undo_remove_community(
   let mod_ = get_user_from_activity(remove, client, pool).await?;
   let group = GroupExt::from_any_base(remove.object().to_owned().one().unwrap())?.unwrap();
 
-  let community_actor_id = CommunityForm::from_apub(&group, client, pool, &mod_.actor_id()?)
+  let community_actor_id = CommunityForm::from_apub(&group, client, pool, Some(mod_.actor_id()?))
     .await?
     .actor_id;
 
@@ -431,6 +468,8 @@ async fn receive_undo_remove_community(
     private_key: community.private_key,
     public_key: community.public_key,
     last_refreshed_at: None,
+    icon: Some(community.icon.to_owned()),
+    banner: Some(community.banner.to_owned()),
   };
 
   let community_id = community.id;
@@ -470,9 +509,9 @@ async fn receive_undo_like_comment(
   let user = get_user_from_activity(like, client, pool).await?;
   let note = Note::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
 
-  let comment = CommentForm::from_apub(&note, client, pool, &user.actor_id()?).await?;
+  let comment = CommentForm::from_apub(&note, client, pool, None).await?;
 
-  let comment_id = get_or_fetch_and_insert_remote_comment(&comment.get_ap_id()?, client, pool)
+  let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, client, pool)
     .await?
     .id;
 
@@ -516,9 +555,9 @@ async fn receive_undo_like_post(
   let user = get_user_from_activity(like, client, pool).await?;
   let page = PageExt::from_any_base(like.object().to_owned().one().unwrap())?.unwrap();
 
-  let post = PostForm::from_apub(&page, client, pool, &user.actor_id()?).await?;
+  let post = PostForm::from_apub(&page, client, pool, None).await?;
 
-  let post_id = get_or_fetch_and_insert_remote_post(&post.get_ap_id()?, client, pool)
+  let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, client, pool)
     .await?
     .id;