2020-07-23 14:36:45 +00:00
|
|
|
use crate::{
|
|
|
|
api::{comment::CommentResponse, community::CommunityResponse, post::PostResponse},
|
|
|
|
apub::{
|
2020-07-29 11:46:11 +00:00
|
|
|
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
|
2020-07-28 16:08:28 +00:00
|
|
|
inbox::shared_inbox::{
|
2020-07-29 11:46:11 +00:00
|
|
|
announce_if_community_is_local,
|
|
|
|
get_user_from_activity,
|
|
|
|
receive_unhandled_activity,
|
2020-07-28 16:08:28 +00:00
|
|
|
},
|
2020-08-06 12:53:58 +00:00
|
|
|
ActorType,
|
2020-07-29 11:46:11 +00:00
|
|
|
FromApub,
|
|
|
|
GroupExt,
|
|
|
|
PageExt,
|
2020-07-23 14:36:45 +00:00
|
|
|
},
|
|
|
|
blocking,
|
|
|
|
websocket::{
|
|
|
|
server::{SendComment, SendCommunityRoomMessage, SendPost},
|
|
|
|
UserOperation,
|
|
|
|
},
|
2020-08-18 13:43:50 +00:00
|
|
|
LemmyContext,
|
2020-07-29 11:46:11 +00:00
|
|
|
LemmyError,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
2020-08-06 12:53:58 +00:00
|
|
|
use activitystreams::{
|
|
|
|
activity::*,
|
|
|
|
base::{AnyBase, AsBase},
|
|
|
|
object::Note,
|
|
|
|
prelude::*,
|
|
|
|
};
|
2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::HttpResponse;
|
2020-08-11 14:31:05 +00:00
|
|
|
use anyhow::{anyhow, Context};
|
2020-07-23 14:36:45 +00:00
|
|
|
use lemmy_db::{
|
2020-08-12 14:43:45 +00:00
|
|
|
comment::{Comment, CommentForm, CommentLike},
|
2020-07-23 14:36:45 +00:00
|
|
|
comment_view::CommentView,
|
|
|
|
community::{Community, CommunityForm},
|
|
|
|
community_view::CommunityView,
|
|
|
|
naive_now,
|
2020-08-12 14:43:45 +00:00
|
|
|
post::{Post, PostForm, PostLike},
|
2020-07-23 14:36:45 +00:00
|
|
|
post_view::PostView,
|
2020-07-29 11:46:11 +00:00
|
|
|
Crud,
|
|
|
|
Likeable,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
2020-08-11 14:31:05 +00:00
|
|
|
use lemmy_utils::location_info;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
pub async fn receive_undo(
|
|
|
|
activity: AnyBase,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let undo = Undo::from_any_base(activity)?.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
match undo.object().as_single_kind_str() {
|
2020-08-18 13:43:50 +00:00
|
|
|
Some("Delete") => receive_undo_delete(undo, context).await,
|
|
|
|
Some("Remove") => receive_undo_remove(undo, context).await,
|
|
|
|
Some("Like") => receive_undo_like(undo, context).await,
|
|
|
|
Some("Dislike") => receive_undo_dislike(undo, context).await,
|
2020-07-23 14:36:45 +00:00
|
|
|
_ => receive_unhandled_activity(undo),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:53:58 +00:00
|
|
|
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()?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let outer_actor_uri = outer_actor
|
|
|
|
.as_single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
2020-08-06 12:53:58 +00:00
|
|
|
|
|
|
|
let inner_actor = inner_activity.actor()?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let inner_actor_uri = inner_actor
|
|
|
|
.as_single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
2020-08-06 12:53:58 +00:00
|
|
|
|
2020-08-06 19:44:47 +00:00
|
|
|
if outer_actor_uri.domain() != inner_actor_uri.domain() {
|
|
|
|
Err(anyhow!("Cant undo activities from a different instance").into())
|
2020-08-06 12:53:58 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 14:36:45 +00:00
|
|
|
async fn receive_undo_delete(
|
|
|
|
undo: Undo,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let delete = Delete::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-08-06 12:53:58 +00:00
|
|
|
check_is_undo_valid(&undo, &delete)?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let type_ = delete
|
|
|
|
.object()
|
|
|
|
.as_single_kind_str()
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
match type_ {
|
2020-08-18 13:43:50 +00:00
|
|
|
"Note" => receive_undo_delete_comment(undo, &delete, context).await,
|
|
|
|
"Page" => receive_undo_delete_post(undo, &delete, context).await,
|
|
|
|
"Group" => receive_undo_delete_community(undo, &delete, context).await,
|
2020-08-01 14:04:42 +00:00
|
|
|
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
|
2020-07-23 14:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_remove(
|
|
|
|
undo: Undo,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let remove = Remove::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-08-06 12:53:58 +00:00
|
|
|
check_is_undo_valid(&undo, &remove)?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-11 14:31:05 +00:00
|
|
|
let type_ = remove
|
|
|
|
.object()
|
|
|
|
.as_single_kind_str()
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
match type_ {
|
2020-08-18 13:43:50 +00:00
|
|
|
"Note" => receive_undo_remove_comment(undo, &remove, context).await,
|
|
|
|
"Page" => receive_undo_remove_post(undo, &remove, context).await,
|
|
|
|
"Group" => receive_undo_remove_community(undo, &remove, context).await,
|
2020-08-01 14:04:42 +00:00
|
|
|
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
|
2020-07-23 14:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
async fn receive_undo_like(undo: Undo, context: &LemmyContext) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let like = Like::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-08-06 12:53:58 +00:00
|
|
|
check_is_undo_valid(&undo, &like)?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-11 14:31:05 +00:00
|
|
|
let type_ = like
|
|
|
|
.object()
|
|
|
|
.as_single_kind_str()
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
match type_ {
|
2020-08-18 13:43:50 +00:00
|
|
|
"Note" => receive_undo_like_comment(undo, &like, context).await,
|
|
|
|
"Page" => receive_undo_like_post(undo, &like, context).await,
|
2020-08-01 14:04:42 +00:00
|
|
|
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
|
2020-07-23 14:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_dislike(
|
|
|
|
undo: Undo,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-11 14:31:05 +00:00
|
|
|
let dislike = Dislike::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-08-06 12:53:58 +00:00
|
|
|
check_is_undo_valid(&undo, &dislike)?;
|
|
|
|
|
2020-08-11 14:31:05 +00:00
|
|
|
let type_ = dislike
|
|
|
|
.object()
|
|
|
|
.as_single_kind_str()
|
|
|
|
.context(location_info!())?;
|
2020-08-12 14:43:45 +00:00
|
|
|
match type_ {
|
2020-08-18 13:43:50 +00:00
|
|
|
"Note" => receive_undo_dislike_comment(undo, &dislike, context).await,
|
|
|
|
"Page" => receive_undo_dislike_post(undo, &dislike, context).await,
|
2020-08-12 14:43:45 +00:00
|
|
|
d => Err(anyhow!("Undo Delete type {} not supported", d).into()),
|
|
|
|
}
|
2020-07-23 14:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_delete_comment(
|
|
|
|
undo: Undo,
|
|
|
|
delete: &Delete,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(delete, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let note = Note::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_ap_id = CommentForm::from_apub(¬e, context, Some(user.actor_id()?))
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.get_ap_id()?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
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: None,
|
|
|
|
deleted: Some(false),
|
|
|
|
read: None,
|
|
|
|
published: None,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
ap_id: comment.ap_id,
|
|
|
|
local: comment.local,
|
|
|
|
};
|
|
|
|
let comment_id = comment.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-07-23 14:36:45 +00:00
|
|
|
Comment::update(conn, comment_id, &comment_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let comment_id = comment.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::read(conn, comment_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// TODO get those recipient actor ids from somewhere
|
|
|
|
let recipient_ids = vec![];
|
|
|
|
let res = CommentResponse {
|
|
|
|
comment: comment_view,
|
|
|
|
recipient_ids,
|
2020-07-28 16:08:28 +00:00
|
|
|
form_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendComment {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::EditComment,
|
|
|
|
comment: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_remove_comment(
|
|
|
|
undo: Undo,
|
|
|
|
remove: &Remove,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let mod_ = get_user_from_activity(remove, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let note = Note::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_ap_id = CommentForm::from_apub(¬e, context, None)
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.get_ap_id()?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment = get_or_fetch_and_insert_comment(&comment_ap_id, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
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(false),
|
|
|
|
deleted: None,
|
|
|
|
read: None,
|
|
|
|
published: None,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
ap_id: comment.ap_id,
|
|
|
|
local: comment.local,
|
|
|
|
};
|
|
|
|
let comment_id = comment.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-07-23 14:36:45 +00:00
|
|
|
Comment::update(conn, comment_id, &comment_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let comment_id = comment.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::read(conn, comment_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// TODO get those recipient actor ids from somewhere
|
|
|
|
let recipient_ids = vec![];
|
|
|
|
let res = CommentResponse {
|
|
|
|
comment: comment_view,
|
|
|
|
recipient_ids,
|
2020-07-28 16:08:28 +00:00
|
|
|
form_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendComment {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::EditComment,
|
|
|
|
comment: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &mod_, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_delete_post(
|
|
|
|
undo: Undo,
|
|
|
|
delete: &Delete,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(delete, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let page = PageExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_ap_id = PostForm::from_apub(&page, context, Some(user.actor_id()?))
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.get_ap_id()?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
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: None,
|
|
|
|
deleted: Some(false),
|
|
|
|
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: post.ap_id,
|
|
|
|
local: post.local,
|
|
|
|
published: None,
|
|
|
|
};
|
|
|
|
let post_id = post.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
Post::update(conn, post_id, &post_form)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let post_id = post.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_remove_post(
|
|
|
|
undo: Undo,
|
|
|
|
remove: &Remove,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let mod_ = get_user_from_activity(remove, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let page = PageExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_ap_id = PostForm::from_apub(&page, context, None)
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.get_ap_id()?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = get_or_fetch_and_insert_post(&post_ap_id, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
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(false),
|
|
|
|
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: post.ap_id,
|
|
|
|
local: post.local,
|
|
|
|
published: None,
|
|
|
|
};
|
|
|
|
let post_id = post.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
Post::update(conn, post_id, &post_form)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
|
|
|
let post_id = post.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &mod_, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_delete_community(
|
|
|
|
undo: Undo,
|
|
|
|
delete: &Delete,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(delete, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let group = GroupExt::from_any_base(delete.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let community_actor_id = CommunityForm::from_apub(&group, context, Some(user.actor_id()?))
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.actor_id;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
2020-07-23 14:36:45 +00:00
|
|
|
Community::read_from_actor_id(conn, &community_actor_id)
|
|
|
|
})
|
|
|
|
.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: None,
|
|
|
|
published: None,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
deleted: Some(false),
|
|
|
|
nsfw: community.nsfw,
|
|
|
|
actor_id: community.actor_id,
|
|
|
|
local: community.local,
|
|
|
|
private_key: community.private_key,
|
|
|
|
public_key: community.public_key,
|
|
|
|
last_refreshed_at: None,
|
2020-08-05 16:03:46 +00:00
|
|
|
icon: Some(community.icon.to_owned()),
|
|
|
|
banner: Some(community.banner.to_owned()),
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let community_id = community.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-07-23 14:36:45 +00:00
|
|
|
Community::update(conn, community_id, &community_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let community_id = community.id;
|
|
|
|
let res = CommunityResponse {
|
2020-08-18 13:43:50 +00:00
|
|
|
community: blocking(context.pool(), move |conn| {
|
2020-07-23 14:36:45 +00:00
|
|
|
CommunityView::read(conn, community_id, None)
|
|
|
|
})
|
|
|
|
.await??,
|
|
|
|
};
|
|
|
|
|
|
|
|
let community_id = res.community.id;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendCommunityRoomMessage {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::EditCommunity,
|
|
|
|
response: res,
|
|
|
|
community_id,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_remove_community(
|
|
|
|
undo: Undo,
|
|
|
|
remove: &Remove,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let mod_ = get_user_from_activity(remove, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let group = GroupExt::from_any_base(remove.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let community_actor_id = CommunityForm::from_apub(&group, context, Some(mod_.actor_id()?))
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.actor_id;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let community = blocking(context.pool(), move |conn| {
|
2020-07-23 14:36:45 +00:00
|
|
|
Community::read_from_actor_id(conn, &community_actor_id)
|
|
|
|
})
|
|
|
|
.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(false),
|
|
|
|
published: None,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
deleted: None,
|
|
|
|
nsfw: community.nsfw,
|
|
|
|
actor_id: community.actor_id,
|
|
|
|
local: community.local,
|
|
|
|
private_key: community.private_key,
|
|
|
|
public_key: community.public_key,
|
|
|
|
last_refreshed_at: None,
|
2020-08-05 16:03:46 +00:00
|
|
|
icon: Some(community.icon.to_owned()),
|
|
|
|
banner: Some(community.banner.to_owned()),
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let community_id = community.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-07-23 14:36:45 +00:00
|
|
|
Community::update(conn, community_id, &community_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let community_id = community.id;
|
|
|
|
let res = CommunityResponse {
|
2020-08-18 13:43:50 +00:00
|
|
|
community: blocking(context.pool(), move |conn| {
|
2020-07-23 14:36:45 +00:00
|
|
|
CommunityView::read(conn, community_id, None)
|
|
|
|
})
|
|
|
|
.await??,
|
|
|
|
};
|
|
|
|
|
|
|
|
let community_id = res.community.id;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendCommunityRoomMessage {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::EditCommunity,
|
|
|
|
response: res,
|
|
|
|
community_id,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &mod_, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_like_comment(
|
|
|
|
undo: Undo,
|
|
|
|
like: &Like,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(like, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let note = Note::from_any_base(like.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment = CommentForm::from_apub(¬e, context, None).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
2020-08-12 14:43:45 +00:00
|
|
|
let user_id = user.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-08-12 14:43:45 +00:00
|
|
|
CommentLike::remove(conn, user_id, comment_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::read(conn, comment_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// TODO get those recipient actor ids from somewhere
|
|
|
|
let recipient_ids = vec![];
|
|
|
|
let res = CommentResponse {
|
|
|
|
comment: comment_view,
|
|
|
|
recipient_ids,
|
2020-07-28 16:08:28 +00:00
|
|
|
form_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendComment {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::CreateCommentLike,
|
|
|
|
comment: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_like_post(
|
|
|
|
undo: Undo,
|
|
|
|
like: &Like,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-07-23 14:36:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(like, context).await?;
|
2020-08-11 14:31:05 +00:00
|
|
|
let page = PageExt::from_any_base(like.object().to_owned().one().context(location_info!())?)?
|
|
|
|
.context(location_info!())?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = PostForm::from_apub(&page, context, None).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
|
2020-07-23 14:36:45 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
2020-08-12 14:43:45 +00:00
|
|
|
let user_id = user.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
PostLike::remove(conn, user_id, post_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-08-12 14:43:45 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-08-12 14:43:45 +00:00
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
2020-08-12 14:43:45 +00:00
|
|
|
op: UserOperation::CreatePostLike,
|
|
|
|
post: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-08-12 14:43:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &user, context).await?;
|
2020-08-12 14:43:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_dislike_comment(
|
|
|
|
undo: Undo,
|
|
|
|
dislike: &Dislike,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-08-12 14:43:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(dislike, context).await?;
|
2020-08-12 14:43:45 +00:00
|
|
|
let note = Note::from_any_base(
|
|
|
|
dislike
|
|
|
|
.object()
|
|
|
|
.to_owned()
|
|
|
|
.one()
|
|
|
|
.context(location_info!())?,
|
|
|
|
)?
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment = CommentForm::from_apub(¬e, context, None).await?;
|
2020-08-12 14:43:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_id = get_or_fetch_and_insert_comment(&comment.get_ap_id()?, context)
|
2020-08-12 14:43:45 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
let user_id = user.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-08-12 14:43:45 +00:00
|
|
|
CommentLike::remove(conn, user_id, comment_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Refetch the view
|
2020-08-18 13:43:50 +00:00
|
|
|
let comment_view = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::read(conn, comment_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-08-12 14:43:45 +00:00
|
|
|
|
|
|
|
// TODO get those recipient actor ids from somewhere
|
|
|
|
let recipient_ids = vec![];
|
|
|
|
let res = CommentResponse {
|
|
|
|
comment: comment_view,
|
|
|
|
recipient_ids,
|
|
|
|
form_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
};
|
2020-08-12 14:43:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendComment {
|
2020-08-12 14:43:45 +00:00
|
|
|
op: UserOperation::CreateCommentLike,
|
|
|
|
comment: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-08-12 14:43:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &user, context).await?;
|
2020-08-12 14:43:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn receive_undo_dislike_post(
|
|
|
|
undo: Undo,
|
|
|
|
dislike: &Dislike,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &LemmyContext,
|
2020-08-12 14:43:45 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_activity(dislike, context).await?;
|
2020-08-12 14:43:45 +00:00
|
|
|
let page = PageExt::from_any_base(
|
|
|
|
dislike
|
|
|
|
.object()
|
|
|
|
.to_owned()
|
|
|
|
.one()
|
|
|
|
.context(location_info!())?,
|
|
|
|
)?
|
|
|
|
.context(location_info!())?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = PostForm::from_apub(&page, context, None).await?;
|
2020-08-12 14:43:45 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_id = get_or_fetch_and_insert_post(&post.get_ap_id()?, context)
|
2020-08-12 14:43:45 +00:00
|
|
|
.await?
|
|
|
|
.id;
|
|
|
|
|
|
|
|
let user_id = user.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
PostLike::remove(conn, user_id, post_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
// Refetch the view
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
|
|
|
PostView::read(conn, post_id, None)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-23 14:36:45 +00:00
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
2020-07-23 14:36:45 +00:00
|
|
|
op: UserOperation::CreatePostLike,
|
|
|
|
post: res,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: None,
|
2020-07-23 14:36:45 +00:00
|
|
|
});
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
announce_if_community_is_local(undo, &user, context).await?;
|
2020-07-23 14:36:45 +00:00
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|