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,
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,
naive_now,
post::{Post, PostForm, PostLike, PostLikeForm},
post_view::PostView,
- Crud, Likeable,
+ Crud,
+ Likeable,
};
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,
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()),
}
}
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()),
}
}
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()),
}
}
_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(
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(¬e, client, pool, &user.actor_id()?)
+ let comment_ap_id = CommentForm::from_apub(¬e, 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(),
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, &mod_.actor_id()?)
+ let comment_ap_id = CommentForm::from_apub(¬e, 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(),
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(),
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(),
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;
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;
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;
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;
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(¬e, client, pool, &user.actor_id()?).await?;
+ let comment = CommentForm::from_apub(¬e, 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;
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;