]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/post.rs
Implement federated user following (fixes #752) (#2577)
[lemmy.git] / crates / apub / src / activities / create_or_update / post.rs
1 use crate::{
2   activities::{
3     check_community_deleted_or_removed,
4     community::{announce::GetCommunity, 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   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
12   protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
13   ActorType,
14 };
15 use activitypub_federation::{
16   core::object_id::ObjectId,
17   data::Data,
18   traits::{ActivityHandler, ApubObject},
19   utils::{verify_domains_match, verify_urls_match},
20 };
21 use activitystreams_kinds::public;
22 use lemmy_db_schema::{
23   source::{
24     community::Community,
25     post::{PostLike, PostLikeForm},
26   },
27   traits::{Crud, Likeable},
28 };
29 use lemmy_utils::error::LemmyError;
30 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
31 use url::Url;
32
33 impl CreateOrUpdatePost {
34   pub(crate) async fn new(
35     post: ApubPost,
36     actor: &ApubPerson,
37     community: &ApubCommunity,
38     kind: CreateOrUpdateType,
39     context: &LemmyContext,
40   ) -> Result<CreateOrUpdatePost, LemmyError> {
41     let id = generate_activity_id(
42       kind.clone(),
43       &context.settings().get_protocol_and_hostname(),
44     )?;
45     Ok(CreateOrUpdatePost {
46       actor: ObjectId::new(actor.actor_id()),
47       to: vec![public()],
48       object: post.into_apub(context).await?,
49       cc: vec![community.actor_id()],
50       kind,
51       id: id.clone(),
52     })
53   }
54
55   #[tracing::instrument(skip_all)]
56   pub async fn send(
57     post: ApubPost,
58     actor: &ApubPerson,
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();
64
65     let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?;
66     let is_mod_action = create_or_update.object.is_mod_action(context).await?;
67     let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
68     send_activity_in_community(activity, actor, &community, vec![], is_mod_action, context).await?;
69     Ok(())
70   }
71 }
72
73 #[async_trait::async_trait(?Send)]
74 impl ActivityHandler for CreateOrUpdatePost {
75   type DataType = LemmyContext;
76   type Error = LemmyError;
77
78   fn id(&self) -> &Url {
79     &self.id
80   }
81
82   fn actor(&self) -> &Url {
83     self.actor.inner()
84   }
85
86   #[tracing::instrument(skip_all)]
87   async fn verify(
88     &self,
89     context: &Data<LemmyContext>,
90     request_counter: &mut i32,
91   ) -> Result<(), LemmyError> {
92     verify_is_public(&self.to, &self.cc)?;
93     let community = self.get_community(context, request_counter).await?;
94     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
95     check_community_deleted_or_removed(&community)?;
96
97     match self.kind {
98       CreateOrUpdateType::Create => {
99         verify_domains_match(self.actor.inner(), self.object.id.inner())?;
100         verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
101         // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
102         // However, when fetching a remote post we generate a new create activity with the current
103         // locked/stickied value, so this check may fail. So only check if its a local community,
104         // because then we will definitely receive all create and update activities separately.
105         let is_stickied_or_locked =
106           self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
107         if community.local && is_stickied_or_locked {
108           return Err(LemmyError::from_message(
109             "New post cannot be stickied or locked",
110           ));
111         }
112       }
113       CreateOrUpdateType::Update => {
114         let is_mod_action = self.object.is_mod_action(context).await?;
115         if is_mod_action {
116           verify_mod_action(
117             &self.actor,
118             self.object.id.inner(),
119             community.id,
120             context,
121             request_counter,
122           )
123           .await?;
124         } else {
125           verify_domains_match(self.actor.inner(), self.object.id.inner())?;
126           verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
127         }
128       }
129     }
130     ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
131     Ok(())
132   }
133
134   #[tracing::instrument(skip_all)]
135   async fn receive(
136     self,
137     context: &Data<LemmyContext>,
138     request_counter: &mut i32,
139   ) -> Result<(), LemmyError> {
140     let post = ApubPost::from_apub(self.object, context, request_counter).await?;
141
142     // author likes their own post by default
143     let like_form = PostLikeForm {
144       post_id: post.id,
145       person_id: post.creator_id,
146       score: 1,
147     };
148     PostLike::like(context.pool(), &like_form).await?;
149
150     let notif_type = match self.kind {
151       CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
152       CreateOrUpdateType::Update => UserOperationCrud::EditPost,
153     };
154     send_post_ws_message(post.id, notif_type, None, None, context).await?;
155     Ok(())
156   }
157 }
158
159 #[async_trait::async_trait(?Send)]
160 impl GetCommunity for CreateOrUpdatePost {
161   #[tracing::instrument(skip_all)]
162   async fn get_community(
163     &self,
164     context: &LemmyContext,
165     request_counter: &mut i32,
166   ) -> Result<ApubCommunity, LemmyError> {
167     self
168       .object
169       .extract_community(context, request_counter)
170       .await
171   }
172 }