Show federated posts on user profile. Fixes #4228 (#4229)

* Show federated posts on user profile. Fixes #4228

* Make sure posts are hidden if no listing_type is given.
api-tests-explicit-limit
Dessalines 2023-12-11 14:36:12 -05:00 committed by GitHub
parent e317947b75
commit f764996745
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 56 deletions

View File

@ -65,7 +65,6 @@ pub async fn read_person(
saved_only, saved_only,
local_user: local_user_view.as_ref(), local_user: local_user_view.as_ref(),
community_id, community_id,
is_profile_view: true,
page, page,
limit, limit,
creator_id, creator_id,
@ -79,7 +78,6 @@ pub async fn read_person(
sort: sort.map(post_to_comment_sort_type), sort: sort.map(post_to_comment_sort_type),
saved_only, saved_only,
community_id, community_id,
is_profile_view: true,
page, page,
limit, limit,
creator_id, creator_id,

View File

@ -177,14 +177,18 @@ fn queries<'a>() -> Queries<
}; };
let list = move |mut conn: DbConn<'a>, options: CommentQuery<'a>| async move { let list = move |mut conn: DbConn<'a>, options: CommentQuery<'a>| async move {
let person_id = options.local_user.map(|l| l.person.id); let my_person_id = options.local_user.map(|l| l.person.id);
let local_user_id = options.local_user.map(|l| l.local_user.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 // The left join below will return None in this case
let person_id_join = person_id.unwrap_or(PersonId(-1)); let person_id_join = my_person_id.unwrap_or(PersonId(-1));
let local_user_id_join = local_user_id.unwrap_or(LocalUserId(-1)); let local_user_id_join = my_local_user_id.unwrap_or(LocalUserId(-1));
let mut query = all_joins(comment::table.into_boxed(), person_id, options.saved_only); let mut query = all_joins(
comment::table.into_boxed(),
my_person_id,
options.saved_only,
);
if let Some(creator_id) = options.creator_id { if let Some(creator_id) = options.creator_id {
query = query.filter(comment::creator_id.eq(creator_id)); query = query.filter(comment::creator_id.eq(creator_id));
@ -373,7 +377,6 @@ pub struct CommentQuery<'a> {
pub saved_only: bool, pub saved_only: bool,
pub liked_only: bool, pub liked_only: bool,
pub disliked_only: bool, pub disliked_only: bool,
pub is_profile_view: bool,
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub max_depth: Option<i32>, pub max_depth: Option<i32>,

View File

@ -301,16 +301,16 @@ fn queries<'a>() -> Queries<
}; };
let list = move |mut conn: DbConn<'a>, options: PostQuery<'a>| async move { let list = move |mut conn: DbConn<'a>, options: PostQuery<'a>| async move {
let person_id = options.local_user.map(|l| l.person.id); let my_person_id = options.local_user.map(|l| l.person.id);
let local_user_id = options.local_user.map(|l| l.local_user.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 // The left join below will return None in this case
let person_id_join = person_id.unwrap_or(PersonId(-1)); let person_id_join = my_person_id.unwrap_or(PersonId(-1));
let local_user_id_join = local_user_id.unwrap_or(LocalUserId(-1)); let local_user_id_join = my_local_user_id.unwrap_or(LocalUserId(-1));
let mut query = all_joins( let mut query = all_joins(
post_aggregates::table.into_boxed(), post_aggregates::table.into_boxed(),
person_id, my_person_id,
options.saved_only, options.saved_only,
); );
@ -318,7 +318,7 @@ fn queries<'a>() -> Queries<
query = query.filter(community::deleted.eq(false)); query = query.filter(community::deleted.eq(false));
// only show deleted posts to creator // only show deleted posts to creator
if let Some(person_id) = person_id { if let Some(person_id) = my_person_id {
query = query.filter(post::deleted.eq(false).or(post::creator_id.eq(person_id))); query = query.filter(post::deleted.eq(false).or(post::creator_id.eq(person_id)));
} else { } else {
query = query.filter(post::deleted.eq(false)); query = query.filter(post::deleted.eq(false));
@ -329,7 +329,7 @@ fn queries<'a>() -> Queries<
.map(|l| l.local_user.admin) .map(|l| l.local_user.admin)
.unwrap_or(false); .unwrap_or(false);
// only show removed posts to admin when viewing user profile // only show removed posts to admin when viewing user profile
if !(options.is_profile_view && is_admin) { if !(options.creator_id.is_some() && is_admin) {
query = query query = query
.filter(community::removed.eq(false)) .filter(community::removed.eq(false))
.filter(post::removed.eq(false)); .filter(post::removed.eq(false));
@ -352,7 +352,8 @@ fn queries<'a>() -> Queries<
query = query.filter(post_aggregates::creator_id.eq(creator_id)); query = query.filter(post_aggregates::creator_id.eq(creator_id));
} }
if let Some(person_id) = person_id { if let Some(listing_type) = options.listing_type {
if let Some(person_id) = my_person_id {
let is_subscribed = exists( let is_subscribed = exists(
community_follower::table.filter( community_follower::table.filter(
post_aggregates::community_id post_aggregates::community_id
@ -360,7 +361,7 @@ fn queries<'a>() -> Queries<
.and(community_follower::person_id.eq(person_id)), .and(community_follower::person_id.eq(person_id)),
), ),
); );
match options.listing_type.unwrap_or_default() { match listing_type {
ListingType::Subscribed => query = query.filter(is_subscribed), ListingType::Subscribed => query = query.filter(is_subscribed),
ListingType::Local => { ListingType::Local => {
query = query query = query
@ -378,8 +379,10 @@ fn queries<'a>() -> Queries<
)); ));
} }
} }
} else { }
match options.listing_type.unwrap_or_default() { // If your person_id is missing, only show local
else {
match listing_type {
ListingType::Local => { ListingType::Local => {
query = query query = query
.filter(community::local.eq(true)) .filter(community::local.eq(true))
@ -388,6 +391,9 @@ fn queries<'a>() -> Queries<
_ => query = query.filter(community::hidden.eq(false)), _ => query = query.filter(community::hidden.eq(false)),
} }
} }
} else {
query = query.filter(community::hidden.eq(false));
}
if let Some(url_search) = &options.url_search { if let Some(url_search) = &options.url_search {
query = query.filter(post::url.eq(url_search)); query = query.filter(post::url.eq(url_search));
@ -420,7 +426,7 @@ fn queries<'a>() -> Queries<
query = query.filter(person::bot_account.eq(false)); query = query.filter(person::bot_account.eq(false));
}; };
if let (true, Some(person_id)) = (options.saved_only, person_id) { if let (true, Some(person_id)) = (options.saved_only, my_person_id) {
query = query.filter(is_saved(person_id)); query = query.filter(is_saved(person_id));
} }
// Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read
@ -431,12 +437,12 @@ fn queries<'a>() -> Queries<
.unwrap_or(true) .unwrap_or(true)
{ {
// Do not hide read posts when it is a user profile view // Do not hide read posts when it is a user profile view
if let (false, Some(person_id)) = (options.is_profile_view, person_id) { if let (Some(_creator_id), Some(person_id)) = (options.creator_id, my_person_id) {
query = query.filter(not(is_read(person_id))); query = query.filter(not(is_read(person_id)));
} }
} }
if let Some(person_id) = person_id { if let Some(person_id) = my_person_id {
if options.liked_only { if options.liked_only {
query = query.filter(score(person_id).eq(1)); query = query.filter(score(person_id).eq(1));
} else if options.disliked_only { } else if options.disliked_only {
@ -446,7 +452,7 @@ fn queries<'a>() -> Queries<
// Dont filter blocks or missing languages for moderator view type // Dont filter blocks or missing languages for moderator view type
if let (Some(person_id), false) = ( if let (Some(person_id), false) = (
person_id, my_person_id,
options.listing_type.unwrap_or_default() == ListingType::ModeratorView, options.listing_type.unwrap_or_default() == ListingType::ModeratorView,
) { ) {
// Filter out the rows with missing languages // Filter out the rows with missing languages
@ -630,7 +636,6 @@ pub struct PostQuery<'a> {
pub liked_only: bool, pub liked_only: bool,
pub disliked_only: bool, pub disliked_only: bool,
pub moderator_view: bool, pub moderator_view: bool,
pub is_profile_view: bool,
pub page: Option<i64>, pub page: Option<i64>,
pub limit: Option<i64>, pub limit: Option<i64>,
pub page_after: Option<PaginationCursorData>, pub page_after: Option<PaginationCursorData>,
@ -766,6 +771,7 @@ mod tests {
inserted_bot: Person, inserted_bot: Person,
inserted_community: Community, inserted_community: Community,
inserted_post: Post, inserted_post: Post,
inserted_bot_post: Post,
} }
async fn init_data(pool: &mut DbPool<'_>) -> Data { async fn init_data(pool: &mut DbPool<'_>) -> Data {
@ -850,7 +856,7 @@ mod tests {
.community_id(inserted_community.id) .community_id(inserted_community.id)
.build(); .build();
let _inserted_bot_post = Post::create(pool, &new_bot_post).await.unwrap(); let inserted_bot_post = Post::create(pool, &new_bot_post).await.unwrap();
let local_user_view = LocalUserView { let local_user_view = LocalUserView {
local_user: inserted_local_user, local_user: inserted_local_user,
person: inserted_person, person: inserted_person,
@ -864,6 +870,7 @@ mod tests {
inserted_bot, inserted_bot,
inserted_community, inserted_community,
inserted_post, inserted_post,
inserted_bot_post,
} }
} }
@ -1253,7 +1260,7 @@ mod tests {
// Remove the post // Remove the post
Post::update( Post::update(
pool, pool,
data.inserted_post.id, data.inserted_bot_post.id,
&PostUpdateForm { &PostUpdateForm {
removed: Some(true), removed: Some(true),
..Default::default() ..Default::default()
@ -1273,18 +1280,21 @@ mod tests {
.unwrap(); .unwrap();
assert_eq!(1, post_listings_no_admin.len()); assert_eq!(1, post_listings_no_admin.len());
// Removed post is shown to admins on profile page // Removed bot post is shown to admins on its profile page
data.local_user_view.local_user.admin = true; data.local_user_view.local_user.admin = true;
let post_listings_is_admin = PostQuery { let post_listings_is_admin = PostQuery {
sort: Some(SortType::New), sort: Some(SortType::New),
creator_id: Some(data.inserted_bot.id),
local_user: Some(&data.local_user_view), local_user: Some(&data.local_user_view),
is_profile_view: true,
..Default::default() ..Default::default()
} }
.list(pool) .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(2, post_listings_is_admin.len()); assert_eq!(
data.inserted_bot.id,
post_listings_is_admin[0].post.creator_id
);
cleanup(data, pool).await; cleanup(data, pool).await;
} }