Remove TypedBuilder from db_views and db_views_actor (#3637)

* change pool fields to parameters for list

* remove my_person_id and admin fields

* Change recipient id to list param

* Remove TypedBuilder from db_views and db_views_actor
This commit is contained in:
dullbananas 2023-07-17 03:20:25 -07:00 committed by GitHub
parent 6688a8a5d4
commit 88215bfbc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 672 additions and 740 deletions

2
Cargo.lock generated
View file

@ -2769,7 +2769,6 @@ dependencies = [
"tokio",
"tracing",
"ts-rs",
"typed-builder",
]
[[package]]
@ -2782,7 +2781,6 @@ dependencies = [
"serde",
"serde_with",
"ts-rs",
"typed-builder",
]
[[package]]

View file

@ -22,24 +22,19 @@ impl Perform for ListCommentReports {
let data: &ListCommentReports = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
let person_id = local_user_view.person.id;
let admin = local_user_view.person.admin;
let community_id = data.community_id;
let unresolved_only = data.unresolved_only;
let page = data.page;
let limit = data.limit;
let comment_reports = CommentReportQuery::builder()
.pool(&mut context.pool())
.my_person_id(person_id)
.admin(admin)
.community_id(community_id)
.unresolved_only(unresolved_only)
.page(page)
.limit(limit)
.build()
.list()
.await?;
let comment_reports = CommentReportQuery {
community_id,
unresolved_only,
page,
limit,
}
.list(&mut context.pool(), &local_user_view.person)
.await?;
Ok(ListCommentReportsResponse { comment_reports })
}

View file

@ -27,18 +27,17 @@ impl Perform for GetPersonMentions {
let person_id = Some(local_user_view.person.id);
let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts);
let mentions = PersonMentionQuery::builder()
.pool(&mut context.pool())
.recipient_id(person_id)
.my_person_id(person_id)
.sort(sort)
.unread_only(unread_only)
.show_bot_accounts(show_bot_accounts)
.page(page)
.limit(limit)
.build()
.list()
.await?;
let mentions = PersonMentionQuery {
recipient_id: person_id,
my_person_id: person_id,
sort,
unread_only,
show_bot_accounts,
page,
limit,
}
.list(&mut context.pool())
.await?;
Ok(GetPersonMentionsResponse { mentions })
}

View file

@ -24,18 +24,17 @@ impl Perform for GetReplies {
let person_id = Some(local_user_view.person.id);
let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts);
let replies = CommentReplyQuery::builder()
.pool(&mut context.pool())
.recipient_id(person_id)
.my_person_id(person_id)
.sort(sort)
.unread_only(unread_only)
.show_bot_accounts(show_bot_accounts)
.page(page)
.limit(limit)
.build()
.list()
.await?;
let replies = CommentReplyQuery {
recipient_id: person_id,
my_person_id: person_id,
sort,
unread_only,
show_bot_accounts,
page,
limit,
}
.list(&mut context.pool())
.await?;
Ok(GetRepliesResponse { replies })
}

View file

@ -22,24 +22,19 @@ impl Perform for ListPostReports {
let data: &ListPostReports = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
let person_id = local_user_view.person.id;
let admin = local_user_view.person.admin;
let community_id = data.community_id;
let unresolved_only = data.unresolved_only;
let page = data.page;
let limit = data.limit;
let post_reports = PostReportQuery::builder()
.pool(&mut context.pool())
.my_person_id(person_id)
.admin(admin)
.community_id(community_id)
.unresolved_only(unresolved_only)
.page(page)
.limit(limit)
.build()
.list()
.await?;
let post_reports = PostReportQuery {
community_id,
unresolved_only,
page,
limit,
}
.list(&mut context.pool(), &local_user_view.person)
.await?;
Ok(ListPostReportsResponse { post_reports })
}

View file

@ -21,14 +21,13 @@ impl Perform for ListPrivateMessageReports {
let unresolved_only = self.unresolved_only;
let page = self.page;
let limit = self.limit;
let private_message_reports = PrivateMessageReportQuery::builder()
.pool(&mut context.pool())
.unresolved_only(unresolved_only)
.page(page)
.limit(limit)
.build()
.list()
.await?;
let private_message_reports = PrivateMessageReportQuery {
unresolved_only,
page,
limit,
}
.list(&mut context.pool())
.await?;
Ok(ListPrivateMessageReportsResponse {
private_message_reports,

View file

@ -23,19 +23,18 @@ impl Perform for ListRegistrationApplications {
is_admin(&local_user_view)?;
let unread_only = data.unread_only;
let verified_email_only = local_site.require_email_verification;
let verified_email_only = Some(local_site.require_email_verification);
let page = data.page;
let limit = data.limit;
let registration_applications = RegistrationApplicationQuery::builder()
.pool(&mut context.pool())
.unread_only(unread_only)
.verified_email_only(Some(verified_email_only))
.page(page)
.limit(limit)
.build()
.list()
.await?;
let registration_applications = RegistrationApplicationQuery {
unread_only,
verified_email_only,
page,
limit,
}
.list(&mut context.pool())
.await?;
Ok(Self::Response {
registration_applications,

View file

@ -667,13 +667,13 @@ pub async fn remove_user_data_in_community(
// Comments
// TODO Diesel doesn't allow updates with joins, so this has to be a loop
let comments = CommentQuery::builder()
.pool(pool)
.creator_id(Some(banned_person_id))
.community_id(Some(community_id))
.build()
.list()
.await?;
let comments = CommentQuery {
creator_id: Some(banned_person_id),
community_id: Some(community_id),
..Default::default()
}
.list(pool)
.await?;
for comment_view in &comments {
let comment_id = comment_view.comment.id;

View file

@ -31,18 +31,18 @@ impl PerformCrud for ListCommunities {
let page = data.page;
let limit = data.limit;
let local_user = local_user_view.map(|l| l.local_user);
let communities = CommunityQuery::builder()
.pool(&mut context.pool())
.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()
.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(&mut context.pool())
.await?;
// Return the jwt
Ok(ListCommunitiesResponse { communities })

View file

@ -100,12 +100,12 @@ impl PerformCrud for GetPost {
// Fetch the cross_posts
let cross_posts = if let Some(url) = &post_view.post.url {
let mut x_posts = PostQuery::builder()
.pool(&mut context.pool())
.url_search(Some(url.inner().as_str().into()))
.build()
.list()
.await?;
let mut x_posts = PostQuery {
url_search: Some(url.inner().as_str().into()),
..Default::default()
}
.list(&mut context.pool())
.await?;
// Don't return this post as one of the cross_posts
x_posts.retain(|x| x.post.id != post_id);

View file

@ -24,15 +24,13 @@ impl PerformCrud for GetPrivateMessages {
let page = data.page;
let limit = data.limit;
let unread_only = data.unread_only;
let mut messages = PrivateMessageQuery::builder()
.pool(&mut context.pool())
.recipient_id(person_id)
.page(page)
.limit(limit)
.unread_only(unread_only)
.build()
.list()
.await?;
let mut messages = PrivateMessageQuery {
page,
limit,
unread_only,
}
.list(&mut context.pool(), person_id)
.await?;
// Messages sent by ourselves should be marked as read. The `read` column in database is only
// for the recipient, and shouldnt be exposed to sender.

View file

@ -39,7 +39,11 @@ pub async fn list_comments(
let limit = data.limit;
let parent_id = data.parent_id;
let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?;
let listing_type = Some(listing_type_with_default(
data.type_,
&local_site,
community_id,
)?);
// If a parent_id is given, fetch the comment to get the path
let parent_path = if let Some(parent_id) = parent_id {
@ -51,22 +55,22 @@ pub async fn list_comments(
let parent_path_cloned = parent_path.clone();
let post_id = data.post_id;
let local_user = local_user_view.map(|l| l.local_user);
let comments = CommentQuery::builder()
.pool(&mut context.pool())
.listing_type(Some(listing_type))
.sort(sort)
.max_depth(max_depth)
.saved_only(saved_only)
.community_id(community_id)
.parent_path(parent_path_cloned)
.post_id(post_id)
.local_user(local_user.as_ref())
.page(page)
.limit(limit)
.build()
.list()
.await
.with_lemmy_type(LemmyErrorType::CouldntGetComments)?;
let comments = CommentQuery {
listing_type,
sort,
max_depth,
saved_only,
community_id,
parent_path: parent_path_cloned,
post_id,
local_user: local_user.as_ref(),
page,
limit,
..Default::default()
}
.list(&mut context.pool())
.await
.with_lemmy_type(LemmyErrorType::CouldntGetComments)?;
Ok(Json(GetCommentsResponse { comments }))
}

View file

@ -36,27 +36,32 @@ pub async fn list_posts(
};
let saved_only = data.saved_only;
let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?;
let listing_type = Some(listing_type_with_default(
data.type_,
&local_site,
community_id,
)?);
let is_mod_or_admin =
let is_mod_or_admin = Some(
is_mod_or_admin_opt(&mut context.pool(), local_user_view.as_ref(), community_id)
.await
.is_ok();
.is_ok(),
);
let posts = PostQuery::builder()
.pool(&mut context.pool())
.local_user(local_user_view.map(|l| l.local_user).as_ref())
.listing_type(Some(listing_type))
.sort(sort)
.community_id(community_id)
.saved_only(saved_only)
.page(page)
.limit(limit)
.is_mod_or_admin(Some(is_mod_or_admin))
.build()
.list()
.await
.with_lemmy_type(LemmyErrorType::CouldntGetPosts)?;
let posts = PostQuery {
local_user: local_user_view.map(|l| l.local_user).as_ref(),
listing_type,
sort,
community_id,
saved_only,
page,
limit,
is_mod_or_admin,
..Default::default()
}
.list(&mut context.pool())
.await
.with_lemmy_type(LemmyErrorType::CouldntGetPosts)?;
Ok(Json(GetPostsResponse { posts }))
}

View file

@ -56,49 +56,49 @@ pub async fn read_person(
let local_user = local_user_view.map(|l| l.local_user);
let local_user_clone = local_user.clone();
let posts = PostQuery::builder()
.pool(&mut context.pool())
.sort(sort)
.saved_only(saved_only)
.local_user(local_user.as_ref())
.community_id(community_id)
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.creator_id(
let posts = PostQuery {
sort,
saved_only,
local_user:local_user.as_ref(),
community_id,
is_mod_or_admin: is_admin,
page,
limit,
creator_id:
// If its saved only, you don't care what creator it was
// Or, if its not saved, then you only want it for that specific creator
if !saved_only.unwrap_or(false) {
Some(person_details_id)
} else {
None
},
)
.build()
.list()
.await?;
}
,
..Default::default()
}
.list(&mut context.pool())
.await?;
let comments = CommentQuery::builder()
.pool(&mut context.pool())
.local_user(local_user_clone.as_ref())
.sort(sort.map(post_to_comment_sort_type))
.saved_only(saved_only)
.show_deleted_and_removed(Some(false))
.community_id(community_id)
.page(page)
.limit(limit)
.creator_id(
let comments = CommentQuery {
local_user: (local_user_clone.as_ref()),
sort: (sort.map(post_to_comment_sort_type)),
saved_only: (saved_only),
show_deleted_and_removed: (Some(false)),
community_id: (community_id),
page: (page),
limit: (limit),
creator_id: (
// If its saved only, you don't care what creator it was
// Or, if its not saved, then you only want it for that specific creator
if !saved_only.unwrap_or(false) {
Some(person_details_id)
} else {
None
},
)
.build()
.list()
.await?;
}
),
..Default::default()
}
.list(&mut context.pool())
.await?;
let moderates =
CommunityModeratorView::for_person(&mut context.pool(), person_details_id).await?;

View file

@ -53,60 +53,59 @@ pub async fn search(
let local_user = local_user_view.map(|l| l.local_user);
match search_type {
SearchType::Posts => {
posts = PostQuery::builder()
.pool(&mut context.pool())
.sort(sort)
.listing_type(listing_type)
.community_id(community_id)
.creator_id(creator_id)
.local_user(local_user.as_ref())
.search_term(Some(q))
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.build()
.list()
.await?;
posts = PostQuery {
sort: (sort),
listing_type: (listing_type),
community_id: (community_id),
creator_id: (creator_id),
local_user: (local_user.as_ref()),
search_term: (Some(q)),
is_mod_or_admin: (is_admin),
page: (page),
limit: (limit),
..Default::default()
}
.list(&mut context.pool())
.await?;
}
SearchType::Comments => {
comments = CommentQuery::builder()
.pool(&mut context.pool())
.sort(sort.map(post_to_comment_sort_type))
.listing_type(listing_type)
.search_term(Some(q))
.community_id(community_id)
.creator_id(creator_id)
.local_user(local_user.as_ref())
.page(page)
.limit(limit)
.build()
.list()
.await?;
comments = CommentQuery {
sort: (sort.map(post_to_comment_sort_type)),
listing_type: (listing_type),
search_term: (Some(q)),
community_id: (community_id),
creator_id: (creator_id),
local_user: (local_user.as_ref()),
page: (page),
limit: (limit),
..Default::default()
}
.list(&mut context.pool())
.await?;
}
SearchType::Communities => {
communities = CommunityQuery::builder()
.pool(&mut context.pool())
.sort(sort)
.listing_type(listing_type)
.search_term(Some(q))
.local_user(local_user.as_ref())
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.build()
.list()
.await?;
communities = CommunityQuery {
sort: (sort),
listing_type: (listing_type),
search_term: (Some(q)),
local_user: (local_user.as_ref()),
is_mod_or_admin: (is_admin),
page: (page),
limit: (limit),
..Default::default()
}
.list(&mut context.pool())
.await?;
}
SearchType::Users => {
users = PersonQuery::builder()
.pool(&mut context.pool())
.sort(sort)
.search_term(Some(q))
.page(page)
.limit(limit)
.build()
.list()
.await?;
users = PersonQuery {
sort: (sort),
search_term: (Some(q)),
page: (page),
limit: (limit),
}
.list(&mut context.pool())
.await?;
}
SearchType::All => {
// If the community or creator is included, dont search communities or users
@ -114,55 +113,55 @@ pub async fn search(
data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
let local_user_ = local_user.clone();
posts = PostQuery::builder()
.pool(&mut context.pool())
.sort(sort)
.listing_type(listing_type)
.community_id(community_id)
.creator_id(creator_id)
.local_user(local_user_.as_ref())
.search_term(Some(q))
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.build()
.list()
.await?;
posts = PostQuery {
sort: (sort),
listing_type: (listing_type),
community_id: (community_id),
creator_id: (creator_id),
local_user: (local_user_.as_ref()),
search_term: (Some(q)),
is_mod_or_admin: (is_admin),
page: (page),
limit: (limit),
..Default::default()
}
.list(&mut context.pool())
.await?;
let q = data.q.clone();
let local_user_ = local_user.clone();
comments = CommentQuery::builder()
.pool(&mut context.pool())
.sort(sort.map(post_to_comment_sort_type))
.listing_type(listing_type)
.search_term(Some(q))
.community_id(community_id)
.creator_id(creator_id)
.local_user(local_user_.as_ref())
.page(page)
.limit(limit)
.build()
.list()
.await?;
comments = CommentQuery {
sort: (sort.map(post_to_comment_sort_type)),
listing_type: (listing_type),
search_term: (Some(q)),
community_id: (community_id),
creator_id: (creator_id),
local_user: (local_user_.as_ref()),
page: (page),
limit: (limit),
..Default::default()
}
.list(&mut context.pool())
.await?;
let q = data.q.clone();
communities = if community_or_creator_included {
vec![]
} else {
CommunityQuery::builder()
.pool(&mut context.pool())
.sort(sort)
.listing_type(listing_type)
.search_term(Some(q))
.local_user(local_user.as_ref())
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.build()
.list()
.await?
CommunityQuery {
sort: (sort),
listing_type: (listing_type),
search_term: (Some(q)),
local_user: (local_user.as_ref()),
is_mod_or_admin: (is_admin),
page: (page),
limit: (limit),
..Default::default()
}
.list(&mut context.pool())
.await?
};
let q = data.q.clone();
@ -170,31 +169,30 @@ pub async fn search(
users = if community_or_creator_included {
vec![]
} else {
PersonQuery::builder()
.pool(&mut context.pool())
.sort(sort)
.search_term(Some(q))
.page(page)
.limit(limit)
.build()
.list()
.await?
PersonQuery {
sort: (sort),
search_term: (Some(q)),
page: (page),
limit: (limit),
}
.list(&mut context.pool())
.await?
};
}
SearchType::Url => {
posts = PostQuery::builder()
.pool(&mut context.pool())
.sort(sort)
.listing_type(listing_type)
.community_id(community_id)
.creator_id(creator_id)
.url_search(Some(q))
.is_mod_or_admin(is_admin)
.page(page)
.limit(limit)
.build()
.list()
.await?;
posts = PostQuery {
sort: (sort),
listing_type: (listing_type),
community_id: (community_id),
creator_id: (creator_id),
url_search: (Some(q)),
is_mod_or_admin: (is_admin),
page: (page),
limit: (limit),
..Default::default()
}
.list(&mut context.pool())
.await?;
}
};

View file

@ -29,7 +29,6 @@ diesel_ltree = { workspace = true, optional = true }
serde = { workspace = true }
serde_with = { workspace = true }
tracing = { workspace = true, optional = true }
typed-builder = { workspace = true }
ts-rs = { workspace = true, optional = true }
[dev-dependencies]

View file

@ -33,7 +33,6 @@ use lemmy_db_schema::{
traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool},
};
use typed_builder::TypedBuilder;
impl CommentReportView {
/// returns the CommentReportView for the provided report_id
@ -137,24 +136,21 @@ impl CommentReportView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct CommentReportQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
#[builder(!default)]
my_person_id: PersonId,
#[builder(!default)]
admin: bool,
community_id: Option<CommunityId>,
page: Option<i64>,
limit: Option<i64>,
unresolved_only: Option<bool>,
#[derive(Default)]
pub struct CommentReportQuery {
pub community_id: Option<CommunityId>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub unresolved_only: Option<bool>,
}
impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<CommentReportView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl CommentReportQuery {
pub async fn list(
self,
pool: &mut DbPool<'_>,
my_person: &Person,
) -> Result<Vec<CommentReportView>, Error> {
let conn = &mut get_conn(pool).await?;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
@ -183,7 +179,7 @@ impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> {
comment_like::table.on(
comment::id
.eq(comment_like::comment_id)
.and(comment_like::person_id.eq(self.my_person_id)),
.and(comment_like::person_id.eq(my_person.id)),
),
)
.left_join(
@ -220,13 +216,13 @@ impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> {
.offset(offset);
// If its not an admin, get only the ones you mod
let res = if !self.admin {
let res = if !my_person.admin {
query
.inner_join(
community_moderator::table.on(
community_moderator::community_id
.eq(post::community_id)
.and(community_moderator::person_id.eq(self.my_person_id)),
.and(community_moderator::person_id.eq(my_person.id)),
),
)
.load::<<CommentReportView as JoinView>::JoinTuple>(conn)
@ -514,12 +510,8 @@ mod tests {
};
// Do a batch read of timmys reports
let reports = CommentReportQuery::builder()
.pool(pool)
.my_person_id(inserted_timmy.id)
.admin(false)
.build()
.list()
let reports = CommentReportQuery::default()
.list(pool, &inserted_timmy)
.await
.unwrap();
@ -590,15 +582,13 @@ mod tests {
// Do a batch read of timmys reports
// It should only show saras, which is unresolved
let reports_after_resolve = CommentReportQuery::builder()
.pool(pool)
.my_person_id(inserted_timmy.id)
.admin(false)
.unresolved_only(Some(true))
.build()
.list()
.await
.unwrap();
let reports_after_resolve = CommentReportQuery {
unresolved_only: (Some(true)),
..Default::default()
}
.list(pool, &inserted_timmy)
.await
.unwrap();
assert_eq!(reports_after_resolve[0], expected_sara_report_view);
assert_eq!(reports_after_resolve.len(), 1);

View file

@ -40,7 +40,6 @@ use lemmy_db_schema::{
CommentSortType,
ListingType,
};
use typed_builder::TypedBuilder;
type CommentViewTuple = (
Comment,
@ -156,29 +155,26 @@ impl CommentView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct CommentQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
listing_type: Option<ListingType>,
sort: Option<CommentSortType>,
community_id: Option<CommunityId>,
post_id: Option<PostId>,
parent_path: Option<Ltree>,
creator_id: Option<PersonId>,
local_user: Option<&'a LocalUser>,
search_term: Option<String>,
saved_only: Option<bool>,
show_deleted_and_removed: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
max_depth: Option<i32>,
#[derive(Default)]
pub struct CommentQuery<'a> {
pub listing_type: Option<ListingType>,
pub sort: Option<CommentSortType>,
pub community_id: Option<CommunityId>,
pub post_id: Option<PostId>,
pub parent_path: Option<Ltree>,
pub creator_id: Option<PersonId>,
pub local_user: Option<&'a LocalUser>,
pub search_term: Option<String>,
pub saved_only: Option<bool>,
pub show_deleted_and_removed: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub max_depth: Option<i32>,
}
impl<'a, 'b: 'a> CommentQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<CommentView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl<'a> CommentQuery<'a> {
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommentView>, Error> {
let conn = &mut get_conn(pool).await?;
// The left join below will return None in this case
let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
@ -602,29 +598,29 @@ mod tests {
let mut expected_comment_view_with_person = expected_comment_view_no_person.clone();
expected_comment_view_with_person.my_vote = Some(1);
let read_comment_views_no_person = CommentQuery::builder()
.pool(pool)
.sort(Some(CommentSortType::Old))
.post_id(Some(data.inserted_post.id))
.build()
.list()
.await
.unwrap();
let read_comment_views_no_person = CommentQuery {
sort: (Some(CommentSortType::Old)),
post_id: (Some(data.inserted_post.id)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(
expected_comment_view_no_person,
read_comment_views_no_person[0]
);
let read_comment_views_with_person = CommentQuery::builder()
.pool(pool)
.sort(Some(CommentSortType::Old))
.post_id(Some(data.inserted_post.id))
.local_user(Some(&data.inserted_local_user))
.build()
.list()
.await
.unwrap();
let read_comment_views_with_person = CommentQuery {
sort: (Some(CommentSortType::Old)),
post_id: (Some(data.inserted_post.id)),
local_user: (Some(&data.inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(
expected_comment_view_with_person,
@ -656,24 +652,24 @@ mod tests {
let data = init_data(pool).await;
let top_path = data.inserted_comment_0.path.clone();
let read_comment_views_top_path = CommentQuery::builder()
.pool(pool)
.post_id(Some(data.inserted_post.id))
.parent_path(Some(top_path))
.build()
.list()
.await
.unwrap();
let read_comment_views_top_path = CommentQuery {
post_id: (Some(data.inserted_post.id)),
parent_path: (Some(top_path)),
..Default::default()
}
.list(pool)
.await
.unwrap();
let child_path = data.inserted_comment_1.path.clone();
let read_comment_views_child_path = CommentQuery::builder()
.pool(pool)
.post_id(Some(data.inserted_post.id))
.parent_path(Some(child_path))
.build()
.list()
.await
.unwrap();
let read_comment_views_child_path = CommentQuery {
post_id: (Some(data.inserted_post.id)),
parent_path: (Some(child_path)),
..Default::default()
}
.list(pool)
.await
.unwrap();
// Make sure the comment parent-limited fetch is correct
assert_eq!(6, read_comment_views_top_path.len());
@ -687,14 +683,14 @@ mod tests {
assert!(child_comments.contains(&data.inserted_comment_1));
assert!(!child_comments.contains(&data.inserted_comment_2));
let read_comment_views_top_max_depth = CommentQuery::builder()
.pool(pool)
.post_id(Some(data.inserted_post.id))
.max_depth(Some(1))
.build()
.list()
.await
.unwrap();
let read_comment_views_top_max_depth = CommentQuery {
post_id: (Some(data.inserted_post.id)),
max_depth: (Some(1)),
..Default::default()
}
.list(pool)
.await
.unwrap();
// Make sure a depth limited one only has the top comment
assert_eq!(
@ -704,16 +700,16 @@ mod tests {
assert_eq!(1, read_comment_views_top_max_depth.len());
let child_path = data.inserted_comment_1.path.clone();
let read_comment_views_parent_max_depth = CommentQuery::builder()
.pool(pool)
.post_id(Some(data.inserted_post.id))
.parent_path(Some(child_path))
.max_depth(Some(1))
.sort(Some(CommentSortType::New))
.build()
.list()
.await
.unwrap();
let read_comment_views_parent_max_depth = CommentQuery {
post_id: (Some(data.inserted_post.id)),
parent_path: (Some(child_path)),
max_depth: (Some(1)),
sort: (Some(CommentSortType::New)),
..Default::default()
}
.list(pool)
.await
.unwrap();
// Make sure a depth limited one, and given child comment 1, has 3
assert!(read_comment_views_parent_max_depth[2]
@ -734,13 +730,13 @@ mod tests {
// by default, user has all languages enabled and should see all comments
// (except from blocked user)
let all_languages = CommentQuery::builder()
.pool(pool)
.local_user(Some(&data.inserted_local_user))
.build()
.list()
.await
.unwrap();
let all_languages = CommentQuery {
local_user: (Some(&data.inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(5, all_languages.len());
// change user lang to finnish, should only show one post in finnish and one undetermined
@ -751,13 +747,13 @@ mod tests {
LocalUserLanguage::update(pool, vec![finnish_id], data.inserted_local_user.id)
.await
.unwrap();
let finnish_comments = CommentQuery::builder()
.pool(pool)
.local_user(Some(&data.inserted_local_user))
.build()
.list()
.await
.unwrap();
let finnish_comments = CommentQuery {
local_user: (Some(&data.inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(2, finnish_comments.len());
let finnish_comment = finnish_comments
.iter()
@ -772,13 +768,13 @@ mod tests {
LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id)
.await
.unwrap();
let undetermined_comment = CommentQuery::builder()
.pool(pool)
.local_user(Some(&data.inserted_local_user))
.build()
.list()
.await
.unwrap();
let undetermined_comment = CommentQuery {
local_user: (Some(&data.inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(1, undetermined_comment.len());
cleanup(data, pool).await;

View file

@ -30,7 +30,6 @@ use lemmy_db_schema::{
traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool},
};
use typed_builder::TypedBuilder;
type PostReportViewTuple = (
PostReport,
@ -159,24 +158,21 @@ impl PostReportView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct PostReportQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
#[builder(!default)]
my_person_id: PersonId,
#[builder(!default)]
admin: bool,
community_id: Option<CommunityId>,
page: Option<i64>,
limit: Option<i64>,
unresolved_only: Option<bool>,
#[derive(Default)]
pub struct PostReportQuery {
pub community_id: Option<CommunityId>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub unresolved_only: Option<bool>,
}
impl<'a, 'b: 'a> PostReportQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<PostReportView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl PostReportQuery {
pub async fn list(
self,
pool: &mut DbPool<'_>,
my_person: &Person,
) -> Result<Vec<PostReportView>, Error> {
let conn = &mut get_conn(pool).await?;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
let mut query = post_report::table
@ -195,7 +191,7 @@ impl<'a, 'b: 'a> PostReportQuery<'a, 'b> {
post_like::table.on(
post::id
.eq(post_like::post_id)
.and(post_like::person_id.eq(self.my_person_id)),
.and(post_like::person_id.eq(my_person.id)),
),
)
.inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
@ -231,13 +227,13 @@ impl<'a, 'b: 'a> PostReportQuery<'a, 'b> {
.offset(offset);
// If its not an admin, get only the ones you mod
let res = if !self.admin {
let res = if !my_person.admin {
query
.inner_join(
community_moderator::table.on(
community_moderator::community_id
.eq(post::community_id)
.and(community_moderator::person_id.eq(self.my_person_id)),
.and(community_moderator::person_id.eq(my_person.id)),
),
)
.load::<PostReportViewTuple>(conn)
@ -506,12 +502,8 @@ mod tests {
};
// Do a batch read of timmys reports
let reports = PostReportQuery::builder()
.pool(pool)
.my_person_id(inserted_timmy.id)
.admin(false)
.build()
.list()
let reports = PostReportQuery::default()
.list(pool, &inserted_timmy)
.await
.unwrap();
@ -580,15 +572,13 @@ mod tests {
// Do a batch read of timmys reports
// It should only show saras, which is unresolved
let reports_after_resolve = PostReportQuery::builder()
.pool(pool)
.my_person_id(inserted_timmy.id)
.admin(false)
.unresolved_only(Some(true))
.build()
.list()
.await
.unwrap();
let reports_after_resolve = PostReportQuery {
unresolved_only: (Some(true)),
..Default::default()
}
.list(pool, &inserted_timmy)
.await
.unwrap();
assert_eq!(reports_after_resolve[0], expected_sara_report_view);
// Make sure the counts are correct

View file

@ -45,7 +45,6 @@ use lemmy_db_schema::{
SortType,
};
use tracing::debug;
use typed_builder::TypedBuilder;
type PostViewTuple = (
Post,
@ -193,28 +192,25 @@ impl PostView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct PostQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
listing_type: Option<ListingType>,
sort: Option<SortType>,
creator_id: Option<PersonId>,
community_id: Option<CommunityId>,
local_user: Option<&'a LocalUser>,
search_term: Option<String>,
url_search: Option<String>,
saved_only: Option<bool>,
#[derive(Default)]
pub struct PostQuery<'a> {
pub listing_type: Option<ListingType>,
pub sort: Option<SortType>,
pub creator_id: Option<PersonId>,
pub community_id: Option<CommunityId>,
pub local_user: Option<&'a LocalUser>,
pub search_term: Option<String>,
pub url_search: Option<String>,
pub saved_only: Option<bool>,
/// Used to show deleted or removed posts for admins
is_mod_or_admin: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
pub is_mod_or_admin: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,
}
impl<'a, 'b: 'a> PostQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<PostView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl<'a> PostQuery<'a> {
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PostView>, Error> {
let conn = &mut get_conn(pool).await?;
// The left join below will return None in this case
let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
@ -619,15 +615,15 @@ mod tests {
.await
.unwrap();
let read_post_listing = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.local_user(Some(&inserted_local_user))
.build()
.list()
.await
.unwrap();
let read_post_listing = PostQuery {
sort: (Some(SortType::New)),
community_id: (Some(data.inserted_community.id)),
local_user: (Some(&inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
let post_listing_single_with_person = PostView::read(
pool,
@ -658,15 +654,15 @@ mod tests {
.await
.unwrap();
let post_listings_with_bots = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.local_user(Some(&inserted_local_user))
.build()
.list()
.await
.unwrap();
let post_listings_with_bots = PostQuery {
sort: (Some(SortType::New)),
community_id: (Some(data.inserted_community.id)),
local_user: (Some(&inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
// should include bot post which has "undetermined" language
assert_eq!(2, post_listings_with_bots.len());
@ -680,14 +676,14 @@ mod tests {
let pool = &mut pool.into();
let data = init_data(pool).await;
let read_post_listing_multiple_no_person = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.build()
.list()
.await
.unwrap();
let read_post_listing_multiple_no_person = PostQuery {
sort: (Some(SortType::New)),
community_id: (Some(data.inserted_community.id)),
..Default::default()
}
.list(pool)
.await
.unwrap();
let read_post_listing_single_no_person =
PostView::read(pool, data.inserted_post.id, None, None)
@ -724,15 +720,15 @@ mod tests {
};
CommunityBlock::block(pool, &community_block).await.unwrap();
let read_post_listings_with_person_after_block = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.local_user(Some(&data.inserted_local_user))
.build()
.list()
.await
.unwrap();
let read_post_listings_with_person_after_block = PostQuery {
sort: (Some(SortType::New)),
community_id: (Some(data.inserted_community.id)),
local_user: (Some(&data.inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
// Should be 0 posts after the community block
assert_eq!(0, read_post_listings_with_person_after_block.len());
@ -789,15 +785,15 @@ mod tests {
.await
.unwrap();
let read_post_listing = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.community_id(Some(data.inserted_community.id))
.local_user(Some(&inserted_local_user))
.build()
.list()
.await
.unwrap();
let read_post_listing = PostQuery {
sort: (Some(SortType::New)),
community_id: (Some(data.inserted_community.id)),
local_user: (Some(&inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(1, read_post_listing.len());
assert_eq!(expected_post_with_upvote, read_post_listing[0]);
@ -829,14 +825,14 @@ mod tests {
Post::create(pool, &post_spanish).await.unwrap();
let post_listings_all = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.build()
.list()
.await
.unwrap();
let post_listings_all = PostQuery {
sort: (Some(SortType::New)),
local_user: (Some(&data.inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
// no language filters specified, all posts should be returned
assert_eq!(3, post_listings_all.len());
@ -849,14 +845,14 @@ mod tests {
.await
.unwrap();
let post_listing_french = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.build()
.list()
.await
.unwrap();
let post_listing_french = PostQuery {
sort: (Some(SortType::New)),
local_user: (Some(&data.inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
// only one post in french and one undetermined should be returned
assert_eq!(2, post_listing_french.len());
@ -871,14 +867,14 @@ mod tests {
)
.await
.unwrap();
let post_listings_french_und = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.build()
.list()
.await
.unwrap();
let post_listings_french_und = PostQuery {
sort: (Some(SortType::New)),
local_user: (Some(&data.inserted_local_user)),
..Default::default()
}
.list(pool)
.await
.unwrap();
// french post and undetermined language post should be returned
assert_eq!(2, post_listings_french_und.len());
@ -908,28 +904,28 @@ mod tests {
.unwrap();
// Make sure you don't see the deleted post in the results
let post_listings_no_admin = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.is_mod_or_admin(Some(false))
.build()
.list()
.await
.unwrap();
let post_listings_no_admin = PostQuery {
sort: (Some(SortType::New)),
local_user: (Some(&data.inserted_local_user)),
is_mod_or_admin: (Some(false)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(1, post_listings_no_admin.len());
// Make sure they see both
let post_listings_is_admin = PostQuery::builder()
.pool(pool)
.sort(Some(SortType::New))
.local_user(Some(&data.inserted_local_user))
.is_mod_or_admin(Some(true))
.build()
.list()
.await
.unwrap();
let post_listings_is_admin = PostQuery {
sort: (Some(SortType::New)),
local_user: (Some(&data.inserted_local_user)),
is_mod_or_admin: (Some(true)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(2, post_listings_is_admin.len());

View file

@ -12,7 +12,6 @@ use lemmy_db_schema::{
traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool},
};
use typed_builder::TypedBuilder;
type PrivateMessageReportViewTuple = (
PrivateMessageReport,
@ -81,19 +80,16 @@ impl PrivateMessageReportView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct PrivateMessageReportQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
page: Option<i64>,
limit: Option<i64>,
unresolved_only: Option<bool>,
#[derive(Default)]
pub struct PrivateMessageReportQuery {
pub page: Option<i64>,
pub limit: Option<i64>,
pub unresolved_only: Option<bool>,
}
impl<'a, 'b: 'a> PrivateMessageReportQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<PrivateMessageReportView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl PrivateMessageReportQuery {
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PrivateMessageReportView>, Error> {
let conn = &mut get_conn(pool).await?;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
let mut query = private_message_report::table
@ -208,10 +204,8 @@ mod tests {
.await
.unwrap();
let reports = PrivateMessageReportQuery::builder()
.pool(pool)
.build()
.list()
let reports = PrivateMessageReportQuery::default()
.list(pool)
.await
.unwrap();
assert_eq!(1, reports.len());
@ -233,13 +227,13 @@ mod tests {
.await
.unwrap();
let reports = PrivateMessageReportQuery::builder()
.pool(pool)
.unresolved_only(Some(false))
.build()
.list()
.await
.unwrap();
let reports = PrivateMessageReportQuery {
unresolved_only: (Some(false)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(1, reports.len());
assert!(reports[0].private_message_report.resolved);
assert!(reports[0].resolver.is_some());

View file

@ -17,7 +17,6 @@ use lemmy_db_schema::{
utils::{get_conn, limit_and_offset, DbPool},
};
use tracing::debug;
use typed_builder::TypedBuilder;
type PrivateMessageViewTuple = (PrivateMessage, Person, Person);
@ -68,21 +67,20 @@ impl PrivateMessageView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct PrivateMessageQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
#[builder(!default)]
recipient_id: PersonId,
unread_only: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
#[derive(Default)]
pub struct PrivateMessageQuery {
pub unread_only: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,
}
impl<'a, 'b: 'a> PrivateMessageQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<PrivateMessageView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl PrivateMessageQuery {
pub async fn list(
self,
pool: &mut DbPool<'_>,
recipient_id: PersonId,
) -> Result<Vec<PrivateMessageView>, Error> {
let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1);
let mut query = private_message::table
@ -101,14 +99,14 @@ impl<'a, 'b: 'a> PrivateMessageQuery<'a, 'b> {
if self.unread_only.unwrap_or(false) {
query = query
.filter(private_message::read.eq(false))
.filter(private_message::recipient_id.eq(self.recipient_id));
.filter(private_message::recipient_id.eq(recipient_id));
}
// Otherwise, I want the ALL view to show both sent and received
else {
query = query.filter(
private_message::recipient_id
.eq(self.recipient_id)
.or(private_message::creator_id.eq(self.recipient_id)),
.eq(recipient_id)
.or(private_message::creator_id.eq(recipient_id)),
)
}

View file

@ -18,7 +18,6 @@ use lemmy_db_schema::{
traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool},
};
use typed_builder::TypedBuilder;
type RegistrationApplicationViewTuple =
(RegistrationApplication, LocalUser, Person, Option<Person>);
@ -89,20 +88,20 @@ impl RegistrationApplicationView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct RegistrationApplicationQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
unread_only: Option<bool>,
verified_email_only: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
#[derive(Default)]
pub struct RegistrationApplicationQuery {
pub unread_only: Option<bool>,
pub verified_email_only: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,
}
impl<'a, 'b: 'a> RegistrationApplicationQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<RegistrationApplicationView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl RegistrationApplicationQuery {
pub async fn list(
self,
pool: &mut DbPool<'_>,
) -> Result<Vec<RegistrationApplicationView>, Error> {
let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1);
let mut query = registration_application::table
@ -327,13 +326,13 @@ mod tests {
assert_eq!(read_sara_app_view, expected_sara_app_view);
// Do a batch read of the applications
let apps = RegistrationApplicationQuery::builder()
.pool(pool)
.unread_only(Some(true))
.build()
.list()
.await
.unwrap();
let apps = RegistrationApplicationQuery {
unread_only: (Some(true)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(
apps,
@ -403,13 +402,13 @@ mod tests {
// Do a batch read of apps again
// It should show only jessicas which is unresolved
let apps_after_resolve = RegistrationApplicationQuery::builder()
.pool(pool)
.unread_only(Some(true))
.build()
.list()
.await
.unwrap();
let apps_after_resolve = RegistrationApplicationQuery {
unread_only: (Some(true)),
..Default::default()
}
.list(pool)
.await
.unwrap();
assert_eq!(apps_after_resolve, vec![read_jess_app_view]);
// Make sure the counts are correct
@ -419,10 +418,8 @@ mod tests {
assert_eq!(unread_count_after_approve, 1);
// Make sure the not undenied_only has all the apps
let all_apps = RegistrationApplicationQuery::builder()
.pool(pool)
.build()
.list()
let all_apps = RegistrationApplicationQuery::default()
.list(pool)
.await
.unwrap();
assert_eq!(all_apps.len(), 2);

View file

@ -27,5 +27,4 @@ diesel-async = { workspace = true, features = [
], optional = true }
serde = { workspace = true }
serde_with = { workspace = true }
typed-builder = { workspace = true }
ts-rs = { workspace = true, optional = true }

View file

@ -36,7 +36,6 @@ use lemmy_db_schema::{
utils::{get_conn, limit_and_offset, DbPool},
CommentSortType,
};
use typed_builder::TypedBuilder;
type CommentReplyViewTuple = (
CommentReply,
@ -175,23 +174,20 @@ impl CommentReplyView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct CommentReplyQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
my_person_id: Option<PersonId>,
recipient_id: Option<PersonId>,
sort: Option<CommentSortType>,
unread_only: Option<bool>,
show_bot_accounts: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
#[derive(Default)]
pub struct CommentReplyQuery {
pub my_person_id: Option<PersonId>,
pub recipient_id: Option<PersonId>,
pub sort: Option<CommentSortType>,
pub unread_only: Option<bool>,
pub show_bot_accounts: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,
}
impl<'a, 'b: 'a> CommentReplyQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<CommentReplyView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl CommentReplyQuery {
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommentReplyView>, Error> {
let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1);

View file

@ -23,7 +23,6 @@ use lemmy_db_schema::{
ListingType,
SortType,
};
use typed_builder::TypedBuilder;
type CommunityViewTuple = (
Community,
@ -100,26 +99,23 @@ impl CommunityView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct CommunityQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
listing_type: Option<ListingType>,
sort: Option<SortType>,
local_user: Option<&'a LocalUser>,
search_term: Option<String>,
is_mod_or_admin: Option<bool>,
show_nsfw: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
#[derive(Default)]
pub struct CommunityQuery<'a> {
pub listing_type: Option<ListingType>,
pub sort: Option<SortType>,
pub local_user: Option<&'a LocalUser>,
pub search_term: Option<String>,
pub is_mod_or_admin: Option<bool>,
pub show_nsfw: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,
}
impl<'a, 'b: 'a> CommunityQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<CommunityView>, Error> {
impl<'a> CommunityQuery<'a> {
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommunityView>, Error> {
use SortType::*;
let conn = &mut get_conn(self.pool).await?;
let conn = &mut get_conn(pool).await?;
// The left join below will return None in this case
let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));

View file

@ -37,7 +37,6 @@ use lemmy_db_schema::{
utils::{get_conn, limit_and_offset, DbPool},
CommentSortType,
};
use typed_builder::TypedBuilder;
type PersonMentionViewTuple = (
PersonMention,
@ -175,23 +174,20 @@ impl PersonMentionView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct PersonMentionQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
my_person_id: Option<PersonId>,
recipient_id: Option<PersonId>,
sort: Option<CommentSortType>,
unread_only: Option<bool>,
show_bot_accounts: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
#[derive(Default)]
pub struct PersonMentionQuery {
pub my_person_id: Option<PersonId>,
pub recipient_id: Option<PersonId>,
pub sort: Option<CommentSortType>,
pub unread_only: Option<bool>,
pub show_bot_accounts: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,
}
impl<'a, 'b: 'a> PersonMentionQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<PersonMentionView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl PersonMentionQuery {
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PersonMentionView>, Error> {
let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1);

View file

@ -19,7 +19,6 @@ use lemmy_db_schema::{
SortType,
};
use std::iter::Iterator;
use typed_builder::TypedBuilder;
type PersonViewTuple = (Person, PersonAggregates);
@ -79,20 +78,17 @@ impl PersonView {
}
}
#[derive(TypedBuilder)]
#[builder(field_defaults(default))]
pub struct PersonQuery<'a, 'b: 'a> {
#[builder(!default)]
pool: &'a mut DbPool<'b>,
sort: Option<SortType>,
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
#[derive(Default)]
pub struct PersonQuery {
pub sort: Option<SortType>,
pub search_term: Option<String>,
pub page: Option<i64>,
pub limit: Option<i64>,
}
impl<'a, 'b: 'a> PersonQuery<'a, 'b> {
pub async fn list(self) -> Result<Vec<PersonView>, Error> {
let conn = &mut get_conn(self.pool).await?;
impl PersonQuery {
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PersonView>, Error> {
let conn = &mut get_conn(pool).await?;
let mut query = person::table
.inner_join(person_aggregates::table)
.select((person::all_columns, person_aggregates::all_columns))

View file

@ -124,15 +124,15 @@ async fn get_feed_data(
) -> Result<HttpResponse, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?;
let posts = PostQuery::builder()
.pool(&mut context.pool())
.listing_type(Some(listing_type))
.sort(Some(sort_type))
.limit(Some(limit))
.page(Some(page))
.build()
.list()
.await?;
let posts = PostQuery {
listing_type: (Some(listing_type)),
sort: (Some(sort_type)),
limit: (Some(limit)),
page: (Some(page)),
..Default::default()
}
.list(&mut context.pool())
.await?;
let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
@ -243,16 +243,16 @@ async fn get_feed_user(
let site_view = SiteView::read_local(pool).await?;
let person = Person::read_from_name(pool, user_name, false).await?;
let posts = PostQuery::builder()
.pool(pool)
.listing_type(Some(ListingType::All))
.sort(Some(*sort_type))
.creator_id(Some(person.id))
.limit(Some(*limit))
.page(Some(*page))
.build()
.list()
.await?;
let posts = PostQuery {
listing_type: (Some(ListingType::All)),
sort: (Some(*sort_type)),
creator_id: (Some(person.id)),
limit: (Some(*limit)),
page: (Some(*page)),
..Default::default()
}
.list(pool)
.await?;
let items = create_post_items(posts, protocol_and_hostname)?;
@ -278,15 +278,15 @@ async fn get_feed_community(
let site_view = SiteView::read_local(pool).await?;
let community = Community::read_from_name(pool, community_name, false).await?;
let posts = PostQuery::builder()
.pool(pool)
.sort(Some(*sort_type))
.community_id(Some(community.id))
.limit(Some(*limit))
.page(Some(*page))
.build()
.list()
.await?;
let posts = PostQuery {
sort: (Some(*sort_type)),
community_id: (Some(community.id)),
limit: (Some(*limit)),
page: (Some(*page)),
..Default::default()
}
.list(pool)
.await?;
let items = create_post_items(posts, protocol_and_hostname)?;
@ -318,16 +318,16 @@ async fn get_feed_front(
let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
let local_user = LocalUser::read(pool, local_user_id).await?;
let posts = PostQuery::builder()
.pool(pool)
.listing_type(Some(ListingType::Subscribed))
.local_user(Some(&local_user))
.sort(Some(*sort_type))
.limit(Some(*limit))
.page(Some(*page))
.build()
.list()
.await?;
let posts = PostQuery {
listing_type: (Some(ListingType::Subscribed)),
local_user: (Some(&local_user)),
sort: (Some(*sort_type)),
limit: (Some(*limit)),
page: (Some(*page)),
..Default::default()
}
.list(pool)
.await?;
let items = create_post_items(posts, protocol_and_hostname)?;
@ -360,27 +360,27 @@ async fn get_feed_inbox(
let sort = CommentSortType::New;
let replies = CommentReplyQuery::builder()
.pool(pool)
.recipient_id(Some(person_id))
.my_person_id(Some(person_id))
.show_bot_accounts(Some(show_bot_accounts))
.sort(Some(sort))
.limit(Some(RSS_FETCH_LIMIT))
.build()
.list()
.await?;
let replies = CommentReplyQuery {
recipient_id: (Some(person_id)),
my_person_id: (Some(person_id)),
show_bot_accounts: (Some(show_bot_accounts)),
sort: (Some(sort)),
limit: (Some(RSS_FETCH_LIMIT)),
..Default::default()
}
.list(pool)
.await?;
let mentions = PersonMentionQuery::builder()
.pool(pool)
.recipient_id(Some(person_id))
.my_person_id(Some(person_id))
.show_bot_accounts(Some(show_bot_accounts))
.sort(Some(sort))
.limit(Some(RSS_FETCH_LIMIT))
.build()
.list()
.await?;
let mentions = PersonMentionQuery {
recipient_id: (Some(person_id)),
my_person_id: (Some(person_id)),
show_bot_accounts: (Some(show_bot_accounts)),
sort: (Some(sort)),
limit: (Some(RSS_FETCH_LIMIT)),
..Default::default()
}
.list(pool)
.await?;
let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;