3 check_community_deleted_or_removed,
4 community::{announce::GetCommunity, send_activity_in_community},
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
11 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
12 protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
15 use activitypub_federation::{
16 core::object_id::ObjectId,
18 traits::{ActivityHandler, ApubObject},
19 utils::{verify_domains_match, verify_urls_match},
21 use activitystreams_kinds::public;
22 use lemmy_api_common::utils::blocking;
23 use lemmy_db_schema::{
26 post::{PostLike, PostLikeForm},
28 traits::{Crud, Likeable},
30 use lemmy_utils::error::LemmyError;
31 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
34 impl CreateOrUpdatePost {
35 pub(crate) async fn new(
38 community: &ApubCommunity,
39 kind: CreateOrUpdateType,
40 context: &LemmyContext,
41 ) -> Result<CreateOrUpdatePost, LemmyError> {
42 let id = generate_activity_id(
44 &context.settings().get_protocol_and_hostname(),
46 Ok(CreateOrUpdatePost {
47 actor: ObjectId::new(actor.actor_id()),
49 object: post.into_apub(context).await?,
50 cc: vec![community.actor_id()],
53 unparsed: Default::default(),
57 #[tracing::instrument(skip_all)]
61 kind: CreateOrUpdateType,
62 context: &LemmyContext,
63 ) -> Result<(), LemmyError> {
64 let community_id = post.community_id;
65 let community: ApubCommunity = blocking(context.pool(), move |conn| {
66 Community::read(conn, community_id)
71 let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?;
72 let activity = AnnouncableActivities::CreateOrUpdatePost(Box::new(create_or_update));
73 send_activity_in_community(activity, actor, &community, vec![], context).await
77 #[async_trait::async_trait(?Send)]
78 impl ActivityHandler for CreateOrUpdatePost {
79 type DataType = LemmyContext;
80 type Error = LemmyError;
82 fn id(&self) -> &Url {
86 fn actor(&self) -> &Url {
90 #[tracing::instrument(skip_all)]
93 context: &Data<LemmyContext>,
94 request_counter: &mut i32,
95 ) -> Result<(), LemmyError> {
96 verify_is_public(&self.to, &self.cc)?;
97 let community = self.get_community(context, request_counter).await?;
98 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
99 check_community_deleted_or_removed(&community)?;
102 CreateOrUpdateType::Create => {
103 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
104 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
105 // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
106 // However, when fetching a remote post we generate a new create activity with the current
107 // locked/stickied value, so this check may fail. So only check if its a local community,
108 // because then we will definitely receive all create and update activities separately.
109 let is_stickied_or_locked =
110 self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
111 if community.local && is_stickied_or_locked {
112 return Err(LemmyError::from_message(
113 "New post cannot be stickied or locked",
117 CreateOrUpdateType::Update => {
118 let is_mod_action = self.object.is_mod_action(context).await?;
122 self.object.id.inner(),
129 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
130 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
134 ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
138 #[tracing::instrument(skip_all)]
141 context: &Data<LemmyContext>,
142 request_counter: &mut i32,
143 ) -> Result<(), LemmyError> {
144 let post = ApubPost::from_apub(self.object, context, request_counter).await?;
146 // author likes their own post by default
147 let like_form = PostLikeForm {
149 person_id: post.creator_id,
152 blocking(context.pool(), move |conn: &'_ _| {
153 PostLike::like(conn, &like_form)
157 let notif_type = match self.kind {
158 CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
159 CreateOrUpdateType::Update => UserOperationCrud::EditPost,
161 send_post_ws_message(post.id, notif_type, None, None, context).await?;
166 #[async_trait::async_trait(?Send)]
167 impl GetCommunity for CreateOrUpdatePost {
168 #[tracing::instrument(skip_all)]
169 async fn get_community(
171 context: &LemmyContext,
172 request_counter: &mut i32,
173 ) -> Result<ApubCommunity, LemmyError> {
176 .extract_community(context, request_counter)