Merge branch 'dev'

This commit is contained in:
Dessalines 2019-12-08 13:06:55 -08:00
commit 4c7da003a2
10 changed files with 566 additions and 266 deletions

View file

@ -349,9 +349,13 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;
let communities: Vec<CommunityView> = CommunityView::list( let communities = CommunityQueryBuilder::create(&conn)
&conn, &sort, user_id, show_nsfw, None, data.page, data.limit, .sort(&sort)
)?; .from_user_id_optional(user_id)
.show_nsfw(show_nsfw)
.page_optional(data.page)
.limit_optional(data.limit)
.list()?;
// Return the jwt // Return the jwt
Ok(ListCommunitiesResponse { Ok(ListCommunitiesResponse {

View file

@ -178,17 +178,11 @@ impl Perform<GetPostResponse> for Oper<GetPost> {
Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post"))?, Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_post"))?,
}; };
let comments = CommentView::list( let comments = CommentQueryBuilder::create(&conn)
&conn, .for_post_id(data.id)
&SortType::New, .my_user_id_optional(user_id)
Some(data.id), .limit(9999)
None, .list()?;
None,
user_id,
false,
None,
Some(9999),
)?;
let community = CommunityView::read(&conn, post_view.community_id, user_id)?; let community = CommunityView::read(&conn, post_view.community_id, user_id)?;

View file

@ -329,31 +329,28 @@ impl Perform<SearchResponse> for Oper<Search> {
.list()?; .list()?;
} }
SearchType::Comments => { SearchType::Comments => {
comments = CommentView::list( comments = CommentQueryBuilder::create(&conn)
&conn, .sort(&sort)
&sort, .search_term(data.q.to_owned())
None, .page_optional(data.page)
None, .limit_optional(data.limit)
Some(data.q.to_owned()), .list()?;
None,
false,
data.page,
data.limit,
)?;
} }
SearchType::Communities => { SearchType::Communities => {
communities = CommunityView::list( communities = CommunityQueryBuilder::create(&conn)
&conn, .sort(&sort)
&sort, .search_term(data.q.to_owned())
None, .page_optional(data.page)
true, .limit_optional(data.limit)
Some(data.q.to_owned()), .list()?;
data.page,
data.limit,
)?;
} }
SearchType::Users => { SearchType::Users => {
users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?; users = UserQueryBuilder::create(&conn)
.sort(&sort)
.search_term(data.q.to_owned())
.page_optional(data.page)
.limit_optional(data.limit)
.list()?;
} }
SearchType::All => { SearchType::All => {
posts = PostQueryBuilder::create(&conn) posts = PostQueryBuilder::create(&conn)
@ -365,27 +362,26 @@ impl Perform<SearchResponse> for Oper<Search> {
.limit_optional(data.limit) .limit_optional(data.limit)
.list()?; .list()?;
comments = CommentView::list( comments = CommentQueryBuilder::create(&conn)
&conn, .sort(&sort)
&sort, .search_term(data.q.to_owned())
None, .page_optional(data.page)
None, .limit_optional(data.limit)
Some(data.q.to_owned()), .list()?;
None,
false, communities = CommunityQueryBuilder::create(&conn)
data.page, .sort(&sort)
data.limit, .search_term(data.q.to_owned())
)?; .page_optional(data.page)
communities = CommunityView::list( .limit_optional(data.limit)
&conn, .list()?;
&sort,
None, users = UserQueryBuilder::create(&conn)
true, .sort(&sort)
Some(data.q.to_owned()), .search_term(data.q.to_owned())
data.page, .page_optional(data.page)
data.limit, .limit_optional(data.limit)
)?; .list()?;
users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
} }
SearchType::Url => { SearchType::Url => {
posts = PostQueryBuilder::create(&conn) posts = PostQueryBuilder::create(&conn)

View file

@ -375,38 +375,22 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
.page_optional(data.page) .page_optional(data.page)
.limit_optional(data.limit); .limit_optional(data.limit);
let mut comments_query = CommentQueryBuilder::create(&conn)
.sort(&sort)
.saved_only(data.saved_only)
.my_user_id_optional(user_id)
.page_optional(data.page)
.limit_optional(data.limit);
// If its saved only, you don't care what creator it was // 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 !data.saved_only { if !data.saved_only {
posts_query = posts_query.for_creator_id(user_details_id); posts_query = posts_query.for_creator_id(user_details_id);
comments_query = comments_query.for_creator_id(user_details_id);
} }
let posts = posts_query.list()?; let posts = posts_query.list()?;
let comments = comments_query.list()?;
let comments = if data.saved_only {
CommentView::list(
&conn,
&sort,
None,
None,
None,
Some(user_details_id),
data.saved_only,
data.page,
data.limit,
)?
} else {
CommentView::list(
&conn,
&sort,
None,
Some(user_details_id),
None,
user_id,
data.saved_only,
data.page,
data.limit,
)?
};
let follows = CommunityFollowerView::for_user(&conn, user_details_id)?; let follows = CommunityFollowerView::for_user(&conn, user_details_id)?;
let moderates = CommunityModeratorView::for_user(&conn, user_details_id)?; let moderates = CommunityModeratorView::for_user(&conn, user_details_id)?;
@ -569,14 +553,12 @@ impl Perform<GetRepliesResponse> for Oper<GetReplies> {
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;
let replies = ReplyView::get_replies( let replies = ReplyQueryBuilder::create(&conn, user_id)
&conn, .sort(&sort)
user_id, .unread_only(data.unread_only)
&sort, .page_optional(data.page)
data.unread_only, .limit_optional(data.limit)
data.page, .list()?;
data.limit,
)?;
Ok(GetRepliesResponse { Ok(GetRepliesResponse {
op: self.op.to_string(), op: self.op.to_string(),
@ -599,14 +581,12 @@ impl Perform<GetUserMentionsResponse> for Oper<GetUserMentions> {
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;
let mentions = UserMentionView::get_mentions( let mentions = UserMentionQueryBuilder::create(&conn, user_id)
&conn, .sort(&sort)
user_id, .unread_only(data.unread_only)
&sort, .page_optional(data.page)
data.unread_only, .limit_optional(data.limit)
data.page, .list()?;
data.limit,
)?;
Ok(GetUserMentionsResponse { Ok(GetUserMentionsResponse {
op: self.op.to_string(), op: self.op.to_string(),
@ -662,7 +642,11 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
let user_id = claims.id; let user_id = claims.id;
let replies = ReplyView::get_replies(&conn, user_id, &SortType::New, true, Some(1), Some(999))?; let replies = ReplyQueryBuilder::create(&conn, user_id)
.unread_only(true)
.page(1)
.limit(999)
.list()?;
for reply in &replies { for reply in &replies {
let comment_form = CommentForm { let comment_form = CommentForm {
@ -683,8 +667,11 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
} }
// Mentions // Mentions
let mentions = let mentions = UserMentionQueryBuilder::create(&conn, user_id)
UserMentionView::get_mentions(&conn, user_id, &SortType::New, true, Some(1), Some(999))?; .unread_only(true)
.page(1)
.limit(999)
.list()?;
for mention in &mentions { for mention in &mentions {
let mention_form = UserMentionForm { let mention_form = UserMentionForm {
@ -728,17 +715,10 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
} }
// Comments // Comments
let comments = CommentView::list( let comments = CommentQueryBuilder::create(&conn)
&conn, .for_creator_id(user_id)
&SortType::New, .limit(std::i64::MAX)
None, .list()?;
Some(user_id),
None,
None,
false,
None,
Some(std::i64::MAX),
)?;
for comment in &comments { for comment in &comments {
let comment_form = CommentForm { let comment_form = CommentForm {

View file

@ -1,4 +1,5 @@
use super::*; use super::*;
use diesel::pg::Pg;
// The faked schema since diesel doesn't do views // The faked schema since diesel doesn't do views
table! { table! {
@ -53,48 +54,138 @@ pub struct CommentView {
pub saved: Option<bool>, pub saved: Option<bool>,
} }
impl CommentView { pub struct CommentQueryBuilder<'a> {
pub fn list( conn: &'a PgConnection,
conn: &PgConnection, query: super::comment_view::comment_view::BoxedQuery<'a, Pg>,
sort: &SortType, sort: &'a SortType,
for_post_id: Option<i32>, for_post_id: Option<i32>,
for_creator_id: Option<i32>, for_creator_id: Option<i32>,
search_term: Option<String>, search_term: Option<String>,
my_user_id: Option<i32>, my_user_id: Option<i32>,
saved_only: bool, saved_only: bool,
page: Option<i64>, page: Option<i64>,
limit: Option<i64>, limit: Option<i64>,
) -> Result<Vec<Self>, Error> { }
impl<'a> CommentQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection) -> Self {
use super::comment_view::comment_view::dsl::*; use super::comment_view::comment_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit); let query = comment_view.into_boxed();
let mut query = comment_view.into_boxed(); CommentQueryBuilder {
conn,
query,
sort: &SortType::New,
for_post_id: None,
for_creator_id: None,
search_term: None,
my_user_id: None,
saved_only: false,
page: None,
limit: None,
}
}
pub fn sort(mut self, sort: &'a SortType) -> Self {
self.sort = sort;
self
}
pub fn for_post_id(mut self, for_post_id: i32) -> Self {
self.for_post_id = Some(for_post_id);
self
}
pub fn for_post_id_optional(mut self, for_post_id: Option<i32>) -> Self {
self.for_post_id = for_post_id;
self
}
pub fn for_creator_id(mut self, for_creator_id: i32) -> Self {
self.for_creator_id = Some(for_creator_id);
self
}
pub fn for_creator_id_optional(mut self, for_creator_id: Option<i32>) -> Self {
self.for_creator_id = for_creator_id;
self
}
pub fn search_term(mut self, search_term: String) -> Self {
self.search_term = Some(search_term);
self
}
pub fn search_term_optional(mut self, search_term: Option<String>) -> Self {
self.search_term = search_term;
self
}
pub fn my_user_id(mut self, my_user_id: i32) -> Self {
self.my_user_id = Some(my_user_id);
self
}
pub fn my_user_id_optional(mut self, my_user_id: Option<i32>) -> Self {
self.my_user_id = my_user_id;
self
}
pub fn saved_only(mut self, saved_only: bool) -> Self {
self.saved_only = saved_only;
self
}
pub fn page(mut self, page: i64) -> Self {
self.page = Some(page);
self
}
pub fn page_optional(mut self, page: Option<i64>) -> Self {
self.page = page;
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self
}
pub fn list(self) -> Result<Vec<CommentView>, Error> {
use super::comment_view::comment_view::dsl::*;
let mut query = self.query;
// The view lets you pass a null user_id, if you're not logged in // The view lets you pass a null user_id, if you're not logged in
if let Some(my_user_id) = my_user_id { if let Some(my_user_id) = self.my_user_id {
query = query.filter(user_id.eq(my_user_id)); query = query.filter(user_id.eq(my_user_id));
} else { } else {
query = query.filter(user_id.is_null()); query = query.filter(user_id.is_null());
} }
if let Some(for_creator_id) = for_creator_id { if let Some(for_creator_id) = self.for_creator_id {
query = query.filter(creator_id.eq(for_creator_id)); query = query.filter(creator_id.eq(for_creator_id));
}; };
if let Some(for_post_id) = for_post_id { if let Some(for_post_id) = self.for_post_id {
query = query.filter(post_id.eq(for_post_id)); query = query.filter(post_id.eq(for_post_id));
}; };
if let Some(search_term) = search_term { if let Some(search_term) = self.search_term {
query = query.filter(content.ilike(fuzzy_search(&search_term))); query = query.filter(content.ilike(fuzzy_search(&search_term)));
}; };
if saved_only { if self.saved_only {
query = query.filter(saved.eq(true)); query = query.filter(saved.eq(true));
} }
query = match sort { query = match self.sort {
// SortType::Hot => query.order(hot_rank.desc(), published.desc()), // SortType::Hot => query.order(hot_rank.desc(), published.desc()),
SortType::New => query.order_by(published.desc()), SortType::New => query.order_by(published.desc()),
SortType::TopAll => query.order_by(score.desc()), SortType::TopAll => query.order_by(score.desc()),
@ -113,10 +204,17 @@ impl CommentView {
_ => query.order_by(published.desc()), _ => query.order_by(published.desc()),
}; };
// Note: deleted and removed comments are done on the front side let (limit, offset) = limit_and_offset(self.page, self.limit);
query.limit(limit).offset(offset).load::<Self>(conn)
}
// Note: deleted and removed comments are done on the front side
query
.limit(limit)
.offset(offset)
.load::<CommentView>(self.conn)
}
}
impl CommentView {
pub fn read( pub fn read(
conn: &PgConnection, conn: &PgConnection,
from_comment_id: i32, from_comment_id: i32,
@ -196,30 +294,77 @@ pub struct ReplyView {
pub recipient_id: i32, pub recipient_id: i32,
} }
impl ReplyView { pub struct ReplyQueryBuilder<'a> {
pub fn get_replies( conn: &'a PgConnection,
conn: &PgConnection, query: super::comment_view::reply_view::BoxedQuery<'a, Pg>,
for_user_id: i32, for_user_id: i32,
sort: &SortType, sort: &'a SortType,
unread_only: bool, unread_only: bool,
page: Option<i64>, page: Option<i64>,
limit: Option<i64>, limit: Option<i64>,
) -> Result<Vec<Self>, Error> { }
impl<'a> ReplyQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
use super::comment_view::reply_view::dsl::*; use super::comment_view::reply_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit); let query = reply_view.into_boxed();
let mut query = reply_view.into_boxed(); ReplyQueryBuilder {
conn,
query,
for_user_id: for_user_id,
sort: &SortType::New,
unread_only: false,
page: None,
limit: None,
}
}
pub fn sort(mut self, sort: &'a SortType) -> Self {
self.sort = sort;
self
}
pub fn unread_only(mut self, unread_only: bool) -> Self {
self.unread_only = unread_only;
self
}
pub fn page(mut self, page: i64) -> Self {
self.page = Some(page);
self
}
pub fn page_optional(mut self, page: Option<i64>) -> Self {
self.page = page;
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self
}
pub fn list(self) -> Result<Vec<ReplyView>, Error> {
use super::comment_view::reply_view::dsl::*;
let mut query = self.query;
query = query query = query
.filter(user_id.eq(for_user_id)) .filter(user_id.eq(self.for_user_id))
.filter(recipient_id.eq(for_user_id)); .filter(recipient_id.eq(self.for_user_id));
if unread_only { if self.unread_only {
query = query.filter(read.eq(false)); query = query.filter(read.eq(false));
} }
query = match sort { query = match self.sort {
// SortType::Hot => query.order_by(hot_rank.desc()), // SortType::Hot => query.order_by(hot_rank.desc()),
SortType::New => query.order_by(published.desc()), SortType::New => query.order_by(published.desc()),
SortType::TopAll => query.order_by(score.desc()), SortType::TopAll => query.order_by(score.desc()),
@ -238,7 +383,11 @@ impl ReplyView {
_ => query.order_by(published.desc()), _ => query.order_by(published.desc()),
}; };
query.limit(limit).offset(offset).load::<Self>(conn) let (limit, offset) = limit_and_offset(self.page, self.limit);
query
.limit(limit)
.offset(offset)
.load::<ReplyView>(self.conn)
} }
} }
@ -368,30 +517,16 @@ mod tests {
saved: None, saved: None,
}; };
let read_comment_views_no_user = CommentView::list( let read_comment_views_no_user = CommentQueryBuilder::create(&conn)
&conn, .for_post_id(inserted_post.id)
&SortType::New, .list()
Some(inserted_post.id), .unwrap();
None, let read_comment_views_with_user = CommentQueryBuilder::create(&conn)
None, .for_post_id(inserted_post.id)
None, .my_user_id(inserted_user.id)
false, .list()
None, .unwrap();
None,
)
.unwrap();
let read_comment_views_with_user = CommentView::list(
&conn,
&SortType::New,
Some(inserted_post.id),
None,
None,
Some(inserted_user.id),
false,
None,
None,
)
.unwrap();
let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap(); let like_removed = CommentLike::remove(&conn, &comment_like_form).unwrap();
let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap(); let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
Post::delete(&conn, inserted_post.id).unwrap(); Post::delete(&conn, inserted_post.id).unwrap();

View file

@ -1,4 +1,6 @@
use super::community_view::community_view::BoxedQuery;
use super::*; use super::*;
use diesel::pg::Pg;
table! { table! {
community_view (id) { community_view (id) {
@ -99,6 +101,134 @@ pub struct CommunityView {
pub subscribed: Option<bool>, pub subscribed: Option<bool>,
} }
pub struct CommunityQueryBuilder<'a> {
conn: &'a PgConnection,
query: BoxedQuery<'a, Pg>,
sort: &'a SortType,
from_user_id: Option<i32>,
show_nsfw: bool,
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
}
impl<'a> CommunityQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection) -> Self {
use super::community_view::community_view::dsl::*;
let query = community_view.into_boxed();
CommunityQueryBuilder {
conn,
query,
sort: &SortType::Hot,
from_user_id: None,
show_nsfw: true,
search_term: None,
page: None,
limit: None,
}
}
pub fn sort(mut self, sort: &'a SortType) -> Self {
self.sort = sort;
self
}
pub fn from_user_id(mut self, from_user_id: i32) -> Self {
self.from_user_id = Some(from_user_id);
self
}
pub fn from_user_id_optional(self, from_user_id: Option<i32>) -> Self {
match from_user_id {
Some(from_user_id) => self.from_user_id(from_user_id),
None => self,
}
}
pub fn show_nsfw(mut self, show_nsfw: bool) -> Self {
self.show_nsfw = show_nsfw;
self
}
pub fn search_term(mut self, search_term: String) -> Self {
self.search_term = Some(search_term);
self
}
pub fn search_term_optional(mut self, search_term: Option<String>) -> Self {
self.search_term = search_term;
self
}
pub fn page(mut self, page: i64) -> Self {
self.page = Some(page);
self
}
pub fn page_optional(mut self, page: Option<i64>) -> Self {
self.page = page;
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self
}
pub fn list(self) -> Result<Vec<CommunityView>, Error> {
use super::community_view::community_view::dsl::*;
let mut query = self.query;
if let Some(search_term) = self.search_term {
query = query.filter(name.ilike(fuzzy_search(&search_term)));
};
// The view lets you pass a null user_id, if you're not logged in
match self.sort {
SortType::Hot => {
query = query
.order_by(hot_rank.desc())
.then_order_by(number_of_subscribers.desc())
.filter(user_id.is_null())
}
SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
SortType::TopAll => match self.from_user_id {
Some(from_user_id) => {
query = query
.filter(user_id.eq(from_user_id))
.order_by((subscribed.asc(), number_of_subscribers.desc()))
}
None => {
query = query
.order_by(number_of_subscribers.desc())
.filter(user_id.is_null())
}
},
_ => (),
};
if !self.show_nsfw {
query = query.filter(nsfw.eq(false));
};
let (limit, offset) = limit_and_offset(self.page, self.limit);
query
.limit(limit)
.offset(offset)
.filter(removed.eq(false))
.filter(deleted.eq(false))
.load::<CommunityView>(self.conn)
}
}
impl CommunityView { impl CommunityView {
pub fn read( pub fn read(
conn: &PgConnection, conn: &PgConnection,
@ -120,60 +250,6 @@ impl CommunityView {
query.first::<Self>(conn) query.first::<Self>(conn)
} }
pub fn list(
conn: &PgConnection,
sort: &SortType,
from_user_id: Option<i32>,
show_nsfw: bool,
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
) -> Result<Vec<Self>, Error> {
use super::community_view::community_view::dsl::*;
let mut query = community_view.into_boxed();
let (limit, offset) = limit_and_offset(page, limit);
if let Some(search_term) = search_term {
query = query.filter(name.ilike(fuzzy_search(&search_term)));
};
// The view lets you pass a null user_id, if you're not logged in
match sort {
SortType::Hot => {
query = query
.order_by(hot_rank.desc())
.then_order_by(number_of_subscribers.desc())
.filter(user_id.is_null())
}
SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()),
SortType::TopAll => match from_user_id {
Some(from_user_id) => {
query = query
.filter(user_id.eq(from_user_id))
.order_by((subscribed.asc(), number_of_subscribers.desc()))
}
None => {
query = query
.order_by(number_of_subscribers.desc())
.filter(user_id.is_null())
}
},
_ => (),
};
if !show_nsfw {
query = query.filter(nsfw.eq(false));
};
query
.limit(limit)
.offset(offset)
.filter(removed.eq(false))
.filter(deleted.eq(false))
.load::<Self>(conn)
}
} }
#[derive( #[derive(

View file

@ -1,4 +1,6 @@
use super::user_mention_view::user_mention_view::BoxedQuery;
use super::*; use super::*;
use diesel::pg::Pg;
// The faked schema since diesel doesn't do views // The faked schema since diesel doesn't do views
table! { table! {
@ -57,30 +59,77 @@ pub struct UserMentionView {
pub recipient_id: i32, pub recipient_id: i32,
} }
impl UserMentionView { pub struct UserMentionQueryBuilder<'a> {
pub fn get_mentions( conn: &'a PgConnection,
conn: &PgConnection, query: BoxedQuery<'a, Pg>,
for_user_id: i32, for_user_id: i32,
sort: &SortType, sort: &'a SortType,
unread_only: bool, unread_only: bool,
page: Option<i64>, page: Option<i64>,
limit: Option<i64>, limit: Option<i64>,
) -> Result<Vec<Self>, Error> { }
impl<'a> UserMentionQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection, for_user_id: i32) -> Self {
use super::user_mention_view::user_mention_view::dsl::*; use super::user_mention_view::user_mention_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit); let query = user_mention_view.into_boxed();
let mut query = user_mention_view.into_boxed(); UserMentionQueryBuilder {
conn,
query,
for_user_id: for_user_id,
sort: &SortType::New,
unread_only: false,
page: None,
limit: None,
}
}
query = query pub fn sort(mut self, sort: &'a SortType) -> Self {
.filter(user_id.eq(for_user_id)) self.sort = sort;
.filter(recipient_id.eq(for_user_id)); self
}
if unread_only { pub fn unread_only(mut self, unread_only: bool) -> Self {
self.unread_only = unread_only;
self
}
pub fn page(mut self, page: i64) -> Self {
self.page = Some(page);
self
}
pub fn page_optional(mut self, page: Option<i64>) -> Self {
self.page = page;
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self
}
pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
use super::user_mention_view::user_mention_view::dsl::*;
let mut query = self.query;
if self.unread_only {
query = query.filter(read.eq(false)); query = query.filter(read.eq(false));
} }
query = match sort { query = query
.filter(user_id.eq(self.for_user_id))
.filter(recipient_id.eq(self.for_user_id));
query = match self.sort {
// SortType::Hot => query.order_by(hot_rank.desc()), // SortType::Hot => query.order_by(hot_rank.desc()),
SortType::New => query.order_by(published.desc()), SortType::New => query.order_by(published.desc()),
SortType::TopAll => query.order_by(score.desc()), SortType::TopAll => query.order_by(score.desc()),
@ -99,9 +148,15 @@ impl UserMentionView {
_ => query.order_by(published.desc()), _ => query.order_by(published.desc()),
}; };
query.limit(limit).offset(offset).load::<Self>(conn) let (limit, offset) = limit_and_offset(self.page, self.limit);
query
.limit(limit)
.offset(offset)
.load::<UserMentionView>(self.conn)
} }
}
impl UserMentionView {
pub fn read( pub fn read(
conn: &PgConnection, conn: &PgConnection,
from_user_mention_id: i32, from_user_mention_id: i32,

View file

@ -1,4 +1,6 @@
use super::user_view::user_view::BoxedQuery;
use super::*; use super::*;
use diesel::pg::Pg;
table! { table! {
user_view (id) { user_view (id) {
@ -32,25 +34,73 @@ pub struct UserView {
pub comment_score: i64, pub comment_score: i64,
} }
impl UserView { pub struct UserQueryBuilder<'a> {
pub fn list( conn: &'a PgConnection,
conn: &PgConnection, query: BoxedQuery<'a, Pg>,
sort: &SortType, sort: &'a SortType,
search_term: Option<String>, page: Option<i64>,
page: Option<i64>, limit: Option<i64>,
limit: Option<i64>, }
) -> Result<Vec<Self>, Error> {
impl<'a> UserQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection) -> Self {
use super::user_view::user_view::dsl::*; use super::user_view::user_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit); let query = user_view.into_boxed();
let mut query = user_view.into_boxed(); UserQueryBuilder {
conn,
query,
sort: &SortType::Hot,
page: None,
limit: None,
}
}
if let Some(search_term) = search_term { pub fn sort(mut self, sort: &'a SortType) -> Self {
query = query.filter(name.ilike(fuzzy_search(&search_term))); self.sort = sort;
}; self
}
query = match sort { pub fn search_term(mut self, search_term: String) -> Self {
use super::user_view::user_view::dsl::*;
self.query = self.query.filter(name.ilike(fuzzy_search(&search_term)));
self
}
pub fn search_term_optional(self, search_term: Option<String>) -> Self {
match search_term {
Some(search_term) => self.search_term(search_term),
None => self,
}
}
pub fn page(mut self, page: i64) -> Self {
self.page = Some(page);
self
}
pub fn page_optional(mut self, page: Option<i64>) -> Self {
self.page = page;
self
}
pub fn limit(mut self, limit: i64) -> Self {
self.limit = Some(limit);
self
}
pub fn limit_optional(mut self, limit: Option<i64>) -> Self {
self.limit = limit;
self
}
pub fn list(self) -> Result<Vec<UserView>, Error> {
use super::user_view::user_view::dsl::*;
let mut query = self.query;
query = match self.sort {
SortType::Hot => query SortType::Hot => query
.order_by(comment_score.desc()) .order_by(comment_score.desc())
.then_order_by(published.desc()), .then_order_by(published.desc()),
@ -70,11 +120,14 @@ impl UserView {
.order_by(comment_score.desc()), .order_by(comment_score.desc()),
}; };
let (limit, offset) = limit_and_offset(self.page, self.limit);
query = query.limit(limit).offset(offset); query = query.limit(limit).offset(offset);
query.load::<Self>(conn) query.load::<UserView>(self.conn)
} }
}
impl UserView {
pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> { pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> {
use super::user_view::user_view::dsl::*; use super::user_view::user_view::dsl::*;

View file

@ -1,12 +1,12 @@
extern crate rss; extern crate rss;
use super::*; use super::*;
use crate::db::comment_view::ReplyView; use crate::db::comment_view::{ReplyQueryBuilder, ReplyView};
use crate::db::community::Community; use crate::db::community::Community;
use crate::db::community_view::SiteView; use crate::db::community_view::SiteView;
use crate::db::post_view::{PostQueryBuilder, PostView}; use crate::db::post_view::{PostQueryBuilder, PostView};
use crate::db::user::User_; use crate::db::user::User_;
use crate::db::user_mention_view::UserMentionView; use crate::db::user_mention_view::{UserMentionQueryBuilder, UserMentionView};
use crate::db::{establish_connection, ListingType, SortType}; use crate::db::{establish_connection, ListingType, SortType};
use crate::Settings; use crate::Settings;
use actix_web::body::Body; use actix_web::body::Body;
@ -193,9 +193,13 @@ fn get_feed_inbox(jwt: String) -> Result<String, Error> {
let sort = SortType::New; let sort = SortType::New;
let replies = ReplyView::get_replies(&conn, user_id, &sort, false, None, None)?; let replies = ReplyQueryBuilder::create(&conn, user_id)
.sort(&sort)
.list()?;
let mentions = UserMentionView::get_mentions(&conn, user_id, &sort, false, None, None)?; let mentions = UserMentionQueryBuilder::create(&conn, user_id)
.sort(&sort)
.list()?;
let items = create_reply_and_mention_items(replies, mentions); let items = create_reply_and_mention_items(replies, mentions);

View file

@ -180,7 +180,10 @@ export class User extends Component<any, UserState> {
componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) { componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
// Necessary if you are on a post and you click another post (same route) // Necessary if you are on a post and you click another post (same route)
if (lastProps.location.pathname !== lastProps.history.location.pathname) { if (
lastProps.location.pathname.split('/')[2] !==
lastProps.history.location.pathname.split('/')[2]
) {
// Couldnt get a refresh working. This does for now. // Couldnt get a refresh working. This does for now.
location.reload(); location.reload();
} }