Merge remote-tracking branch 'origin/main' into mod_admin_ordering

This commit is contained in:
Dessalines 2024-07-07 13:12:31 -04:00
commit 1712cb1db3
30 changed files with 242 additions and 234 deletions

View File

@ -17,9 +17,13 @@ pub async fn distinguish_comment(
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> LemmyResult<Json<CommentResponse>> {
let orig_comment = CommentView::read(&mut context.pool(), data.comment_id, None)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let orig_comment = CommentView::read(
&mut context.pool(),
data.comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
check_community_user_action(
&local_user_view.person,
@ -54,7 +58,7 @@ pub async fn distinguish_comment(
let comment_view = CommentView::read(
&mut context.pool(),
data.comment_id,
Some(local_user_view.person.id),
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;

View File

@ -35,9 +35,13 @@ pub async fn like_comment(
check_bot_account(&local_user_view.person)?;
let comment_id = data.comment_id;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let orig_comment = CommentView::read(
&mut context.pool(),
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
check_community_user_action(
&local_user_view.person,

View File

@ -17,7 +17,7 @@ pub async fn list_comment_likes(
let comment_view = CommentView::read(
&mut context.pool(),
data.comment_id,
Some(local_user_view.person.id),
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;

View File

@ -32,10 +32,13 @@ pub async fn save_comment(
}
let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id))
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let comment_view = CommentView::read(
&mut context.pool(),
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
Ok(Json(CommentResponse {
comment_view,

View File

@ -35,9 +35,13 @@ pub async fn create_comment_report(
let person_id = local_user_view.person.id;
let comment_id = data.comment_id;
let comment_view = CommentView::read(&mut context.pool(), comment_id, None)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let comment_view = CommentView::read(
&mut context.pool(),
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
check_community_user_action(
&local_user_view.person,

View File

@ -50,10 +50,14 @@ pub async fn block_community(
.with_lemmy_type(LemmyErrorType::CommunityBlockAlreadyExists)?;
}
let community_view =
CommunityView::read(&mut context.pool(), community_id, Some(person_id), false)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
let community_view = CommunityView::read(
&mut context.pool(),
community_id,
Some(&local_user_view.local_user),
false,
)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
ActivityChannel::submit_activity(
SendActivityData::FollowCommunity(

View File

@ -62,11 +62,14 @@ pub async fn follow_community(
}
let community_id = data.community_id;
let person_id = local_user_view.person.id;
let community_view =
CommunityView::read(&mut context.pool(), community_id, Some(person_id), false)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
let community_view = CommunityView::read(
&mut context.pool(),
community_id,
Some(&local_user_view.local_user),
false,
)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;

View File

@ -76,11 +76,14 @@ pub async fn transfer_community(
ModTransferCommunity::create(&mut context.pool(), &form).await?;
let community_id = data.community_id;
let person_id = local_user_view.person.id;
let community_view =
CommunityView::read(&mut context.pool(), community_id, Some(person_id), false)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
let community_view = CommunityView::read(
&mut context.pool(),
community_id,
Some(&local_user_view.local_user),
false,
)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
let community_id = data.community_id;
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id)

View File

@ -72,11 +72,5 @@ pub async fn feature_post(
)
.await?;
build_post_response(
&context,
orig_post.community_id,
&local_user_view.person,
post_id,
)
.await
build_post_response(&context, orig_post.community_id, local_user_view, post_id).await
}

View File

@ -85,11 +85,5 @@ pub async fn like_post(
)
.await?;
build_post_response(
context.deref(),
post.community_id,
&local_user_view.person,
post_id,
)
.await
build_post_response(context.deref(), post.community_id, local_user_view, post_id).await
}

View File

@ -63,11 +63,5 @@ pub async fn lock_post(
)
.await?;
build_post_response(
&context,
orig_post.community_id,
&local_user_view.person,
post_id,
)
.await
build_post_response(&context, orig_post.community_id, local_user_view, post_id).await
}

View File

@ -34,9 +34,14 @@ pub async fn save_post(
let post_id = data.post_id;
let person_id = local_user_view.person.id;
let post_view = PostView::read(&mut context.pool(), post_id, Some(person_id), false)
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?;
let post_view = PostView::read(
&mut context.pool(),
post_id,
Some(&local_user_view.local_user),
false,
)
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?;
mark_post_as_read(person_id, post_id, &mut context.pool()).await?;

View File

@ -29,9 +29,13 @@ pub async fn purge_comment(
let comment_id = data.comment_id;
// Read the comment to get the post_id and community
let comment_view = CommentView::read(&mut context.pool(), comment_id, None)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let comment_view = CommentView::read(
&mut context.pool(),
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
// Also check that you're a higher admin
check_is_higher_admin(

View File

@ -36,8 +36,8 @@ pub async fn build_comment_response(
local_user_view: Option<LocalUserView>,
recipient_ids: Vec<LocalUserId>,
) -> LemmyResult<CommentResponse> {
let person_id = local_user_view.map(|l| l.person.id);
let comment_view = CommentView::read(&mut context.pool(), comment_id, person_id)
let local_user = local_user_view.map(|l| l.local_user);
let comment_view = CommentView::read(&mut context.pool(), comment_id, local_user.as_ref())
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
Ok(CommentResponse {
@ -54,11 +54,11 @@ pub async fn build_community_response(
let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), &local_user_view.person, community_id)
.await
.is_ok();
let person_id = local_user_view.person.id;
let local_user = local_user_view.local_user;
let community_view = CommunityView::read(
&mut context.pool(),
community_id,
Some(person_id),
Some(&local_user),
is_mod_or_admin,
)
.await?
@ -74,16 +74,17 @@ pub async fn build_community_response(
pub async fn build_post_response(
context: &LemmyContext,
community_id: CommunityId,
person: &Person,
local_user_view: LocalUserView,
post_id: PostId,
) -> LemmyResult<Json<PostResponse>> {
let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), person, community_id)
let local_user = local_user_view.local_user;
let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), &local_user_view.person, community_id)
.await
.is_ok();
let post_view = PostView::read(
&mut context.pool(),
post_id,
Some(person.id),
Some(&local_user),
is_mod_or_admin,
)
.await?
@ -103,6 +104,7 @@ pub async fn send_local_notifs(
let mut recipient_ids = Vec::new();
let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
// let person = my_local_user.person;
// Read the comment view to get extra info
let comment_view = CommentView::read(&mut context.pool(), comment_id, None)
.await?

View File

@ -54,7 +54,7 @@ pub async fn create_comment(
let post_view = PostView::read(
&mut context.pool(),
post_id,
Some(local_user_view.person.id),
Some(&local_user_view.local_user),
true,
)
.await?

View File

@ -21,9 +21,13 @@ pub async fn delete_comment(
local_user_view: LocalUserView,
) -> LemmyResult<Json<CommentResponse>> {
let comment_id = data.comment_id;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let orig_comment = CommentView::read(
&mut context.pool(),
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
// Dont delete it if its already been deleted.
if orig_comment.comment.deleted == data.deleted {

View File

@ -25,9 +25,13 @@ pub async fn remove_comment(
local_user_view: LocalUserView,
) -> LemmyResult<Json<CommentResponse>> {
let comment_id = data.comment_id;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let orig_comment = CommentView::read(
&mut context.pool(),
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
check_community_mod_action(
&local_user_view.person,
@ -76,14 +80,8 @@ pub async fn remove_comment(
};
ModRemoveComment::create(&mut context.pool(), &form).await?;
let recipient_ids = send_local_notifs(
vec![],
comment_id,
&local_user_view.person.clone(),
false,
&context,
)
.await?;
let recipient_ids =
send_local_notifs(vec![], comment_id, &local_user_view.person, false, &context).await?;
let updated_comment_id = updated_comment.id;
ActivityChannel::submit_activity(

View File

@ -36,9 +36,13 @@ pub async fn update_comment(
let local_site = LocalSite::read(&mut context.pool()).await?;
let comment_id = data.comment_id;
let orig_comment = CommentView::read(&mut context.pool(), comment_id, None)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
let orig_comment = CommentView::read(
&mut context.pool(),
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
check_community_user_action(
&local_user_view.person,

View File

@ -188,5 +188,5 @@ pub async fn create_post(
}
};
build_post_response(&context, community_id, &local_user_view.person, post_id).await
build_post_response(&context, community_id, local_user_view, post_id).await
}

View File

@ -62,7 +62,7 @@ pub async fn delete_post(
build_post_response(
&context,
orig_post.community_id,
&local_user_view.person,
local_user_view,
data.post_id,
)
.await

View File

@ -55,9 +55,15 @@ pub async fn get_post(
.await
.is_ok();
let post_view = PostView::read(&mut context.pool(), post_id, person_id, is_mod_or_admin)
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?;
let local_user = local_user_view.map(|l| l.local_user);
let post_view = PostView::read(
&mut context.pool(),
post_id,
local_user.as_ref(),
is_mod_or_admin,
)
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?;
let post_id = post_view.post.id;
if let Some(person_id) = person_id {
@ -76,20 +82,19 @@ pub async fn get_post(
let community_view = CommunityView::read(
&mut context.pool(),
community_id,
person_id,
local_user.as_ref(),
is_mod_or_admin,
)
.await?
.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: local_user.as_ref(),
..Default::default()
}
.list(&local_site.site, &mut context.pool())

View File

@ -81,11 +81,5 @@ pub async fn remove_post(
)
.await?;
build_post_response(
&context,
orig_post.community_id,
&local_user_view.person,
post_id,
)
.await
build_post_response(&context, orig_post.community_id, local_user_view, post_id).await
}

View File

@ -137,7 +137,7 @@ pub async fn update_post(
build_post_response(
context.deref(),
orig_post.community_id,
&local_user_view.person,
local_user_view,
post_id,
)
.await

View File

@ -29,7 +29,7 @@ pub async fn get_community(
check_private_instance(&local_user_view, &local_site)?;
let person_id = local_user_view.as_ref().map(|u| u.person.id);
let local_user = local_user_view.as_ref().map(|u| &u.local_user);
let community_id = match data.id {
Some(id) => id,
@ -53,7 +53,7 @@ pub async fn get_community(
let community_view = CommunityView::read(
&mut context.pool(),
community_id,
person_id,
local_user,
is_mod_or_admin,
)
.await?

View File

@ -10,7 +10,7 @@ use lemmy_api_common::{
site::{ResolveObject, ResolveObjectResponse},
utils::check_private_instance,
};
use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool};
use lemmy_db_schema::{source::local_site::LocalSite, utils::DbPool};
use lemmy_db_views::structs::{CommentView, LocalUserView, PostView};
use lemmy_db_views_actor::structs::{CommunityView, PersonView};
use lemmy_utils::error::{LemmyErrorExt2, LemmyErrorType, LemmyResult};
@ -23,10 +23,9 @@ pub async fn resolve_object(
) -> LemmyResult<Json<ResolveObjectResponse>> {
let local_site = LocalSite::read(&mut context.pool()).await?;
check_private_instance(&local_user_view, &local_site)?;
let person_id = local_user_view.map(|v| v.person.id);
// If we get a valid personId back we can safely assume that the user is authenticated,
// if there's no personId then the JWT was missing or invalid.
let is_authenticated = person_id.is_some();
let is_authenticated = local_user_view.is_some();
let res = if is_authenticated {
// user is fully authenticated; allow remote lookups as well.
@ -37,24 +36,26 @@ pub async fn resolve_object(
}
.with_lemmy_type(LemmyErrorType::CouldntFindObject)?;
convert_response(res, person_id, &mut context.pool())
convert_response(res, local_user_view, &mut context.pool())
.await
.with_lemmy_type(LemmyErrorType::CouldntFindObject)
}
async fn convert_response(
object: SearchableObjects,
user_id: Option<PersonId>,
local_user_view: Option<LocalUserView>,
pool: &mut DbPool<'_>,
) -> LemmyResult<Json<ResolveObjectResponse>> {
use SearchableObjects::*;
let removed_or_deleted;
let mut res = ResolveObjectResponse::default();
let local_user = local_user_view.map(|l| l.local_user);
match object {
Post(p) => {
removed_or_deleted = p.deleted || p.removed;
res.post = Some(
PostView::read(pool, p.id, user_id, false)
PostView::read(pool, p.id, local_user.as_ref(), false)
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?,
)
@ -62,7 +63,7 @@ async fn convert_response(
Comment(c) => {
removed_or_deleted = c.deleted || c.removed;
res.comment = Some(
CommentView::read(pool, c.id, user_id)
CommentView::read(pool, c.id, local_user.as_ref())
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?,
)
@ -79,7 +80,7 @@ async fn convert_response(
UserOrCommunity::Community(c) => {
removed_or_deleted = c.deleted || c.removed;
res.community = Some(
CommunityView::read(pool, c.id, user_id, false)
CommunityView::read(pool, c.id, local_user.as_ref(), false)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?,
)

View File

@ -1,6 +1,6 @@
use crate::{
newtypes::{CommunityId, DbUrl, LanguageId, LocalUserId, PersonId},
schema::{community_moderator, local_user, person, registration_application},
schema::{community, community_moderator, local_user, person, registration_application},
source::{
actor_language::LocalUserLanguage,
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
@ -13,6 +13,7 @@ use crate::{
now,
DbPool,
},
CommunityVisibility,
};
use bcrypt::{hash, DEFAULT_COST};
use diesel::{
@ -295,6 +296,12 @@ pub trait LocalUserOptionHelper {
fn show_read_posts(&self) -> bool;
fn is_admin(&self) -> bool;
fn show_nsfw(&self, site: &Site) -> bool;
fn visible_communities_only<Q>(&self, query: Q) -> Q
where
Q: diesel::query_dsl::methods::FilterDsl<
diesel::dsl::Eq<community::visibility, CommunityVisibility>,
Output = Q,
>;
}
impl LocalUserOptionHelper for Option<&LocalUser> {
@ -323,6 +330,20 @@ impl LocalUserOptionHelper for Option<&LocalUser> {
.map(|l| l.show_nsfw)
.unwrap_or(site.content_warning.is_some())
}
fn visible_communities_only<Q>(&self, query: Q) -> Q
where
Q: diesel::query_dsl::methods::FilterDsl<
diesel::dsl::Eq<community::visibility, CommunityVisibility>,
Output = Q,
>,
{
if self.is_none() {
query.filter(community::visibility.eq(CommunityVisibility::Public))
} else {
query
}
}
}
impl LocalUserInsertForm {

View File

@ -1,15 +1,7 @@
use crate::{
diesel::ExpressionMethods,
newtypes::{DbUrl, PersonId},
schema::community,
CommentSortType,
CommunityVisibility,
SortType,
};
use crate::{newtypes::DbUrl, CommentSortType, SortType};
use chrono::{DateTime, TimeDelta, Utc};
use deadpool::Runtime;
use diesel::{
dsl,
helper_types::AsExprOf,
pg::Pg,
query_builder::{Query, QueryFragment},
@ -592,20 +584,6 @@ 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 {

View File

@ -36,22 +36,13 @@ use lemmy_db_schema::{
post,
},
source::local_user::LocalUser,
utils::{
fuzzy_search,
limit_and_offset,
visible_communities_only,
DbConn,
DbPool,
ListFn,
Queries,
ReadFn,
},
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
CommentSortType,
ListingType,
};
fn queries<'a>() -> Queries<
impl ReadFn<'a, CommentView, (CommentId, Option<PersonId>)>,
impl ReadFn<'a, CommentView, (CommentId, Option<&'a LocalUser>)>,
impl ListFn<'a, CommentView, CommentQuery<'a>>,
> {
let is_creator_banned_from_community = exists(
@ -182,9 +173,12 @@ 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);
query = visible_communities_only(my_person_id, query);
(comment_id, my_local_user): (CommentId, Option<&'a LocalUser>)| async move {
let mut query = all_joins(
comment::table.find(comment_id).into_boxed(),
my_local_user.person_id(),
);
query = my_local_user.visible_communities_only(query);
query.first(&mut conn).await
};
@ -301,7 +295,7 @@ fn queries<'a>() -> Queries<
query = query.filter(not(is_creator_blocked(person_id_join)));
};
query = visible_communities_only(options.local_user.person_id(), query);
query = options.local_user.visible_communities_only(query);
// A Max depth given means its a tree fetch
let (limit, offset) = if let Some(max_depth) = options.max_depth {
@ -366,16 +360,16 @@ fn queries<'a>() -> Queries<
}
impl CommentView {
pub async fn read(
pub async fn read<'a>(
pool: &mut DbPool<'_>,
comment_id: CommentId,
my_person_id: Option<PersonId>,
my_local_user: Option<&'a LocalUser>,
) -> Result<Option<Self>, Error> {
// If a person is given, then my_vote (res.9), if None, should be 0, not null
// Necessary to differentiate between other person's votes
if let Ok(Some(res)) = queries().read(pool, (comment_id, my_person_id)).await {
if let Ok(Some(res)) = queries().read(pool, (comment_id, my_local_user)).await {
let mut new_view = res.clone();
if my_person_id.is_some() && res.my_vote.is_none() {
if my_local_user.is_some() && res.my_vote.is_none() {
new_view.my_vote = Some(0);
}
if res.comment.deleted || res.comment.removed {
@ -673,7 +667,7 @@ mod tests {
let read_comment_from_blocked_person = CommentView::read(
pool,
data.inserted_comment_1.id,
Some(data.timmy_local_user_view.person.id),
Some(&data.timmy_local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
@ -1168,7 +1162,7 @@ mod tests {
let authenticated_comment = CommentView::read(
pool,
data.inserted_comment_0.id,
Some(data.timmy_local_user_view.person.id),
Some(&data.timmy_local_user_view.local_user),
)
.await;
assert!(authenticated_comment.is_ok());
@ -1208,7 +1202,7 @@ mod tests {
let comment_view = CommentView::read(
pool,
data.inserted_comment_0.id,
Some(inserted_banned_from_comm_local_user.person_id),
Some(&inserted_banned_from_comm_local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
@ -1229,7 +1223,7 @@ mod tests {
let comment_view = CommentView::read(
pool,
data.inserted_comment_0.id,
Some(data.timmy_local_user_view.person.id),
Some(&data.timmy_local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;

View File

@ -49,7 +49,6 @@ use lemmy_db_schema::{
get_conn,
limit_and_offset,
now,
visible_communities_only,
Commented,
DbConn,
DbPool,
@ -64,7 +63,7 @@ use lemmy_db_schema::{
use tracing::debug;
fn queries<'a>() -> Queries<
impl ReadFn<'a, PostView, (PostId, Option<PersonId>, bool)>,
impl ReadFn<'a, PostView, (PostId, Option<&'a LocalUser>, bool)>,
impl ListFn<'a, PostView, (PostQuery<'a>, &'a Site)>,
> {
let is_creator_banned_from_community = exists(
@ -142,6 +141,7 @@ fn queries<'a>() -> Queries<
.single_value()
};
// TODO maybe this should go to localuser also
let all_joins = move |query: post_aggregates::BoxedQuery<'a, Pg>,
my_person_id: Option<PersonId>| {
let is_local_user_banned_from_community_selection: Box<
@ -250,52 +250,56 @@ fn queries<'a>() -> Queries<
))
};
let read =
move |mut conn: DbConn<'a>,
(post_id, my_person_id, is_mod_or_admin): (PostId, Option<PersonId>, bool)| async move {
// The left join below will return None in this case
let person_id_join = my_person_id.unwrap_or(PersonId(-1));
let read = move |mut conn: DbConn<'a>,
(post_id, my_local_user, is_mod_or_admin): (
PostId,
Option<&'a LocalUser>,
bool,
)| async move {
// The left join below will return None in this case
let my_person_id = my_local_user.person_id();
let person_id_join = my_person_id.unwrap_or(PersonId(-1));
let mut query = all_joins(
post_aggregates::table
.filter(post_aggregates::post_id.eq(post_id))
.into_boxed(),
my_person_id,
);
let mut query = all_joins(
post_aggregates::table
.filter(post_aggregates::post_id.eq(post_id))
.into_boxed(),
my_person_id,
);
// Hide deleted and removed for non-admins or mods
if !is_mod_or_admin {
query = query
.filter(
community::removed
.eq(false)
.or(post::creator_id.eq(person_id_join)),
)
.filter(
post::removed
.eq(false)
.or(post::creator_id.eq(person_id_join)),
)
// users can see their own deleted posts
.filter(
community::deleted
.eq(false)
.or(post::creator_id.eq(person_id_join)),
)
.filter(
post::deleted
.eq(false)
.or(post::creator_id.eq(person_id_join)),
);
}
// Hide deleted and removed for non-admins or mods
if !is_mod_or_admin {
query = query
.filter(
community::removed
.eq(false)
.or(post::creator_id.eq(person_id_join)),
)
.filter(
post::removed
.eq(false)
.or(post::creator_id.eq(person_id_join)),
)
// users can see their own deleted posts
.filter(
community::deleted
.eq(false)
.or(post::creator_id.eq(person_id_join)),
)
.filter(
post::deleted
.eq(false)
.or(post::creator_id.eq(person_id_join)),
);
}
query = visible_communities_only(my_person_id, query);
query = my_local_user.visible_communities_only(query);
Commented::new(query)
.text("PostView::read")
.first(&mut conn)
.await
};
Commented::new(query)
.text("PostView::read")
.first(&mut conn)
.await
};
let list = move |mut conn: DbConn<'a>, (options, site): (PostQuery<'a>, &'a Site)| async move {
// The left join below will return None in this case
@ -437,7 +441,7 @@ fn queries<'a>() -> Queries<
}
};
query = visible_communities_only(options.local_user.person_id(), query);
query = options.local_user.visible_communities_only(query);
// Dont filter blocks or missing languages for moderator view type
if let (Some(person_id), false) = (
@ -552,14 +556,14 @@ fn queries<'a>() -> Queries<
}
impl PostView {
pub async fn read(
pub async fn read<'a>(
pool: &mut DbPool<'_>,
post_id: PostId,
my_person_id: Option<PersonId>,
my_local_user: Option<&'a LocalUser>,
is_mod_or_admin: bool,
) -> Result<Option<Self>, Error> {
queries()
.read(pool, (post_id, my_person_id, is_mod_or_admin))
.read(pool, (post_id, my_local_user, is_mod_or_admin))
.await
}
}
@ -938,7 +942,7 @@ mod tests {
let post_listing_single_with_person = PostView::read(
pool,
data.inserted_post.id,
Some(data.local_user_view.person.id),
Some(&data.local_user_view.local_user),
false,
)
.await?
@ -1067,7 +1071,7 @@ mod tests {
let post_listing_single_with_person = PostView::read(
pool,
data.inserted_post.id,
Some(data.local_user_view.person.id),
Some(&data.local_user_view.local_user),
false,
)
.await?
@ -1755,7 +1759,7 @@ mod tests {
let authenticated_post = PostView::read(
pool,
data.inserted_post.id,
Some(data.local_user_view.person.id),
Some(&data.local_user_view.local_user),
false,
)
.await;
@ -1797,7 +1801,7 @@ mod tests {
let post_view = PostView::read(
pool,
data.inserted_post.id,
Some(inserted_banned_from_comm_local_user.person_id),
Some(&inserted_banned_from_comm_local_user),
false,
)
.await?
@ -1819,7 +1823,7 @@ mod tests {
let post_view = PostView::read(
pool,
data.inserted_post.id,
Some(data.local_user_view.person.id),
Some(&data.local_user_view.local_user),
false,
)
.await?

View File

@ -22,27 +22,18 @@ use lemmy_db_schema::{
instance_block,
},
source::{community::CommunityFollower, local_user::LocalUser, site::Site},
utils::{
fuzzy_search,
limit_and_offset,
visible_communities_only,
DbConn,
DbPool,
ListFn,
Queries,
ReadFn,
},
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
ListingType,
SortType,
};
fn queries<'a>() -> Queries<
impl ReadFn<'a, CommunityView, (CommunityId, Option<PersonId>, bool)>,
impl ReadFn<'a, CommunityView, (CommunityId, Option<&'a LocalUser>, bool)>,
impl ListFn<'a, CommunityView, (CommunityQuery<'a>, &'a Site)>,
> {
let all_joins = |query: community::BoxedQuery<'a, Pg>, my_person_id: Option<PersonId>| {
let all_joins = |query: community::BoxedQuery<'a, Pg>, my_local_user: Option<&'a LocalUser>| {
// 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 = my_local_user.person_id().unwrap_or(PersonId(-1));
query
.inner_join(community_aggregates::table)
@ -89,14 +80,14 @@ fn queries<'a>() -> Queries<
.and(community::deleted.eq(false));
let read = move |mut conn: DbConn<'a>,
(community_id, my_person_id, is_mod_or_admin): (
(community_id, my_local_user, is_mod_or_admin): (
CommunityId,
Option<PersonId>,
Option<&'a LocalUser>,
bool,
)| async move {
let mut query = all_joins(
community::table.find(community_id).into_boxed(),
my_person_id,
my_local_user,
)
.select(selection);
@ -105,7 +96,7 @@ fn queries<'a>() -> Queries<
query = query.filter(not_removed_or_deleted);
}
query = visible_communities_only(my_person_id, query);
query = my_local_user.visible_communities_only(query);
query.first(&mut conn).await
};
@ -116,11 +107,7 @@ fn queries<'a>() -> Queries<
// The left join below will return None in this case
let person_id_join = options.local_user.person_id().unwrap_or(PersonId(-1));
let mut query = all_joins(
community::table.into_boxed(),
options.local_user.person_id(),
)
.select(selection);
let mut query = all_joins(community::table.into_boxed(), options.local_user).select(selection);
if let Some(search_term) = options.search_term {
let searcher = fuzzy_search(&search_term);
@ -173,7 +160,7 @@ fn queries<'a>() -> Queries<
query = query.filter(community::nsfw.eq(false));
}
query = visible_communities_only(options.local_user.person_id(), query);
query = options.local_user.visible_communities_only(query);
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
query
@ -187,14 +174,14 @@ fn queries<'a>() -> Queries<
}
impl CommunityView {
pub async fn read(
pub async fn read<'a>(
pool: &mut DbPool<'_>,
community_id: CommunityId,
my_person_id: Option<PersonId>,
my_local_user: Option<&'a LocalUser>,
is_mod_or_admin: bool,
) -> Result<Option<Self>, Error> {
queries()
.read(pool, (community_id, my_person_id, is_mod_or_admin))
.read(pool, (community_id, my_local_user, is_mod_or_admin))
.await
}
@ -385,7 +372,7 @@ mod tests {
let authenticated_community = CommunityView::read(
pool,
data.inserted_community.id,
Some(data.local_user.person_id),
Some(&data.local_user),
false,
)
.await;