]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/post/create_or_update.rs
2e9c8071d5e38e8058131976f45f1ae0e9ff2581
[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_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 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
63     let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?;
64     let id = create_or_update.id.clone();
65     let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
66     send_activity_in_community(activity, &id, actor, &community, vec![], context).await
67   }
68 }
69
70 #[async_trait::async_trait(?Send)]
71 impl ActivityHandler for CreateOrUpdatePost {
72   type DataType = LemmyContext;
73   async fn verify(
74     &self,
75     context: &Data<LemmyContext>,
76     request_counter: &mut i32,
77   ) -> Result<(), LemmyError> {
78     verify_is_public(&self.to, &self.cc)?;
79     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
80     let community = self.get_community(context, request_counter).await?;
81     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
82     check_community_deleted_or_removed(&community)?;
83
84     match self.kind {
85       CreateOrUpdateType::Create => {
86         verify_domains_match(self.actor.inner(), self.object.id.inner())?;
87         verify_urls_match(self.actor.inner(), self.object.attributed_to.inner())?;
88         // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
89         // However, when fetching a remote post we generate a new create activity with the current
90         // locked/stickied value, so this check may fail. So only check if its a local community,
91         // because then we will definitely receive all create and update activities separately.
92         let is_stickied_or_locked =
93           self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
94         if community.local && is_stickied_or_locked {
95           return Err(anyhow!("New post cannot be stickied or locked").into());
96         }
97       }
98       CreateOrUpdateType::Update => {
99         let is_mod_action = self.object.is_mod_action(context).await?;
100         if is_mod_action {
101           verify_mod_action(&self.actor, &community, context, request_counter).await?;
102         } else {
103           verify_domains_match(self.actor.inner(), self.object.id.inner())?;
104           verify_urls_match(self.actor.inner(), self.object.attributed_to.inner())?;
105         }
106       }
107     }
108     ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
109     Ok(())
110   }
111
112   async fn receive(
113     self,
114     context: &Data<LemmyContext>,
115     request_counter: &mut i32,
116   ) -> Result<(), LemmyError> {
117     let post = ApubPost::from_apub(self.object, context, request_counter).await?;
118
119     let notif_type = match self.kind {
120       CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
121       CreateOrUpdateType::Update => UserOperationCrud::EditPost,
122     };
123     send_post_ws_message(post.id, notif_type, None, None, context).await?;
124     Ok(())
125   }
126 }
127
128 #[async_trait::async_trait(?Send)]
129 impl GetCommunity for CreateOrUpdatePost {
130   async fn get_community(
131     &self,
132     context: &LemmyContext,
133     request_counter: &mut i32,
134   ) -> Result<ApubCommunity, LemmyError> {
135     self
136       .object
137       .extract_community(context, request_counter)
138       .await
139   }
140 }