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},
19 use activitypub_federation::{
20 core::object_id::ObjectId,
22 traits::{ActivityHandler, ApubObject},
23 utils::{verify_domains_match, verify_urls_match},
25 use activitystreams_kinds::public;
26 use lemmy_api_common::{
27 context::LemmyContext,
28 post::{CreatePost, EditPost, LockPost, PostResponse, StickyPost},
29 utils::get_local_user_view_from_jwt,
30 websocket::{send::send_post_ws_message, UserOperationCrud},
32 use lemmy_db_schema::{
37 post::{Post, PostLike, PostLikeForm},
39 traits::{Crud, Likeable},
41 use lemmy_utils::error::LemmyError;
44 #[async_trait::async_trait(?Send)]
45 impl SendActivity for CreatePost {
46 type Response = PostResponse;
48 async fn send_activity(
50 response: &Self::Response,
51 context: &LemmyContext,
52 ) -> Result<(), LemmyError> {
53 CreateOrUpdatePage::send(
54 &response.post_view.post,
55 response.post_view.creator.id,
56 CreateOrUpdateType::Create,
63 #[async_trait::async_trait(?Send)]
64 impl SendActivity for EditPost {
65 type Response = PostResponse;
67 async fn send_activity(
69 response: &Self::Response,
70 context: &LemmyContext,
71 ) -> Result<(), LemmyError> {
72 CreateOrUpdatePage::send(
73 &response.post_view.post,
74 response.post_view.creator.id,
75 CreateOrUpdateType::Update,
82 #[async_trait::async_trait(?Send)]
83 impl SendActivity for LockPost {
84 type Response = PostResponse;
86 async fn send_activity(
88 response: &Self::Response,
89 context: &LemmyContext,
90 ) -> Result<(), LemmyError> {
92 get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
93 CreateOrUpdatePage::send(
94 &response.post_view.post,
95 local_user_view.person.id,
96 CreateOrUpdateType::Update,
103 #[async_trait::async_trait(?Send)]
104 impl SendActivity for StickyPost {
105 type Response = PostResponse;
107 async fn send_activity(
109 response: &Self::Response,
110 context: &LemmyContext,
111 ) -> Result<(), LemmyError> {
112 let local_user_view =
113 get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
114 CreateOrUpdatePage::send(
115 &response.post_view.post,
116 local_user_view.person.id,
117 CreateOrUpdateType::Update,
124 impl CreateOrUpdatePage {
125 pub(crate) async fn new(
128 community: &ApubCommunity,
129 kind: CreateOrUpdateType,
130 context: &LemmyContext,
131 ) -> Result<CreateOrUpdatePage, LemmyError> {
132 let id = generate_activity_id(
134 &context.settings().get_protocol_and_hostname(),
136 Ok(CreateOrUpdatePage {
137 actor: ObjectId::new(actor.actor_id()),
139 object: post.into_apub(context).await?,
140 cc: vec![community.actor_id()],
143 audience: Some(ObjectId::new(community.actor_id())),
147 #[tracing::instrument(skip_all)]
151 kind: CreateOrUpdateType,
152 context: &LemmyContext,
153 ) -> Result<(), LemmyError> {
154 let post = ApubPost(post.clone());
155 let community_id = post.community_id;
156 let person: ApubPerson = Person::read(context.pool(), person_id).await?.into();
157 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
159 let create_or_update =
160 CreateOrUpdatePage::new(post, &person, &community, kind, context).await?;
161 let is_mod_action = create_or_update.object.is_mod_action(context).await?;
162 let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
163 send_activity_in_community(
176 #[async_trait::async_trait(?Send)]
177 impl ActivityHandler for CreateOrUpdatePage {
178 type DataType = LemmyContext;
179 type Error = LemmyError;
181 fn id(&self) -> &Url {
185 fn actor(&self) -> &Url {
189 #[tracing::instrument(skip_all)]
192 context: &Data<LemmyContext>,
193 request_counter: &mut i32,
194 ) -> Result<(), LemmyError> {
195 verify_is_public(&self.to, &self.cc)?;
196 let community = self.community(context, request_counter).await?;
197 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
198 check_community_deleted_or_removed(&community)?;
201 CreateOrUpdateType::Create => {
202 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
203 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
204 // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
205 // However, when fetching a remote post we generate a new create activity with the current
206 // locked/stickied value, so this check may fail. So only check if its a local community,
207 // because then we will definitely receive all create and update activities separately.
208 let is_stickied_or_locked =
209 self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
210 if community.local && is_stickied_or_locked {
211 return Err(LemmyError::from_message(
212 "New post cannot be stickied or locked",
216 CreateOrUpdateType::Update => {
217 let is_mod_action = self.object.is_mod_action(context).await?;
221 self.object.id.inner(),
228 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
229 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
233 ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
237 #[tracing::instrument(skip_all)]
240 context: &Data<LemmyContext>,
241 request_counter: &mut i32,
242 ) -> Result<(), LemmyError> {
243 let post = ApubPost::from_apub(self.object, context, request_counter).await?;
245 // author likes their own post by default
246 let like_form = PostLikeForm {
248 person_id: post.creator_id,
251 PostLike::like(context.pool(), &like_form).await?;
253 let notif_type = match self.kind {
254 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
255 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
257 send_post_ws_message(post.id, notif_type, None, None, context).await?;