]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/create_or_update/post.rs
Merge websocket crate into api_common
[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_api_common::{
26   websocket::{send::send_post_ws_message, UserOperationCrud},
27   LemmyContext,
28 };
29 use lemmy_db_schema::{
30   source::{
31     community::Community,
32     post::{PostLike, PostLikeForm},
33   },
34   traits::{Crud, Likeable},
35 };
36 use lemmy_utils::error::LemmyError;
37 use url::Url;
38
39 impl CreateOrUpdatePage {
40   pub(crate) async fn new(
41     post: ApubPost,
42     actor: &ApubPerson,
43     community: &ApubCommunity,
44     kind: CreateOrUpdateType,
45     context: &LemmyContext,
46   ) -> Result<CreateOrUpdatePage, LemmyError> {
47     let id = generate_activity_id(
48       kind.clone(),
49       &context.settings().get_protocol_and_hostname(),
50     )?;
51     Ok(CreateOrUpdatePage {
52       actor: ObjectId::new(actor.actor_id()),
53       to: vec![public()],
54       object: post.into_apub(context).await?,
55       cc: vec![community.actor_id()],
56       kind,
57       id: id.clone(),
58       audience: Some(ObjectId::new(community.actor_id())),
59     })
60   }
61
62   #[tracing::instrument(skip_all)]
63   pub async fn send(
64     post: ApubPost,
65     actor: &ApubPerson,
66     kind: CreateOrUpdateType,
67     context: &LemmyContext,
68   ) -> Result<(), LemmyError> {
69     let community_id = post.community_id;
70     let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
71
72     let create_or_update = CreateOrUpdatePage::new(post, actor, &community, kind, context).await?;
73     let is_mod_action = create_or_update.object.is_mod_action(context).await?;
74     let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
75     send_activity_in_community(activity, actor, &community, vec![], is_mod_action, context).await?;
76     Ok(())
77   }
78 }
79
80 #[async_trait::async_trait(?Send)]
81 impl ActivityHandler for CreateOrUpdatePage {
82   type DataType = LemmyContext;
83   type Error = LemmyError;
84
85   fn id(&self) -> &Url {
86     &self.id
87   }
88
89   fn actor(&self) -> &Url {
90     self.actor.inner()
91   }
92
93   #[tracing::instrument(skip_all)]
94   async fn verify(
95     &self,
96     context: &Data<LemmyContext>,
97     request_counter: &mut i32,
98   ) -> Result<(), LemmyError> {
99     verify_is_public(&self.to, &self.cc)?;
100     let community = self.community(context, request_counter).await?;
101     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
102     check_community_deleted_or_removed(&community)?;
103
104     match self.kind {
105       CreateOrUpdateType::Create => {
106         verify_domains_match(self.actor.inner(), self.object.id.inner())?;
107         verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
108         // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
109         // However, when fetching a remote post we generate a new create activity with the current
110         // locked/stickied value, so this check may fail. So only check if its a local community,
111         // because then we will definitely receive all create and update activities separately.
112         let is_stickied_or_locked =
113           self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
114         if community.local && is_stickied_or_locked {
115           return Err(LemmyError::from_message(
116             "New post cannot be stickied or locked",
117           ));
118         }
119       }
120       CreateOrUpdateType::Update => {
121         let is_mod_action = self.object.is_mod_action(context).await?;
122         if is_mod_action {
123           verify_mod_action(
124             &self.actor,
125             self.object.id.inner(),
126             community.id,
127             context,
128             request_counter,
129           )
130           .await?;
131         } else {
132           verify_domains_match(self.actor.inner(), self.object.id.inner())?;
133           verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
134         }
135       }
136     }
137     ApubPost::verify(&self.object, self.actor.inner(), context, request_counter).await?;
138     Ok(())
139   }
140
141   #[tracing::instrument(skip_all)]
142   async fn receive(
143     self,
144     context: &Data<LemmyContext>,
145     request_counter: &mut i32,
146   ) -> Result<(), LemmyError> {
147     let post = ApubPost::from_apub(self.object, context, request_counter).await?;
148
149     // author likes their own post by default
150     let like_form = PostLikeForm {
151       post_id: post.id,
152       person_id: post.creator_id,
153       score: 1,
154     };
155     PostLike::like(context.pool(), &like_form).await?;
156
157     let notif_type = match self.kind {
158       CreateOrUpdateType::Create => UserOperationCrud::CreatePost,
159       CreateOrUpdateType::Update => UserOperationCrud::EditPost,
160     };
161     send_post_ws_message(post.id, notif_type, None, None, context).await?;
162     Ok(())
163   }
164 }