|
|
|
@ -1,27 +1,17 @@
|
|
|
|
|
use crate::{
|
|
|
|
|
activities::receive::{
|
|
|
|
|
announce_if_community_is_local,
|
|
|
|
|
get_actor_as_user,
|
|
|
|
|
receive_unhandled_activity,
|
|
|
|
|
},
|
|
|
|
|
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
|
|
|
|
|
activities::receive::{find_by_id, get_actor_as_user, FindResults},
|
|
|
|
|
ActorType,
|
|
|
|
|
FromApub,
|
|
|
|
|
GroupExt,
|
|
|
|
|
PageExt,
|
|
|
|
|
};
|
|
|
|
|
use activitystreams::{activity::Remove, base::AnyBase, object::Note, prelude::*};
|
|
|
|
|
use activitystreams::{activity::Remove, base::AnyBase, error::DomainError, prelude::*};
|
|
|
|
|
use actix_web::HttpResponse;
|
|
|
|
|
use anyhow::{anyhow, Context};
|
|
|
|
|
use anyhow::Context;
|
|
|
|
|
use lemmy_db::{
|
|
|
|
|
comment::{Comment, CommentForm},
|
|
|
|
|
comment::Comment,
|
|
|
|
|
comment_view::CommentView,
|
|
|
|
|
community::{Community, CommunityForm},
|
|
|
|
|
community::Community,
|
|
|
|
|
community_view::CommunityView,
|
|
|
|
|
naive_now,
|
|
|
|
|
post::{Post, PostForm},
|
|
|
|
|
post::Post,
|
|
|
|
|
post_view::PostView,
|
|
|
|
|
Crud,
|
|
|
|
|
};
|
|
|
|
|
use lemmy_structs::{
|
|
|
|
|
blocking,
|
|
|
|
@ -52,112 +42,69 @@ pub async fn receive_remove(
|
|
|
|
|
.map(|c| c.as_xsd_any_uri())
|
|
|
|
|
.flatten()
|
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
|
|
|
|
|
let object = remove
|
|
|
|
|
.object()
|
|
|
|
|
.to_owned()
|
|
|
|
|
.single_xsd_any_uri()
|
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
|
|
|
|
|
// Ensure that remove comes from the same domain as the community
|
|
|
|
|
if actor.actor_id()?.domain() != community_id.domain() {
|
|
|
|
|
return Err(anyhow!("Remove receive are only allowed on local objects").into());
|
|
|
|
|
return Err(DomainError.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match remove.object().as_single_kind_str() {
|
|
|
|
|
Some("Page") => receive_remove_post(remove, context).await,
|
|
|
|
|
Some("Note") => receive_remove_comment(remove, context).await,
|
|
|
|
|
Some("Group") => receive_remove_community(remove, context).await,
|
|
|
|
|
_ => receive_unhandled_activity(remove),
|
|
|
|
|
// Ensure that remove activity comes from the same domain as the community
|
|
|
|
|
remove.id(community_id.domain().context(location_info!())?)?;
|
|
|
|
|
|
|
|
|
|
match find_by_id(context, object).await {
|
|
|
|
|
Ok(FindResults::Post(p)) => receive_remove_post(context, remove, p).await,
|
|
|
|
|
Ok(FindResults::Comment(c)) => receive_remove_comment(context, remove, c).await,
|
|
|
|
|
Ok(FindResults::Community(c)) => receive_remove_community(context, remove, c).await,
|
|
|
|
|
// if we dont have the object, no need to do anything
|
|
|
|
|
Err(_) => Ok(HttpResponse::Ok().finish()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn receive_remove_post(
|
|
|
|
|
remove: Remove,
|
|
|
|
|
context: &LemmyContext,
|
|
|
|
|
_remove: Remove,
|
|
|
|
|
post: Post,
|
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
|
let mod_ = get_actor_as_user(&remove, context).await?;
|
|
|
|
|
let page = PageExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
|
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
|
|
|
|
|
let post_ap_id = PostForm::from_apub(&page, context, None)
|
|
|
|
|
.await?
|
|
|
|
|
.get_ap_id()?;
|
|
|
|
|
|
|
|
|
|
let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
|
|
|
|
|
|
|
|
|
|
let post_form = PostForm {
|
|
|
|
|
name: post.name.to_owned(),
|
|
|
|
|
url: post.url.to_owned(),
|
|
|
|
|
body: post.body.to_owned(),
|
|
|
|
|
creator_id: post.creator_id.to_owned(),
|
|
|
|
|
community_id: post.community_id,
|
|
|
|
|
removed: Some(true),
|
|
|
|
|
deleted: None,
|
|
|
|
|
nsfw: post.nsfw,
|
|
|
|
|
locked: None,
|
|
|
|
|
stickied: None,
|
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
|
embed_title: post.embed_title,
|
|
|
|
|
embed_description: post.embed_description,
|
|
|
|
|
embed_html: post.embed_html,
|
|
|
|
|
thumbnail_url: post.thumbnail_url,
|
|
|
|
|
ap_id: Some(post.ap_id),
|
|
|
|
|
local: post.local,
|
|
|
|
|
published: None,
|
|
|
|
|
};
|
|
|
|
|
let post_id = post.id;
|
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
|
Post::update(conn, post_id, &post_form)
|
|
|
|
|
let removed_post = blocking(context.pool(), move |conn| {
|
|
|
|
|
Post::update_removed(conn, post.id, true)
|
|
|
|
|
})
|
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
|
let post_id = post.id;
|
|
|
|
|
let post_id = removed_post.id;
|
|
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
|
})
|
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
|
op: UserOperation::EditPost,
|
|
|
|
|
post: res,
|
|
|
|
|
websocket_id: None,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
announce_if_community_is_local(remove, &mod_, context).await?;
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn receive_remove_comment(
|
|
|
|
|
remove: Remove,
|
|
|
|
|
context: &LemmyContext,
|
|
|
|
|
_remove: Remove,
|
|
|
|
|
comment: Comment,
|
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
|
let mod_ = get_actor_as_user(&remove, context).await?;
|
|
|
|
|
let note = Note::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
|
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
|
|
|
|
|
let comment_ap_id = CommentForm::from_apub(¬e, context, None)
|
|
|
|
|
.await?
|
|
|
|
|
.get_ap_id()?;
|
|
|
|
|
|
|
|
|
|
let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
|
|
|
|
|
|
|
|
|
|
let comment_form = CommentForm {
|
|
|
|
|
content: comment.content.to_owned(),
|
|
|
|
|
parent_id: comment.parent_id,
|
|
|
|
|
post_id: comment.post_id,
|
|
|
|
|
creator_id: comment.creator_id,
|
|
|
|
|
removed: Some(true),
|
|
|
|
|
deleted: None,
|
|
|
|
|
read: None,
|
|
|
|
|
published: None,
|
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
|
ap_id: Some(comment.ap_id),
|
|
|
|
|
local: comment.local,
|
|
|
|
|
};
|
|
|
|
|
let comment_id = comment.id;
|
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
|
Comment::update(conn, comment_id, &comment_form)
|
|
|
|
|
let removed_comment = blocking(context.pool(), move |conn| {
|
|
|
|
|
Comment::update_removed(conn, comment.id, true)
|
|
|
|
|
})
|
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
|
let comment_id = comment.id;
|
|
|
|
|
let comment_id = removed_comment.id;
|
|
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
|
|
|
|
CommentView::read(conn, comment_id, None)
|
|
|
|
|
})
|
|
|
|
@ -170,62 +117,26 @@ async fn receive_remove_comment(
|
|
|
|
|
recipient_ids,
|
|
|
|
|
form_id: None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendComment {
|
|
|
|
|
op: UserOperation::EditComment,
|
|
|
|
|
comment: res,
|
|
|
|
|
websocket_id: None,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
announce_if_community_is_local(remove, &mod_, context).await?;
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn receive_remove_community(
|
|
|
|
|
remove: Remove,
|
|
|
|
|
context: &LemmyContext,
|
|
|
|
|
_remove: Remove,
|
|
|
|
|
community: Community,
|
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
|
let mod_ = get_actor_as_user(&remove, context).await?;
|
|
|
|
|
let group = GroupExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
|
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
|
|
|
|
|
let community_actor_id = CommunityForm::from_apub(&group, context, Some(mod_.actor_id()?))
|
|
|
|
|
.await?
|
|
|
|
|
.actor_id
|
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
|
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
|
Community::read_from_actor_id(conn, &community_actor_id)
|
|
|
|
|
let removed_community = blocking(context.pool(), move |conn| {
|
|
|
|
|
Community::update_removed(conn, community.id, true)
|
|
|
|
|
})
|
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
|
|
let community_form = CommunityForm {
|
|
|
|
|
name: community.name.to_owned(),
|
|
|
|
|
title: community.title.to_owned(),
|
|
|
|
|
description: community.description.to_owned(),
|
|
|
|
|
category_id: community.category_id, // Note: need to keep this due to foreign key constraint
|
|
|
|
|
creator_id: community.creator_id, // Note: need to keep this due to foreign key constraint
|
|
|
|
|
removed: Some(true),
|
|
|
|
|
published: None,
|
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
|
deleted: None,
|
|
|
|
|
nsfw: community.nsfw,
|
|
|
|
|
actor_id: Some(community.actor_id),
|
|
|
|
|
local: community.local,
|
|
|
|
|
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;
|
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
|
Community::update(conn, community_id, &community_form)
|
|
|
|
|
})
|
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
|
|
let community_id = community.id;
|
|
|
|
|
let community_id = removed_community.id;
|
|
|
|
|
let res = CommunityResponse {
|
|
|
|
|
community: blocking(context.pool(), move |conn| {
|
|
|
|
|
CommunityView::read(conn, community_id, None)
|
|
|
|
@ -234,7 +145,6 @@ async fn receive_remove_community(
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let community_id = res.community.id;
|
|
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendCommunityRoomMessage {
|
|
|
|
|
op: UserOperation::EditCommunity,
|
|
|
|
|
response: res,
|
|
|
|
@ -242,6 +152,5 @@ async fn receive_remove_community(
|
|
|
|
|
websocket_id: None,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
announce_if_community_is_local(remove, &mod_, context).await?;
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
|
}
|
|
|
|
|