3 community::announce::AnnouncableActivities,
6 post::send_websocket_message,
8 verify_person_in_community,
10 activity_queue::send_to_community_new,
11 extensions::context::lemmy_context,
12 fetcher::person::get_or_fetch_and_upsert_person,
13 objects::{post::Page, FromApub, ToApub},
16 use activitystreams::activity::kind::CreateType;
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::{LemmyContext, UserOperationCrud};
32 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
33 #[serde(rename_all = "camelCase")]
34 pub struct CreatePost {
40 common: ActivityCommonFields,
44 pub async fn send(post: &Post, actor: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
45 let community_id = post.community_id;
46 let community = blocking(context.pool(), move |conn| {
47 Community::read(conn, community_id)
51 let id = generate_activity_id(CreateType::Create)?;
52 let create = CreatePost {
53 to: PublicUrl::Public,
54 object: post.to_apub(context.pool()).await?,
55 cc: [community.actor_id()],
56 r#type: Default::default(),
57 common: ActivityCommonFields {
58 context: lemmy_context(),
60 actor: actor.actor_id(),
61 unparsed: Default::default(),
65 let activity = AnnouncableActivities::CreatePost(create);
66 send_to_community_new(activity, &id, actor, &community, vec![], context).await
70 #[async_trait::async_trait(?Send)]
71 impl ActivityHandler for CreatePost {
74 context: &LemmyContext,
75 request_counter: &mut i32,
76 ) -> Result<(), LemmyError> {
77 let community = extract_community(&self.cc, context, request_counter).await?;
78 let community_id = &community.actor_id();
80 verify_activity(self.common())?;
81 verify_person_in_community(&self.common.actor, community_id, context, request_counter).await?;
82 verify_domains_match(&self.common.actor, &self.object.id)?;
83 verify_urls_match(&self.common.actor, &self.object.attributed_to)?;
84 // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
85 // However, when fetching a remote post we generate a new create activity with the current
86 // locked/stickied value, so this check may fail. So only check if its a local community,
87 // because then we will definitely receive all create and update activities separately.
88 let is_stickied_or_locked =
89 self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
90 if community.local && is_stickied_or_locked {
91 return Err(anyhow!("New post cannot be stickied or locked").into());
93 self.object.verify(context, request_counter).await?;
99 context: &LemmyContext,
100 request_counter: &mut i32,
101 ) -> Result<(), LemmyError> {
103 get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
104 let post = Post::from_apub(
113 send_websocket_message(post.id, UserOperationCrud::CreatePost, context).await
116 fn common(&self) -> &ActivityCommonFields {