]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/post.rs
Use audience field to federate items in groups (fixes #2464) (#2584)
[lemmy.git] / crates / apub / src / activities / create_or_update / post.rs
1 use crate::{
2   activities::{
3     check_community_deleted_or_removed,
4     community::send_activity_in_community,
5     generate_activity_id,
6     verify_is_public,
7     verify_mod_action,
8     verify_person_in_community,
9   },
10   activity_lists::AnnouncableActivities,
11   objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
12   protocol::{
13     activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
14     InCommunity,
15   },
16   ActorType,
17 };
18 use activitypub_federation::{
19   core::object_id::ObjectId,
20   data::Data,
21   traits::{ActivityHandler, ApubObject},
22   utils::{verify_domains_match, verify_urls_match},
23 };
24 use activitystreams_kinds::public;
25 use lemmy_db_schema::{
26   source::{
27     community::Community,
28     post::{PostLike, PostLikeForm},
29   },
30   traits::{Crud, Likeable},
31 };
32 use lemmy_utils::error::LemmyError;
33 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperationCrud};
34 use url::Url;
35
36 impl CreateOrUpdatePage {
37   pub(crate) async fn new(
38     post: ApubPost,
39     actor: &ApubPerson,
40     community: &ApubCommunity,
41     kind: CreateOrUpdateType,
42     context: &LemmyContext,
43   ) -> Result<CreateOrUpdatePage, LemmyError> {
44     let id = generate_activity_id(
45       kind.clone(),
46       &context.settings().get_protocol_and_hostname(),
47     )?;
48     Ok(CreateOrUpdatePage {
49       actor: ObjectId::new(actor.actor_id()),
50       to: vec![public()],
51       object: post.into_apub(context).await?,
52       cc: vec![community.actor_id()],
53       kind,
54       id: id.clone(),
55       audience: Some(ObjectId::new(community.actor_id())),
56     })
57   }
58
59   #[tracing::instrument(skip_all)]
60   pub async fn send(
61     post: ApubPost,
62     actor: &ApubPerson,
63     kind: CreateOrUpdateType,
64     context: &LemmyContext,
65   ) -> Result<(), LemmyError> {
66     let community_id = post.community_id;
67     let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
68
69     let create_or_update = CreateOrUpdatePage::new(post, actor, &community, kind, context).await?;
70     let is_mod_action = create_or_update.object.is_mod_action(context).await?;
71     let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
72     send_activity_in_community(activity, actor, &community, vec![], is_mod_action, context).await?;
73     Ok(())
74   }
75 }
76
77 #[async_trait::async_trait(?Send)]
78 impl ActivityHandler for CreateOrUpdatePage {
79   type DataType = LemmyContext;
80   type Error = LemmyError;
81
82   fn id(&self) -> &Url {
83     &self.id
84   }
85
86   fn actor(&self) -> &Url {
87     self.actor.inner()
88   }
89
90   #[tracing::instrument(skip_all)]
91   async fn verify(
92     &self,
93     context: &Data<LemmyContext>,
94     request_counter: &mut i32,
95   ) -> Result<(), LemmyError> {
96     verify_is_public(&self.to, &self.cc)?;
97     let community = self.community(context, request_counter).await?;
98     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
99     check_community_deleted_or_removed(&community)?;
100
101     match self.kind {
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",
114           ));
115         }
116       }
117       CreateOrUpdateType::Update => {
118         let is_mod_action = self.object.is_mod_action(context).await?;
119         if is_mod_action {
120           verify_mod_action(
121             &self.actor,
122             self.object.id.inner(),
123             community.id,
124             context,
125             request_counter,
126           )
127           .await?;
128         } else {
129           verify_domains_match(self.actor.inner(), self.object.id.inner())?;
130           verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
131         }
132       }
133     }
134     ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
135     Ok(())
136   }
137
138   #[tracing::instrument(skip_all)]
139   async fn receive(
140     self,
141     context: &Data<LemmyContext>,
142     request_counter: &mut i32,
143   ) -> Result<(), LemmyError> {
144     let post = ApubPost::from_apub(self.object, context, request_counter).await?;
145
146     // author likes their own post by default
147     let like_form = PostLikeForm {
148       post_id: post.id,
149       person_id: post.creator_id,
150       score: 1,
151     };
152     PostLike::like(context.pool(), &like_form).await?;
153
154     let notif_type = match self.kind {
155       CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
156       CreateOrUpdateType::Update => UserOperationCrud::EditPost,
157     };
158     send_post_ws_message(post.id, notif_type, None, None, context).await?;
159     Ok(())
160   }
161 }