From: Dessalines Date: Mon, 26 Apr 2021 14:44:19 +0000 (-0400) Subject: Merge branch 'main' into feature/mark_post_as_read X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/%22%7B%7D/%7BpictshareAvatarThumbnail%28admin.avatar%29%7D?a=commitdiff_plain;h=880c51687ab72a7c141c312931e527b8f1055f51;p=lemmy.git Merge branch 'main' into feature/mark_post_as_read --- 880c51687ab72a7c141c312931e527b8f1055f51 diff --cc crates/api/src/site.rs index fa8dd5e4,c55ac664..dab45830 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@@ -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); @@@ -165,12 -170,13 +173,14 @@@ 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) @@@ -216,14 -227,19 +231,20 @@@ .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) @@@ -277,10 -304,10 +309,11 @@@ 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) diff --cc crates/api_common/src/lib.rs index 47f4752d,1a644b7d..f2b41be5 --- a/crates/api_common/src/lib.rs +++ b/crates/api_common/src/lib.rs @@@ -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) -> 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) -> 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) -> 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 { blocking(pool, move |conn| Post::read(conn, post_id)) .await? diff --cc crates/api_crud/src/comment/read.rs index 4d144382,f82b9748..12ccbd5a --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@@ -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 = from_opt_str_to_opt_enum(&data.sort); + let listing_type: Option = from_opt_str_to_opt_enum(&data.type_); let community_id = data.community_id; let community_name = data.community_name.to_owned(); diff --cc crates/api_crud/src/post/read.rs index d44f029a,14c6cad5..d49764bb --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@@ -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 = from_opt_str_to_opt_enum(&data.sort); + let listing_type: Option = from_opt_str_to_opt_enum(&data.type_); let page = data.page; let limit = data.limit; @@@ -120,11 -111,10 +116,11 @@@ 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) diff --cc crates/api_crud/src/user/read.rs index e0e3808c,39431128..132bdd78 --- a/crates/api_crud/src/user/read.rs +++ b/crates/api_crud/src/user/read.rs @@@ -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 = from_opt_str_to_opt_enum(&data.sort); let username = data .username @@@ -71,10 -68,9 +67,10 @@@ 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) diff --cc crates/db_views/src/post_view.rs index 8a533412,fb8f0733..1d8ea80e --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@@ -163,10 -163,10 +163,10 @@@ pub struct PostQueryBuilder<'a> my_person_id: Option, search_term: Option, url_search: Option, - show_nsfw: bool, - show_bot_accounts: bool, - show_read_posts: bool, - saved_only: bool, + show_nsfw: Option, + show_bot_accounts: Option, ++ show_read_posts: Option, + saved_only: Option, - unread_only: Option, page: Option, limit: Option, } @@@ -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, } @@@ -242,13 -242,8 +242,13 @@@ 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>(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>(mut self, saved_only: T) -> Self { + self.saved_only = saved_only.get_optional(); self } @@@ -367,15 -364,15 +369,15 @@@ 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( diff --cc crates/routes/src/feeds.rs index 2ed58673,a4f6a45a..06b9ac13 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@@ -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) + .show_read_posts(show_read_posts) - .sort(sort_type) + .sort(*sort_type) .list()?; let items = create_post_items(posts)?;