mirror of
https://github.com/LemmyNet/lemmy
synced 2024-11-10 06:54:12 +00:00
Refactor LocalUser settings conditions in database views (#4746)
* Create viewer.rs * Rename viewer.rs to viewer.rs * Update viewer.rs * Update post_view.rs * Update distinguish.rs * Update like.rs * Update viewer.rs * Update list_comment_likes.rs * Update like.rs * Update save.rs * Update like.rs * revert changes in api crate * Update post_view.rs * Update post_view.rs * Update comment_view.rs * Update post_view.rs * Update community_view.rs * Update comment_view.rs * Update post_view.rs * Update viewer.rs * Update post_view.rs * Update community_view.rs * Update local_user_view.rs * Update viewer.rs * Update community_view.rs * Update viewer.rs * Update lib.rs * Update comment_view.rs * Update post_view.rs * Update viewer.rs * Update viewer.rs * Update viewer.rs * Update viewer.rs * Update local_user_view.rs * Update viewer.rs * Update viewer.rs * Update local_user_view.rs * Update community_view.rs * Update viewer.rs * Update crates/db_schema/src/viewer.rs Co-authored-by: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com> * Update viewer.rs * Update viewer.rs * Update viewer.rs * Update viewer.rs * Update post_view.rs * Update community_view.rs * Update comment_view.rs * Update viewer.rs * Update post_view.rs * Update save.rs * Update resolve_object.rs * Update viewer.rs * Update save.rs * Update resolve_object.rs * Update comment_view.rs * Update post_view.rs * Update community_view.rs * Update local_user_view.rs * Update post_view.rs * Update viewer.rs * Update comment_view.rs * Update post_view.rs * Update community_view.rs * Update viewer.rs * Update viewer.rs * Update viewer.rs * Update viewer.rs * Update viewer.rs * Some additions to localuser DB view helpers. (#39) * Some additions to localuser DB view helpers. - Getting rid of generics. - Passing in only LocalUser to views. * Formatting fixes. * Getting rid of unecessary as_refs * Fixing clippy. --------- Co-authored-by: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com> Co-authored-by: Dessalines <dessalines@users.noreply.github.com> Co-authored-by: Dessalines <tyhou13@gmx.com>
This commit is contained in:
parent
65620913fc
commit
6497ec519e
11 changed files with 171 additions and 130 deletions
|
@ -83,12 +83,13 @@ pub async fn get_post(
|
|||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
|
||||
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;
|
||||
let local_user = local_user_view.as_ref().map(|u| &u.local_user);
|
||||
|
||||
// Fetch the cross_posts
|
||||
let cross_posts = if let Some(url) = &post_view.post.url {
|
||||
let mut x_posts = PostQuery {
|
||||
url_search: Some(url.inner().as_str().into()),
|
||||
local_user: local_user_view.as_ref(),
|
||||
local_user,
|
||||
..Default::default()
|
||||
}
|
||||
.list(&local_site.site, &mut context.pool())
|
||||
|
|
|
@ -70,6 +70,8 @@ pub async fn list_comments(
|
|||
|
||||
let parent_path_cloned = parent_path.clone();
|
||||
let post_id = data.post_id;
|
||||
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
||||
|
||||
let comments = CommentQuery {
|
||||
listing_type,
|
||||
sort,
|
||||
|
@ -80,7 +82,7 @@ pub async fn list_comments(
|
|||
community_id,
|
||||
parent_path: parent_path_cloned,
|
||||
post_id,
|
||||
local_user: local_user_view.as_ref(),
|
||||
local_user,
|
||||
page,
|
||||
limit,
|
||||
..Default::default()
|
||||
|
|
|
@ -49,17 +49,17 @@ pub async fn list_posts(
|
|||
return Err(LemmyError::from(LemmyErrorType::ContradictingFilters));
|
||||
}
|
||||
|
||||
let local_user_ref = local_user_view.as_ref().map(|u| &u.local_user);
|
||||
let local_user = local_user_view.as_ref().map(|u| &u.local_user);
|
||||
let listing_type = Some(listing_type_with_default(
|
||||
data.type_,
|
||||
local_user_ref,
|
||||
local_user,
|
||||
&local_site.local_site,
|
||||
community_id,
|
||||
));
|
||||
|
||||
let sort = Some(sort_type_with_default(
|
||||
data.sort,
|
||||
local_user_ref,
|
||||
local_user,
|
||||
&local_site.local_site,
|
||||
));
|
||||
|
||||
|
@ -71,7 +71,7 @@ pub async fn list_posts(
|
|||
};
|
||||
|
||||
let posts = PostQuery {
|
||||
local_user: local_user_view.as_ref(),
|
||||
local_user,
|
||||
listing_type,
|
||||
sort,
|
||||
community_id,
|
||||
|
|
|
@ -65,10 +65,12 @@ pub async fn read_person(
|
|||
None
|
||||
};
|
||||
|
||||
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
||||
|
||||
let posts = PostQuery {
|
||||
sort,
|
||||
saved_only,
|
||||
local_user: local_user_view.as_ref(),
|
||||
local_user,
|
||||
community_id,
|
||||
page,
|
||||
limit,
|
||||
|
@ -79,7 +81,7 @@ pub async fn read_person(
|
|||
.await?;
|
||||
|
||||
let comments = CommentQuery {
|
||||
local_user: local_user_view.as_ref(),
|
||||
local_user,
|
||||
sort: sort.map(post_to_comment_sort_type),
|
||||
saved_only,
|
||||
community_id,
|
||||
|
|
|
@ -55,7 +55,7 @@ pub async fn search(
|
|||
data.community_id
|
||||
};
|
||||
let creator_id = data.creator_id;
|
||||
let local_user = local_user_view.as_ref().map(|luv| &luv.local_user);
|
||||
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
||||
|
||||
match search_type {
|
||||
SearchType::Posts => {
|
||||
|
@ -64,7 +64,7 @@ pub async fn search(
|
|||
listing_type: (listing_type),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
local_user: (local_user_view.as_ref()),
|
||||
local_user,
|
||||
search_term: (Some(q)),
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
|
@ -80,7 +80,7 @@ pub async fn search(
|
|||
search_term: (Some(q)),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
local_user: (local_user_view.as_ref()),
|
||||
local_user,
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
..Default::default()
|
||||
|
@ -125,7 +125,7 @@ pub async fn search(
|
|||
listing_type: (listing_type),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
local_user: (local_user_view.as_ref()),
|
||||
local_user,
|
||||
search_term: (Some(q)),
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
|
@ -142,7 +142,7 @@ pub async fn search(
|
|||
search_term: (Some(q)),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
local_user: (local_user_view.as_ref()),
|
||||
local_user,
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
..Default::default()
|
||||
|
@ -192,6 +192,7 @@ pub async fn search(
|
|||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
url_search: (Some(q)),
|
||||
local_user,
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
..Default::default()
|
||||
|
|
|
@ -5,6 +5,7 @@ use crate::{
|
|||
actor_language::LocalUserLanguage,
|
||||
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
|
||||
local_user_vote_display_mode::{LocalUserVoteDisplayMode, LocalUserVoteDisplayModeInsertForm},
|
||||
site::Site,
|
||||
},
|
||||
utils::{
|
||||
functions::{coalesce, lower},
|
||||
|
@ -216,6 +217,44 @@ impl LocalUser {
|
|||
}
|
||||
}
|
||||
|
||||
/// Adds some helper functions for an optional LocalUser
|
||||
pub trait LocalUserOptionHelper {
|
||||
fn person_id(&self) -> Option<PersonId>;
|
||||
fn local_user_id(&self) -> Option<LocalUserId>;
|
||||
fn show_bot_accounts(&self) -> bool;
|
||||
fn show_read_posts(&self) -> bool;
|
||||
fn is_admin(&self) -> bool;
|
||||
fn show_nsfw(&self, site: &Site) -> bool;
|
||||
}
|
||||
|
||||
impl LocalUserOptionHelper for Option<&LocalUser> {
|
||||
fn person_id(&self) -> Option<PersonId> {
|
||||
self.map(|l| l.person_id)
|
||||
}
|
||||
|
||||
fn local_user_id(&self) -> Option<LocalUserId> {
|
||||
self.map(|l| l.id)
|
||||
}
|
||||
|
||||
fn show_bot_accounts(&self) -> bool {
|
||||
self.map(|l| l.show_bot_accounts).unwrap_or(true)
|
||||
}
|
||||
|
||||
fn show_read_posts(&self) -> bool {
|
||||
self.map(|l| l.show_read_posts).unwrap_or(true)
|
||||
}
|
||||
|
||||
fn is_admin(&self) -> bool {
|
||||
self.map(|l| l.admin).unwrap_or(false)
|
||||
}
|
||||
|
||||
fn show_nsfw(&self, site: &Site) -> bool {
|
||||
self
|
||||
.map(|l| l.show_nsfw)
|
||||
.unwrap_or(site.content_warning.is_some())
|
||||
}
|
||||
}
|
||||
|
||||
impl LocalUserInsertForm {
|
||||
pub fn test_form(person_id: PersonId) -> Self {
|
||||
Self::builder()
|
||||
|
|
|
@ -1,7 +1,15 @@
|
|||
use crate::{newtypes::DbUrl, CommentSortType, SortType};
|
||||
use crate::{
|
||||
diesel::ExpressionMethods,
|
||||
newtypes::{DbUrl, PersonId},
|
||||
schema::community,
|
||||
CommentSortType,
|
||||
CommunityVisibility,
|
||||
SortType,
|
||||
};
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use deadpool::Runtime;
|
||||
use diesel::{
|
||||
dsl,
|
||||
helper_types::AsExprOf,
|
||||
pg::Pg,
|
||||
query_builder::{Query, QueryFragment},
|
||||
|
@ -567,6 +575,20 @@ impl<RF, LF> Queries<RF, LF> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn visible_communities_only<Q>(my_person_id: Option<PersonId>, query: Q) -> Q
|
||||
where
|
||||
Q: diesel::query_dsl::methods::FilterDsl<
|
||||
dsl::Eq<community::visibility, CommunityVisibility>,
|
||||
Output = Q,
|
||||
>,
|
||||
{
|
||||
if my_person_id.is_none() {
|
||||
query.filter(community::visibility.eq(CommunityVisibility::Public))
|
||||
} else {
|
||||
query
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
mod tests {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::structs::{CommentView, LocalUserView};
|
||||
use crate::structs::CommentView;
|
||||
use diesel::{
|
||||
dsl::{exists, not},
|
||||
pg::Pg,
|
||||
|
@ -16,6 +16,7 @@ use diesel::{
|
|||
use diesel_async::RunQueryDsl;
|
||||
use diesel_ltree::{nlevel, subpath, Ltree, LtreeExtensions};
|
||||
use lemmy_db_schema::{
|
||||
impls::local_user::LocalUserOptionHelper,
|
||||
newtypes::{CommentId, CommunityId, LocalUserId, PersonId, PostId},
|
||||
schema::{
|
||||
comment,
|
||||
|
@ -34,9 +35,18 @@ use lemmy_db_schema::{
|
|||
person_block,
|
||||
post,
|
||||
},
|
||||
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
|
||||
source::local_user::LocalUser,
|
||||
utils::{
|
||||
fuzzy_search,
|
||||
limit_and_offset,
|
||||
visible_communities_only,
|
||||
DbConn,
|
||||
DbPool,
|
||||
ListFn,
|
||||
Queries,
|
||||
ReadFn,
|
||||
},
|
||||
CommentSortType,
|
||||
CommunityVisibility,
|
||||
ListingType,
|
||||
};
|
||||
|
||||
|
@ -174,22 +184,19 @@ fn queries<'a>() -> Queries<
|
|||
let read = move |mut conn: DbConn<'a>,
|
||||
(comment_id, my_person_id): (CommentId, Option<PersonId>)| async move {
|
||||
let mut query = all_joins(comment::table.find(comment_id).into_boxed(), my_person_id);
|
||||
// Hide local only communities from unauthenticated users
|
||||
if my_person_id.is_none() {
|
||||
query = query.filter(community::visibility.eq(CommunityVisibility::Public));
|
||||
}
|
||||
query = visible_communities_only(my_person_id, query);
|
||||
query.first(&mut conn).await
|
||||
};
|
||||
|
||||
let list = move |mut conn: DbConn<'a>, options: CommentQuery<'a>| async move {
|
||||
let my_person_id = options.local_user.map(|l| l.person.id);
|
||||
let my_local_user_id = options.local_user.map(|l| l.local_user.id);
|
||||
|
||||
// The left join below will return None in this case
|
||||
let person_id_join = my_person_id.unwrap_or(PersonId(-1));
|
||||
let local_user_id_join = my_local_user_id.unwrap_or(LocalUserId(-1));
|
||||
let person_id_join = options.local_user.person_id().unwrap_or(PersonId(-1));
|
||||
let local_user_id_join = options
|
||||
.local_user
|
||||
.local_user_id()
|
||||
.unwrap_or(LocalUserId(-1));
|
||||
|
||||
let mut query = all_joins(comment::table.into_boxed(), my_person_id);
|
||||
let mut query = all_joins(comment::table.into_boxed(), options.local_user.person_id());
|
||||
|
||||
if let Some(creator_id) = options.creator_id {
|
||||
query = query.filter(comment::creator_id.eq(creator_id));
|
||||
|
@ -251,7 +258,7 @@ fn queries<'a>() -> Queries<
|
|||
.then_order_by(comment_saved::published.desc());
|
||||
}
|
||||
|
||||
if let Some(my_id) = my_person_id {
|
||||
if let Some(my_id) = options.local_user.person_id() {
|
||||
let not_creator_filter = comment::creator_id.ne(my_id);
|
||||
if options.liked_only {
|
||||
query = query.filter(not_creator_filter).filter(score(my_id).eq(1));
|
||||
|
@ -260,11 +267,7 @@ fn queries<'a>() -> Queries<
|
|||
}
|
||||
}
|
||||
|
||||
if !options
|
||||
.local_user
|
||||
.map(|l| l.local_user.show_bot_accounts)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
if !options.local_user.show_bot_accounts() {
|
||||
query = query.filter(person::bot_account.eq(false));
|
||||
};
|
||||
|
||||
|
@ -298,10 +301,7 @@ fn queries<'a>() -> Queries<
|
|||
query = query.filter(not(is_creator_blocked(person_id_join)));
|
||||
};
|
||||
|
||||
// Hide comments in local only communities from unauthenticated users
|
||||
if options.local_user.is_none() {
|
||||
query = query.filter(community::visibility.eq(CommunityVisibility::Public));
|
||||
}
|
||||
query = visible_communities_only(options.local_user.person_id(), query);
|
||||
|
||||
// A Max depth given means its a tree fetch
|
||||
let (limit, offset) = if let Some(max_depth) = options.max_depth {
|
||||
|
@ -396,7 +396,7 @@ pub struct CommentQuery<'a> {
|
|||
pub post_id: Option<PostId>,
|
||||
pub parent_path: Option<Ltree>,
|
||||
pub creator_id: Option<PersonId>,
|
||||
pub local_user: Option<&'a LocalUserView>,
|
||||
pub local_user: Option<&'a LocalUser>,
|
||||
pub search_term: Option<String>,
|
||||
pub saved_only: bool,
|
||||
pub liked_only: bool,
|
||||
|
@ -659,7 +659,7 @@ mod tests {
|
|||
let read_comment_views_with_person = CommentQuery {
|
||||
sort: (Some(CommentSortType::Old)),
|
||||
post_id: (Some(data.inserted_post.id)),
|
||||
local_user: (Some(&data.timmy_local_user_view)),
|
||||
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
||||
..Default::default()
|
||||
}
|
||||
.list(pool)
|
||||
|
@ -711,7 +711,7 @@ mod tests {
|
|||
CommentLike::like(pool, &comment_like_form).await?;
|
||||
|
||||
let read_liked_comment_views = CommentQuery {
|
||||
local_user: (Some(&data.timmy_local_user_view)),
|
||||
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
||||
liked_only: (true),
|
||||
..Default::default()
|
||||
}
|
||||
|
@ -727,7 +727,7 @@ mod tests {
|
|||
assert_length!(1, read_liked_comment_views);
|
||||
|
||||
let read_disliked_comment_views: Vec<CommentView> = CommentQuery {
|
||||
local_user: (Some(&data.timmy_local_user_view)),
|
||||
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
||||
disliked_only: (true),
|
||||
..Default::default()
|
||||
}
|
||||
|
@ -822,7 +822,7 @@ mod tests {
|
|||
// by default, user has all languages enabled and should see all comments
|
||||
// (except from blocked user)
|
||||
let all_languages = CommentQuery {
|
||||
local_user: (Some(&data.timmy_local_user_view)),
|
||||
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
||||
..Default::default()
|
||||
}
|
||||
.list(pool)
|
||||
|
@ -840,7 +840,7 @@ mod tests {
|
|||
)
|
||||
.await?;
|
||||
let finnish_comments = CommentQuery {
|
||||
local_user: (Some(&data.timmy_local_user_view)),
|
||||
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
||||
..Default::default()
|
||||
}
|
||||
.list(pool)
|
||||
|
@ -866,7 +866,7 @@ mod tests {
|
|||
)
|
||||
.await?;
|
||||
let undetermined_comment = CommentQuery {
|
||||
local_user: (Some(&data.timmy_local_user_view)),
|
||||
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
||||
..Default::default()
|
||||
}
|
||||
.list(pool)
|
||||
|
@ -979,7 +979,7 @@ mod tests {
|
|||
|
||||
// Fetch the saved comments
|
||||
let comments = CommentQuery {
|
||||
local_user: Some(&data.timmy_local_user_view),
|
||||
local_user: Some(&data.timmy_local_user_view.local_user),
|
||||
saved_only: true,
|
||||
..Default::default()
|
||||
}
|
||||
|
@ -1158,7 +1158,7 @@ mod tests {
|
|||
assert_eq!(0, unauthenticated_query.len());
|
||||
|
||||
let authenticated_query = CommentQuery {
|
||||
local_user: Some(&data.timmy_local_user_view),
|
||||
local_user: Some(&data.timmy_local_user_view.local_user),
|
||||
..Default::default()
|
||||
}
|
||||
.list(pool)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::structs::{LocalUserView, PaginationCursor, PostView};
|
||||
use crate::structs::{PaginationCursor, PostView};
|
||||
use diesel::{
|
||||
debug_query,
|
||||
dsl::{exists, not, IntervalDsl},
|
||||
|
@ -20,6 +20,7 @@ use diesel_async::RunQueryDsl;
|
|||
use i_love_jesus::PaginatedQueryBuilder;
|
||||
use lemmy_db_schema::{
|
||||
aggregates::structs::{post_aggregates_keys as key, PostAggregates},
|
||||
impls::local_user::LocalUserOptionHelper,
|
||||
newtypes::{CommunityId, LocalUserId, PersonId, PostId},
|
||||
schema::{
|
||||
community,
|
||||
|
@ -40,13 +41,14 @@ use lemmy_db_schema::{
|
|||
post_read,
|
||||
post_saved,
|
||||
},
|
||||
source::site::Site,
|
||||
source::{local_user::LocalUser, site::Site},
|
||||
utils::{
|
||||
functions::coalesce,
|
||||
fuzzy_search,
|
||||
get_conn,
|
||||
limit_and_offset,
|
||||
now,
|
||||
visible_communities_only,
|
||||
Commented,
|
||||
DbConn,
|
||||
DbPool,
|
||||
|
@ -55,7 +57,6 @@ use lemmy_db_schema::{
|
|||
ReadFn,
|
||||
ReverseTimestampKey,
|
||||
},
|
||||
CommunityVisibility,
|
||||
ListingType,
|
||||
SortType,
|
||||
};
|
||||
|
@ -285,10 +286,7 @@ fn queries<'a>() -> Queries<
|
|||
);
|
||||
}
|
||||
|
||||
// Hide posts in local only communities from unauthenticated users
|
||||
if my_person_id.is_none() {
|
||||
query = query.filter(community::visibility.eq(CommunityVisibility::Public));
|
||||
}
|
||||
query = visible_communities_only(my_person_id, query);
|
||||
|
||||
Commented::new(query)
|
||||
.text("PostView::read")
|
||||
|
@ -297,31 +295,30 @@ fn queries<'a>() -> Queries<
|
|||
};
|
||||
|
||||
let list = move |mut conn: DbConn<'a>, (options, site): (PostQuery<'a>, &'a Site)| async move {
|
||||
let my_person_id = options.local_user.map(|l| l.person.id);
|
||||
let my_local_user_id = options.local_user.map(|l| l.local_user.id);
|
||||
|
||||
// The left join below will return None in this case
|
||||
let person_id_join = my_person_id.unwrap_or(PersonId(-1));
|
||||
let local_user_id_join = my_local_user_id.unwrap_or(LocalUserId(-1));
|
||||
let person_id_join = options.local_user.person_id().unwrap_or(PersonId(-1));
|
||||
let local_user_id_join = options
|
||||
.local_user
|
||||
.local_user_id()
|
||||
.unwrap_or(LocalUserId(-1));
|
||||
|
||||
let mut query = all_joins(post_aggregates::table.into_boxed(), my_person_id);
|
||||
let mut query = all_joins(
|
||||
post_aggregates::table.into_boxed(),
|
||||
options.local_user.person_id(),
|
||||
);
|
||||
|
||||
// hide posts from deleted communities
|
||||
query = query.filter(community::deleted.eq(false));
|
||||
|
||||
// only show deleted posts to creator
|
||||
if let Some(person_id) = my_person_id {
|
||||
if let Some(person_id) = options.local_user.person_id() {
|
||||
query = query.filter(post::deleted.eq(false).or(post::creator_id.eq(person_id)));
|
||||
} else {
|
||||
query = query.filter(post::deleted.eq(false));
|
||||
}
|
||||
|
||||
let is_admin = options
|
||||
.local_user
|
||||
.map(|l| l.local_user.admin)
|
||||
.unwrap_or(false);
|
||||
// only show removed posts to admin when viewing user profile
|
||||
if !(options.creator_id.is_some() && is_admin) {
|
||||
if !(options.creator_id.is_some() && options.local_user.is_admin()) {
|
||||
query = query
|
||||
.filter(community::removed.eq(false))
|
||||
.filter(post::removed.eq(false));
|
||||
|
@ -335,7 +332,7 @@ fn queries<'a>() -> Queries<
|
|||
}
|
||||
|
||||
if let Some(listing_type) = options.listing_type {
|
||||
if let Some(person_id) = my_person_id {
|
||||
if let Some(person_id) = options.local_user.person_id() {
|
||||
let is_subscribed = exists(
|
||||
community_follower::table.filter(
|
||||
post_aggregates::community_id
|
||||
|
@ -392,54 +389,40 @@ fn queries<'a>() -> Queries<
|
|||
.filter(not(post::removed.or(post::deleted)));
|
||||
}
|
||||
|
||||
// If there is a content warning, show nsfw content by default.
|
||||
let has_content_warning = site.content_warning.is_some();
|
||||
if !options
|
||||
.local_user
|
||||
.map(|l| l.local_user.show_nsfw)
|
||||
.unwrap_or(has_content_warning)
|
||||
{
|
||||
if !options.local_user.show_nsfw(site) {
|
||||
query = query
|
||||
.filter(post::nsfw.eq(false))
|
||||
.filter(community::nsfw.eq(false));
|
||||
};
|
||||
|
||||
if !options
|
||||
.local_user
|
||||
.map(|l| l.local_user.show_bot_accounts)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
if !options.local_user.show_bot_accounts() {
|
||||
query = query.filter(person::bot_account.eq(false));
|
||||
};
|
||||
|
||||
// If its saved only, then filter, and order by the saved time, not the comment creation time.
|
||||
if let (true, Some(_person_id)) = (options.saved_only, my_person_id) {
|
||||
if options.saved_only {
|
||||
query = query
|
||||
.filter(post_saved::person_id.is_not_null())
|
||||
.then_order_by(post_saved::published.desc());
|
||||
}
|
||||
// Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read
|
||||
// setting wont be able to see saved posts.
|
||||
else if !options
|
||||
.local_user
|
||||
.map(|l| l.local_user.show_read_posts)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
else if !options.local_user.show_read_posts() {
|
||||
// Do not hide read posts when it is a user profile view
|
||||
// Or, only hide read posts on non-profile views
|
||||
if let (None, Some(person_id)) = (options.creator_id, my_person_id) {
|
||||
if let (None, Some(person_id)) = (options.creator_id, options.local_user.person_id()) {
|
||||
query = query.filter(not(is_read(person_id)));
|
||||
}
|
||||
}
|
||||
|
||||
if !options.show_hidden {
|
||||
// If a creator id isn't given (IE its on home or community pages), hide the hidden posts
|
||||
if let (None, Some(person_id)) = (options.creator_id, my_person_id) {
|
||||
if let (None, Some(person_id)) = (options.creator_id, options.local_user.person_id()) {
|
||||
query = query.filter(not(is_hidden(person_id)));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(my_id) = my_person_id {
|
||||
if let Some(my_id) = options.local_user.person_id() {
|
||||
let not_creator_filter = post_aggregates::creator_id.ne(my_id);
|
||||
if options.liked_only {
|
||||
query = query.filter(not_creator_filter).filter(score(my_id).eq(1));
|
||||
|
@ -448,14 +431,11 @@ fn queries<'a>() -> Queries<
|
|||
}
|
||||
};
|
||||
|
||||
// Hide posts in local only communities from unauthenticated users
|
||||
if options.local_user.is_none() {
|
||||
query = query.filter(community::visibility.eq(CommunityVisibility::Public));
|
||||
}
|
||||
query = visible_communities_only(options.local_user.person_id(), query);
|
||||
|
||||
// Dont filter blocks or missing languages for moderator view type
|
||||
if let (Some(person_id), false) = (
|
||||
my_person_id,
|
||||
options.local_user.person_id(),
|
||||
options.listing_type.unwrap_or_default() == ListingType::ModeratorView,
|
||||
) {
|
||||
// Filter out the rows with missing languages
|
||||
|
@ -618,7 +598,7 @@ pub struct PostQuery<'a> {
|
|||
// if true, the query should be handled as if community_id was not given except adding the
|
||||
// literal filter
|
||||
pub community_id_just_for_prefetch: bool,
|
||||
pub local_user: Option<&'a LocalUserView>,
|
||||
pub local_user: Option<&'a LocalUser>,
|
||||
pub search_term: Option<String>,
|
||||
pub url_search: Option<String>,
|
||||
pub saved_only: bool,
|
||||
|
@ -663,11 +643,7 @@ impl<'a> PostQuery<'a> {
|
|||
"legacy pagination cannot be combined with v2 pagination".into(),
|
||||
));
|
||||
}
|
||||
let self_person_id = self
|
||||
.local_user
|
||||
.expect("part of the above if")
|
||||
.local_user
|
||||
.person_id;
|
||||
let self_person_id = self.local_user.expect("part of the above if").person_id;
|
||||
let largest_subscribed = {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
community_follower
|
||||
|
@ -807,7 +783,7 @@ mod tests {
|
|||
fn default_post_query(&self) -> PostQuery<'_> {
|
||||
PostQuery {
|
||||
sort: Some(SortType::New),
|
||||
local_user: Some(&self.local_user_view),
|
||||
local_user: Some(&self.local_user_view.local_user),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
@ -1324,8 +1300,8 @@ mod tests {
|
|||
// Deleted post is only shown to creator
|
||||
for (local_user, expect_contains_deleted) in [
|
||||
(None, false),
|
||||
(Some(&data.blocked_local_user_view), false),
|
||||
(Some(&data.local_user_view), true),
|
||||
(Some(&data.blocked_local_user_view.local_user), false),
|
||||
(Some(&data.local_user_view.local_user), true),
|
||||
] {
|
||||
let contains_deleted = PostQuery {
|
||||
local_user,
|
||||
|
@ -1563,7 +1539,7 @@ mod tests {
|
|||
// Make sure it does come back with the show_hidden option
|
||||
let post_listings_show_hidden = PostQuery {
|
||||
sort: Some(SortType::New),
|
||||
local_user: Some(&data.local_user_view),
|
||||
local_user: Some(&data.local_user_view.local_user),
|
||||
show_hidden: true,
|
||||
..Default::default()
|
||||
}
|
||||
|
@ -1738,7 +1714,7 @@ mod tests {
|
|||
assert_eq!(0, unauthenticated_query.len());
|
||||
|
||||
let authenticated_query = PostQuery {
|
||||
local_user: Some(&data.local_user_view),
|
||||
local_user: Some(&data.local_user_view.local_user),
|
||||
..Default::default()
|
||||
}
|
||||
.list(&data.site, pool)
|
||||
|
|
|
@ -11,6 +11,7 @@ use diesel::{
|
|||
};
|
||||
use diesel_async::RunQueryDsl;
|
||||
use lemmy_db_schema::{
|
||||
impls::local_user::LocalUserOptionHelper,
|
||||
newtypes::{CommunityId, PersonId},
|
||||
schema::{
|
||||
community,
|
||||
|
@ -19,11 +20,18 @@ use lemmy_db_schema::{
|
|||
community_follower,
|
||||
community_person_ban,
|
||||
instance_block,
|
||||
local_user,
|
||||
},
|
||||
source::{community::CommunityFollower, local_user::LocalUser, site::Site},
|
||||
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
|
||||
CommunityVisibility,
|
||||
utils::{
|
||||
fuzzy_search,
|
||||
limit_and_offset,
|
||||
visible_communities_only,
|
||||
DbConn,
|
||||
DbPool,
|
||||
ListFn,
|
||||
Queries,
|
||||
ReadFn,
|
||||
},
|
||||
ListingType,
|
||||
SortType,
|
||||
};
|
||||
|
@ -97,10 +105,7 @@ fn queries<'a>() -> Queries<
|
|||
query = query.filter(not_removed_or_deleted);
|
||||
}
|
||||
|
||||
// Hide local only communities from unauthenticated users
|
||||
if my_person_id.is_none() {
|
||||
query = query.filter(community::visibility.eq(CommunityVisibility::Public));
|
||||
}
|
||||
query = visible_communities_only(my_person_id, query);
|
||||
|
||||
query.first(&mut conn).await
|
||||
};
|
||||
|
@ -108,14 +113,14 @@ fn queries<'a>() -> Queries<
|
|||
let list = move |mut conn: DbConn<'a>, (options, site): (CommunityQuery<'a>, &'a Site)| async move {
|
||||
use SortType::*;
|
||||
|
||||
let my_person_id = options.local_user.map(|l| l.person_id);
|
||||
|
||||
// The left join below will return None in this case
|
||||
let person_id_join = my_person_id.unwrap_or(PersonId(-1));
|
||||
let person_id_join = options.local_user.person_id().unwrap_or(PersonId(-1));
|
||||
|
||||
let mut query = all_joins(community::table.into_boxed(), my_person_id)
|
||||
.left_join(local_user::table.on(local_user::person_id.eq(person_id_join)))
|
||||
.select(selection);
|
||||
let mut query = all_joins(
|
||||
community::table.into_boxed(),
|
||||
options.local_user.person_id(),
|
||||
)
|
||||
.select(selection);
|
||||
|
||||
if let Some(search_term) = options.search_term {
|
||||
let searcher = fuzzy_search(&search_term);
|
||||
|
@ -162,21 +167,14 @@ fn queries<'a>() -> Queries<
|
|||
|
||||
// Don't show blocked communities and communities on blocked instances. nsfw communities are
|
||||
// also hidden (based on profile setting)
|
||||
if options.local_user.is_some() {
|
||||
query = query.filter(instance_block::person_id.is_null());
|
||||
query = query.filter(community_block::person_id.is_null());
|
||||
query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true)));
|
||||
} else {
|
||||
// No person in request, only show nsfw communities if show_nsfw is passed into request or if
|
||||
// site has content warning.
|
||||
let has_content_warning = site.content_warning.is_some();
|
||||
if !options.show_nsfw && !has_content_warning {
|
||||
query = query.filter(community::nsfw.eq(false));
|
||||
}
|
||||
// Hide local only communities from unauthenticated users
|
||||
query = query.filter(community::visibility.eq(CommunityVisibility::Public));
|
||||
query = query.filter(instance_block::person_id.is_null());
|
||||
query = query.filter(community_block::person_id.is_null());
|
||||
if !(options.local_user.show_nsfw(site) || options.show_nsfw) {
|
||||
query = query.filter(community::nsfw.eq(false));
|
||||
}
|
||||
|
||||
query = visible_communities_only(options.local_user.person_id(), query);
|
||||
|
||||
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
|
||||
query
|
||||
.limit(limit)
|
||||
|
|
|
@ -355,7 +355,7 @@ async fn get_feed_front(
|
|||
|
||||
let posts = PostQuery {
|
||||
listing_type: (Some(ListingType::Subscribed)),
|
||||
local_user: (Some(&local_user)),
|
||||
local_user: (Some(&local_user.local_user)),
|
||||
sort: (Some(*sort_type)),
|
||||
limit: (Some(*limit)),
|
||||
page: (Some(*page)),
|
||||
|
|
Loading…
Reference in a new issue