]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/post.rs
Remove SendActivity and Perform traits, rely on channel (#3596)
[lemmy.git] / crates / apub / src / activities / create_or_update / post.rs
1 use crate::{
2   activities::{
3     check_community_deleted_or_removed,
4     community::send_activity_in_community,
5     generate_activity_id,
6     verify_is_public,
7     verify_mod_action,
8     verify_person_in_community,
9   },
10   activity_lists::AnnouncableActivities,
11   insert_received_activity,
12   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
13   protocol::{
14     activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
15     InCommunity,
16   },
17   SendActivity,
18 };
19 use activitypub_federation::{
20   config::Data,
21   kinds::public,
22   protocol::verification::{verify_domains_match, verify_urls_match},
23   traits::{ActivityHandler, Actor, Object},
24 };
25 use lemmy_api_common::{
26   context::LemmyContext,
27   post::{EditPost, PostResponse},
28 };
29 use lemmy_db_schema::{
30   aggregates::structs::PostAggregates,
31   newtypes::PersonId,
32   source::{
33     community::Community,
34     person::Person,
35     post::{Post, PostLike, PostLikeForm},
36   },
37   traits::{Crud, Likeable},
38 };
39 use lemmy_utils::error::{LemmyError, LemmyErrorType};
40 use url::Url;
41
42 #[async_trait::async_trait]
43 impl SendActivity for EditPost {
44   type Response = PostResponse;
45
46   async fn send_activity(
47     _request: &Self,
48     response: &Self::Response,
49     context: &Data<LemmyContext>,
50   ) -> Result<(), LemmyError> {
51     CreateOrUpdatePage::send(
52       response.post_view.post.clone(),
53       response.post_view.creator.id,
54       CreateOrUpdateType::Update,
55       context.reset_request_count(),
56     )
57     .await
58   }
59 }
60
61 impl CreateOrUpdatePage {
62   pub(crate) async fn new(
63     post: ApubPost,
64     actor: &ApubPerson,
65     community: &ApubCommunity,
66     kind: CreateOrUpdateType,
67     context: &Data<LemmyContext>,
68   ) -> Result<CreateOrUpdatePage, LemmyError> {
69     let id = generate_activity_id(
70       kind.clone(),
71       &context.settings().get_protocol_and_hostname(),
72     )?;
73     Ok(CreateOrUpdatePage {
74       actor: actor.id().into(),
75       to: vec![public()],
76       object: post.into_json(context).await?,
77       cc: vec![community.id()],
78       kind,
79       id: id.clone(),
80       audience: Some(community.id().into()),
81     })
82   }
83
84   #[tracing::instrument(skip_all)]
85   pub(crate) async fn send(
86     post: Post,
87     person_id: PersonId,
88     kind: CreateOrUpdateType,
89     context: Data<LemmyContext>,
90   ) -> Result<(), LemmyError> {
91     let post = ApubPost(post);
92     let community_id = post.community_id;
93     let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into();
94     let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
95       .await?
96       .into();
97
98     let create_or_update =
99       CreateOrUpdatePage::new(post, &person, &community, kind, &context).await?;
100     let is_mod_action = create_or_update.object.is_mod_action(&context).await?;
101     let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
102     send_activity_in_community(
103       activity,
104       &person,
105       &community,
106       vec![],
107       is_mod_action,
108       &context,
109     )
110     .await?;
111     Ok(())
112   }
113 }
114
115 #[async_trait::async_trait]
116 impl ActivityHandler for CreateOrUpdatePage {
117   type DataType = LemmyContext;
118   type Error = LemmyError;
119
120   fn id(&self) -> &Url {
121     &self.id
122   }
123
124   fn actor(&self) -> &Url {
125     self.actor.inner()
126   }
127
128   #[tracing::instrument(skip_all)]
129   async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
130     insert_received_activity(&self.id, context).await?;
131     verify_is_public(&self.to, &self.cc)?;
132     let community = self.community(context).await?;
133     verify_person_in_community(&self.actor, &community, context).await?;
134     check_community_deleted_or_removed(&community)?;
135
136     match self.kind {
137       CreateOrUpdateType::Create => {
138         verify_domains_match(self.actor.inner(), self.object.id.inner())?;
139         verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
140         // Check that the post isnt locked, as that isnt possible for newly created posts.
141         // However, when fetching a remote post we generate a new create activity with the current
142         // locked value, so this check may fail. So only check if its a local community,
143         // because then we will definitely receive all create and update activities separately.
144         let is_locked = self.object.comments_enabled == Some(false);
145         if community.local && is_locked {
146           return Err(LemmyErrorType::NewPostCannotBeLocked)?;
147         }
148       }
149       CreateOrUpdateType::Update => {
150         let is_mod_action = self.object.is_mod_action(context).await?;
151         if is_mod_action {
152           verify_mod_action(&self.actor, self.object.id.inner(), community.id, context).await?;
153         } else {
154           verify_domains_match(self.actor.inner(), self.object.id.inner())?;
155           verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
156         }
157       }
158     }
159     ApubPost::verify(&self.object, self.actor.inner(), context).await?;
160     Ok(())
161   }
162
163   #[tracing::instrument(skip_all)]
164   async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
165     let post = ApubPost::from_json(self.object, context).await?;
166
167     // author likes their own post by default
168     let like_form = PostLikeForm {
169       post_id: post.id,
170       person_id: post.creator_id,
171       score: 1,
172     };
173     PostLike::like(&mut context.pool(), &like_form).await?;
174
175     // Calculate initial hot_rank for post
176     PostAggregates::update_hot_rank(&mut context.pool(), post.id).await?;
177
178     Ok(())
179   }
180 }