]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/post/create_or_update.rs
05a7f38d08ae6612f8a4b97f99405534f2316430
[lemmy.git] / crates / apub / src / activities / post / create_or_update.rs
1 use crate::{
2   activities::{
3     check_community_deleted_or_removed,
4     community::{announce::GetCommunity, send_to_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::public;
16 use anyhow::anyhow;
17 use lemmy_api_common::blocking;
18 use lemmy_apub_lib::{
19   data::Data,
20   object_id::ObjectId,
21   traits::{ActivityHandler, ActorType, ApubObject},
22   verify::{verify_domains_match, verify_urls_match},
23 };
24 use lemmy_db_schema::{source::community::Community, traits::Crud};
25 use lemmy_utils::LemmyError;
26 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
27
28 impl CreateOrUpdatePost {
29   pub(crate) async fn new(
30     post: ApubPost,
31     actor: &ApubPerson,
32     community: &ApubCommunity,
33     kind: CreateOrUpdateType,
34     context: &LemmyContext,
35   ) -> Result<CreateOrUpdatePost, LemmyError> {
36     let id = generate_activity_id(
37       kind.clone(),
38       &context.settings().get_protocol_and_hostname(),
39     )?;
40     Ok(CreateOrUpdatePost {
41       actor: ObjectId::new(actor.actor_id()),
42       to: vec![public()],
43       object: post.into_apub(context).await?,
44       cc: vec![community.actor_id()],
45       kind,
46       id: id.clone(),
47       unparsed: Default::default(),
48     })
49   }
50   pub async fn send(
51     post: ApubPost,
52     actor: &ApubPerson,
53     kind: CreateOrUpdateType,
54     context: &LemmyContext,
55   ) -> Result<(), LemmyError> {
56     let community_id = post.community_id;
57     let community: ApubCommunity = blocking(context.pool(), move |conn| {
58       Community::read(conn, community_id)
59     })
60     .await??
61     .into();
62     let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?;
63     let id = create_or_update.id.clone();
64     let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
65     send_to_community(activity, &id, actor, &community, vec![], context).await
66   }
67 }
68
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for CreateOrUpdatePost {
71   type DataType = LemmyContext;
72   async fn verify(
73     &self,
74     context: &Data<LemmyContext>,
75     request_counter: &mut i32,
76   ) -> Result<(), LemmyError> {
77     verify_is_public(&self.to, &self.cc)?;
78     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
79     let community = self.get_community(context, request_counter).await?;
80     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
81     check_community_deleted_or_removed(&community)?;
82
83     match self.kind {
84       CreateOrUpdateType::Create => {
85         verify_domains_match(self.actor.inner(), self.object.id.inner())?;
86         verify_urls_match(self.actor.inner(), self.object.attributed_to.inner())?;
87         // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
88         // However, when fetching a remote post we generate a new create activity with the current
89         // locked/stickied value, so this check may fail. So only check if its a local community,
90         // because then we will definitely receive all create and update activities separately.
91         let is_stickied_or_locked =
92           self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
93         if community.local && is_stickied_or_locked {
94           return Err(anyhow!("New post cannot be stickied or locked").into());
95         }
96       }
97       CreateOrUpdateType::Update => {
98         let is_mod_action = self.object.is_mod_action(context).await?;
99         if is_mod_action {
100           verify_mod_action(&self.actor, &community, context, request_counter).await?;
101         } else {
102           verify_domains_match(self.actor.inner(), self.object.id.inner())?;
103           verify_urls_match(self.actor.inner(), self.object.attributed_to.inner())?;
104         }
105       }
106     }
107     ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
108     Ok(())
109   }
110
111   async fn receive(
112     self,
113     context: &Data<LemmyContext>,
114     request_counter: &mut i32,
115   ) -> Result<(), LemmyError> {
116     let post = ApubPost::from_apub(self.object, context, request_counter).await?;
117
118     let notif_type = match self.kind {
119       CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
120       CreateOrUpdateType::Update => UserOperationCrud::EditPost,
121     };
122     send_post_ws_message(post.id, notif_type, None, None, context).await?;
123     Ok(())
124   }
125 }
126
127 #[async_trait::async_trait(?Send)]
128 impl GetCommunity for CreateOrUpdatePost {
129   async fn get_community(
130     &self,
131     context: &LemmyContext,
132     request_counter: &mut i32,
133   ) -> Result<ApubCommunity, LemmyError> {
134     self
135       .object
136       .extract_community(context, request_counter)
137       .await
138   }
139 }