convert undo_delete and undo_remove receivers

This commit is contained in:
Felix Ableitner 2021-06-29 03:08:47 +02:00
parent 004a07c41b
commit ab5b072e21
17 changed files with 261 additions and 232 deletions

View file

@ -1,73 +0,0 @@
use activitystreams::activity::{Dislike, Like};
use lemmy_api_common::{blocking, comment::CommentResponse};
use lemmy_db_queries::{source::comment::Comment_, Likeable};
use lemmy_db_schema::source::comment::{Comment, CommentLike};
use lemmy_db_views::comment_view::CommentView;
use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation, UserOperationCrud};
pub(crate) async fn receive_undo_delete_comment(
context: &LemmyContext,
comment: Comment,
) -> Result<(), LemmyError> {
let deleted_comment = blocking(context.pool(), move |conn| {
Comment::update_deleted(conn, comment.id, false)
})
.await??;
// Refetch the view
let comment_id = deleted_comment.id;
let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, None)
})
.await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
let res = CommentResponse {
comment_view,
recipient_ids,
form_id: None,
};
context.chat_server().do_send(SendComment {
op: UserOperationCrud::EditComment,
comment: res,
websocket_id: None,
});
Ok(())
}
pub(crate) async fn receive_undo_remove_comment(
context: &LemmyContext,
comment: Comment,
) -> Result<(), LemmyError> {
let removed_comment = blocking(context.pool(), move |conn| {
Comment::update_removed(conn, comment.id, false)
})
.await??;
// Refetch the view
let comment_id = removed_comment.id;
let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, None)
})
.await??;
// TODO get those recipient actor ids from somewhere
let recipient_ids = vec![];
let res = CommentResponse {
comment_view,
recipient_ids,
form_id: None,
};
context.chat_server().do_send(SendComment {
op: UserOperationCrud::EditComment,
comment: res,
websocket_id: None,
});
Ok(())
}

View file

@ -4,17 +4,11 @@ use activitystreams::{
error::DomainError,
};
use anyhow::{anyhow, Context};
use lemmy_apub::fetcher::person::get_or_fetch_and_upsert_person;
use lemmy_db_schema::source::person::Person;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug;
use std::fmt::Debug;
use url::Url;
pub(crate) mod comment_undo;
pub(crate) mod post_undo;
/// Return HTTP 501 for unsupported activities in inbox.
pub(crate) fn receive_unhandled_activity<A>(activity: A) -> Result<(), LemmyError>
where

View file

@ -1,60 +0,0 @@
use activitystreams::activity::{Dislike, Like};
use lemmy_api_common::{blocking, post::PostResponse};
use lemmy_db_queries::{source::post::Post_, Likeable};
use lemmy_db_schema::source::post::{Post, PostLike};
use lemmy_db_views::post_view::PostView;
use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation, UserOperationCrud};
pub(crate) async fn receive_undo_delete_post(
context: &LemmyContext,
post: Post,
) -> Result<(), LemmyError> {
let deleted_post = blocking(context.pool(), move |conn| {
Post::update_deleted(conn, post.id, false)
})
.await??;
// Refetch the view
let post_id = deleted_post.id;
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, None)
})
.await??;
let res = PostResponse { post_view };
context.chat_server().do_send(SendPost {
op: UserOperationCrud::EditPost,
post: res,
websocket_id: None,
});
Ok(())
}
pub(crate) async fn receive_undo_remove_post(
context: &LemmyContext,
post: Post,
) -> Result<(), LemmyError> {
let removed_post = blocking(context.pool(), move |conn| {
Post::update_removed(conn, post.id, false)
})
.await??;
// Refetch the view
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_view };
context.chat_server().do_send(SendPost {
op: UserOperationCrud::EditPost,
post: res,
websocket_id: None,
});
Ok(())
}

View file

@ -12,9 +12,9 @@ use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteComment {
actor: Url,
pub(in crate::activities_new::comment) actor: Url,
to: PublicUrl,
object: Url,
pub(in crate::activities_new::comment) object: Url,
cc: [Url; 1],
#[serde(rename = "type")]
kind: DeleteType,
@ -44,7 +44,6 @@ impl ReceiveActivity for Activity<DeleteComment> {
})
.await??;
// TODO get those recipient actor ids from somewhere
send_websocket_message(
deleted_comment.id,
vec![],

View file

@ -22,8 +22,10 @@ pub mod delete;
pub mod dislike;
pub mod like;
pub mod remove;
pub mod undo_delete;
pub mod undo_dislike;
pub mod undo_like;
pub mod undo_remove;
pub mod update;
async fn get_notif_recipients(
@ -45,6 +47,8 @@ async fn get_notif_recipients(
send_local_notifs(mentions, comment.clone(), actor, post, context.pool(), true).await
}
// TODO: in many call sites we are setting an empty vec for recipient_ids, we should get the actual
// recipient actors from somewhere
async fn send_websocket_message<OP: ToString + Send + lemmy_websocket::OperationType + 'static>(
comment_id: CommentId,
recipient_ids: Vec<LocalUserId>,
@ -96,7 +100,6 @@ async fn like_or_dislike_comment(
})
.await??;
// TODO get those recipient actor ids from somewhere
send_websocket_message(
comment_id,
vec![],
@ -122,7 +125,6 @@ async fn undo_like_or_dislike_comment(
})
.await??;
// TODO get those recipient actor ids from somewhere
send_websocket_message(
comment.id,
vec![],

View file

@ -17,7 +17,7 @@ use url::Url;
pub struct RemoveComment {
actor: Url,
to: PublicUrl,
object: Url,
pub(in crate::activities_new::comment) object: Url,
cc: [Url; 1],
#[serde(rename = "type")]
kind: RemoveType,
@ -47,7 +47,6 @@ impl ReceiveActivity for Activity<RemoveComment> {
})
.await??;
// TODO get those recipient actor ids from somewhere
send_websocket_message(
removed_comment.id,
vec![],

View file

@ -0,0 +1,60 @@
use crate::{
activities_new::comment::{delete::DeleteComment, send_websocket_message},
inbox::new_inbox_routing::Activity,
};
use activitystreams::activity::kind::UndoType;
use lemmy_api_common::blocking;
use lemmy_apub::{check_is_apub_id_valid, fetcher::objects::get_or_fetch_and_insert_comment};
use lemmy_apub_lib::{verify_domains_match, PublicUrl, ReceiveActivity, VerifyActivity};
use lemmy_db_queries::source::comment::Comment_;
use lemmy_db_schema::source::comment::Comment;
use lemmy_utils::LemmyError;
use lemmy_websocket::{LemmyContext, UserOperationCrud};
use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UndoDeleteComment {
actor: Url,
to: PublicUrl,
object: Activity<DeleteComment>,
cc: [Url; 1],
#[serde(rename = "type")]
kind: UndoType,
}
#[async_trait::async_trait(?Send)]
impl VerifyActivity for Activity<UndoDeleteComment> {
async fn verify(&self, context: &LemmyContext) -> Result<(), LemmyError> {
verify_domains_match(&self.inner.actor, self.id_unchecked())?;
verify_domains_match(&self.inner.actor, &self.inner.object.inner.actor)?;
check_is_apub_id_valid(&self.inner.actor, false)?;
self.inner.object.verify(context).await
}
}
#[async_trait::async_trait(?Send)]
impl ReceiveActivity for Activity<UndoDeleteComment> {
async fn receive(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let comment =
get_or_fetch_and_insert_comment(&self.inner.object.inner.object, context, request_counter)
.await?;
let deleted_comment = blocking(context.pool(), move |conn| {
Comment::update_deleted(conn, comment.id, false)
})
.await??;
send_websocket_message(
deleted_comment.id,
vec![],
UserOperationCrud::EditComment,
context,
)
.await
}
}

View file

@ -1,8 +1,5 @@
use crate::{
activities_new::{
comment::{dislike::DislikeComment, undo_like_or_dislike_comment},
post::like::LikePost,
},
activities_new::comment::{dislike::DislikeComment, undo_like_or_dislike_comment},
inbox::new_inbox_routing::Activity,
};
use activitystreams::activity::kind::UndoType;

View file

@ -1,8 +1,5 @@
use crate::{
activities_new::{
comment::{like::LikeComment, undo_like_or_dislike_comment},
post::like::LikePost,
},
activities_new::comment::{like::LikeComment, undo_like_or_dislike_comment},
inbox::new_inbox_routing::Activity,
};
use activitystreams::activity::kind::UndoType;

View file

@ -0,0 +1,63 @@
use crate::{
activities_new::{
comment::{remove::RemoveComment, send_websocket_message},
verify_mod_action,
},
inbox::new_inbox_routing::Activity,
};
use activitystreams::activity::kind::UndoType;
use lemmy_api_common::blocking;
use lemmy_apub::{check_is_apub_id_valid, fetcher::objects::get_or_fetch_and_insert_comment};
use lemmy_apub_lib::{verify_domains_match, PublicUrl, ReceiveActivity, VerifyActivity};
use lemmy_db_queries::source::comment::Comment_;
use lemmy_db_schema::source::comment::Comment;
use lemmy_utils::LemmyError;
use lemmy_websocket::{LemmyContext, UserOperationCrud};
use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UndoRemoveComment {
actor: Url,
to: PublicUrl,
object: Activity<RemoveComment>,
cc: [Url; 1],
#[serde(rename = "type")]
kind: UndoType,
}
#[async_trait::async_trait(?Send)]
impl VerifyActivity for Activity<UndoRemoveComment> {
async fn verify(&self, context: &LemmyContext) -> Result<(), LemmyError> {
verify_domains_match(&self.inner.actor, self.id_unchecked())?;
check_is_apub_id_valid(&self.inner.actor, false)?;
verify_mod_action(self.inner.actor.clone(), self.inner.cc[0].clone(), context).await?;
self.inner.object.verify(context).await
}
}
#[async_trait::async_trait(?Send)]
impl ReceiveActivity for Activity<UndoRemoveComment> {
async fn receive(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let comment =
get_or_fetch_and_insert_comment(&self.inner.object.inner.object, context, request_counter)
.await?;
let removed_comment = blocking(context.pool(), move |conn| {
Comment::update_removed(conn, comment.id, false)
})
.await??;
send_websocket_message(
removed_comment.id,
vec![],
UserOperationCrud::EditComment,
context,
)
.await
}
}

View file

@ -12,9 +12,9 @@ use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DeletePost {
actor: Url,
pub(in crate::activities_new::post) actor: Url,
to: PublicUrl,
object: Url,
pub(in crate::activities_new::post) object: Url,
cc: [Url; 1],
#[serde(rename = "type")]
kind: DeleteType,

View file

@ -18,8 +18,10 @@ pub mod delete;
pub mod dislike;
pub mod like;
pub mod remove;
pub mod undo_delete;
pub mod undo_dislike;
pub mod undo_like;
pub mod undo_remove;
pub mod update;
async fn send_websocket_message<OP: ToString + Send + lemmy_websocket::OperationType + 'static>(

View file

@ -17,7 +17,7 @@ use url::Url;
pub struct RemovePost {
actor: Url,
to: PublicUrl,
object: Url,
pub(in crate::activities_new::post) object: Url,
cc: [Url; 1],
#[serde(rename = "type")]
kind: RemoveType,
@ -39,7 +39,7 @@ impl ReceiveActivity for Activity<RemovePost> {
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
// TODO: check that actor is instance mod if community is local (same for RemoveComment)
// TODO: check that actor is instance mod if community is local (same for undo, RemoveComment)
let post = get_or_fetch_and_insert_post(&self.inner.object, context, request_counter).await?;
let removed_post = blocking(context.pool(), move |conn| {

View file

@ -0,0 +1,54 @@
use crate::{
activities_new::post::{delete::DeletePost, send_websocket_message},
inbox::new_inbox_routing::Activity,
};
use activitystreams::activity::kind::UndoType;
use lemmy_api_common::blocking;
use lemmy_apub::{check_is_apub_id_valid, fetcher::objects::get_or_fetch_and_insert_post};
use lemmy_apub_lib::{verify_domains_match, PublicUrl, ReceiveActivity, VerifyActivity};
use lemmy_db_queries::source::post::Post_;
use lemmy_db_schema::source::post::Post;
use lemmy_utils::LemmyError;
use lemmy_websocket::{LemmyContext, UserOperationCrud};
use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UndoDeletePost {
actor: Url,
to: PublicUrl,
object: Activity<DeletePost>,
cc: [Url; 1],
#[serde(rename = "type")]
kind: UndoType,
}
#[async_trait::async_trait(?Send)]
impl VerifyActivity for Activity<UndoDeletePost> {
async fn verify(&self, context: &LemmyContext) -> Result<(), LemmyError> {
verify_domains_match(&self.inner.actor, self.id_unchecked())?;
verify_domains_match(&self.inner.actor, &self.inner.object.inner.actor)?;
check_is_apub_id_valid(&self.inner.actor, false)?;
self.inner.object.verify(context).await
}
}
#[async_trait::async_trait(?Send)]
impl ReceiveActivity for Activity<UndoDeletePost> {
async fn receive(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let post =
get_or_fetch_and_insert_post(&self.inner.object.inner.object, context, request_counter)
.await?;
let deleted_post = blocking(context.pool(), move |conn| {
Post::update_deleted(conn, post.id, false)
})
.await??;
send_websocket_message(deleted_post.id, UserOperationCrud::EditPost, context).await
}
}

View file

@ -0,0 +1,57 @@
use crate::{
activities_new::{
post::{remove::RemovePost, send_websocket_message},
verify_mod_action,
},
inbox::new_inbox_routing::Activity,
};
use activitystreams::activity::kind::UndoType;
use lemmy_api_common::blocking;
use lemmy_apub::{check_is_apub_id_valid, fetcher::objects::get_or_fetch_and_insert_post};
use lemmy_apub_lib::{verify_domains_match, PublicUrl, ReceiveActivity, VerifyActivity};
use lemmy_db_queries::source::post::Post_;
use lemmy_db_schema::source::post::Post;
use lemmy_utils::LemmyError;
use lemmy_websocket::{LemmyContext, UserOperationCrud};
use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UndoRemovePost {
actor: Url,
to: PublicUrl,
object: Activity<RemovePost>,
cc: [Url; 1],
#[serde(rename = "type")]
kind: UndoType,
}
#[async_trait::async_trait(?Send)]
impl VerifyActivity for Activity<UndoRemovePost> {
async fn verify(&self, context: &LemmyContext) -> Result<(), LemmyError> {
verify_domains_match(&self.inner.actor, self.id_unchecked())?;
check_is_apub_id_valid(&self.inner.actor, false)?;
verify_mod_action(self.inner.actor.clone(), self.inner.cc[0].clone(), context).await?;
self.inner.object.verify(context).await
}
}
#[async_trait::async_trait(?Send)]
impl ReceiveActivity for Activity<UndoRemovePost> {
async fn receive(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let post =
get_or_fetch_and_insert_post(&self.inner.object.inner.object, context, request_counter)
.await?;
let removed_post = blocking(context.pool(), move |conn| {
Post::update_removed(conn, post.id, false)
})
.await??;
send_websocket_message(removed_post.id, UserOperationCrud::EditPost, context).await
}
}

View file

@ -5,8 +5,10 @@ use crate::activities_new::{
dislike::DislikeComment,
like::LikeComment,
remove::RemoveComment,
undo_delete::UndoDeleteComment,
undo_dislike::UndoDislikeComment,
undo_like::UndoLikeComment,
undo_remove::UndoRemoveComment,
update::UpdateComment,
},
community::{
@ -23,8 +25,10 @@ use crate::activities_new::{
dislike::DislikePost,
like::LikePost,
remove::RemovePost,
undo_delete::UndoDeletePost,
undo_dislike::UndoDislikePost,
undo_like::UndoLikePost,
undo_remove::UndoRemovePost,
update::UpdatePost,
},
private_message::{
@ -81,13 +85,17 @@ pub enum PersonAcceptedActivitiesNew {
UndoLikeComment(UndoLikeComment),
UndoDislikeComment(UndoDislikeComment),
DeleteComment(DeleteComment),
UndoDeleteComment(UndoDeleteComment),
RemoveComment(RemoveComment),
UndoRemoveComment(UndoRemoveComment),
CreatePost(CreatePost),
UpdatePost(UpdatePost),
LikePost(LikePost),
DislikePost(DislikePost),
DeletePost(DeletePost),
UndoDeletePost(UndoDeletePost),
RemovePost(RemovePost),
UndoRemovePost(UndoRemovePost),
UndoLikePost(UndoLikePost),
UndoDislikePost(UndoDislikePost),
UpdateCommunity(UpdateCommunity),

View file

@ -1,14 +1,9 @@
use crate::{
activities::receive::{
comment_undo::{receive_undo_delete_comment, receive_undo_remove_comment},
post_undo::{receive_undo_delete_post, receive_undo_remove_post},
receive_unhandled_activity,
verify_activity_domains_valid,
},
activities::receive::{receive_unhandled_activity, verify_activity_domains_valid},
inbox::verify_is_addressed_to_public,
};
use activitystreams::{
activity::{ActorAndObjectRef, Add, Announce, Block, Delete, OptTargetRef, Remove, Undo},
activity::{ActorAndObjectRef, Add, Announce, Block, OptTargetRef, Remove, Undo},
base::AnyBase,
object::AsObject,
prelude::*,
@ -17,12 +12,8 @@ use anyhow::{anyhow, Context};
use lemmy_api_common::blocking;
use lemmy_apub::{
fetcher::person::get_or_fetch_and_upsert_person,
find_object_by_id,
find_post_or_comment_by_id,
generate_moderators_url,
CommunityType,
Object,
PostOrComment,
};
use lemmy_db_queries::{
source::community::CommunityModerator_,
@ -135,12 +126,8 @@ pub(in crate::inbox) async fn receive_undo_for_community(
.as_single_kind_str()
.and_then(|s| s.parse().ok())
{
Some(Delete) => {
receive_undo_delete_for_community(context, undo, expected_domain, request_counter).await
}
Some(Remove) => {
receive_undo_remove_for_community(context, undo, announce, expected_domain).await
}
Some(Delete) => todo!(),
Some(Remove) => todo!(),
Some(Like) => todo!(),
Some(Dislike) => todo!(),
Some(Block) => {
@ -157,63 +144,6 @@ pub(in crate::inbox) async fn receive_undo_for_community(
}
}
/// A post, comment or community deletion being reverted
pub(in crate::inbox) async fn receive_undo_delete_for_community(
context: &LemmyContext,
undo: Undo,
expected_domain: &Url,
_request_counter: &mut i32,
) -> Result<(), LemmyError> {
let delete = Delete::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
verify_is_addressed_to_public(&delete)?;
let object = delete
.object()
.to_owned()
.single_xsd_any_uri()
.context(location_info!())?;
match find_object_by_id(context, object).await {
Ok(Object::Post(p)) => {
verify_activity_domains_valid(&delete, &expected_domain, true)?;
receive_undo_delete_post(context, *p).await
}
Ok(Object::Comment(c)) => {
verify_activity_domains_valid(&delete, &expected_domain, true)?;
receive_undo_delete_comment(context, *c).await
}
Ok(Object::Community(_)) => todo!(),
// if we dont have the object or dont support its deletion, no need to do anything
_ => Ok(()),
}
}
/// A post or comment removal being reverted
pub(in crate::inbox) async fn receive_undo_remove_for_community(
context: &LemmyContext,
undo: Undo,
announce: Option<Announce>,
expected_domain: &Url,
) -> Result<(), LemmyError> {
let remove = Remove::from_any_base(undo.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
verify_activity_domains_valid(&remove, &expected_domain, false)?;
verify_is_addressed_to_public(&remove)?;
verify_undo_remove_actor_instance(&undo, &remove, &announce, context).await?;
let object = remove
.object()
.to_owned()
.single_xsd_any_uri()
.context(location_info!())?;
match find_post_or_comment_by_id(context, object).await {
Ok(PostOrComment::Post(p)) => receive_undo_remove_post(context, *p).await,
Ok(PostOrComment::Comment(c)) => receive_undo_remove_comment(context, *c).await,
// if we dont have the object, no need to do anything
Err(_) => Ok(()),
}
}
/// Add a new mod to the community (can only be done by an existing mod).
pub(in crate::inbox) async fn receive_add_for_community(
context: &LemmyContext,