3 check_community_deleted_or_removed,
4 community::{announce::GetCommunity, send_to_community},
9 verify_person_in_community,
11 activity_lists::AnnouncableActivities,
12 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
13 protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
15 use activitystreams::public;
17 use lemmy_api_common::blocking;
21 traits::{ActivityHandler, ActorType, ApubObject},
22 verify::{verify_domains_match, verify_urls_match},
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};
28 impl CreateOrUpdatePost {
29 pub(crate) async fn new(
32 community: &ApubCommunity,
33 kind: CreateOrUpdateType,
34 context: &LemmyContext,
35 ) -> Result<CreateOrUpdatePost, LemmyError> {
36 let id = generate_activity_id(
38 &context.settings().get_protocol_and_hostname(),
40 Ok(CreateOrUpdatePost {
41 actor: ObjectId::new(actor.actor_id()),
43 object: post.into_apub(context).await?,
44 cc: vec![community.actor_id()],
47 unparsed: Default::default(),
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)
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
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for CreateOrUpdatePost {
71 type DataType = LemmyContext;
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)?;
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());
97 CreateOrUpdateType::Update => {
98 let is_mod_action = self.object.is_mod_action(context).await?;
100 verify_mod_action(&self.actor, &community, context, request_counter).await?;
102 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
103 verify_urls_match(self.actor.inner(), self.object.attributed_to.inner())?;
107 ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
113 context: &Data<LemmyContext>,
114 request_counter: &mut i32,
115 ) -> Result<(), LemmyError> {
116 let post = ApubPost::from_apub(self.object, context, request_counter).await?;
118 let notif_type = match self.kind {
119 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
120 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
122 send_post_ws_message(post.id, notif_type, None, None, context).await?;
127 #[async_trait::async_trait(?Send)]
128 impl GetCommunity for CreateOrUpdatePost {
129 async fn get_community(
131 context: &LemmyContext,
132 request_counter: &mut i32,
133 ) -> Result<ApubCommunity, LemmyError> {
136 .extract_community(context, request_counter)