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