]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/activities/create_or_update/post.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / apub / src / activities / create_or_update / post.rs
index b545f523948a1b3a7931b4fc2e9b01c057a9adcc..e0ce0fec4cc48a85e8bfdada8cb1a0c77c17d2e7 100644 (file)
@@ -27,6 +27,7 @@ use lemmy_api_common::{
   post::{CreatePost, EditPost, PostResponse},
 };
 use lemmy_db_schema::{
+  aggregates::structs::PostAggregates,
   newtypes::PersonId,
   source::{
     community::Community,
@@ -35,7 +36,7 @@ use lemmy_db_schema::{
   },
   traits::{Crud, Likeable},
 };
-use lemmy_utils::error::LemmyError;
+use lemmy_utils::error::{LemmyError, LemmyErrorType};
 use url::Url;
 
 #[async_trait::async_trait]
@@ -108,8 +109,10 @@ impl CreateOrUpdatePage {
   ) -> Result<(), LemmyError> {
     let post = ApubPost(post.clone());
     let community_id = post.community_id;
-    let person: ApubPerson = Person::read(context.pool(), person_id).await?.into();
-    let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
+    let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into();
+    let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
+      .await?
+      .into();
 
     let create_or_update =
       CreateOrUpdatePage::new(post, &person, &community, kind, context).await?;
@@ -152,16 +155,13 @@ impl ActivityHandler for CreateOrUpdatePage {
       CreateOrUpdateType::Create => {
         verify_domains_match(self.actor.inner(), self.object.id.inner())?;
         verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
-        // Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
+        // Check that the post isnt locked, as that isnt possible for newly created posts.
         // However, when fetching a remote post we generate a new create activity with the current
-        // locked/stickied value, so this check may fail. So only check if its a local community,
+        // locked value, so this check may fail. So only check if its a local community,
         // because then we will definitely receive all create and update activities separately.
-        let is_featured_or_locked =
-          self.object.stickied == Some(true) || self.object.comments_enabled == Some(false);
-        if community.local && is_featured_or_locked {
-          return Err(LemmyError::from_message(
-            "New post cannot be stickied or locked",
-          ));
+        let is_locked = self.object.comments_enabled == Some(false);
+        if community.local && is_locked {
+          return Err(LemmyErrorType::NewPostCannotBeLocked)?;
         }
       }
       CreateOrUpdateType::Update => {
@@ -189,7 +189,11 @@ impl ActivityHandler for CreateOrUpdatePage {
       person_id: post.creator_id,
       score: 1,
     };
-    PostLike::like(context.pool(), &like_form).await?;
+    PostLike::like(&mut context.pool(), &like_form).await?;
+
+    // Calculate initial hot_rank for post
+    PostAggregates::update_hot_rank(&mut context.pool(), post.id).await?;
+
     Ok(())
   }
 }