3 check_community_deleted_or_removed,
4 community::send_activity_in_community,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
11 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
13 activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
18 use activitypub_federation::{
19 core::object_id::ObjectId,
21 traits::{ActivityHandler, ApubObject},
22 utils::{verify_domains_match, verify_urls_match},
24 use activitystreams_kinds::public;
25 use lemmy_api_common::{
26 websocket::{send::send_post_ws_message, UserOperationCrud},
29 use lemmy_db_schema::{
32 post::{PostLike, PostLikeForm},
34 traits::{Crud, Likeable},
36 use lemmy_utils::error::LemmyError;
39 impl CreateOrUpdatePage {
40 pub(crate) async fn new(
43 community: &ApubCommunity,
44 kind: CreateOrUpdateType,
45 context: &LemmyContext,
46 ) -> Result<CreateOrUpdatePage, LemmyError> {
47 let id = generate_activity_id(
49 &context.settings().get_protocol_and_hostname(),
51 Ok(CreateOrUpdatePage {
52 actor: ObjectId::new(actor.actor_id()),
54 object: post.into_apub(context).await?,
55 cc: vec![community.actor_id()],
58 audience: Some(ObjectId::new(community.actor_id())),
62 #[tracing::instrument(skip_all)]
66 kind: CreateOrUpdateType,
67 context: &LemmyContext,
68 ) -> Result<(), LemmyError> {
69 let community_id = post.community_id;
70 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
72 let create_or_update = CreateOrUpdatePage::new(post, actor, &community, kind, context).await?;
73 let is_mod_action = create_or_update.object.is_mod_action(context).await?;
74 let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
75 send_activity_in_community(activity, actor, &community, vec![], is_mod_action, context).await?;
80 #[async_trait::async_trait(?Send)]
81 impl ActivityHandler for CreateOrUpdatePage {
82 type DataType = LemmyContext;
83 type Error = LemmyError;
85 fn id(&self) -> &Url {
89 fn actor(&self) -> &Url {
93 #[tracing::instrument(skip_all)]
96 context: &Data<LemmyContext>,
97 request_counter: &mut i32,
98 ) -> Result<(), LemmyError> {
99 verify_is_public(&self.to, &self.cc)?;
100 let community = self.community(context, request_counter).await?;
101 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
102 check_community_deleted_or_removed(&community)?;
105 CreateOrUpdateType::Create => {
106 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
107 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
108 // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
109 // However, when fetching a remote post we generate a new create activity with the current
110 // locked/stickied value, so this check may fail. So only check if its a local community,
111 // because then we will definitely receive all create and update activities separately.
112 let is_stickied_or_locked =
113 self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
114 if community.local && is_stickied_or_locked {
115 return Err(LemmyError::from_message(
116 "New post cannot be stickied or locked",
120 CreateOrUpdateType::Update => {
121 let is_mod_action = self.object.is_mod_action(context).await?;
125 self.object.id.inner(),
132 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
133 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
137 ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
141 #[tracing::instrument(skip_all)]
144 context: &Data<LemmyContext>,
145 request_counter: &mut i32,
146 ) -> Result<(), LemmyError> {
147 let post = ApubPost::from_apub(self.object, context, request_counter).await?;
149 // author likes their own post by default
150 let like_form = PostLikeForm {
152 person_id: post.creator_id,
155 PostLike::like(context.pool(), &like_form).await?;
157 let notif_type = match self.kind {
158 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
159 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
161 send_post_ws_message(post.id, notif_type, None, None, context).await?;