]> Untitled Git - lemmy.git/blobdiff - crates/db_views/src/post_view.rs
Add support for Featured Posts (#2585)
[lemmy.git] / crates / db_views / src / post_view.rs
index d47db578437e801cdc33a63558b1d22288d608c7..4a36b816fbe7dc55545f873f80b3c2d74321d913 100644 (file)
@@ -1,7 +1,7 @@
 use crate::structs::PostView;
 use diesel::{
   debug_query,
-  dsl::*,
+  dsl::{now, IntervalDsl},
   pg::Pg,
   result::Error,
   sql_function,
@@ -207,7 +207,6 @@ pub struct PostQuery<'a> {
 
 impl<'a> PostQuery<'a> {
   pub async fn list(self) -> Result<Vec<PostView>, Error> {
-    use diesel::dsl::*;
     let conn = &mut get_conn(self.pool).await?;
 
     // The left join below will return None in this case
@@ -325,17 +324,16 @@ impl<'a> PostQuery<'a> {
         }
       }
     }
-
-    if let Some(community_id) = self.community_id {
+    if self.community_id.is_none() && self.community_actor_id.is_none() {
+      query = query.then_order_by(post_aggregates::featured_local.desc());
+    } else if let Some(community_id) = self.community_id {
       query = query
         .filter(post::community_id.eq(community_id))
-        .then_order_by(post_aggregates::stickied.desc());
-    }
-
-    if let Some(community_actor_id) = self.community_actor_id {
+        .then_order_by(post_aggregates::featured_community.desc());
+    } else if let Some(community_actor_id) = self.community_actor_id {
       query = query
         .filter(community::actor_id.eq(community_actor_id))
-        .then_order_by(post_aggregates::stickied.desc());
+        .then_order_by(post_aggregates::featured_community.desc());
     }
 
     if let Some(url_search) = self.url_search {
@@ -346,7 +344,7 @@ impl<'a> PostQuery<'a> {
       let searcher = fuzzy_search(&search_term);
       query = query.filter(
         post::name
-          .ilike(searcher.to_owned())
+          .ilike(searcher.clone())
           .or(post::body.ilike(searcher)),
       );
     }
@@ -472,14 +470,14 @@ mod tests {
     newtypes::LanguageId,
     source::{
       actor_language::LocalUserLanguage,
-      community::*,
+      community::{Community, CommunityInsertForm, CommunitySafe},
       community_block::{CommunityBlock, CommunityBlockForm},
       instance::Instance,
       language::Language,
       local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
-      person::*,
+      person::{Person, PersonInsertForm, PersonSafe},
       person_block::{PersonBlock, PersonBlockForm},
-      post::*,
+      post::{Post, PostInsertForm, PostLike, PostLikeForm},
     },
     traits::{Blockable, Crud, Likeable},
     utils::{build_db_pool_for_tests, DbPool},
@@ -504,7 +502,7 @@ mod tests {
     let person_name = "tegan".to_string();
 
     let new_person = PersonInsertForm::builder()
-      .name(person_name.to_owned())
+      .name(person_name.clone())
       .public_key("pubkey".to_string())
       .instance_id(inserted_instance.id)
       .build();
@@ -513,7 +511,7 @@ mod tests {
 
     let local_user_form = LocalUserInsertForm::builder()
       .person_id(inserted_person.id)
-      .password_encrypted("".to_string())
+      .password_encrypted(String::new())
       .build();
     let inserted_local_user = LocalUser::create(pool, &local_user_form).await.unwrap();
 
@@ -861,15 +859,16 @@ mod tests {
         removed: false,
         deleted: false,
         locked: false,
-        stickied: false,
         nsfw: false,
         embed_title: None,
         embed_description: None,
         embed_video_url: None,
         thumbnail_url: None,
-        ap_id: inserted_post.ap_id.to_owned(),
+        ap_id: inserted_post.ap_id.clone(),
         local: true,
         language_id: LanguageId(47),
+        featured_community: false,
+        featured_local: false,
       },
       my_vote: None,
       unread_comments: 0,
@@ -879,7 +878,7 @@ mod tests {
         display_name: None,
         published: inserted_person.published,
         avatar: None,
-        actor_id: inserted_person.actor_id.to_owned(),
+        actor_id: inserted_person.actor_id.clone(),
         local: true,
         admin: false,
         bot_account: false,
@@ -888,7 +887,7 @@ mod tests {
         bio: None,
         banner: None,
         updated: None,
-        inbox_url: inserted_person.inbox_url.to_owned(),
+        inbox_url: inserted_person.inbox_url.clone(),
         shared_inbox_url: None,
         matrix_user_id: None,
         ban_expires: None,
@@ -902,7 +901,7 @@ mod tests {
         removed: false,
         deleted: false,
         nsfw: false,
-        actor_id: inserted_community.actor_id.to_owned(),
+        actor_id: inserted_community.actor_id.clone(),
         local: true,
         title: "nada".to_owned(),
         description: None,
@@ -920,10 +919,11 @@ mod tests {
         score: 0,
         upvotes: 0,
         downvotes: 0,
-        stickied: false,
         published: agg.published,
         newest_comment_time_necro: inserted_post.published,
         newest_comment_time: inserted_post.published,
+        featured_community: false,
+        featured_local: false,
       },
       subscribed: SubscribedType::NotSubscribed,
       read: false,