3 check_community_deleted_or_removed,
4 community::{announce::GetCommunity, send_activity_in_community},
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
11 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
12 protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
15 use activitypub_federation::{
16 core::object_id::ObjectId,
18 traits::{ActivityHandler, ApubObject},
19 utils::{verify_domains_match, verify_urls_match},
21 use activitystreams_kinds::public;
22 use lemmy_db_schema::{
25 post::{PostLike, PostLikeForm},
27 traits::{Crud, Likeable},
29 use lemmy_utils::error::LemmyError;
30 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
33 impl CreateOrUpdatePost {
34 pub(crate) async fn new(
37 community: &ApubCommunity,
38 kind: CreateOrUpdateType,
39 context: &LemmyContext,
40 ) -> Result<CreateOrUpdatePost, LemmyError> {
41 let id = generate_activity_id(
43 &context.settings().get_protocol_and_hostname(),
45 Ok(CreateOrUpdatePost {
46 actor: ObjectId::new(actor.actor_id()),
48 object: post.into_apub(context).await?,
49 cc: vec![community.actor_id()],
55 #[tracing::instrument(skip_all)]
59 kind: CreateOrUpdateType,
60 context: &LemmyContext,
61 ) -> Result<(), LemmyError> {
62 let community_id = post.community_id;
63 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
65 let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?;
66 let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
67 send_activity_in_community(activity, actor, &community, vec![], context).await?;
72 #[async_trait::async_trait(?Send)]
73 impl ActivityHandler for CreateOrUpdatePost {
74 type DataType = LemmyContext;
75 type Error = LemmyError;
77 fn id(&self) -> &Url {
81 fn actor(&self) -> &Url {
85 #[tracing::instrument(skip_all)]
88 context: &Data<LemmyContext>,
89 request_counter: &mut i32,
90 ) -> Result<(), LemmyError> {
91 verify_is_public(&self.to, &self.cc)?;
92 let community = self.get_community(context, request_counter).await?;
93 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
94 check_community_deleted_or_removed(&community)?;
97 CreateOrUpdateType::Create => {
98 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
99 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
100 // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
101 // However, when fetching a remote post we generate a new create activity with the current
102 // locked/stickied value, so this check may fail. So only check if its a local community,
103 // because then we will definitely receive all create and update activities separately.
104 let is_stickied_or_locked =
105 self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
106 if community.local && is_stickied_or_locked {
107 return Err(LemmyError::from_message(
108 "New post cannot be stickied or locked",
112 CreateOrUpdateType::Update => {
113 let is_mod_action = self.object.is_mod_action(context).await?;
117 self.object.id.inner(),
124 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
125 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
129 ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
133 #[tracing::instrument(skip_all)]
136 context: &Data<LemmyContext>,
137 request_counter: &mut i32,
138 ) -> Result<(), LemmyError> {
139 let post = ApubPost::from_apub(self.object, context, request_counter).await?;
141 // author likes their own post by default
142 let like_form = PostLikeForm {
144 person_id: post.creator_id,
147 PostLike::like(context.pool(), &like_form).await?;
149 let notif_type = match self.kind {
150 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
151 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
153 send_post_ws_message(post.id, notif_type, None, None, context).await?;
158 #[async_trait::async_trait(?Send)]
159 impl GetCommunity for CreateOrUpdatePost {
160 #[tracing::instrument(skip_all)]
161 async fn get_community(
163 context: &LemmyContext,
164 request_counter: &mut i32,
165 ) -> Result<ApubCommunity, LemmyError> {
168 .extract_community(context, request_counter)