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, PostResponse},
29 websocket::{send::send_post_ws_message, UserOperationCrud},
31 use lemmy_db_schema::{
36 post::{Post, PostLike, PostLikeForm},
38 traits::{Crud, Likeable},
40 use lemmy_utils::error::LemmyError;
43 #[async_trait::async_trait(?Send)]
44 impl SendActivity for CreatePost {
45 type Response = PostResponse;
47 async fn send_activity(
49 response: &Self::Response,
50 context: &LemmyContext,
51 ) -> Result<(), LemmyError> {
52 CreateOrUpdatePage::send(
53 &response.post_view.post,
54 response.post_view.creator.id,
55 CreateOrUpdateType::Create,
62 #[async_trait::async_trait(?Send)]
63 impl SendActivity for EditPost {
64 type Response = PostResponse;
66 async fn send_activity(
68 response: &Self::Response,
69 context: &LemmyContext,
70 ) -> Result<(), LemmyError> {
71 CreateOrUpdatePage::send(
72 &response.post_view.post,
73 response.post_view.creator.id,
74 CreateOrUpdateType::Update,
81 impl CreateOrUpdatePage {
82 pub(crate) async fn new(
85 community: &ApubCommunity,
86 kind: CreateOrUpdateType,
87 context: &LemmyContext,
88 ) -> Result<CreateOrUpdatePage, LemmyError> {
89 let id = generate_activity_id(
91 &context.settings().get_protocol_and_hostname(),
93 Ok(CreateOrUpdatePage {
94 actor: ObjectId::new(actor.actor_id()),
96 object: post.into_apub(context).await?,
97 cc: vec![community.actor_id()],
100 audience: Some(ObjectId::new(community.actor_id())),
104 #[tracing::instrument(skip_all)]
105 pub(crate) async fn send(
108 kind: CreateOrUpdateType,
109 context: &LemmyContext,
110 ) -> Result<(), LemmyError> {
111 let post = ApubPost(post.clone());
112 let community_id = post.community_id;
113 let person: ApubPerson = Person::read(context.pool(), person_id).await?.into();
114 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
116 let create_or_update =
117 CreateOrUpdatePage::new(post, &person, &community, kind, context).await?;
118 let is_mod_action = create_or_update.object.is_mod_action(context).await?;
119 let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
120 send_activity_in_community(
133 #[async_trait::async_trait(?Send)]
134 impl ActivityHandler for CreateOrUpdatePage {
135 type DataType = LemmyContext;
136 type Error = LemmyError;
138 fn id(&self) -> &Url {
142 fn actor(&self) -> &Url {
146 #[tracing::instrument(skip_all)]
149 context: &Data<LemmyContext>,
150 request_counter: &mut i32,
151 ) -> Result<(), LemmyError> {
152 verify_is_public(&self.to, &self.cc)?;
153 let community = self.community(context, request_counter).await?;
154 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
155 check_community_deleted_or_removed(&community)?;
158 CreateOrUpdateType::Create => {
159 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
160 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
161 // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
162 // However, when fetching a remote post we generate a new create activity with the current
163 // locked/stickied value, so this check may fail. So only check if its a local community,
164 // because then we will definitely receive all create and update activities separately.
165 let is_featured_or_locked =
166 self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
167 if community.local && is_featured_or_locked {
168 return Err(LemmyError::from_message(
169 "New post cannot be stickied or locked",
173 CreateOrUpdateType::Update => {
174 let is_mod_action = self.object.is_mod_action(context).await?;
178 self.object.id.inner(),
185 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
186 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
190 ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
194 #[tracing::instrument(skip_all)]
197 context: &Data<LemmyContext>,
198 request_counter: &mut i32,
199 ) -> Result<(), LemmyError> {
200 let post = ApubPost::from_apub(self.object, context, request_counter).await?;
202 // author likes their own post by default
203 let like_form = PostLikeForm {
205 person_id: post.creator_id,
208 PostLike::like(context.pool(), &like_form).await?;
210 let notif_type = match self.kind {
211 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
212 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
214 send_post_ws_message(post.id, notif_type, None, None, context).await?;