3 check_community_deleted_or_removed,
4 community::{announce::GetCommunity, send_activity_in_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_kinds::public;
16 use lemmy_api_common::blocking;
20 traits::{ActivityHandler, ActorType, ApubObject},
21 verify::{verify_domains_match, verify_urls_match},
23 use lemmy_db_schema::{source::community::Community, traits::Crud};
24 use lemmy_utils::LemmyError;
25 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
27 impl CreateOrUpdatePost {
28 pub(crate) async fn new(
31 community: &ApubCommunity,
32 kind: CreateOrUpdateType,
33 context: &LemmyContext,
34 ) -> Result<CreateOrUpdatePost, LemmyError> {
35 let id = generate_activity_id(
37 &context.settings().get_protocol_and_hostname(),
39 Ok(CreateOrUpdatePost {
40 actor: ObjectId::new(actor.actor_id()),
42 object: post.into_apub(context).await?,
43 cc: vec![community.actor_id()],
46 unparsed: Default::default(),
50 #[tracing::instrument(skip_all)]
54 kind: CreateOrUpdateType,
55 context: &LemmyContext,
56 ) -> Result<(), LemmyError> {
57 let community_id = post.community_id;
58 let community: ApubCommunity = blocking(context.pool(), move |conn| {
59 Community::read(conn, community_id)
64 let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?;
65 let id = create_or_update.id.clone();
66 let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
67 send_activity_in_community(activity, &id, actor, &community, vec![], context).await
71 #[async_trait::async_trait(?Send)]
72 impl ActivityHandler for CreateOrUpdatePost {
73 type DataType = LemmyContext;
75 #[tracing::instrument(skip_all)]
78 context: &Data<LemmyContext>,
79 request_counter: &mut i32,
80 ) -> Result<(), LemmyError> {
81 verify_is_public(&self.to, &self.cc)?;
82 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
83 let community = self.get_community(context, request_counter).await?;
84 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
85 check_community_deleted_or_removed(&community)?;
88 CreateOrUpdateType::Create => {
89 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
90 verify_urls_match(self.actor.inner(), self.object.attributed_to.inner())?;
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(LemmyError::from_message(
99 "New post cannot be stickied or locked",
103 CreateOrUpdateType::Update => {
104 let is_mod_action = self.object.is_mod_action(context).await?;
106 verify_mod_action(&self.actor, &community, context, request_counter).await?;
108 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
109 verify_urls_match(self.actor.inner(), self.object.attributed_to.inner())?;
113 ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
117 #[tracing::instrument(skip_all)]
120 context: &Data<LemmyContext>,
121 request_counter: &mut i32,
122 ) -> Result<(), LemmyError> {
123 let post = ApubPost::from_apub(self.object, context, request_counter).await?;
125 let notif_type = match self.kind {
126 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
127 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
129 send_post_ws_message(post.id, notif_type, None, None, context).await?;
134 #[async_trait::async_trait(?Send)]
135 impl GetCommunity for CreateOrUpdatePost {
136 #[tracing::instrument(skip_all)]
137 async fn get_community(
139 context: &LemmyContext,
140 request_counter: &mut i32,
141 ) -> Result<ApubCommunity, LemmyError> {
144 .extract_community(context, request_counter)