]> Untitled Git - lemmy.git/commitdiff
Merge branch 'main' into feature/mark_post_as_read
authorDessalines <tyhou13@gmx.com>
Mon, 26 Apr 2021 14:44:19 +0000 (10:44 -0400)
committerDessalines <tyhou13@gmx.com>
Mon, 26 Apr 2021 14:44:19 +0000 (10:44 -0400)
1  2 
crates/api/src/local_user.rs
crates/api/src/site.rs
crates/api_common/src/lib.rs
crates/api_common/src/person.rs
crates/api_crud/src/comment/read.rs
crates/api_crud/src/post/read.rs
crates/api_crud/src/user/read.rs
crates/db_views/src/post_view.rs
crates/routes/src/feeds.rs

Simple merge
index fa8dd5e4c76e9cd0bb00fbd02455287780c86b39,c55ac6645d202f4f45fc7d628a37e168e6dd445c..dab4583041ef455141d350679c4461adcd3f7da7
@@@ -9,12 -9,18 +9,16 @@@ use lemmy_api_common::
    get_local_user_view_from_jwt_opt,
    is_admin,
    site::*,
--  user_show_bot_accounts,
--  user_show_nsfw,
-   user_show_read_posts,
  };
  use lemmy_apub::fetcher::search::search_by_apub_id;
- use lemmy_db_queries::{source::site::Site_, Crud, SearchType, SortType};
+ use lemmy_db_queries::{
+   from_opt_str_to_opt_enum,
+   source::site::Site_,
+   Crud,
+   ListingType,
+   SearchType,
+   SortType,
+ };
  use lemmy_db_schema::source::{moderator::*, site::Site};
  use lemmy_db_views::{
    comment_view::CommentQueryBuilder,
@@@ -140,9 -145,8 +143,13 @@@ impl Perform for Search 
  
      let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
  
--    let show_nsfw = user_show_nsfw(&local_user_view);
--    let show_bot_accounts = user_show_bot_accounts(&local_user_view);
-     let show_read_posts = user_show_read_posts(&local_user_view);
++    let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
++    let show_bot_accounts = local_user_view
++      .as_ref()
++      .map(|t| t.local_user.show_bot_accounts);
++    let show_read_posts = local_user_view
++      .as_ref()
++      .map(|t| t.local_user.show_read_posts);
  
      let person_id = local_user_view.map(|u| u.person.id);
  
        SearchType::Posts => {
          posts = blocking(context.pool(), move |conn| {
            PostQueryBuilder::create(conn)
-             .sort(&sort)
+             .sort(sort)
              .show_nsfw(show_nsfw)
              .show_bot_accounts(show_bot_accounts)
 +            .show_read_posts(show_read_posts)
+             .listing_type(listing_type)
              .community_id(community_id)
              .community_name(community_name)
+             .creator_id(creator_id)
              .my_person_id(person_id)
              .search_term(q)
              .page(page)
          .await??;
        }
        SearchType::All => {
+         // If the community or creator is included, dont search communities or users
+         let community_or_creator_included =
+           data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
          posts = blocking(context.pool(), move |conn| {
            PostQueryBuilder::create(conn)
-             .sort(&sort)
+             .sort(sort)
              .show_nsfw(show_nsfw)
              .show_bot_accounts(show_bot_accounts)
 +            .show_read_posts(show_read_posts)
+             .listing_type(listing_type)
              .community_id(community_id)
              .community_name(community_name)
+             .creator_id(creator_id)
              .my_person_id(person_id)
              .search_term(q)
              .page(page)
        SearchType::Url => {
          posts = blocking(context.pool(), move |conn| {
            PostQueryBuilder::create(conn)
-             .sort(&sort)
+             .sort(sort)
              .show_nsfw(show_nsfw)
              .show_bot_accounts(show_bot_accounts)
 +            .show_read_posts(show_read_posts)
+             .listing_type(listing_type)
              .my_person_id(person_id)
              .community_id(community_id)
              .community_name(community_name)
index 47f4752dd489acaafe3c0f82831fb4f87501d7e0,1a644b7d4d775745de63bb1d14fe1c604b9e54cb..f2b41be50c8c027e619b4c93d4e7ac95bec07a67
@@@ -237,30 -236,22 +237,6 @@@ pub fn is_admin(local_user_view: &Local
    Ok(())
  }
  
--/// A helper method for showing the bot account
--pub fn user_show_bot_accounts(local_user_view: &Option<LocalUserView>) -> bool {
--  match local_user_view {
--    Some(uv) => uv.to_owned().local_user.show_bot_accounts,
--    None => true,
--  }
--}
--
--/// A helper method for showing nsfw
--pub fn user_show_nsfw(local_user_view: &Option<LocalUserView>) -> bool {
--  match &local_user_view {
--    Some(uv) => uv.local_user.show_nsfw,
--    None => false,
-   }
- }
- /// A helper method for showing read posts
- pub fn user_show_read_posts(local_user_view: &Option<LocalUserView>) -> bool {
-   match local_user_view {
-     Some(uv) => uv.to_owned().local_user.show_read_posts,
-     None => true,
--  }
--}
--
  pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
    blocking(pool, move |conn| Post::read(conn, post_id))
      .await?
Simple merge
index 4d144382089688aa2b1a2eeb137941b4dad884b1,f82b97481844eab8c314ebb2dd339662b87fa906..12ccbd5a1a6b6b6b7427d66ac3c44d7072673da9
@@@ -1,12 -1,12 +1,7 @@@
  use crate::PerformCrud;
  use actix_web::web::Data;
--use lemmy_api_common::{
--  blocking,
--  comment::*,
--  get_local_user_view_from_jwt_opt,
--  user_show_bot_accounts,
--};
- use lemmy_db_queries::{ListingType, SortType};
++use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt};
+ use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType};
  use lemmy_db_views::comment_view::CommentQueryBuilder;
  use lemmy_utils::{ApiError, ConnectionId, LemmyError};
  use lemmy_websocket::LemmyContext;
@@@ -24,11 -23,11 +18,13 @@@ impl PerformCrud for GetComments 
      let data: &GetComments = &self;
      let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
  
--    let show_bot_accounts = user_show_bot_accounts(&local_user_view);
++    let show_bot_accounts = local_user_view
++      .as_ref()
++      .map(|t| t.local_user.show_bot_accounts);
      let person_id = local_user_view.map(|u| u.person.id);
  
-     let type_ = ListingType::from_str(&data.type_)?;
-     let sort = SortType::from_str(&data.sort)?;
+     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
+     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
  
      let community_id = data.community_id;
      let community_name = data.community_name.to_owned();
index d44f029a0973b07a801fe88969fe4e0707d3b158,14c6cad5f453d1ccd1e9d6433d50c78d0fc6315a..d49764bba269783b09970ad4425aee4eeb09be50
@@@ -1,15 -1,13 +1,7 @@@
  use crate::PerformCrud;
  use actix_web::web::Data;
--use lemmy_api_common::{
--  blocking,
--  get_local_user_view_from_jwt_opt,
-   mark_post_as_read,
--  post::*,
--  user_show_bot_accounts,
--  user_show_nsfw,
-   user_show_read_posts,
--};
- use lemmy_db_queries::{ListingType, SortType};
++use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, mark_post_as_read, post::*};
+ use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType};
  use lemmy_db_views::{
    comment_view::CommentQueryBuilder,
    post_view::{PostQueryBuilder, PostView},
@@@ -34,8 -31,8 +25,9 @@@ impl PerformCrud for GetPost 
      let data: &GetPost = &self;
      let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
  
--    let show_bot_accounts = user_show_bot_accounts(&local_user_view);
--
++    let show_bot_accounts = local_user_view
++      .as_ref()
++      .map(|t| t.local_user.show_bot_accounts);
      let person_id = local_user_view.map(|u| u.person.id);
  
      let id = data.id;
@@@ -105,12 -97,11 +97,16 @@@ impl PerformCrud for GetPosts 
  
      let person_id = local_user_view.to_owned().map(|l| l.person.id);
  
--    let show_nsfw = user_show_nsfw(&local_user_view);
--    let show_bot_accounts = user_show_bot_accounts(&local_user_view);
-     let show_read_posts = user_show_read_posts(&local_user_view);
++    let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
++    let show_bot_accounts = local_user_view
++      .as_ref()
++      .map(|t| t.local_user.show_bot_accounts);
++    let show_read_posts = local_user_view
++      .as_ref()
++      .map(|t| t.local_user.show_read_posts);
  
-     let type_ = ListingType::from_str(&data.type_)?;
-     let sort = SortType::from_str(&data.sort)?;
+     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
+     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
  
      let page = data.page;
      let limit = data.limit;
  
      let posts = blocking(context.pool(), move |conn| {
        PostQueryBuilder::create(conn)
-         .listing_type(&type_)
-         .sort(&sort)
+         .listing_type(listing_type)
+         .sort(sort)
          .show_nsfw(show_nsfw)
          .show_bot_accounts(show_bot_accounts)
 +        .show_read_posts(show_read_posts)
          .community_id(community_id)
          .community_name(community_name)
          .saved_only(saved_only)
index e0e3808c17ced23885055b17c034fe1caae0fbf2,39431128aee3d70846a5278ff5bb2425b9d84907..132bdd78f4e32d633332f098b97fac9b081aea14
@@@ -1,14 -1,13 +1,7 @@@
  use crate::PerformCrud;
  use actix_web::web::Data;
--use lemmy_api_common::{
--  blocking,
--  get_local_user_view_from_jwt_opt,
--  person::*,
--  user_show_bot_accounts,
--  user_show_nsfw,
-   user_show_read_posts,
--};
- use lemmy_db_queries::{source::person::Person_, SortType};
++use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*};
+ use lemmy_db_queries::{from_opt_str_to_opt_enum, source::person::Person_, SortType};
  use lemmy_db_schema::source::person::*;
  use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
  use lemmy_db_views_actor::{
@@@ -32,11 -30,10 +24,15 @@@ impl PerformCrud for GetPersonDetails 
      let data: &GetPersonDetails = &self;
      let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
  
--    let show_nsfw = user_show_nsfw(&local_user_view);
--    let show_bot_accounts = user_show_bot_accounts(&local_user_view);
-     let show_read_posts = user_show_read_posts(&local_user_view);
++    let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
++    let show_bot_accounts = local_user_view
++      .as_ref()
++      .map(|t| t.local_user.show_bot_accounts);
++    let show_read_posts = local_user_view
++      .as_ref()
++      .map(|t| t.local_user.show_read_posts);
  
-     let sort = SortType::from_str(&data.sort)?;
+     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
  
      let username = data
        .username
  
      let (posts, comments) = blocking(context.pool(), move |conn| {
        let mut posts_query = PostQueryBuilder::create(conn)
-         .sort(&sort)
+         .sort(sort)
          .show_nsfw(show_nsfw)
          .show_bot_accounts(show_bot_accounts)
 +        .show_read_posts(show_read_posts)
          .saved_only(saved_only)
          .community_id(community_id)
          .my_person_id(person_id)
index 8a533412f7da77abfbbf2925dcf0afcac427c11e,fb8f073365ad49b5c8e496e971ae99759966a530..1d8ea80e607720a0e4b2492b79eacb1de16be9ba
@@@ -163,10 -163,10 +163,10 @@@ pub struct PostQueryBuilder<'a> 
    my_person_id: Option<PersonId>,
    search_term: Option<String>,
    url_search: Option<String>,
-   show_nsfw: bool,
-   show_bot_accounts: bool,
-   show_read_posts: bool,
-   saved_only: bool,
+   show_nsfw: Option<bool>,
+   show_bot_accounts: Option<bool>,
++  show_read_posts: Option<bool>,
+   saved_only: Option<bool>,
 -  unread_only: Option<bool>,
    page: Option<i64>,
    limit: Option<i64>,
  }
@@@ -183,10 -183,10 +183,10 @@@ impl<'a> PostQueryBuilder<'a> 
        my_person_id: None,
        search_term: None,
        url_search: None,
-       show_nsfw: true,
-       show_bot_accounts: true,
-       show_read_posts: true,
-       saved_only: false,
+       show_nsfw: None,
+       show_bot_accounts: None,
++      show_read_posts: None,
+       saved_only: None,
 -      unread_only: None,
        page: None,
        limit: None,
      }
      self
    }
  
-   pub fn show_read_posts(mut self, show_read_posts: bool) -> Self {
-     self.show_read_posts = show_read_posts;
++  pub fn show_read_posts<T: MaybeOptional<bool>>(mut self, show_read_posts: T) -> Self {
++    self.show_read_posts = show_read_posts.get_optional();
 +    self
 +  }
 +
-   pub fn saved_only(mut self, saved_only: bool) -> Self {
-     self.saved_only = saved_only;
+   pub fn saved_only<T: MaybeOptional<bool>>(mut self, saved_only: T) -> Self {
+     self.saved_only = saved_only.get_optional();
      self
    }
  
        query = query.filter(person::bot_account.eq(false));
      };
  
-     if self.saved_only {
 -    if self.saved_only.unwrap_or(false) {
--      query = query.filter(post_saved::id.is_not_null());
++    if !self.show_read_posts.unwrap_or(true) {
++      query = query.filter(post_read::id.is_null());
      };
  
-     if !self.show_read_posts {
-       query = query.filter(post_read::id.is_null());
 -    if self.unread_only.unwrap_or(false) {
 -      query = query.filter(post_read::id.is_not_null());
++    if self.saved_only.unwrap_or(false) {
++      query = query.filter(post_saved::id.is_not_null());
      };
  
-     query = match self.sort {
+     query = match self.sort.unwrap_or(SortType::Hot) {
        SortType::Active => query
          .then_order_by(
            hot_rank(
index 2ed586737f3e9e688d3eb36119a09af08bd37ebe,a4f6a45a06c9c60502670094b7c34bb0791e4f63..06b9ac13aef395940803d911cb0e3691029b0f12
@@@ -231,14 -230,12 +230,14 @@@ fn get_feed_front
    let local_user = LocalUser::read(&conn, local_user_id)?;
    let person_id = local_user.person_id;
    let show_bot_accounts = local_user.show_bot_accounts;
 +  let show_read_posts = local_user.show_read_posts;
  
    let posts = PostQueryBuilder::create(&conn)
-     .listing_type(&ListingType::Subscribed)
+     .listing_type(ListingType::Subscribed)
      .my_person_id(person_id)
      .show_bot_accounts(show_bot_accounts)
-     .sort(sort_type)
 +    .show_read_posts(show_read_posts)
+     .sort(*sort_type)
      .list()?;
  
    let items = create_post_items(posts)?;