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)
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_to_community(activity, &id, actor, &community, vec![], context).await
70 #[async_trait::async_trait(?Send)]
71 impl ActivityHandler for CreateOrUpdatePost {
72 type DataType = LemmyContext;
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)?;
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());
98 CreateOrUpdateType::Update => {
99 let is_mod_action = self.object.is_mod_action(context).await?;
101 verify_mod_action(&self.actor, &community, context, request_counter).await?;
103 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
104 verify_urls_match(self.actor.inner(), self.object.attributed_to.inner())?;
108 ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
114 context: &Data<LemmyContext>,
115 request_counter: &mut i32,
116 ) -> Result<(), LemmyError> {
117 let post = ApubPost::from_apub(self.object, context, request_counter).await?;
119 let notif_type = match self.kind {
120 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
121 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
123 send_post_ws_message(post.id, notif_type, None, None, context).await?;
128 #[async_trait::async_trait(?Send)]
129 impl GetCommunity for CreateOrUpdatePost {
130 async fn get_community(
132 context: &LemmyContext,
133 request_counter: &mut i32,
134 ) -> Result<ApubCommunity, LemmyError> {
137 .extract_community(context, request_counter)