Merge logic for post create and update

rewrite-comment
Felix Ableitner 2021-07-31 19:26:17 +02:00
parent 3eb46868ff
commit dc363c8f35
12 changed files with 105 additions and 163 deletions

View File

@ -9,7 +9,10 @@ use lemmy_api_common::{
mark_post_as_read, mark_post_as_read,
post::*, post::*,
}; };
use lemmy_apub::{activities::post::update::UpdatePost, ApubLikeableType}; use lemmy_apub::{
activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType},
ApubLikeableType,
};
use lemmy_db_queries::{source::post::Post_, Crud, Likeable, Saveable}; use lemmy_db_queries::{source::post::Post_, Crud, Likeable, Saveable};
use lemmy_db_schema::source::{moderator::*, post::*}; use lemmy_db_schema::source::{moderator::*, post::*};
use lemmy_db_views::post_view::PostView; use lemmy_db_views::post_view::PostView;
@ -140,7 +143,13 @@ impl Perform for LockPost {
blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??; blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??;
// apub updates // apub updates
UpdatePost::send(&updated_post, &local_user_view.person, context).await?; CreateOrUpdatePost::send(
&updated_post,
&local_user_view.person,
CreateOrUpdateType::Update,
context,
)
.await?;
// Refetch the post // Refetch the post
let post_id = data.post_id; let post_id = data.post_id;
@ -212,7 +221,13 @@ impl Perform for StickyPost {
// Apub updates // Apub updates
// TODO stickied should pry work like locked for ease of use // TODO stickied should pry work like locked for ease of use
UpdatePost::send(&updated_post, &local_user_view.person, context).await?; CreateOrUpdatePost::send(
&updated_post,
&local_user_view.person,
CreateOrUpdateType::Update,
context,
)
.await?;
// Refetch the post // Refetch the post
let post_id = data.post_id; let post_id = data.post_id;

View File

@ -9,7 +9,7 @@ use lemmy_api_common::{
send_local_notifs, send_local_notifs,
}; };
use lemmy_apub::{ use lemmy_apub::{
activities::comment::create_or_update::{CreateOrUpdateComment, CreateOrUpdateType}, activities::{comment::create_or_update::CreateOrUpdateComment, CreateOrUpdateType},
generate_apub_endpoint, generate_apub_endpoint,
ApubLikeableType, ApubLikeableType,
EndpointType, EndpointType,

View File

@ -7,8 +7,8 @@ use lemmy_api_common::{
get_local_user_view_from_jwt, get_local_user_view_from_jwt,
send_local_notifs, send_local_notifs,
}; };
use lemmy_apub::activities::comment::create_or_update::{ use lemmy_apub::activities::{
CreateOrUpdateComment, comment::create_or_update::CreateOrUpdateComment,
CreateOrUpdateType, CreateOrUpdateType,
}; };
use lemmy_db_queries::{source::comment::Comment_, DeleteableOrRemoveable}; use lemmy_db_queries::{source::comment::Comment_, DeleteableOrRemoveable};
@ -61,6 +61,7 @@ impl PerformCrud for EditComment {
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_comment"))?; .map_err(|_| ApiError::err("couldnt_update_comment"))?;
// Send the apub update
CreateOrUpdateComment::send( CreateOrUpdateComment::send(
&updated_comment, &updated_comment,
&local_user_view.person, &local_user_view.person,

View File

@ -8,7 +8,7 @@ use lemmy_api_common::{
post::*, post::*,
}; };
use lemmy_apub::{ use lemmy_apub::{
activities::post::create::CreatePost as CreateApubPost, activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType},
generate_apub_endpoint, generate_apub_endpoint,
ApubLikeableType, ApubLikeableType,
EndpointType, EndpointType,
@ -87,7 +87,13 @@ impl PerformCrud for CreatePost {
.await? .await?
.map_err(|_| ApiError::err("couldnt_create_post"))?; .map_err(|_| ApiError::err("couldnt_create_post"))?;
CreateApubPost::send(&updated_post, &local_user_view.person, context).await?; CreateOrUpdatePost::send(
&updated_post,
&local_user_view.person,
CreateOrUpdateType::Create,
context,
)
.await?;
// They like their own post by default // They like their own post by default
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;

View File

@ -1,7 +1,7 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{blocking, check_community_ban, get_local_user_view_from_jwt, post::*}; use lemmy_api_common::{blocking, check_community_ban, get_local_user_view_from_jwt, post::*};
use lemmy_apub::activities::post::update::UpdatePost; use lemmy_apub::activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType};
use lemmy_db_queries::{source::post::Post_, Crud, DeleteableOrRemoveable}; use lemmy_db_queries::{source::post::Post_, Crud, DeleteableOrRemoveable};
use lemmy_db_schema::{naive_now, source::post::*}; use lemmy_db_schema::{naive_now, source::post::*};
use lemmy_db_views::post_view::PostView; use lemmy_db_views::post_view::PostView;
@ -89,7 +89,13 @@ impl PerformCrud for EditPost {
}; };
// Send apub update // Send apub update
UpdatePost::send(&updated_post, &local_user_view.person, context).await?; CreateOrUpdatePost::send(
&updated_post,
&local_user_view.person,
CreateOrUpdateType::Update,
context,
)
.await?;
let post_id = data.post_id; let post_id = data.post_id;
let mut post_view = blocking(context.pool(), move |conn| { let mut post_view = blocking(context.pool(), move |conn| {

View File

@ -6,13 +6,14 @@ use crate::{
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_person_in_community, verify_person_in_community,
CreateOrUpdateType,
}, },
activity_queue::send_to_community_new, activity_queue::send_to_community_new,
extensions::context::lemmy_context, extensions::context::lemmy_context,
objects::{comment::Note, FromApub, ToApub}, objects::{comment::Note, FromApub, ToApub},
ActorType, ActorType,
}; };
use activitystreams::{activity::kind::CreateType, link::Mention}; use activitystreams::link::Mention;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::{ use lemmy_apub_lib::{
values::PublicUrl, values::PublicUrl,
@ -27,12 +28,6 @@ use lemmy_websocket::{LemmyContext, UserOperationCrud};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum CreateOrUpdateType {
Create,
Update,
}
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CreateOrUpdateComment { pub struct CreateOrUpdateComment {
@ -62,10 +57,7 @@ impl CreateOrUpdateComment {
}) })
.await??; .await??;
let id = match kind { let id = generate_activity_id(kind.clone())?;
CreateOrUpdateType::Create => generate_activity_id(CreateType::Create),
CreateOrUpdateType::Update => generate_activity_id(CreateType::Create),
}?;
let maa = collect_non_local_mentions(comment, &community, context).await?; let maa = collect_non_local_mentions(comment, &community, context).await?;
let create_or_update = CreateOrUpdateComment { let create_or_update = CreateOrUpdateComment {

View File

@ -12,7 +12,7 @@ use crate::{
undo_delete::UndoDeletePostCommentOrCommunity, undo_delete::UndoDeletePostCommentOrCommunity,
}, },
generate_activity_id, generate_activity_id,
post::{create::CreatePost, update::UpdatePost}, post::create_or_update::CreateOrUpdatePost,
removal::{ removal::{
remove::RemovePostCommentCommunityOrMod, remove::RemovePostCommentCommunityOrMod,
undo_remove::UndoRemovePostCommentOrCommunity, undo_remove::UndoRemovePostCommentOrCommunity,
@ -45,8 +45,7 @@ use url::Url;
#[serde(untagged)] #[serde(untagged)]
pub enum AnnouncableActivities { pub enum AnnouncableActivities {
CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdateComment(CreateOrUpdateComment),
CreatePost(CreatePost), CreateOrUpdatePost(Box<CreateOrUpdatePost>),
UpdatePost(UpdatePost),
LikePostOrComment(LikePostOrComment), LikePostOrComment(LikePostOrComment),
DislikePostOrComment(DislikePostOrComment), DislikePostOrComment(DislikePostOrComment),
UndoLikePostOrComment(UndoLikePostOrComment), UndoLikePostOrComment(UndoLikePostOrComment),
@ -86,7 +85,7 @@ impl AnnounceActivity {
kind: AnnounceType::Announce, kind: AnnounceType::Announce,
common: ActivityCommonFields { common: ActivityCommonFields {
context: lemmy_context(), context: lemmy_context(),
id: generate_activity_id(AnnounceType::Announce)?, id: generate_activity_id(&AnnounceType::Announce)?,
actor: community.actor_id(), actor: community.actor_id(),
unparsed: Default::default(), unparsed: Default::default(),
}, },

View File

@ -15,6 +15,8 @@ use lemmy_db_schema::{
use lemmy_db_views_actor::community_view::CommunityView; use lemmy_db_views_actor::community_view::CommunityView;
use lemmy_utils::{settings::structs::Settings, LemmyError}; use lemmy_utils::{settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use strum_macros::ToString;
use url::{ParseError, Url}; use url::{ParseError, Url};
use uuid::Uuid; use uuid::Uuid;
@ -28,6 +30,13 @@ pub mod removal;
pub mod send; pub mod send;
pub mod voting; pub mod voting;
#[derive(Clone, Debug, ToString, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum CreateOrUpdateType {
Create,
Update,
}
/// Checks that the specified Url actually identifies a Person (by fetching it), and that the person /// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
/// doesn't have a site ban. /// doesn't have a site ban.
async fn verify_person( async fn verify_person(

View File

@ -5,7 +5,9 @@ use crate::{
generate_activity_id, generate_activity_id,
post::send_websocket_message, post::send_websocket_message,
verify_activity, verify_activity,
verify_mod_action,
verify_person_in_community, verify_person_in_community,
CreateOrUpdateType,
}, },
activity_queue::send_to_community_new, activity_queue::send_to_community_new,
extensions::context::lemmy_context, extensions::context::lemmy_context,
@ -13,7 +15,6 @@ use crate::{
objects::{post::Page, FromApub, ToApub}, objects::{post::Page, FromApub, ToApub},
ActorType, ActorType,
}; };
use activitystreams::activity::kind::CreateType;
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::{ use lemmy_apub_lib::{
@ -31,29 +32,35 @@ use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CreatePost { pub struct CreateOrUpdatePost {
to: PublicUrl, to: PublicUrl,
object: Page, object: Page,
cc: [Url; 1], cc: [Url; 1],
r#type: CreateType, #[serde(rename = "type")]
kind: CreateOrUpdateType,
#[serde(flatten)] #[serde(flatten)]
common: ActivityCommonFields, common: ActivityCommonFields,
} }
impl CreatePost { impl CreateOrUpdatePost {
pub async fn send(post: &Post, actor: &Person, context: &LemmyContext) -> Result<(), LemmyError> { pub async fn send(
post: &Post,
actor: &Person,
kind: CreateOrUpdateType,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let community_id = post.community_id; let community_id = post.community_id;
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id) Community::read(conn, community_id)
}) })
.await??; .await??;
let id = generate_activity_id(CreateType::Create)?; let id = generate_activity_id(kind.clone())?;
let create = CreatePost { let create_or_update = CreateOrUpdatePost {
to: PublicUrl::Public, to: PublicUrl::Public,
object: post.to_apub(context.pool()).await?, object: post.to_apub(context.pool()).await?,
cc: [community.actor_id()], cc: [community.actor_id()],
r#type: Default::default(), kind,
common: ActivityCommonFields { common: ActivityCommonFields {
context: lemmy_context(), context: lemmy_context(),
id: id.clone(), id: id.clone(),
@ -62,23 +69,24 @@ impl CreatePost {
}, },
}; };
let activity = AnnouncableActivities::CreatePost(create); let activity = AnnouncableActivities::CreateOrUpdatePost(Box::new(create_or_update));
send_to_community_new(activity, &id, actor, &community, vec![], context).await send_to_community_new(activity, &id, actor, &community, vec![], context).await
} }
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl ActivityHandler for CreatePost { impl ActivityHandler for CreateOrUpdatePost {
async fn verify( async fn verify(
&self, &self,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = extract_community(&self.cc, context, request_counter).await?;
let community_id = &community.actor_id();
verify_activity(self.common())?; verify_activity(self.common())?;
verify_person_in_community(&self.common.actor, community_id, context, request_counter).await?; let community = extract_community(&self.cc, context, request_counter).await?;
let community_id = community.actor_id();
verify_person_in_community(&self.common.actor, &community_id, context, request_counter).await?;
match self.kind {
CreateOrUpdateType::Create => {
verify_domains_match(&self.common.actor, &self.object.id)?; verify_domains_match(&self.common.actor, &self.object.id)?;
verify_urls_match(&self.common.actor, &self.object.attributed_to)?; verify_urls_match(&self.common.actor, &self.object.attributed_to)?;
// Check that the post isnt locked or stickied, as that isnt possible for newly created posts. // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
@ -90,6 +98,17 @@ impl ActivityHandler for CreatePost {
if community.local && is_stickied_or_locked { if community.local && is_stickied_or_locked {
return Err(anyhow!("New post cannot be stickied or locked").into()); return Err(anyhow!("New post cannot be stickied or locked").into());
} }
}
CreateOrUpdateType::Update => {
let is_mod_action = self.object.is_mod_action(context.pool()).await?;
if is_mod_action {
verify_mod_action(&self.common.actor, community_id, context).await?;
} else {
verify_domains_match(&self.common.actor, &self.object.id)?;
verify_urls_match(&self.common.actor, &self.object.attributed_to)?;
}
}
}
self.object.verify(context, request_counter).await?; self.object.verify(context, request_counter).await?;
Ok(()) Ok(())
} }
@ -110,7 +129,11 @@ impl ActivityHandler for CreatePost {
) )
.await?; .await?;
send_websocket_message(post.id, UserOperationCrud::CreatePost, context).await let notif_type = match self.kind {
CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
CreateOrUpdateType::Update => UserOperationCrud::EditPost,
};
send_websocket_message(post.id, notif_type, context).await
} }
fn common(&self) -> &ActivityCommonFields { fn common(&self) -> &ActivityCommonFields {

View File

@ -4,8 +4,7 @@ use lemmy_db_views::post_view::PostView;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendPost, LemmyContext}; use lemmy_websocket::{messages::SendPost, LemmyContext};
pub mod create; pub mod create_or_update;
pub mod update;
pub(crate) async fn send_websocket_message< pub(crate) async fn send_websocket_message<
OP: ToString + Send + lemmy_websocket::OperationType + 'static, OP: ToString + Send + lemmy_websocket::OperationType + 'static,

View File

@ -1,106 +0,0 @@
use crate::{
activities::{
community::announce::AnnouncableActivities,
generate_activity_id,
post::send_websocket_message,
verify_activity,
verify_mod_action,
verify_person_in_community,
},
activity_queue::send_to_community_new,
extensions::context::lemmy_context,
fetcher::community::get_or_fetch_and_upsert_community,
objects::{post::Page, FromApub, ToApub},
ActorType,
};
use activitystreams::activity::kind::UpdateType;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::{community::Community, person::Person, 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 UpdatePost {
to: PublicUrl,
object: Page,
cc: [Url; 1],
r#type: UpdateType,
#[serde(flatten)]
common: ActivityCommonFields,
}
impl UpdatePost {
pub async fn send(post: &Post, actor: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
let community_id = post.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let id = generate_activity_id(UpdateType::Update)?;
let update = UpdatePost {
to: PublicUrl::Public,
object: post.to_apub(context.pool()).await?,
cc: [community.actor_id()],
r#type: Default::default(),
common: ActivityCommonFields {
context: lemmy_context(),
id: id.clone(),
actor: actor.actor_id(),
unparsed: Default::default(),
},
};
let activity = AnnouncableActivities::UpdatePost(update);
send_to_community_new(activity, &id, actor, &community, vec![], context).await
}
}
#[async_trait::async_trait(?Send)]
impl ActivityHandler for UpdatePost {
async fn verify(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let community_id = get_or_fetch_and_upsert_community(&self.cc[0], context, request_counter)
.await?
.actor_id();
let is_mod_action = self.object.is_mod_action(context.pool()).await?;
verify_activity(self.common())?;
verify_person_in_community(&self.common.actor, &community_id, context, request_counter).await?;
if is_mod_action {
verify_mod_action(&self.common.actor, community_id, context).await?;
} else {
verify_urls_match(&self.common.actor, &self.object.attributed_to)?;
}
self.object.verify(context, request_counter).await?;
Ok(())
}
async fn receive(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let post = Post::from_apub(
&self.object,
context,
self.common.actor.clone(),
request_counter,
// TODO: we already check here if the mod action is valid, can remove that check param
true,
)
.await?;
send_websocket_message(post.id, UserOperationCrud::EditPost, context).await
}
fn common(&self) -> &ActivityCommonFields {
&self.common
}
}

View File

@ -9,7 +9,7 @@ use crate::activities::{
}, },
deletion::{delete::DeletePostCommentOrCommunity, undo_delete::UndoDeletePostCommentOrCommunity}, deletion::{delete::DeletePostCommentOrCommunity, undo_delete::UndoDeletePostCommentOrCommunity},
following::{accept::AcceptFollowCommunity, follow::FollowCommunity, undo::UndoFollowCommunity}, following::{accept::AcceptFollowCommunity, follow::FollowCommunity, undo::UndoFollowCommunity},
post::{create::CreatePost, update::UpdatePost}, post::create_or_update::CreateOrUpdatePost,
private_message::{ private_message::{
create::CreatePrivateMessage, create::CreatePrivateMessage,
delete::DeletePrivateMessage, delete::DeletePrivateMessage,
@ -49,8 +49,7 @@ pub enum GroupInboxActivities {
FollowCommunity(FollowCommunity), FollowCommunity(FollowCommunity),
UndoFollowCommunity(UndoFollowCommunity), UndoFollowCommunity(UndoFollowCommunity),
CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdateComment(CreateOrUpdateComment),
CreatePost(CreatePost), CreateOrUpdatePost(Box<CreateOrUpdatePost>),
UpdatePost(UpdatePost),
LikePostOrComment(LikePostOrComment), LikePostOrComment(LikePostOrComment),
DislikePostOrComment(DislikePostOrComment), DislikePostOrComment(DislikePostOrComment),
UndoLikePostOrComment(UndoLikePostOrComment), UndoLikePostOrComment(UndoLikePostOrComment),
@ -72,8 +71,7 @@ pub enum SharedInboxActivities {
FollowCommunity(FollowCommunity), FollowCommunity(FollowCommunity),
UndoFollowCommunity(UndoFollowCommunity), UndoFollowCommunity(UndoFollowCommunity),
CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdateComment(CreateOrUpdateComment),
CreatePost(CreatePost), CreateOrUpdatePost(CreateOrUpdatePost),
UpdatePost(UpdatePost),
LikePostOrComment(LikePostOrComment), LikePostOrComment(LikePostOrComment),
DislikePostOrComment(DislikePostOrComment), DislikePostOrComment(DislikePostOrComment),
UndoDislikePostOrComment(UndoDislikePostOrComment), UndoDislikePostOrComment(UndoDislikePostOrComment),