]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/post.rs
Cache & Optimize Woodpecker CI (#3450)
[lemmy.git] / crates / db_schema / src / impls / post.rs
index 407a83f5369b660d57a1a013abc0ed18181888df..f10d0cd2793ff515dc2ff24190d02cd87e8bbdba 100644 (file)
@@ -6,11 +6,11 @@ use crate::{
     community_id,
     creator_id,
     deleted,
+    featured_community,
     name,
     post,
     published,
     removed,
-    stickied,
     thumbnail_url,
     updated,
     url,
@@ -26,8 +26,8 @@ use crate::{
     PostSavedForm,
     PostUpdateForm,
   },
-  traits::{Crud, DeleteableOrRemoveable, Likeable, Readable, Saveable},
-  utils::{get_conn, naive_now, DbPool, FETCH_LIMIT_MAX},
+  traits::{Crud, Likeable, Readable, Saveable},
+  utils::{get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT, FETCH_LIMIT_MAX},
 };
 use ::url::Url;
 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
@@ -38,17 +38,17 @@ impl Crud for Post {
   type InsertForm = PostInsertForm;
   type UpdateForm = PostUpdateForm;
   type IdType = PostId;
-  async fn read(pool: &DbPool, post_id: PostId) -> Result<Self, Error> {
+  async fn read(pool: &mut DbPool<'_>, post_id: PostId) -> Result<Self, Error> {
     let conn = &mut get_conn(pool).await?;
     post.find(post_id).first::<Self>(conn).await
   }
 
-  async fn delete(pool: &DbPool, post_id: PostId) -> Result<usize, Error> {
+  async fn delete(pool: &mut DbPool<'_>, post_id: PostId) -> Result<usize, Error> {
     let conn = &mut get_conn(pool).await?;
     diesel::delete(post.find(post_id)).execute(conn).await
   }
 
-  async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result<Self, Error> {
+  async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
     let conn = &mut get_conn(pool).await?;
     insert_into(post)
       .values(form)
@@ -60,7 +60,7 @@ impl Crud for Post {
   }
 
   async fn update(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     post_id: PostId,
     new_post: &Self::UpdateForm,
   ) -> Result<Self, Error> {
@@ -74,7 +74,7 @@ impl Crud for Post {
 
 impl Post {
   pub async fn list_for_community(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     the_community_id: CommunityId,
   ) -> Result<Vec<Self>, Error> {
     let conn = &mut get_conn(pool).await?;
@@ -82,27 +82,40 @@ impl Post {
       .filter(community_id.eq(the_community_id))
       .filter(deleted.eq(false))
       .filter(removed.eq(false))
+      .then_order_by(featured_community.desc())
+      .then_order_by(published.desc())
+      .limit(FETCH_LIMIT_MAX)
+      .load::<Self>(conn)
+      .await
+  }
+
+  pub async fn list_featured_for_community(
+    pool: &mut DbPool<'_>,
+    the_community_id: CommunityId,
+  ) -> Result<Vec<Self>, Error> {
+    let conn = &mut get_conn(pool).await?;
+    post
+      .filter(community_id.eq(the_community_id))
+      .filter(deleted.eq(false))
+      .filter(removed.eq(false))
+      .filter(featured_community.eq(true))
       .then_order_by(published.desc())
-      .then_order_by(stickied.desc())
       .limit(FETCH_LIMIT_MAX)
       .load::<Self>(conn)
       .await
   }
 
   pub async fn permadelete_for_creator(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     for_creator_id: PersonId,
   ) -> Result<Vec<Self>, Error> {
     let conn = &mut get_conn(pool).await?;
 
-    let perma_deleted = "*Permananently Deleted*";
-    let perma_deleted_url = "https://deleted.com";
-
     diesel::update(post.filter(creator_id.eq(for_creator_id)))
       .set((
-        name.eq(perma_deleted),
-        url.eq(perma_deleted_url),
-        body.eq(perma_deleted),
+        name.eq(DELETED_REPLACEMENT_TEXT),
+        url.eq(Option::<&str>::None),
+        body.eq(DELETED_REPLACEMENT_TEXT),
         deleted.eq(true),
         updated.eq(naive_now()),
       ))
@@ -111,7 +124,7 @@ impl Post {
   }
 
   pub async fn update_removed_for_creator(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     for_creator_id: PersonId,
     for_community_id: Option<CommunityId>,
     new_removed: bool,
@@ -135,7 +148,10 @@ impl Post {
     person_id == post_creator_id
   }
 
-  pub async fn read_from_apub_id(pool: &DbPool, object_id: Url) -> Result<Option<Self>, Error> {
+  pub async fn read_from_apub_id(
+    pool: &mut DbPool<'_>,
+    object_id: Url,
+  ) -> Result<Option<Self>, Error> {
     let conn = &mut get_conn(pool).await?;
     let object_id: DbUrl = object_id.into();
     Ok(
@@ -149,7 +165,7 @@ impl Post {
   }
 
   pub async fn fetch_pictrs_posts_for_creator(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     for_creator_id: PersonId,
   ) -> Result<Vec<Self>, Error> {
     let conn = &mut get_conn(pool).await?;
@@ -164,7 +180,7 @@ impl Post {
 
   /// Sets the url and thumbnails fields to None
   pub async fn remove_pictrs_post_images_and_thumbnails_for_creator(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     for_creator_id: PersonId,
   ) -> Result<Vec<Self>, Error> {
     let conn = &mut get_conn(pool).await?;
@@ -184,7 +200,7 @@ impl Post {
   }
 
   pub async fn fetch_pictrs_posts_for_community(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     for_community_id: CommunityId,
   ) -> Result<Vec<Self>, Error> {
     let conn = &mut get_conn(pool).await?;
@@ -198,7 +214,7 @@ impl Post {
 
   /// Sets the url and thumbnails fields to None
   pub async fn remove_pictrs_post_images_and_thumbnails_for_community(
-    pool: &DbPool,
+    pool: &mut DbPool<'_>,
     for_community_id: CommunityId,
   ) -> Result<Vec<Self>, Error> {
     let conn = &mut get_conn(pool).await?;
@@ -222,7 +238,7 @@ impl Post {
 impl Likeable for PostLike {
   type Form = PostLikeForm;
   type IdType = PostId;
-  async fn like(pool: &DbPool, post_like_form: &PostLikeForm) -> Result<Self, Error> {
+  async fn like(pool: &mut DbPool<'_>, post_like_form: &PostLikeForm) -> Result<Self, Error> {
     use crate::schema::post_like::dsl::{person_id, post_id, post_like};
     let conn = &mut get_conn(pool).await?;
     insert_into(post_like)
@@ -233,7 +249,11 @@ impl Likeable for PostLike {
       .get_result::<Self>(conn)
       .await
   }
-  async fn remove(pool: &DbPool, person_id: PersonId, post_id: PostId) -> Result<usize, Error> {
+  async fn remove(
+    pool: &mut DbPool<'_>,
+    person_id: PersonId,
+    post_id: PostId,
+  ) -> Result<usize, Error> {
     use crate::schema::post_like::dsl;
     let conn = &mut get_conn(pool).await?;
     diesel::delete(
@@ -249,7 +269,7 @@ impl Likeable for PostLike {
 #[async_trait]
 impl Saveable for PostSaved {
   type Form = PostSavedForm;
-  async fn save(pool: &DbPool, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
+  async fn save(pool: &mut DbPool<'_>, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
     use crate::schema::post_saved::dsl::{person_id, post_id, post_saved};
     let conn = &mut get_conn(pool).await?;
     insert_into(post_saved)
@@ -260,7 +280,7 @@ impl Saveable for PostSaved {
       .get_result::<Self>(conn)
       .await
   }
-  async fn unsave(pool: &DbPool, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
+  async fn unsave(pool: &mut DbPool<'_>, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
     use crate::schema::post_saved::dsl::{person_id, post_id, post_saved};
     let conn = &mut get_conn(pool).await?;
     diesel::delete(
@@ -276,7 +296,10 @@ impl Saveable for PostSaved {
 #[async_trait]
 impl Readable for PostRead {
   type Form = PostReadForm;
-  async fn mark_as_read(pool: &DbPool, post_read_form: &PostReadForm) -> Result<Self, Error> {
+  async fn mark_as_read(
+    pool: &mut DbPool<'_>,
+    post_read_form: &PostReadForm,
+  ) -> Result<Self, Error> {
     use crate::schema::post_read::dsl::{person_id, post_id, post_read};
     let conn = &mut get_conn(pool).await?;
     insert_into(post_read)
@@ -288,7 +311,10 @@ impl Readable for PostRead {
       .await
   }
 
-  async fn mark_as_unread(pool: &DbPool, post_read_form: &PostReadForm) -> Result<usize, Error> {
+  async fn mark_as_unread(
+    pool: &mut DbPool<'_>,
+    post_read_form: &PostReadForm,
+  ) -> Result<usize, Error> {
     use crate::schema::post_read::dsl::{person_id, post_id, post_read};
     let conn = &mut get_conn(pool).await?;
     diesel::delete(
@@ -301,22 +327,11 @@ impl Readable for PostRead {
   }
 }
 
-impl DeleteableOrRemoveable for Post {
-  fn blank_out_deleted_or_removed_info(mut self) -> Self {
-    self.name = String::new();
-    self.url = None;
-    self.body = None;
-    self.embed_title = None;
-    self.embed_description = None;
-    self.embed_video_url = None;
-    self.thumbnail_url = None;
-
-    self
-  }
-}
-
 #[cfg(test)]
 mod tests {
+  #![allow(clippy::unwrap_used)]
+  #![allow(clippy::indexing_slicing)]
+
   use crate::{
     source::{
       community::{Community, CommunityInsertForm},
@@ -343,8 +358,11 @@ mod tests {
   #[serial]
   async fn test_crud() {
     let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
 
-    let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
+    let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+      .await
+      .unwrap();
 
     let new_person = PersonInsertForm::builder()
       .name("jim".into())
@@ -381,7 +399,6 @@ mod tests {
       published: inserted_post.published,
       removed: false,
       locked: false,
-      stickied: false,
       nsfw: false,
       deleted: false,
       updated: None,
@@ -392,6 +409,8 @@ mod tests {
       ap_id: inserted_post.ap_id.clone(),
       local: true,
       language_id: Default::default(),
+      featured_community: false,
+      featured_local: false,
     };
 
     // Post Like