From a42ac3e58adcae58cf07cd6bd3abf33adbc5c965 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 9 Apr 2021 16:09:58 -0400 Subject: [PATCH 1/8] Add creator id to search. Fixes #765 --- crates/api/src/site.rs | 11 +++++++++++ crates/api_common/src/site.rs | 1 + 2 files changed, 12 insertions(+) diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index aae400ca7..a6f09f59b 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -159,6 +159,8 @@ impl Perform for Search { let sort = SortType::from_str(&data.sort)?; let community_id = data.community_id; let community_name = data.community_name.to_owned(); + let community_name_2 = data.community_name.to_owned(); + let creator_id = data.creator_id; match type_ { SearchType::Posts => { posts = blocking(context.pool(), move |conn| { @@ -168,6 +170,7 @@ impl Perform for Search { .show_bot_accounts(show_bot_accounts) .community_id(community_id) .community_name(community_name) + .creator_id(creator_id) .my_person_id(person_id) .search_term(q) .page(page) @@ -182,6 +185,9 @@ impl Perform for Search { .sort(&sort) .search_term(q) .show_bot_accounts(show_bot_accounts) + .community_id(community_id) + .community_name(community_name) + .creator_id(creator_id) .my_person_id(person_id) .page(page) .limit(limit) @@ -220,6 +226,7 @@ impl Perform for Search { .show_bot_accounts(show_bot_accounts) .community_id(community_id) .community_name(community_name) + .creator_id(creator_id) .my_person_id(person_id) .search_term(q) .page(page) @@ -236,6 +243,9 @@ impl Perform for Search { .sort(&sort) .search_term(q) .show_bot_accounts(show_bot_accounts) + .community_id(community_id) + .community_name(community_name_2) + .creator_id(creator_id) .my_person_id(person_id) .page(page) .limit(limit) @@ -279,6 +289,7 @@ impl Perform for Search { .my_person_id(person_id) .community_id(community_id) .community_name(community_name) + .creator_id(creator_id) .url_search(q) .page(page) .limit(limit) diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index ffee7ba86..6d3ebf63e 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -25,6 +25,7 @@ pub struct Search { pub type_: String, pub community_id: Option, pub community_name: Option, + pub creator_id: Option, pub sort: String, pub page: Option, pub limit: Option, From c86f5472fb384a26d9c13825691211145b90adb1 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 9 Apr 2021 16:35:51 -0400 Subject: [PATCH 2/8] Adding listing_type to search. --- crates/api/src/site.rs | 22 +++++++++++++++++----- crates/api_common/src/site.rs | 1 + crates/api_crud/src/comment/read.rs | 4 ++-- crates/db_views/src/comment_view.rs | 6 +++--- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index a6f09f59b..481f99687 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -13,7 +13,7 @@ use lemmy_api_common::{ user_show_nsfw, }; use lemmy_apub::fetcher::search::search_by_apub_id; -use lemmy_db_queries::{source::site::Site_, Crud, SearchType, SortType}; +use lemmy_db_queries::{source::site::Site_, Crud, ListingType, SearchType, SortType}; use lemmy_db_schema::source::{moderator::*, site::Site}; use lemmy_db_views::{ comment_view::CommentQueryBuilder, @@ -144,8 +144,6 @@ impl Perform for Search { let person_id = local_user_view.map(|u| u.person.id); - let type_ = SearchType::from_str(&data.type_)?; - let mut posts = Vec::new(); let mut comments = Vec::new(); let mut communities = Vec::new(); @@ -157,9 +155,10 @@ impl Perform for Search { let page = data.page; let limit = data.limit; let sort = SortType::from_str(&data.sort)?; + let type_ = SearchType::from_str(&data.type_)?; + let listing_type = ListingType::from_str(&data.listing_type)?; let community_id = data.community_id; let community_name = data.community_name.to_owned(); - let community_name_2 = data.community_name.to_owned(); let creator_id = data.creator_id; match type_ { SearchType::Posts => { @@ -168,6 +167,8 @@ impl Perform for Search { .sort(&sort) .show_nsfw(show_nsfw) .show_bot_accounts(show_bot_accounts) + .listing_type(&listing_type) + .show_nsfw(true) .community_id(community_id) .community_name(community_name) .creator_id(creator_id) @@ -183,6 +184,7 @@ impl Perform for Search { comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(&conn) .sort(&sort) + .listing_type(&listing_type) .search_term(q) .show_bot_accounts(show_bot_accounts) .community_id(community_id) @@ -199,6 +201,7 @@ impl Perform for Search { communities = blocking(context.pool(), move |conn| { CommunityQueryBuilder::create(conn) .sort(&sort) + .listing_type(&listing_type) .search_term(q) .my_person_id(person_id) .page(page) @@ -224,6 +227,8 @@ impl Perform for Search { .sort(&sort) .show_nsfw(show_nsfw) .show_bot_accounts(show_bot_accounts) + .listing_type(&listing_type) + .show_nsfw(true) .community_id(community_id) .community_name(community_name) .creator_id(creator_id) @@ -237,14 +242,17 @@ impl Perform for Search { let q = data.q.to_owned(); let sort = SortType::from_str(&data.sort)?; + let listing_type = ListingType::from_str(&data.listing_type)?; + let community_name = data.community_name.to_owned(); comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(conn) .sort(&sort) + .listing_type(&listing_type) .search_term(q) .show_bot_accounts(show_bot_accounts) .community_id(community_id) - .community_name(community_name_2) + .community_name(community_name) .creator_id(creator_id) .my_person_id(person_id) .page(page) @@ -255,10 +263,12 @@ impl Perform for Search { let q = data.q.to_owned(); let sort = SortType::from_str(&data.sort)?; + let listing_type = ListingType::from_str(&data.listing_type)?; communities = blocking(context.pool(), move |conn| { CommunityQueryBuilder::create(conn) .sort(&sort) + .listing_type(&listing_type) .search_term(q) .my_person_id(person_id) .page(page) @@ -286,6 +296,8 @@ impl Perform for Search { .sort(&sort) .show_nsfw(show_nsfw) .show_bot_accounts(show_bot_accounts) + .listing_type(&listing_type) + .show_nsfw(true) .my_person_id(person_id) .community_id(community_id) .community_name(community_name) diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 6d3ebf63e..b244b58a9 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -27,6 +27,7 @@ pub struct Search { pub community_name: Option, pub creator_id: Option, pub sort: String, + pub listing_type: String, pub page: Option, pub limit: Option, pub auth: Option, diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index 4d1443820..70fe964c3 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -27,7 +27,7 @@ impl PerformCrud for GetComments { let show_bot_accounts = user_show_bot_accounts(&local_user_view); let person_id = local_user_view.map(|u| u.person.id); - let type_ = ListingType::from_str(&data.type_)?; + let listing_type = ListingType::from_str(&data.type_)?; let sort = SortType::from_str(&data.sort)?; let community_id = data.community_id; @@ -37,7 +37,7 @@ impl PerformCrud for GetComments { let limit = data.limit; let comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(conn) - .listing_type(type_) + .listing_type(&listing_type) .sort(&sort) .saved_only(saved_only) .community_id(community_id) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index b85b1c77a..cf12442f6 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -172,7 +172,7 @@ impl CommentView { pub struct CommentQueryBuilder<'a> { conn: &'a PgConnection, - listing_type: ListingType, + listing_type: &'a ListingType, sort: &'a SortType, community_id: Option, community_name: Option, @@ -192,7 +192,7 @@ impl<'a> CommentQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { CommentQueryBuilder { conn, - listing_type: ListingType::All, + listing_type: &ListingType::All, sort: &SortType::New, community_id: None, community_name: None, @@ -209,7 +209,7 @@ impl<'a> CommentQueryBuilder<'a> { } } - pub fn listing_type(mut self, listing_type: ListingType) -> Self { + pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self { self.listing_type = listing_type; self } From f8cd6fd445daf9040e4283313c81bdf614d89746 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 14 Apr 2021 23:37:51 -0400 Subject: [PATCH 3/8] Making more fields optional in the API. - Fixes #1569 --- crates/api/src/community.rs | 2 +- crates/api/src/local_user.rs | 12 ++-- crates/api/src/site.rs | 60 ++++++++-------- crates/api_common/src/comment.rs | 8 +-- crates/api_common/src/community.rs | 8 +-- crates/api_common/src/person.rs | 20 +++--- crates/api_common/src/post.rs | 14 ++-- crates/api_common/src/site.rs | 22 +++--- crates/api_crud/src/comment/read.rs | 18 ++--- crates/api_crud/src/community/read.rs | 16 +++-- crates/api_crud/src/community/update.rs | 11 +-- crates/api_crud/src/post/read.rs | 19 ++--- crates/api_crud/src/post/update.rs | 12 ++-- crates/api_crud/src/site/read.rs | 6 +- crates/api_crud/src/site/update.rs | 13 ++-- crates/api_crud/src/user/read.rs | 19 ++--- crates/apub/src/objects/post.rs | 4 +- .../src/aggregates/site_aggregates.rs | 8 +-- crates/db_queries/src/lib.rs | 16 ++++- crates/db_schema/src/source/post.rs | 2 +- crates/db_schema/src/source/site.rs | 8 +-- crates/db_views/src/comment_view.rs | 58 +++++++-------- crates/db_views/src/post_view.rs | 70 ++++++++++--------- crates/db_views/src/private_message_view.rs | 10 +-- crates/db_views_actor/src/community_view.rs | 40 ++++++----- .../db_views_actor/src/person_mention_view.rs | 20 +++--- crates/db_views_actor/src/person_view.rs | 10 +-- crates/routes/src/feeds.rs | 21 +++--- 28 files changed, 258 insertions(+), 269 deletions(-) diff --git a/crates/api/src/community.rs b/crates/api/src/community.rs index fec943343..4d519832f 100644 --- a/crates/api/src/community.rs +++ b/crates/api/src/community.rs @@ -171,7 +171,7 @@ impl Perform for BanFromCommunity { } // Remove/Restore their data if that's desired - if data.remove_data { + if data.remove_data.unwrap_or_default() { // Posts blocking(context.pool(), move |conn: &'_ _| { Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true) diff --git a/crates/api/src/local_user.rs b/crates/api/src/local_user.rs index b273b5820..5cf4f0627 100644 --- a/crates/api/src/local_user.rs +++ b/crates/api/src/local_user.rs @@ -16,6 +16,7 @@ use lemmy_api_common::{ use lemmy_db_queries::{ diesel_option_overwrite, diesel_option_overwrite_to_url, + from_opt_str_to_opt_enum, source::{ comment::Comment_, local_user::LocalUser_, @@ -68,7 +69,6 @@ use lemmy_websocket::{ LemmyContext, UserOperation, }; -use std::str::FromStr; #[async_trait::async_trait(?Send)] impl Perform for Login { @@ -397,7 +397,7 @@ impl Perform for BanPerson { } // Remove their data if that's desired - if data.remove_data { + if data.remove_data.unwrap_or_default() { // Posts blocking(context.pool(), move |conn: &'_ _| { Post::update_removed_for_creator(conn, banned_person_id, None, true) @@ -462,7 +462,7 @@ impl Perform for GetReplies { let data: &GetReplies = &self; let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?; - let sort = SortType::from_str(&data.sort)?; + let sort: Option = from_opt_str_to_opt_enum(&data.sort); let page = data.page; let limit = data.limit; @@ -472,7 +472,7 @@ impl Perform for GetReplies { let replies = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(conn) - .sort(&sort) + .sort(sort) .unread_only(unread_only) .recipient_id(person_id) .show_bot_accounts(show_bot_accounts) @@ -499,7 +499,7 @@ impl Perform for GetPersonMentions { let data: &GetPersonMentions = &self; let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?; - let sort = SortType::from_str(&data.sort)?; + let sort: Option = from_opt_str_to_opt_enum(&data.sort); let page = data.page; let limit = data.limit; @@ -509,7 +509,7 @@ impl Perform for GetPersonMentions { PersonMentionQueryBuilder::create(conn) .recipient_id(person_id) .my_person_id(person_id) - .sort(&sort) + .sort(sort) .unread_only(unread_only) .page(page) .limit(limit) diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index 481f99687..524d2a91f 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -13,7 +13,14 @@ use lemmy_api_common::{ user_show_nsfw, }; use lemmy_apub::fetcher::search::search_by_apub_id; -use lemmy_db_queries::{source::site::Site_, Crud, ListingType, 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, @@ -45,7 +52,6 @@ use lemmy_utils::{ }; use lemmy_websocket::LemmyContext; use log::debug; -use std::str::FromStr; #[async_trait::async_trait(?Send)] impl Perform for GetModlog { @@ -154,21 +160,20 @@ impl Perform for Search { let q = data.q.to_owned(); let page = data.page; let limit = data.limit; - let sort = SortType::from_str(&data.sort)?; - let type_ = SearchType::from_str(&data.type_)?; - let listing_type = ListingType::from_str(&data.listing_type)?; + let sort: Option = from_opt_str_to_opt_enum(&data.sort); + let listing_type: Option = from_opt_str_to_opt_enum(&data.listing_type); + let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All); let community_id = data.community_id; let community_name = data.community_name.to_owned(); let creator_id = data.creator_id; - match type_ { + match search_type { 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) - .listing_type(&listing_type) - .show_nsfw(true) + .listing_type(listing_type) .community_id(community_id) .community_name(community_name) .creator_id(creator_id) @@ -183,8 +188,8 @@ impl Perform for Search { SearchType::Comments => { comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(&conn) - .sort(&sort) - .listing_type(&listing_type) + .sort(sort) + .listing_type(listing_type) .search_term(q) .show_bot_accounts(show_bot_accounts) .community_id(community_id) @@ -200,8 +205,8 @@ impl Perform for Search { SearchType::Communities => { communities = blocking(context.pool(), move |conn| { CommunityQueryBuilder::create(conn) - .sort(&sort) - .listing_type(&listing_type) + .sort(sort) + .listing_type(listing_type) .search_term(q) .my_person_id(person_id) .page(page) @@ -213,7 +218,7 @@ impl Perform for Search { SearchType::Users => { users = blocking(context.pool(), move |conn| { PersonQueryBuilder::create(conn) - .sort(&sort) + .sort(sort) .search_term(q) .page(page) .limit(limit) @@ -224,11 +229,10 @@ impl Perform for Search { SearchType::All => { posts = blocking(context.pool(), move |conn| { PostQueryBuilder::create(conn) - .sort(&sort) + .sort(sort) .show_nsfw(show_nsfw) .show_bot_accounts(show_bot_accounts) - .listing_type(&listing_type) - .show_nsfw(true) + .listing_type(listing_type) .community_id(community_id) .community_name(community_name) .creator_id(creator_id) @@ -241,14 +245,12 @@ impl Perform for Search { .await??; let q = data.q.to_owned(); - let sort = SortType::from_str(&data.sort)?; - let listing_type = ListingType::from_str(&data.listing_type)?; let community_name = data.community_name.to_owned(); comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(conn) - .sort(&sort) - .listing_type(&listing_type) + .sort(sort) + .listing_type(listing_type) .search_term(q) .show_bot_accounts(show_bot_accounts) .community_id(community_id) @@ -262,13 +264,11 @@ impl Perform for Search { .await??; let q = data.q.to_owned(); - let sort = SortType::from_str(&data.sort)?; - let listing_type = ListingType::from_str(&data.listing_type)?; communities = blocking(context.pool(), move |conn| { CommunityQueryBuilder::create(conn) - .sort(&sort) - .listing_type(&listing_type) + .sort(sort) + .listing_type(listing_type) .search_term(q) .my_person_id(person_id) .page(page) @@ -278,11 +278,10 @@ impl Perform for Search { .await??; let q = data.q.to_owned(); - let sort = SortType::from_str(&data.sort)?; users = blocking(context.pool(), move |conn| { PersonQueryBuilder::create(conn) - .sort(&sort) + .sort(sort) .search_term(q) .page(page) .limit(limit) @@ -293,11 +292,10 @@ impl Perform for Search { 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) - .listing_type(&listing_type) - .show_nsfw(true) + .listing_type(listing_type) .my_person_id(person_id) .community_id(community_id) .community_name(community_name) @@ -313,7 +311,7 @@ impl Perform for Search { // Return the jwt Ok(SearchResponse { - type_: data.type_.to_owned(), + type_: search_type.to_string(), comments, posts, communities, diff --git a/crates/api_common/src/comment.rs b/crates/api_common/src/comment.rs index 1457f181a..ce5182cf3 100644 --- a/crates/api_common/src/comment.rs +++ b/crates/api_common/src/comment.rs @@ -5,8 +5,8 @@ use serde::{Deserialize, Serialize}; #[derive(Deserialize)] pub struct CreateComment { pub content: String, - pub parent_id: Option, pub post_id: PostId, + pub parent_id: Option, pub form_id: Option, pub auth: String, } @@ -64,13 +64,13 @@ pub struct CreateCommentLike { #[derive(Deserialize)] pub struct GetComments { - pub type_: String, - pub sort: String, + pub type_: Option, + pub sort: Option, pub page: Option, pub limit: Option, pub community_id: Option, pub community_name: Option, - pub saved_only: bool, + pub saved_only: Option, pub auth: Option, } diff --git a/crates/api_common/src/community.rs b/crates/api_common/src/community.rs index a03440447..129b149ad 100644 --- a/crates/api_common/src/community.rs +++ b/crates/api_common/src/community.rs @@ -39,8 +39,8 @@ pub struct CommunityResponse { #[derive(Deserialize, Debug)] pub struct ListCommunities { - pub type_: String, - pub sort: String, + pub type_: Option, + pub sort: Option, pub page: Option, pub limit: Option, pub auth: Option, @@ -56,7 +56,7 @@ pub struct BanFromCommunity { pub community_id: CommunityId, pub person_id: PersonId, pub ban: bool, - pub remove_data: bool, + pub remove_data: Option, pub reason: Option, pub expires: Option, pub auth: String, @@ -84,7 +84,7 @@ pub struct AddModToCommunityResponse { #[derive(Deserialize)] pub struct EditCommunity { pub community_id: CommunityId, - pub title: String, + pub title: Option, pub description: Option, pub icon: Option, pub banner: Option, diff --git a/crates/api_common/src/person.rs b/crates/api_common/src/person.rs index 5473a4820..8349f2a1f 100644 --- a/crates/api_common/src/person.rs +++ b/crates/api_common/src/person.rs @@ -21,10 +21,10 @@ use lemmy_db_schema::{CommunityId, PersonId, PersonMentionId, PrivateMessageId}; #[derive(Deserialize)] pub struct Register { pub username: String, - pub email: Option, pub password: String, pub password_verify: String, pub show_nsfw: bool, + pub email: Option, pub captcha_uuid: Option, pub captcha_answer: Option, } @@ -80,13 +80,13 @@ pub struct LoginResponse { #[derive(Deserialize)] pub struct GetPersonDetails { - pub person_id: Option, + pub person_id: Option, // One of these two are required pub username: Option, - pub sort: String, + pub sort: Option, pub page: Option, pub limit: Option, pub community_id: Option, - pub saved_only: bool, + pub saved_only: Option, pub auth: Option, } @@ -130,7 +130,7 @@ pub struct AddAdminResponse { pub struct BanPerson { pub person_id: PersonId, pub ban: bool, - pub remove_data: bool, + pub remove_data: Option, pub reason: Option, pub expires: Option, pub auth: String, @@ -144,19 +144,19 @@ pub struct BanPersonResponse { #[derive(Deserialize)] pub struct GetReplies { - pub sort: String, + pub sort: Option, pub page: Option, pub limit: Option, - pub unread_only: bool, + pub unread_only: Option, pub auth: String, } #[derive(Deserialize)] pub struct GetPersonMentions { - pub sort: String, + pub sort: Option, pub page: Option, pub limit: Option, - pub unread_only: bool, + pub unread_only: Option, pub auth: String, } @@ -223,7 +223,7 @@ pub struct MarkPrivateMessageAsRead { #[derive(Deserialize)] pub struct GetPrivateMessages { - pub unread_only: bool, + pub unread_only: Option, pub page: Option, pub limit: Option, pub auth: String, diff --git a/crates/api_common/src/post.rs b/crates/api_common/src/post.rs index 727859bc4..bb6b36249 100644 --- a/crates/api_common/src/post.rs +++ b/crates/api_common/src/post.rs @@ -14,10 +14,10 @@ use url::Url; #[derive(Deserialize, Debug)] pub struct CreatePost { pub name: String, + pub community_id: CommunityId, pub url: Option, pub body: Option, - pub nsfw: bool, - pub community_id: CommunityId, + pub nsfw: Option, pub auth: String, } @@ -43,13 +43,13 @@ pub struct GetPostResponse { #[derive(Deserialize, Debug)] pub struct GetPosts { - pub type_: String, - pub sort: String, + pub type_: Option, + pub sort: Option, pub page: Option, pub limit: Option, pub community_id: Option, pub community_name: Option, - pub saved_only: bool, + pub saved_only: Option, pub auth: Option, } @@ -68,10 +68,10 @@ pub struct CreatePostLike { #[derive(Deserialize)] pub struct EditPost { pub post_id: PostId, - pub name: String, + pub name: Option, pub url: Option, pub body: Option, - pub nsfw: bool, + pub nsfw: Option, pub auth: String, } diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index b244b58a9..e5c167f42 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -22,12 +22,12 @@ use serde::{Deserialize, Serialize}; #[derive(Deserialize, Debug)] pub struct Search { pub q: String, - pub type_: String, pub community_id: Option, pub community_name: Option, pub creator_id: Option, - pub sort: String, - pub listing_type: String, + pub type_: Option, + pub sort: Option, + pub listing_type: Option, pub page: Option, pub limit: Option, pub auth: Option, @@ -70,23 +70,23 @@ pub struct CreateSite { pub description: Option, pub icon: Option, pub banner: Option, - pub enable_downvotes: bool, - pub open_registration: bool, - pub enable_nsfw: bool, - pub community_creation_admin_only: bool, + pub enable_downvotes: Option, + pub open_registration: Option, + pub enable_nsfw: Option, + pub community_creation_admin_only: Option, pub auth: String, } #[derive(Deserialize)] pub struct EditSite { - pub name: String, + pub name: Option, pub sidebar: Option, pub description: Option, pub icon: Option, pub banner: Option, - pub enable_downvotes: bool, - pub open_registration: bool, - pub enable_nsfw: bool, + pub enable_downvotes: Option, + pub open_registration: Option, + pub enable_nsfw: Option, pub community_creation_admin_only: Option, pub auth: String, } diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index 70fe964c3..ceed00959 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -1,16 +1,10 @@ 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, user_show_bot_accounts}; +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; -use std::str::FromStr; #[async_trait::async_trait(?Send)] impl PerformCrud for GetComments { @@ -27,8 +21,8 @@ impl PerformCrud for GetComments { let show_bot_accounts = user_show_bot_accounts(&local_user_view); let person_id = local_user_view.map(|u| u.person.id); - let listing_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(); @@ -37,8 +31,8 @@ impl PerformCrud for GetComments { let limit = data.limit; let comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(conn) - .listing_type(&listing_type) - .sort(&sort) + .listing_type(listing_type) + .sort(sort) .saved_only(saved_only) .community_id(community_id) .community_name(community_name) diff --git a/crates/api_crud/src/community/read.rs b/crates/api_crud/src/community/read.rs index 422161ae1..7836878e9 100644 --- a/crates/api_crud/src/community/read.rs +++ b/crates/api_crud/src/community/read.rs @@ -1,7 +1,12 @@ use crate::PerformCrud; use actix_web::web::Data; use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt}; -use lemmy_db_queries::{source::community::Community_, ListingType, SortType}; +use lemmy_db_queries::{ + from_opt_str_to_opt_enum, + source::community::Community_, + ListingType, + SortType, +}; use lemmy_db_schema::source::community::*; use lemmy_db_views_actor::{ community_moderator_view::CommunityModeratorView, @@ -9,7 +14,6 @@ use lemmy_db_views_actor::{ }; use lemmy_utils::{ApiError, ConnectionId, LemmyError}; use lemmy_websocket::{messages::GetCommunityUsersOnline, LemmyContext}; -use std::str::FromStr; #[async_trait::async_trait(?Send)] impl PerformCrud for GetCommunity { @@ -86,15 +90,15 @@ impl PerformCrud for ListCommunities { None => false, }; - 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; let communities = blocking(context.pool(), move |conn| { CommunityQueryBuilder::create(conn) - .listing_type(&type_) - .sort(&sort) + .listing_type(listing_type) + .sort(sort) .show_nsfw(show_nsfw) .my_person_id(person_id) .page(page) diff --git a/crates/api_crud/src/community/update.rs b/crates/api_crud/src/community/update.rs index 644581a5d..f28f77119 100644 --- a/crates/api_crud/src/community/update.rs +++ b/crates/api_crud/src/community/update.rs @@ -16,12 +16,7 @@ use lemmy_db_views_actor::{ community_moderator_view::CommunityModeratorView, community_view::CommunityView, }; -use lemmy_utils::{ - utils::{check_slurs, check_slurs_opt}, - ApiError, - ConnectionId, - LemmyError, -}; +use lemmy_utils::{utils::check_slurs_opt, ApiError, ConnectionId, LemmyError}; use lemmy_websocket::{LemmyContext, UserOperationCrud}; #[async_trait::async_trait(?Send)] @@ -36,7 +31,7 @@ impl PerformCrud for EditCommunity { let data: &EditCommunity = &self; let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?; - check_slurs(&data.title)?; + check_slurs_opt(&data.title)?; check_slurs_opt(&data.description)?; // Verify its a mod (only mods can edit it) @@ -61,7 +56,7 @@ impl PerformCrud for EditCommunity { let community_form = CommunityForm { name: read_community.name, - title: data.title.to_owned(), + title: data.title.to_owned().unwrap_or(read_community.title), description: data.description.to_owned(), icon, banner, diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index d2231d24b..858a878f5 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -1,13 +1,7 @@ use crate::PerformCrud; use actix_web::web::Data; -use lemmy_api_common::{ - blocking, - get_local_user_view_from_jwt_opt, - post::*, - user_show_bot_accounts, - user_show_nsfw, -}; -use lemmy_db_queries::{ListingType, SortType}; +use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, post::*, user_show_bot_accounts, user_show_nsfw}; +use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType}; use lemmy_db_views::{ comment_view::CommentQueryBuilder, post_view::{PostQueryBuilder, PostView}, @@ -18,7 +12,6 @@ use lemmy_db_views_actor::{ }; use lemmy_utils::{ApiError, ConnectionId, LemmyError}; use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext}; -use std::str::FromStr; #[async_trait::async_trait(?Send)] impl PerformCrud for GetPost { @@ -101,8 +94,8 @@ impl PerformCrud for GetPosts { let show_nsfw = user_show_nsfw(&local_user_view); let show_bot_accounts = user_show_bot_accounts(&local_user_view); - 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; @@ -112,8 +105,8 @@ impl PerformCrud for GetPosts { 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) .community_id(community_id) diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 8ca0dcd1d..ca7634d5a 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -7,7 +7,7 @@ use lemmy_db_schema::{naive_now, source::post::*}; use lemmy_db_views::post_view::PostView; use lemmy_utils::{ request::fetch_iframely_and_pictrs_data, - utils::{check_slurs, check_slurs_opt, is_valid_post_title}, + utils::{check_slurs_opt, is_valid_post_title}, ApiError, ConnectionId, LemmyError, @@ -26,11 +26,13 @@ impl PerformCrud for EditPost { let data: &EditPost = &self; let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?; - check_slurs(&data.name)?; + check_slurs_opt(&data.name)?; check_slurs_opt(&data.body)?; - if !is_valid_post_title(&data.name) { - return Err(ApiError::err("invalid_post_title").into()); + if let Some(name) = &data.name { + if !is_valid_post_title(name) { + return Err(ApiError::err("invalid_post_title").into()); + } } let post_id = data.post_id; @@ -56,7 +58,7 @@ impl PerformCrud for EditPost { let post_form = PostForm { creator_id: orig_post.creator_id.to_owned(), community_id: orig_post.community_id, - name: data.name.trim().to_owned(), + name: data.name.to_owned().unwrap_or(orig_post.name), url: data_url.map(|u| u.to_owned().into()), body: data.body.to_owned(), nsfw: data.nsfw, diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index e378b26b0..fb409f1ca 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -47,9 +47,9 @@ impl PerformCrud for GetSite { description: None, icon: None, banner: None, - enable_downvotes: true, - open_registration: true, - enable_nsfw: true, + enable_downvotes: None, + open_registration: None, + enable_nsfw: None, auth: login_response.jwt, community_creation_admin_only: false, }; diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 58f408492..14716e782 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -18,12 +18,7 @@ use lemmy_db_schema::{ source::site::{Site, SiteForm}, }; use lemmy_db_views::site_view::SiteView; -use lemmy_utils::{ - utils::{check_slurs, check_slurs_opt}, - ApiError, - ConnectionId, - LemmyError, -}; +use lemmy_utils::{utils::check_slurs_opt, ApiError, ConnectionId, LemmyError}; use lemmy_websocket::{messages::SendAllMessage, LemmyContext, UserOperationCrud}; #[async_trait::async_trait(?Send)] @@ -37,7 +32,7 @@ impl PerformCrud for EditSite { let data: &EditSite = &self; let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?; - check_slurs(&data.name)?; + check_slurs_opt(&data.name)?; check_slurs_opt(&data.description)?; // Make sure user is an admin @@ -55,12 +50,12 @@ impl PerformCrud for EditSite { } let site_form = SiteForm { - name: data.name.to_owned(), + creator_id: found_site.creator_id, + name: data.name.to_owned().unwrap_or(found_site.name), sidebar, description, icon, banner, - creator_id: found_site.creator_id, updated: Some(naive_now()), enable_downvotes: data.enable_downvotes, open_registration: data.open_registration, diff --git a/crates/api_crud/src/user/read.rs b/crates/api_crud/src/user/read.rs index 24ac2f61e..f32764160 100644 --- a/crates/api_crud/src/user/read.rs +++ b/crates/api_crud/src/user/read.rs @@ -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, -}; -use lemmy_db_queries::{source::person::Person_, SortType}; +use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*, user_show_nsfw, user_show_bot_accounts}; +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::{ @@ -17,7 +11,6 @@ use lemmy_db_views_actor::{ }; use lemmy_utils::{ApiError, ConnectionId, LemmyError}; use lemmy_websocket::LemmyContext; -use std::str::FromStr; #[async_trait::async_trait(?Send)] impl PerformCrud for GetPersonDetails { @@ -34,7 +27,7 @@ impl PerformCrud for GetPersonDetails { let show_nsfw = user_show_nsfw(&local_user_view); let show_bot_accounts = user_show_bot_accounts(&local_user_view); - let sort = SortType::from_str(&data.sort)?; + let sort: Option = from_opt_str_to_opt_enum(&data.sort); let username = data .username @@ -69,7 +62,7 @@ impl PerformCrud for GetPersonDetails { 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) .saved_only(saved_only) @@ -81,7 +74,7 @@ impl PerformCrud for GetPersonDetails { let mut comments_query = CommentQueryBuilder::create(conn) .my_person_id(person_id) .show_bot_accounts(show_bot_accounts) - .sort(&sort) + .sort(sort) .saved_only(saved_only) .community_id(community_id) .page(page) @@ -89,7 +82,7 @@ impl PerformCrud for GetPersonDetails { // If its saved only, you don't care what creator it was // Or, if its not saved, then you only want it for that specific creator - if !saved_only { + if !saved_only.unwrap_or_default() { posts_query = posts_query.creator_id(person_details_id); comments_query = comments_query.creator_id(person_details_id); } diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 8bbc07605..1c984d84d 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -230,8 +230,8 @@ impl FromApubToForm for PostForm { .as_ref() .map(|u| u.to_owned().naive_local()), deleted: None, - nsfw: ext.sensitive.unwrap_or(false), - stickied: ext.stickied.or(Some(false)), + nsfw: ext.sensitive, + stickied: ext.stickied, embed_title: iframely_title, embed_description: iframely_description, embed_html: iframely_html, diff --git a/crates/db_queries/src/aggregates/site_aggregates.rs b/crates/db_queries/src/aggregates/site_aggregates.rs index 934a73051..109e4bd65 100644 --- a/crates/db_queries/src/aggregates/site_aggregates.rs +++ b/crates/db_queries/src/aggregates/site_aggregates.rs @@ -49,14 +49,14 @@ mod tests { let site_form = SiteForm { name: "test_site".into(), + creator_id: inserted_person.id, sidebar: None, description: None, icon: None, banner: None, - creator_id: inserted_person.id, - enable_downvotes: true, - open_registration: true, - enable_nsfw: true, + enable_downvotes: None, + open_registration: None, + enable_nsfw: None, updated: None, community_creation_admin_only: Some(false), }; diff --git a/crates/db_queries/src/lib.rs b/crates/db_queries/src/lib.rs index 285ba3323..fd5abf504 100644 --- a/crates/db_queries/src/lib.rs +++ b/crates/db_queries/src/lib.rs @@ -163,7 +163,7 @@ pub fn get_database_url_from_env() -> Result { env::var("LEMMY_DATABASE_URL") } -#[derive(EnumString, ToString, Debug, Serialize, Deserialize)] +#[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)] pub enum SortType { Active, Hot, @@ -177,7 +177,7 @@ pub enum SortType { NewComments, } -#[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)] +#[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)] pub enum ListingType { All, Local, @@ -185,7 +185,7 @@ pub enum ListingType { Community, } -#[derive(EnumString, ToString, Debug, Serialize, Deserialize)] +#[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone, Copy)] pub enum SearchType { All, Comments, @@ -195,6 +195,16 @@ pub enum SearchType { Url, } +pub fn from_opt_str_to_opt_enum(opt: &Option) -> Option { + match opt { + Some(t) => match T::from_str(&t) { + Ok(r) => Some(r), + Err(_) => None, + }, + None => None, + } +} + pub fn fuzzy_search(q: &str) -> String { let replaced = q.replace(" ", "%"); format!("%{}%", replaced) diff --git a/crates/db_schema/src/source/post.rs b/crates/db_schema/src/source/post.rs index 2581db95c..536dd9606 100644 --- a/crates/db_schema/src/source/post.rs +++ b/crates/db_schema/src/source/post.rs @@ -37,7 +37,7 @@ pub struct PostForm { pub name: String, pub creator_id: PersonId, pub community_id: CommunityId, - pub nsfw: bool, + pub nsfw: Option, pub url: Option, pub body: Option, pub removed: Option, diff --git a/crates/db_schema/src/source/site.rs b/crates/db_schema/src/source/site.rs index 67bba9933..19c41bf73 100644 --- a/crates/db_schema/src/source/site.rs +++ b/crates/db_schema/src/source/site.rs @@ -23,12 +23,12 @@ pub struct Site { #[table_name = "site"] pub struct SiteForm { pub name: String, - pub sidebar: Option>, pub creator_id: PersonId, + pub sidebar: Option>, pub updated: Option, - pub enable_downvotes: bool, - pub open_registration: bool, - pub enable_nsfw: bool, + pub enable_downvotes: Option, + pub open_registration: Option, + pub enable_nsfw: Option, // when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column. pub icon: Option>, pub banner: Option>, diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index cf12442f6..8c6d44afd 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -172,8 +172,8 @@ impl CommentView { pub struct CommentQueryBuilder<'a> { conn: &'a PgConnection, - listing_type: &'a ListingType, - sort: &'a SortType, + listing_type: Option, + sort: Option, community_id: Option, community_name: Option, post_id: Option, @@ -181,9 +181,9 @@ pub struct CommentQueryBuilder<'a> { recipient_id: Option, my_person_id: Option, search_term: Option, - saved_only: bool, - unread_only: bool, - show_bot_accounts: bool, + saved_only: Option, + unread_only: Option, + show_bot_accounts: Option, page: Option, limit: Option, } @@ -192,8 +192,8 @@ impl<'a> CommentQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { CommentQueryBuilder { conn, - listing_type: &ListingType::All, - sort: &SortType::New, + listing_type: None, + sort: None, community_id: None, community_name: None, post_id: None, @@ -201,21 +201,21 @@ impl<'a> CommentQueryBuilder<'a> { recipient_id: None, my_person_id: None, search_term: None, - saved_only: false, - unread_only: false, - show_bot_accounts: true, + saved_only: None, + unread_only: None, + show_bot_accounts: None, page: None, limit: None, } } - pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self { - self.listing_type = listing_type; + pub fn listing_type>(mut self, listing_type: T) -> Self { + self.listing_type = listing_type.get_optional(); self } - pub fn sort(mut self, sort: &'a SortType) -> Self { - self.sort = sort; + pub fn sort>(mut self, sort: T) -> Self { + self.sort = sort.get_optional(); self } @@ -254,13 +254,13 @@ impl<'a> CommentQueryBuilder<'a> { 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 } - pub fn unread_only(mut self, unread_only: bool) -> Self { - self.unread_only = unread_only; + pub fn unread_only>(mut self, unread_only: T) -> Self { + self.unread_only = unread_only.get_optional(); self } @@ -350,7 +350,7 @@ impl<'a> CommentQueryBuilder<'a> { .filter(comment::removed.eq(false)); } - if self.unread_only { + if self.unread_only.unwrap_or_default() { query = query.filter(comment::read.eq(false)); } @@ -376,22 +376,24 @@ impl<'a> CommentQueryBuilder<'a> { query = query.filter(comment::content.ilike(fuzzy_search(&search_term))); }; - query = match self.listing_type { - // ListingType::Subscribed => query.filter(community_follower::subscribed.eq(true)), - ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), - ListingType::Local => query.filter(community::local.eq(true)), - _ => query, - }; + if let Some(listing_type) = self.listing_type { + query = match listing_type { + // ListingType::Subscribed => query.filter(community_follower::subscribed.eq(true)), + ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), + ListingType::Local => query.filter(community::local.eq(true)), + _ => query, + }; + } - if self.saved_only { + if self.saved_only.unwrap_or_default() { query = query.filter(comment_saved::id.is_not_null()); } - if !self.show_bot_accounts { + if !self.show_bot_accounts.unwrap_or(true) { query = query.filter(person::bot_account.eq(false)); }; - query = match self.sort { + query = match self.sort.unwrap_or(SortType::New) { SortType::Hot | SortType::Active => query .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc()) .then_order_by(comment_aggregates::published.desc()), diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 8248bfd29..192517e06 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -155,18 +155,18 @@ impl PostView { pub struct PostQueryBuilder<'a> { conn: &'a PgConnection, - listing_type: &'a ListingType, - sort: &'a SortType, + listing_type: Option, + sort: Option, creator_id: Option, community_id: Option, community_name: Option, my_person_id: Option, search_term: Option, url_search: Option, - show_nsfw: bool, - show_bot_accounts: bool, - saved_only: bool, - unread_only: bool, + show_nsfw: Option, + show_bot_accounts: Option, + saved_only: Option, + unread_only: Option, page: Option, limit: Option, } @@ -175,30 +175,30 @@ impl<'a> PostQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { PostQueryBuilder { conn, - listing_type: &ListingType::All, - sort: &SortType::Hot, + listing_type: None, + sort: None, creator_id: None, community_id: None, community_name: None, my_person_id: None, search_term: None, url_search: None, - show_nsfw: true, - show_bot_accounts: true, - saved_only: false, - unread_only: false, + show_nsfw: None, + show_bot_accounts: None, + saved_only: None, + unread_only: None, page: None, limit: None, } } - pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self { - self.listing_type = listing_type; + pub fn listing_type>(mut self, listing_type: T) -> Self { + self.listing_type = listing_type.get_optional(); self } - pub fn sort(mut self, sort: &'a SortType) -> Self { - self.sort = sort; + pub fn sort>(mut self, sort: T) -> Self { + self.sort = sort.get_optional(); self } @@ -232,18 +232,18 @@ impl<'a> PostQueryBuilder<'a> { self } - pub fn show_nsfw(mut self, show_nsfw: bool) -> Self { - self.show_nsfw = show_nsfw; + pub fn show_nsfw>(mut self, show_nsfw: T) -> Self { + self.show_nsfw = show_nsfw.get_optional(); self } - pub fn show_bot_accounts(mut self, show_bot_accounts: bool) -> Self { + pub fn show_bot_accounts>(mut self, show_bot_accounts: T) -> Self { self.show_bot_accounts = show_bot_accounts; 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 } @@ -315,11 +315,13 @@ impl<'a> PostQueryBuilder<'a> { )) .into_boxed(); - query = match self.listing_type { - ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), - ListingType::Local => query.filter(community::local.eq(true)), - _ => query, - }; + if let Some(listing_type) = self.listing_type { + query = match listing_type { + ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), + ListingType::Local => query.filter(community::local.eq(true)), + _ => query, + }; + } if let Some(community_id) = self.community_id { query = query @@ -352,7 +354,7 @@ impl<'a> PostQueryBuilder<'a> { query = query.filter(post::creator_id.eq(creator_id)); } - if !self.show_nsfw { + if !self.show_nsfw.unwrap_or(true) { query = query .filter(post::nsfw.eq(false)) .filter(community::nsfw.eq(false)); @@ -363,15 +365,15 @@ impl<'a> PostQueryBuilder<'a> { }; // TODO These two might be wrong - if self.saved_only { + if self.saved_only.unwrap_or_default() { query = query.filter(post_saved::id.is_not_null()); }; - if self.unread_only { + if self.unread_only.unwrap_or_default() { query = query.filter(post_read::id.is_not_null()); }; - query = match self.sort { + query = match self.sort.unwrap_or(SortType::Hot) { SortType::Active => query .then_order_by( hot_rank( @@ -522,8 +524,8 @@ mod tests { }; let read_post_listings_with_person = PostQueryBuilder::create(&conn) - .listing_type(&ListingType::Community) - .sort(&SortType::New) + .listing_type(ListingType::Community) + .sort(SortType::New) .show_bot_accounts(false) .community_id(inserted_community.id) .my_person_id(inserted_person.id) @@ -531,8 +533,8 @@ mod tests { .unwrap(); let read_post_listings_no_person = PostQueryBuilder::create(&conn) - .listing_type(&ListingType::Community) - .sort(&SortType::New) + .listing_type(ListingType::Community) + .sort(SortType::New) .community_id(inserted_community.id) .list() .unwrap(); diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index 5cef7364e..b99286687 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -46,7 +46,7 @@ impl PrivateMessageView { pub struct PrivateMessageQueryBuilder<'a> { conn: &'a PgConnection, recipient_id: PersonId, - unread_only: bool, + unread_only: Option, page: Option, limit: Option, } @@ -56,14 +56,14 @@ impl<'a> PrivateMessageQueryBuilder<'a> { PrivateMessageQueryBuilder { conn, recipient_id, - unread_only: false, + unread_only: None, page: None, limit: None, } } - pub fn unread_only(mut self, unread_only: bool) -> Self { - self.unread_only = unread_only; + pub fn unread_only>(mut self, unread_only: T) -> Self { + self.unread_only = unread_only.get_optional(); self } @@ -89,7 +89,7 @@ impl<'a> PrivateMessageQueryBuilder<'a> { .into_boxed(); // If its unread, I only want the ones to me - if self.unread_only { + if self.unread_only.unwrap_or_default() { query = query .filter(private_message::read.eq(false)) .filter(private_message::recipient_id.eq(self.recipient_id)); diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 0ee503a71..63e529412 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -94,10 +94,10 @@ impl CommunityView { pub struct CommunityQueryBuilder<'a> { conn: &'a PgConnection, - listing_type: &'a ListingType, - sort: &'a SortType, + listing_type: Option, + sort: Option, my_person_id: Option, - show_nsfw: bool, + show_nsfw: Option, search_term: Option, page: Option, limit: Option, @@ -108,27 +108,27 @@ impl<'a> CommunityQueryBuilder<'a> { CommunityQueryBuilder { conn, my_person_id: None, - listing_type: &ListingType::All, - sort: &SortType::Hot, - show_nsfw: true, + listing_type: None, + sort: None, + show_nsfw: None, search_term: None, page: None, limit: None, } } - pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self { - self.listing_type = listing_type; + pub fn listing_type>(mut self, listing_type: T) -> Self { + self.listing_type = listing_type.get_optional(); self } - pub fn sort(mut self, sort: &'a SortType) -> Self { - self.sort = sort; + pub fn sort>(mut self, sort: T) -> Self { + self.sort = sort.get_optional(); self } - pub fn show_nsfw(mut self, show_nsfw: bool) -> Self { - self.show_nsfw = show_nsfw; + pub fn show_nsfw>(mut self, show_nsfw: T) -> Self { + self.show_nsfw = show_nsfw.get_optional(); self } @@ -180,7 +180,7 @@ impl<'a> CommunityQueryBuilder<'a> { .or_filter(community::description.ilike(searcher)); }; - match self.sort { + match self.sort.unwrap_or(SortType::Hot) { SortType::New => query = query.order_by(community::published.desc()), SortType::TopAll => query = query.order_by(community_aggregates::subscribers.desc()), SortType::TopMonth => query = query.order_by(community_aggregates::users_active_month.desc()), @@ -198,15 +198,17 @@ impl<'a> CommunityQueryBuilder<'a> { } }; - if !self.show_nsfw { + if !self.show_nsfw.unwrap_or(true) { query = query.filter(community::nsfw.eq(false)); }; - query = match self.listing_type { - ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), - ListingType::Local => query.filter(community::local.eq(true)), - _ => query, - }; + if let Some(listing_type) = self.listing_type { + query = match listing_type { + ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), + ListingType::Local => query.filter(community::local.eq(true)), + _ => query, + }; + } let (limit, offset) = limit_and_offset(self.page, self.limit); let res = query diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 958d084be..5c6cdddae 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -155,8 +155,8 @@ pub struct PersonMentionQueryBuilder<'a> { conn: &'a PgConnection, my_person_id: Option, recipient_id: Option, - sort: &'a SortType, - unread_only: bool, + sort: Option, + unread_only: Option, page: Option, limit: Option, } @@ -167,20 +167,20 @@ impl<'a> PersonMentionQueryBuilder<'a> { conn, my_person_id: None, recipient_id: None, - sort: &SortType::New, - unread_only: false, + sort: None, + unread_only: None, page: None, limit: None, } } - pub fn sort(mut self, sort: &'a SortType) -> Self { - self.sort = sort; + pub fn sort>(mut self, sort: T) -> Self { + self.sort = sort.get_optional(); self } - pub fn unread_only(mut self, unread_only: bool) -> Self { - self.unread_only = unread_only; + pub fn unread_only>(mut self, unread_only: T) -> Self { + self.unread_only = unread_only.get_optional(); self } @@ -264,11 +264,11 @@ impl<'a> PersonMentionQueryBuilder<'a> { query = query.filter(person_mention::recipient_id.eq(recipient_id)); } - if self.unread_only { + if self.unread_only.unwrap_or_default() { query = query.filter(person_mention::read.eq(false)); } - query = match self.sort { + query = match self.sort.unwrap_or(SortType::Hot) { SortType::Hot | SortType::Active => query .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc()) .then_order_by(comment_aggregates::published.desc()), diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 8e292c3fc..861d333fe 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -57,7 +57,7 @@ impl PersonViewSafe { pub struct PersonQueryBuilder<'a> { conn: &'a PgConnection, - sort: &'a SortType, + sort: Option, search_term: Option, page: Option, limit: Option, @@ -68,14 +68,14 @@ impl<'a> PersonQueryBuilder<'a> { PersonQueryBuilder { conn, search_term: None, - sort: &SortType::Hot, + sort: None, page: None, limit: None, } } - pub fn sort(mut self, sort: &'a SortType) -> Self { - self.sort = sort; + pub fn sort>(mut self, sort: T) -> Self { + self.sort = sort.get_optional(); self } @@ -104,7 +104,7 @@ impl<'a> PersonQueryBuilder<'a> { query = query.filter(person::name.ilike(fuzzy_search(&search_term))); } - query = match self.sort { + query = match self.sort.unwrap_or(SortType::Hot) { SortType::Hot => query .order_by(person_aggregates::comment_score.desc()) .then_order_by(person::published.desc()), diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 21263fd61..a4f6a45a0 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -90,11 +90,10 @@ async fn get_feed_data( ) -> Result { let site_view = blocking(context.pool(), move |conn| SiteView::read(&conn)).await??; - let listing_type_ = listing_type.clone(); let posts = blocking(context.pool(), move |conn| { PostQueryBuilder::create(&conn) - .listing_type(&listing_type_) - .sort(&sort_type) + .listing_type(listing_type) + .sort(sort_type) .list() }) .await??; @@ -174,8 +173,8 @@ fn get_feed_user( let person = Person::find_by_name(&conn, &user_name)?; let posts = PostQueryBuilder::create(&conn) - .listing_type(&ListingType::All) - .sort(sort_type) + .listing_type(ListingType::All) + .sort(*sort_type) .creator_id(person.id) .list()?; @@ -200,8 +199,8 @@ fn get_feed_community( let community = Community::read_from_name(&conn, &community_name)?; let posts = PostQueryBuilder::create(&conn) - .listing_type(&ListingType::All) - .sort(sort_type) + .listing_type(ListingType::All) + .sort(*sort_type) .community_id(community.id) .list()?; @@ -233,10 +232,10 @@ fn get_feed_front( let show_bot_accounts = local_user.show_bot_accounts; 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) + .sort(*sort_type) .list()?; let items = create_post_items(posts)?; @@ -268,13 +267,13 @@ fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result Date: Fri, 23 Apr 2021 02:09:47 -0400 Subject: [PATCH 4/8] Don't search for communities or users when the id is included. --- crates/api/src/site.rs | 51 +++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index 524d2a91f..704b848a9 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -227,6 +227,9 @@ impl Perform for Search { .await??; } SearchType::All => { + // If the community is included, dont search communities or users + let community_included = data.community_id.is_some() || data.community_name.is_some(); + posts = blocking(context.pool(), move |conn| { PostQueryBuilder::create(conn) .sort(sort) @@ -265,29 +268,37 @@ impl Perform for Search { let q = data.q.to_owned(); - communities = blocking(context.pool(), move |conn| { - CommunityQueryBuilder::create(conn) - .sort(sort) - .listing_type(listing_type) - .search_term(q) - .my_person_id(person_id) - .page(page) - .limit(limit) - .list() - }) - .await??; + communities = if community_included { + vec![] + } else { + blocking(context.pool(), move |conn| { + CommunityQueryBuilder::create(conn) + .sort(sort) + .listing_type(listing_type) + .search_term(q) + .my_person_id(person_id) + .page(page) + .limit(limit) + .list() + }) + .await?? + }; let q = data.q.to_owned(); - users = blocking(context.pool(), move |conn| { - PersonQueryBuilder::create(conn) - .sort(sort) - .search_term(q) - .page(page) - .limit(limit) - .list() - }) - .await??; + users = if community_included { + vec![] + } else { + blocking(context.pool(), move |conn| { + PersonQueryBuilder::create(conn) + .sort(sort) + .search_term(q) + .page(page) + .limit(limit) + .list() + }) + .await?? + }; } SearchType::Url => { posts = blocking(context.pool(), move |conn| { From ef7fe7586b6b88e5b5da1f6f5737fd5c2435e3ab Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 23 Apr 2021 02:40:10 -0400 Subject: [PATCH 5/8] Some formatting --- crates/api_crud/src/comment/read.rs | 7 ++++++- crates/api_crud/src/post/read.rs | 8 +++++++- crates/api_crud/src/site/create.rs | 2 +- crates/api_crud/src/site/read.rs | 2 +- crates/api_crud/src/user/read.rs | 8 +++++++- crates/db_views/src/comment_view.rs | 4 ++-- crates/db_views/src/post_view.rs | 4 ++-- 7 files changed, 26 insertions(+), 9 deletions(-) diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index ceed00959..f82b97481 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -1,6 +1,11 @@ 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_api_common::{ + blocking, + comment::*, + get_local_user_view_from_jwt_opt, + user_show_bot_accounts, +}; 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}; diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 858a878f5..14c6cad5f 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -1,6 +1,12 @@ use crate::PerformCrud; use actix_web::web::Data; -use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, post::*, user_show_bot_accounts, user_show_nsfw}; +use lemmy_api_common::{ + blocking, + get_local_user_view_from_jwt_opt, + post::*, + user_show_bot_accounts, + user_show_nsfw, +}; use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType}; use lemmy_db_views::{ comment_view::CommentQueryBuilder, diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index dc6e649d6..2b876c2ff 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -67,7 +67,7 @@ impl PerformCrud for CreateSite { open_registration: data.open_registration, enable_nsfw: data.enable_nsfw, updated: None, - community_creation_admin_only: Some(data.community_creation_admin_only), + community_creation_admin_only: data.community_creation_admin_only, }; let create_site = move |conn: &'_ _| Site::create(conn, &site_form); diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index fb409f1ca..9b73c12f8 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -51,7 +51,7 @@ impl PerformCrud for GetSite { open_registration: None, enable_nsfw: None, auth: login_response.jwt, - community_creation_admin_only: false, + community_creation_admin_only: None, }; create_site.perform(context, websocket_id).await?; info!("Site {} created", setup.site_name); diff --git a/crates/api_crud/src/user/read.rs b/crates/api_crud/src/user/read.rs index f32764160..81194d1e6 100644 --- a/crates/api_crud/src/user/read.rs +++ b/crates/api_crud/src/user/read.rs @@ -1,6 +1,12 @@ use crate::PerformCrud; use actix_web::web::Data; -use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*, user_show_nsfw, user_show_bot_accounts}; +use lemmy_api_common::{ + blocking, + get_local_user_view_from_jwt_opt, + person::*, + user_show_bot_accounts, + user_show_nsfw, +}; 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}; diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 8c6d44afd..7424d9d62 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -264,8 +264,8 @@ impl<'a> CommentQueryBuilder<'a> { self } - pub fn show_bot_accounts(mut self, show_bot_accounts: bool) -> Self { - self.show_bot_accounts = show_bot_accounts; + pub fn show_bot_accounts>(mut self, show_bot_accounts: T) -> Self { + self.show_bot_accounts = show_bot_accounts.get_optional(); self } diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 192517e06..423935754 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -238,7 +238,7 @@ impl<'a> PostQueryBuilder<'a> { } pub fn show_bot_accounts>(mut self, show_bot_accounts: T) -> Self { - self.show_bot_accounts = show_bot_accounts; + self.show_bot_accounts = show_bot_accounts.get_optional(); self } @@ -360,7 +360,7 @@ impl<'a> PostQueryBuilder<'a> { .filter(community::nsfw.eq(false)); }; - if !self.show_bot_accounts { + if !self.show_bot_accounts.unwrap_or(true) { query = query.filter(person::bot_account.eq(false)); }; From ed31deab0040e91b236e11d1297314d722a66065 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 23 Apr 2021 11:54:38 -0400 Subject: [PATCH 6/8] Creator extra search results --- crates/api/src/site.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index 704b848a9..c55ac6645 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -227,8 +227,9 @@ impl Perform for Search { .await??; } SearchType::All => { - // If the community is included, dont search communities or users - let community_included = data.community_id.is_some() || data.community_name.is_some(); + // 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) @@ -268,7 +269,7 @@ impl Perform for Search { let q = data.q.to_owned(); - communities = if community_included { + communities = if community_or_creator_included { vec![] } else { blocking(context.pool(), move |conn| { @@ -286,7 +287,7 @@ impl Perform for Search { let q = data.q.to_owned(); - users = if community_included { + users = if community_or_creator_included { vec![] } else { blocking(context.pool(), move |conn| { From dc1a0dcd5e82be394ddb14ae4731bc132f601f09 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Mon, 26 Apr 2021 13:30:30 +0000 Subject: [PATCH 7/8] Fetch git tags manually in CI (#1590) * Fetch git tags manually in CI * Dont need espeak in CI anymore --- .drone.yml | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/.drone.yml b/.drone.yml index 18db1fad7..f4818556d 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,11 +8,12 @@ platform: steps: - - name: chown repo + - name: prepare repo image: ekidd/rust-musl-builder:1.50.0 user: root commands: - chown 1000:1000 . -R + - git fetch --tags - name: check formatting image: rustdocker/rust:nightly @@ -34,7 +35,7 @@ steps: RUST_TEST_THREADS: 1 commands: - sudo apt-get update - - sudo apt-get -y install --no-install-recommends espeak postgresql-client + - sudo apt-get -y install --no-install-recommends postgresql-client - cargo test --workspace --no-fail-fast - name: cargo build @@ -136,6 +137,15 @@ platform: steps: + - name: prepare repo + image: rust:1.50-slim-buster + user: root + commands: + - chown 1000:1000 . -R + - apt update + - apt install --no-install-recommends --yes git + - git fetch --tags + - name: cargo test image: rust:1.50-slim-buster environment: @@ -145,7 +155,7 @@ steps: RUST_TEST_THREADS: 1 commands: - apt-get update - - apt-get -y install --no-install-recommends espeak postgresql-client libssl-dev pkg-config libpq-dev + - apt-get -y install --no-install-recommends postgresql-client libssl-dev pkg-config libpq-dev - cargo test --workspace --no-fail-fast - cargo build From 6d3778cafec965868193d4ccebf5cf599ae3d038 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 26 Apr 2021 09:50:34 -0400 Subject: [PATCH 8/8] Changing unwrap_default to unwrap_or(false) --- crates/api/src/community.rs | 2 +- crates/api/src/local_user.rs | 2 +- crates/api_crud/src/user/read.rs | 2 +- crates/db_queries/src/lib.rs | 8 +------- crates/db_views/src/comment_view.rs | 5 ++--- crates/db_views/src/post_view.rs | 7 +++---- crates/db_views/src/private_message_view.rs | 2 +- crates/db_views_actor/src/person_mention_view.rs | 2 +- 8 files changed, 11 insertions(+), 19 deletions(-) diff --git a/crates/api/src/community.rs b/crates/api/src/community.rs index 4d519832f..b00160538 100644 --- a/crates/api/src/community.rs +++ b/crates/api/src/community.rs @@ -171,7 +171,7 @@ impl Perform for BanFromCommunity { } // Remove/Restore their data if that's desired - if data.remove_data.unwrap_or_default() { + if data.remove_data.unwrap_or(false) { // Posts blocking(context.pool(), move |conn: &'_ _| { Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true) diff --git a/crates/api/src/local_user.rs b/crates/api/src/local_user.rs index 5cf4f0627..e36e86b4c 100644 --- a/crates/api/src/local_user.rs +++ b/crates/api/src/local_user.rs @@ -397,7 +397,7 @@ impl Perform for BanPerson { } // Remove their data if that's desired - if data.remove_data.unwrap_or_default() { + if data.remove_data.unwrap_or(false) { // Posts blocking(context.pool(), move |conn: &'_ _| { Post::update_removed_for_creator(conn, banned_person_id, None, true) diff --git a/crates/api_crud/src/user/read.rs b/crates/api_crud/src/user/read.rs index 81194d1e6..39431128a 100644 --- a/crates/api_crud/src/user/read.rs +++ b/crates/api_crud/src/user/read.rs @@ -88,7 +88,7 @@ impl PerformCrud for GetPersonDetails { // If its saved only, you don't care what creator it was // Or, if its not saved, then you only want it for that specific creator - if !saved_only.unwrap_or_default() { + if !saved_only.unwrap_or(false) { posts_query = posts_query.creator_id(person_details_id); comments_query = comments_query.creator_id(person_details_id); } diff --git a/crates/db_queries/src/lib.rs b/crates/db_queries/src/lib.rs index fd5abf504..0665ac95d 100644 --- a/crates/db_queries/src/lib.rs +++ b/crates/db_queries/src/lib.rs @@ -196,13 +196,7 @@ pub enum SearchType { } pub fn from_opt_str_to_opt_enum(opt: &Option) -> Option { - match opt { - Some(t) => match T::from_str(&t) { - Ok(r) => Some(r), - Err(_) => None, - }, - None => None, - } + opt.as_ref().map(|t| T::from_str(t).ok()).flatten() } pub fn fuzzy_search(q: &str) -> String { diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 7424d9d62..fbb2b7f7d 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -350,7 +350,7 @@ impl<'a> CommentQueryBuilder<'a> { .filter(comment::removed.eq(false)); } - if self.unread_only.unwrap_or_default() { + if self.unread_only.unwrap_or(false) { query = query.filter(comment::read.eq(false)); } @@ -378,14 +378,13 @@ impl<'a> CommentQueryBuilder<'a> { if let Some(listing_type) = self.listing_type { query = match listing_type { - // ListingType::Subscribed => query.filter(community_follower::subscribed.eq(true)), ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), ListingType::Local => query.filter(community::local.eq(true)), _ => query, }; } - if self.saved_only.unwrap_or_default() { + if self.saved_only.unwrap_or(false) { query = query.filter(comment_saved::id.is_not_null()); } diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 423935754..fb8f07336 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -317,7 +317,7 @@ impl<'a> PostQueryBuilder<'a> { if let Some(listing_type) = self.listing_type { query = match listing_type { - ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), // TODO could be this: and(community_follower::person_id.eq(person_id_join)), + ListingType::Subscribed => query.filter(community_follower::person_id.is_not_null()), ListingType::Local => query.filter(community::local.eq(true)), _ => query, }; @@ -364,12 +364,11 @@ impl<'a> PostQueryBuilder<'a> { query = query.filter(person::bot_account.eq(false)); }; - // TODO These two might be wrong - if self.saved_only.unwrap_or_default() { + if self.saved_only.unwrap_or(false) { query = query.filter(post_saved::id.is_not_null()); }; - if self.unread_only.unwrap_or_default() { + if self.unread_only.unwrap_or(false) { query = query.filter(post_read::id.is_not_null()); }; diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index b99286687..8fee2fc9b 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -89,7 +89,7 @@ impl<'a> PrivateMessageQueryBuilder<'a> { .into_boxed(); // If its unread, I only want the ones to me - if self.unread_only.unwrap_or_default() { + if self.unread_only.unwrap_or(false) { query = query .filter(private_message::read.eq(false)) .filter(private_message::recipient_id.eq(self.recipient_id)); diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 5c6cdddae..b391345ff 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -264,7 +264,7 @@ impl<'a> PersonMentionQueryBuilder<'a> { query = query.filter(person_mention::recipient_id.eq(recipient_id)); } - if self.unread_only.unwrap_or_default() { + if self.unread_only.unwrap_or(false) { query = query.filter(person_mention::read.eq(false)); }