3 check_community_deleted_or_removed,
4 community::send_activity_in_community,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
12 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
14 activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
19 use activitypub_federation::{
22 protocol::verification::{verify_domains_match, verify_urls_match},
23 traits::{ActivityHandler, Actor, Object},
25 use lemmy_api_common::{
26 context::LemmyContext,
27 post::{CreatePost, EditPost, PostResponse},
29 use lemmy_db_schema::{
34 post::{Post, PostLike, PostLikeForm},
36 traits::{Crud, Likeable},
38 use lemmy_utils::error::LemmyError;
41 #[async_trait::async_trait]
42 impl SendActivity for CreatePost {
43 type Response = PostResponse;
45 async fn send_activity(
47 response: &Self::Response,
48 context: &Data<LemmyContext>,
49 ) -> Result<(), LemmyError> {
50 CreateOrUpdatePage::send(
51 &response.post_view.post,
52 response.post_view.creator.id,
53 CreateOrUpdateType::Create,
60 #[async_trait::async_trait]
61 impl SendActivity for EditPost {
62 type Response = PostResponse;
64 async fn send_activity(
66 response: &Self::Response,
67 context: &Data<LemmyContext>,
68 ) -> Result<(), LemmyError> {
69 CreateOrUpdatePage::send(
70 &response.post_view.post,
71 response.post_view.creator.id,
72 CreateOrUpdateType::Update,
79 impl CreateOrUpdatePage {
80 pub(crate) async fn new(
83 community: &ApubCommunity,
84 kind: CreateOrUpdateType,
85 context: &Data<LemmyContext>,
86 ) -> Result<CreateOrUpdatePage, LemmyError> {
87 let id = generate_activity_id(
89 &context.settings().get_protocol_and_hostname(),
91 Ok(CreateOrUpdatePage {
92 actor: actor.id().into(),
94 object: post.into_json(context).await?,
95 cc: vec![community.id()],
98 audience: Some(community.id().into()),
102 #[tracing::instrument(skip_all)]
103 pub(crate) async fn send(
106 kind: CreateOrUpdateType,
107 context: &Data<LemmyContext>,
108 ) -> Result<(), LemmyError> {
109 let post = ApubPost(post.clone());
110 let community_id = post.community_id;
111 let person: ApubPerson = Person::read(context.pool(), person_id).await?.into();
112 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
114 let create_or_update =
115 CreateOrUpdatePage::new(post, &person, &community, kind, context).await?;
116 let is_mod_action = create_or_update.object.is_mod_action(context).await?;
117 let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
118 send_activity_in_community(
131 #[async_trait::async_trait]
132 impl ActivityHandler for CreateOrUpdatePage {
133 type DataType = LemmyContext;
134 type Error = LemmyError;
136 fn id(&self) -> &Url {
140 fn actor(&self) -> &Url {
144 #[tracing::instrument(skip_all)]
145 async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
146 verify_is_public(&self.to, &self.cc)?;
147 let community = self.community(context).await?;
148 verify_person_in_community(&self.actor, &community, context).await?;
149 check_community_deleted_or_removed(&community)?;
152 CreateOrUpdateType::Create => {
153 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
154 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
155 // Check that the post isnt locked, as that isnt possible for newly created posts.
156 // However, when fetching a remote post we generate a new create activity with the current
157 // locked value, so this check may fail. So only check if its a local community,
158 // because then we will definitely receive all create and update activities separately.
159 let is_locked = self.object.comments_enabled == Some(false);
160 if community.local && is_locked {
161 return Err(LemmyError::from_message("New post cannot be locked"));
164 CreateOrUpdateType::Update => {
165 let is_mod_action = self.object.is_mod_action(context).await?;
167 verify_mod_action(&self.actor, self.object.id.inner(), community.id, context).await?;
169 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
170 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
174 ApubPost::verify(&self.object, self.actor.inner(), context).await?;
178 #[tracing::instrument(skip_all)]
179 async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
180 insert_activity(&self.id, &self, false, false, context).await?;
181 let post = ApubPost::from_json(self.object, context).await?;
183 // author likes their own post by default
184 let like_form = PostLikeForm {
186 person_id: post.creator_id,
189 PostLike::like(context.pool(), &like_form).await?;