Skip to content

Commit

Permalink
change post/community query params
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Jan 24, 2024
1 parent a4a4693 commit 2bcef2d
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 353 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 15 additions & 16 deletions crates/api_crud/src/community/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use lemmy_api_common::{
context::LemmyContext,
utils::{check_private_instance, is_admin},
};
use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_db_views::structs::LocalUserView;
use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_db_views_actor::community_view::CommunityQuery;
use lemmy_utils::error::LemmyError;

Expand All @@ -15,32 +14,32 @@ pub async fn list_communities(
context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> Result<Json<ListCommunitiesResponse>, LemmyError> {
let local_site = LocalSite::read(&mut context.pool()).await?;
let local_site = SiteView::read_local(&mut context.pool()).await?;
let is_admin = local_user_view
.as_ref()
.map(|luv| is_admin(luv).is_ok())
.unwrap_or_default();

check_private_instance(&local_user_view, &local_site)?;
check_private_instance(&local_user_view, &local_site.local_site)?;

let sort = data.sort;
let listing_type = data.type_;
let show_nsfw = data.show_nsfw.unwrap_or_default();
let page = data.page;
let limit = data.limit;
let local_user = local_user_view.map(|l| l.local_user);
let communities = CommunityQuery::builder()
.local_site(local_site)
.listing_type(listing_type)
.show_nsfw(show_nsfw)
.sort(sort)
.local_user(local_user.as_ref())
.page(page)
.limit(limit)
.is_mod_or_admin(is_admin)
.build()
.list(&mut context.pool())
.await?;
let communities = CommunityQuery {
listing_type,
show_nsfw,
sort,
local_user: local_user.as_ref(),
page,
limit,
is_mod_or_admin: is_admin,
..Default::default()
}
.list(&local_site.site, &mut context.pool())
.await?;

// Return the jwt
Ok(Json(ListCommunitiesResponse { communities }))
Expand Down
20 changes: 10 additions & 10 deletions crates/api_crud/src/post/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use lemmy_api_common::{
};
use lemmy_db_schema::{
aggregates::structs::{PersonPostAggregates, PersonPostAggregatesForm},
source::{comment::Comment, local_site::LocalSite, post::Post},
source::{comment::Comment, post::Post},
traits::Crud,
};
use lemmy_db_views::{
post_view::PostQuery,
structs::{LocalUserView, PostView},
structs::{LocalUserView, PostView, SiteView},
};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
Expand All @@ -22,9 +22,9 @@ pub async fn get_post(
context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> Result<Json<GetPostResponse>, LemmyError> {
let local_site = LocalSite::read(&mut context.pool()).await?;
let local_site = SiteView::read_local(&mut context.pool()).await?;

check_private_instance(&local_user_view, &local_site)?;
check_private_instance(&local_user_view, &local_site.local_site)?;

let person_id = local_user_view.as_ref().map(|u| u.person.id);

Expand Down Expand Up @@ -89,12 +89,12 @@ pub async fn get_post(

// Fetch the cross_posts
let cross_posts = if let Some(url) = &post_view.post.url {
let mut x_posts = PostQuery::builder()
.local_site(local_site)
.url_search(Some(url.inner().as_str().into()))
.build()
.list(&mut context.pool())
.await?;
let mut x_posts = PostQuery {
url_search: Some(url.inner().as_str().into()),
..Default::default()
}
.list(&local_site.site, &mut context.pool())
.await?;

// Don't return this post as one of the cross_posts
x_posts.retain(|x| x.post.id != post_id);
Expand Down
4 changes: 2 additions & 2 deletions crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub async fn create_site(
inbox_url,
private_key: Some(Some(keypair.private_key)),
public_key: Some(keypair.public_key),
content_warning: diesel_option_overwrite(data.content_warning.clone()),
..Default::default()
};

Expand Down Expand Up @@ -89,7 +90,6 @@ pub async fn create_site(
federation_enabled: data.federation_enabled,
captcha_enabled: data.captcha_enabled,
captcha_difficulty: data.captcha_difficulty.clone(),
content_warning: diesel_option_overwrite(data.content_warning.clone()),
default_post_listing_mode: data.default_post_listing_mode,
..Default::default()
};
Expand Down Expand Up @@ -559,7 +559,7 @@ mod tests {
taglines: None,
registration_mode: site_registration_mode,
content_warning: None,
auto_expand_images: None,
default_post_listing_mode: None,
}
}
}
4 changes: 2 additions & 2 deletions crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub async fn update_site(
description: diesel_option_overwrite(data.description.clone()),
icon: diesel_option_overwrite_to_url(&data.icon)?,
banner: diesel_option_overwrite_to_url(&data.banner)?,
content_warning: diesel_option_overwrite(data.content_warning.clone()),
updated: Some(Some(naive_now())),
..Default::default()
};
Expand Down Expand Up @@ -90,7 +91,6 @@ pub async fn update_site(
captcha_enabled: data.captcha_enabled,
captcha_difficulty: data.captcha_difficulty.clone(),
reports_email_admins: data.reports_email_admins,
content_warning: diesel_option_overwrite(data.content_warning.clone()),
default_post_listing_mode: data.default_post_listing_mode,
..Default::default()
};
Expand Down Expand Up @@ -558,7 +558,7 @@ mod tests {
registration_mode: site_registration_mode,
reports_email_admins: None,
content_warning: None,
auto_expand_images: None,
default_post_listing_mode: None,
}
}
}
42 changes: 21 additions & 21 deletions crates/apub/src/api/list_posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use lemmy_api_common::{
post::{GetPosts, GetPostsResponse},
utils::check_private_instance,
};
use lemmy_db_schema::source::{community::Community, local_site::LocalSite};
use lemmy_db_schema::source::community::Community;
use lemmy_db_views::{
post_view::PostQuery,
structs::{LocalUserView, PaginationCursor},
structs::{LocalUserView, PaginationCursor, SiteView},
};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};

Expand All @@ -23,9 +23,9 @@ pub async fn list_posts(
context: Data<LemmyContext>,
local_user_view: Option<LocalUserView>,
) -> Result<Json<GetPostsResponse>, LemmyError> {
let local_site = LocalSite::read(&mut context.pool()).await?;
let local_site = SiteView::read_local(&mut context.pool()).await?;

check_private_instance(&local_user_view, &local_site)?;
check_private_instance(&local_user_view, &local_site.local_site)?;

let sort = data.sort;

Expand All @@ -47,7 +47,7 @@ pub async fn list_posts(

let listing_type = Some(listing_type_with_default(
data.type_,
&local_site,
&local_site.local_site,
community_id,
)?);
// parse pagination token
Expand All @@ -57,22 +57,22 @@ pub async fn list_posts(
None
};

let posts = PostQuery::builder()
.local_site(local_site)
.local_user(local_user_view.as_ref())
.listing_type(listing_type)
.sort(sort)
.community_id(community_id)
.saved_only(saved_only)
.liked_only(liked_only)
.disliked_only(disliked_only)
.page(page)
.page_after(page_after)
.limit(limit)
.build()
.list(&mut context.pool())
.await
.with_lemmy_type(LemmyErrorType::CouldntGetPosts)?;
let posts = PostQuery {
local_user: local_user_view.as_ref(),
listing_type,
sort,
community_id,
saved_only,
liked_only,
disliked_only,
page,
page_after,
limit,
..Default::default()
}
.list(&local_site.site, &mut context.pool())
.await
.with_lemmy_type(LemmyErrorType::CouldntGetPosts)?;

// if this page wasn't empty, then there is a next page after the last post on this page
let next_page = posts.last().map(PaginationCursor::after_post);
Expand Down
37 changes: 19 additions & 18 deletions crates/apub/src/api/read_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use lemmy_api_common::{
person::{GetPersonDetails, GetPersonDetailsResponse},
utils::check_private_instance,
};
use lemmy_db_schema::{
source::{local_site::LocalSite, person::Person},
utils::post_to_comment_sort_type,
use lemmy_db_schema::{source::person::Person, utils::post_to_comment_sort_type};
use lemmy_db_views::{
comment_view::CommentQuery,
post_view::PostQuery,
structs::{LocalUserView, SiteView},
};
use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery, structs::LocalUserView};
use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonView};
use lemmy_utils::error::{LemmyError, LemmyErrorExt2, LemmyErrorType};

Expand All @@ -25,9 +26,9 @@ pub async fn read_person(
Err(LemmyErrorType::NoIdGiven)?
}

let local_site = LocalSite::read(&mut context.pool()).await?;
let local_site = SiteView::read_local(&mut context.pool()).await?;

check_private_instance(&local_user_view, &local_site)?;
check_private_instance(&local_user_view, &local_site.local_site)?;

let person_details_id = match data.person_id {
Some(id) => id,
Expand Down Expand Up @@ -60,18 +61,18 @@ pub async fn read_person(
None
};

let posts = PostQuery::builder()
.local_site(local_site)
.local_user(local_user_view.as_ref())
.sort(sort)
.community_id(community_id)
.saved_only(saved_only)
.page(page)
.limit(limit)
.creator_id(creator_id)
.build()
.list(&mut context.pool())
.await?;
let posts = PostQuery {
sort,
saved_only,
local_user: local_user_view.as_ref(),
community_id,
page,
limit,
creator_id,
..Default::default()
}
.list(&local_site.site, &mut context.pool())
.await?;

let comments = CommentQuery {
local_user: local_user_view.as_ref(),
Expand Down
Loading

0 comments on commit 2bcef2d

Please sign in to comment.