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,
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)
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?
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;
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();
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},
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;
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)
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::{
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)
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>,
}
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(
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)?;