3 community::announce::AnnouncableActivities,
8 verify_person_in_community,
11 activity_queue::send_to_community_new,
12 extensions::context::lemmy_context,
13 fetcher::person::get_or_fetch_and_upsert_person,
14 objects::{post::Page, FromApub, ToApub},
18 use lemmy_api_common::blocking;
26 use lemmy_db_queries::Crud;
27 use lemmy_db_schema::source::{community::Community, person::Person, post::Post};
28 use lemmy_utils::LemmyError;
29 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
32 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
33 #[serde(rename_all = "camelCase")]
34 pub struct CreateOrUpdatePost {
38 #[serde(rename = "type")]
39 kind: CreateOrUpdateType,
41 common: ActivityCommonFields,
44 impl CreateOrUpdatePost {
48 kind: CreateOrUpdateType,
49 context: &LemmyContext,
50 ) -> Result<(), LemmyError> {
51 let community_id = post.community_id;
52 let community = blocking(context.pool(), move |conn| {
53 Community::read(conn, community_id)
57 let id = generate_activity_id(kind.clone())?;
58 let create_or_update = CreateOrUpdatePost {
59 to: PublicUrl::Public,
60 object: post.to_apub(context.pool()).await?,
61 cc: [community.actor_id()],
63 common: ActivityCommonFields {
64 context: lemmy_context(),
66 actor: actor.actor_id(),
67 unparsed: Default::default(),
71 let activity = AnnouncableActivities::CreateOrUpdatePost(Box::new(create_or_update));
72 send_to_community_new(activity, &id, actor, &community, vec![], context).await
76 #[async_trait::async_trait(?Send)]
77 impl ActivityHandler for CreateOrUpdatePost {
80 context: &LemmyContext,
81 request_counter: &mut i32,
82 ) -> Result<(), LemmyError> {
83 verify_activity(self.common())?;
84 let community = extract_community(&self.cc, context, request_counter).await?;
85 let community_id = community.actor_id();
86 verify_person_in_community(&self.common.actor, &community_id, context, request_counter).await?;
88 CreateOrUpdateType::Create => {
89 verify_domains_match(&self.common.actor, self.object.id_unchecked())?;
90 verify_urls_match(&self.common.actor, &self.object.attributed_to)?;
91 // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
92 // However, when fetching a remote post we generate a new create activity with the current
93 // locked/stickied value, so this check may fail. So only check if its a local community,
94 // because then we will definitely receive all create and update activities separately.
95 let is_stickied_or_locked =
96 self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
97 if community.local && is_stickied_or_locked {
98 return Err(anyhow!("New post cannot be stickied or locked").into());
101 CreateOrUpdateType::Update => {
102 let is_mod_action = self.object.is_mod_action(context.pool()).await?;
104 verify_mod_action(&self.common.actor, community_id, context).await?;
106 verify_domains_match(&self.common.actor, self.object.id_unchecked())?;
107 verify_urls_match(&self.common.actor, &self.object.attributed_to)?;
111 self.object.verify(context, request_counter).await?;
117 context: &LemmyContext,
118 request_counter: &mut i32,
119 ) -> Result<(), LemmyError> {
121 get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
122 let post = Post::from_apub(&self.object, context, &actor.actor_id(), request_counter).await?;
124 let notif_type = match self.kind {
125 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
126 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
128 send_post_ws_message(post.id, notif_type, None, None, context).await?;
132 fn common(&self) -> &ActivityCommonFields {