3 check_community_deleted_or_removed,
4 community::send_activity_in_community,
8 verify_person_in_community,
10 activity_lists::AnnouncableActivities,
11 insert_received_activity,
12 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
14 activities::{create_or_update::page::CreateOrUpdatePage, CreateOrUpdateType},
18 use activitypub_federation::{
21 protocol::verification::{verify_domains_match, verify_urls_match},
22 traits::{ActivityHandler, Actor, Object},
24 use lemmy_api_common::context::LemmyContext;
25 use lemmy_db_schema::{
26 aggregates::structs::PostAggregates,
31 post::{Post, PostLike, PostLikeForm},
33 traits::{Crud, Likeable},
35 use lemmy_utils::error::{LemmyError, LemmyErrorType};
38 impl CreateOrUpdatePage {
39 pub(crate) async fn new(
42 community: &ApubCommunity,
43 kind: CreateOrUpdateType,
44 context: &Data<LemmyContext>,
45 ) -> Result<CreateOrUpdatePage, LemmyError> {
46 let id = generate_activity_id(
48 &context.settings().get_protocol_and_hostname(),
50 Ok(CreateOrUpdatePage {
51 actor: actor.id().into(),
53 object: post.into_json(context).await?,
54 cc: vec![community.id()],
57 audience: Some(community.id().into()),
61 #[tracing::instrument(skip_all)]
62 pub(crate) async fn send(
65 kind: CreateOrUpdateType,
66 context: Data<LemmyContext>,
67 ) -> Result<(), LemmyError> {
68 let post = ApubPost(post);
69 let community_id = post.community_id;
70 let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into();
71 let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
75 let create_or_update =
76 CreateOrUpdatePage::new(post, &person, &community, kind, &context).await?;
77 let is_mod_action = create_or_update.object.is_mod_action(&context).await?;
78 let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
79 send_activity_in_community(
92 #[async_trait::async_trait]
93 impl ActivityHandler for CreateOrUpdatePage {
94 type DataType = LemmyContext;
95 type Error = LemmyError;
97 fn id(&self) -> &Url {
101 fn actor(&self) -> &Url {
105 #[tracing::instrument(skip_all)]
106 async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
107 insert_received_activity(&self.id, context).await?;
108 verify_is_public(&self.to, &self.cc)?;
109 let community = self.community(context).await?;
110 verify_person_in_community(&self.actor, &community, context).await?;
111 check_community_deleted_or_removed(&community)?;
114 CreateOrUpdateType::Create => {
115 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
116 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
117 // Check that the post isnt locked, as that isnt possible for newly created posts.
118 // However, when fetching a remote post we generate a new create activity with the current
119 // locked value, so this check may fail. So only check if its a local community,
120 // because then we will definitely receive all create and update activities separately.
121 let is_locked = self.object.comments_enabled == Some(false);
122 if community.local && is_locked {
123 return Err(LemmyErrorType::NewPostCannotBeLocked)?;
126 CreateOrUpdateType::Update => {
127 let is_mod_action = self.object.is_mod_action(context).await?;
129 verify_mod_action(&self.actor, self.object.id.inner(), community.id, context).await?;
131 verify_domains_match(self.actor.inner(), self.object.id.inner())?;
132 verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
136 ApubPost::verify(&self.object, self.actor.inner(), context).await?;
140 #[tracing::instrument(skip_all)]
141 async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
142 let post = ApubPost::from_json(self.object, context).await?;
144 // author likes their own post by default
145 let like_form = PostLikeForm {
147 person_id: post.creator_id,
150 PostLike::like(&mut context.pool(), &like_form).await?;
152 // Calculate initial hot_rank for post
153 PostAggregates::update_hot_rank(&mut context.pool(), post.id).await?;