Use a dedicated structure in order to search posts

This commit is contained in:
Lyra 2019-12-07 13:03:03 +01:00
parent 4762064ae5
commit 85d1f3c723
6 changed files with 209 additions and 206 deletions

View file

@ -238,21 +238,13 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
let type_ = ListingType::from_str(&data.type_)?; let type_ = ListingType::from_str(&data.type_)?;
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;
let posts = match PostView::list( let posts = match PostViewQuery::create(&conn, type_, &sort, show_nsfw, false, false)
&conn, .for_community_id_optional(data.community_id)
type_, .my_user_id_optional(user_id)
&sort, .page_optional(data.page)
data.community_id, .limit_optional(data.limit)
None, .list()
None, {
None,
user_id,
show_nsfw,
false,
false,
data.page,
data.limit,
) {
Ok(posts) => posts, Ok(posts) => posts,
Err(_e) => return Err(APIError::err(&self.op, "couldnt_get_posts"))?, Err(_e) => return Err(APIError::err(&self.op, "couldnt_get_posts"))?,
}; };

View file

@ -319,21 +319,12 @@ impl Perform<SearchResponse> for Oper<Search> {
match type_ { match type_ {
SearchType::Posts => { SearchType::Posts => {
posts = PostView::list( posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false)
&conn, .for_community_id_optional(data.community_id)
ListingType::All, .search_term(data.q.to_owned())
&sort, .page_optional(data.page)
data.community_id, .limit_optional(data.limit)
None, .list()?;
Some(data.q.to_owned()),
None,
None,
true,
false,
false,
data.page,
data.limit,
)?;
} }
SearchType::Comments => { SearchType::Comments => {
comments = CommentView::list( comments = CommentView::list(
@ -363,21 +354,13 @@ impl Perform<SearchResponse> for Oper<Search> {
users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?; users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
} }
SearchType::All => { SearchType::All => {
posts = PostView::list( posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false)
&conn, .for_community_id_optional(data.community_id)
ListingType::All, .search_term(data.q.to_owned())
&sort, .page_optional(data.page)
data.community_id, .limit_optional(data.limit)
None, .list()?;
Some(data.q.to_owned()),
None,
None,
true,
false,
false,
data.page,
data.limit,
)?;
comments = CommentView::list( comments = CommentView::list(
&conn, &conn,
&sort, &sort,
@ -401,21 +384,12 @@ impl Perform<SearchResponse> for Oper<Search> {
users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?; users = UserView::list(&conn, &sort, Some(data.q.to_owned()), data.page, data.limit)?;
} }
SearchType::Url => { SearchType::Url => {
posts = PostView::list( posts = PostViewQuery::create(&conn, ListingType::All, &sort, true, false, false)
&conn, .for_community_id_optional(data.community_id)
ListingType::All, .url_search(data.q.to_owned())
&sort, .page_optional(data.page)
data.community_id, .limit_optional(data.limit)
None, .list()?;
None,
Some(data.q.to_owned()),
None,
true,
false,
false,
data.page,
data.limit,
)?;
} }
}; };

View file

@ -366,40 +366,26 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
let user_view = UserView::read(&conn, user_details_id)?; let user_view = UserView::read(&conn, user_details_id)?;
let mut posts_query = PostViewQuery::create(
&conn,
ListingType::All,
&sort,
show_nsfw,
data.saved_only,
false,
)
.for_community_id_optional(data.community_id)
.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
let posts = if data.saved_only { if !data.saved_only {
PostView::list( posts_query = posts_query.for_creator_id(user_details_id);
&conn, }
ListingType::All,
&sort, let posts = posts_query.list()?;
data.community_id,
None,
None,
None,
Some(user_details_id),
show_nsfw,
data.saved_only,
false,
data.page,
data.limit,
)?
} else {
PostView::list(
&conn,
ListingType::All,
&sort,
data.community_id,
Some(user_details_id),
None,
None,
user_id,
show_nsfw,
data.saved_only,
false,
data.page,
data.limit,
)?
};
let comments = if data.saved_only { let comments = if data.saved_only {
CommentView::list( CommentView::list(
&conn, &conn,
@ -777,21 +763,10 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> {
} }
// Posts // Posts
let posts = PostView::list( let posts = PostViewQuery::create(&conn, ListingType::All, &SortType::New, true, false, false)
&conn, .for_creator_id(user_id)
ListingType::All, .limit(std::i64::MAX)
&SortType::New, .list()?;
None,
Some(user_id),
None,
None,
None,
true,
false,
false,
None,
Some(std::i64::MAX),
)?;
for post in &posts { for post in &posts {
let post_form = PostForm { let post_form = PostForm {

View file

@ -1,4 +1,6 @@
use super::post_view::post_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! {
@ -73,74 +75,34 @@ pub struct PostView {
pub saved: Option<bool>, pub saved: Option<bool>,
} }
impl PostView { pub struct PostViewQuery<'a> {
pub fn list( conn: &'a PgConnection,
conn: &PgConnection, query: BoxedQuery<'a, Pg>,
type_: ListingType, my_user_id: Option<i32>,
sort: &SortType, page: Option<i64>,
for_community_id: Option<i32>, limit: Option<i64>,
for_creator_id: Option<i32>, }
search_term: Option<String>,
url_search: Option<String>, impl<'a> PostViewQuery<'a> {
my_user_id: Option<i32>, pub fn create(
conn: &'a PgConnection,
r#type: ListingType,
sort: &'a SortType,
show_nsfw: bool, show_nsfw: bool,
saved_only: bool, saved_only: bool,
unread_only: bool, unread_only: bool,
page: Option<i64>, ) -> Self {
limit: Option<i64>,
) -> Result<Vec<Self>, Error> {
use super::post_view::post_view::dsl::*; use super::post_view::post_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit);
let mut query = post_view.into_boxed(); let mut query = post_view.into_boxed();
if let Some(for_creator_id) = for_creator_id { match r#type {
query = query.filter(creator_id.eq(for_creator_id));
};
if let Some(search_term) = search_term {
query = query.filter(name.ilike(fuzzy_search(&search_term)));
};
if let Some(url_search) = url_search {
query = query.filter(url.eq(url_search));
};
if let Some(for_community_id) = for_community_id {
query = query.filter(community_id.eq(for_community_id));
query = query.then_order_by(stickied.desc());
};
// TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
if saved_only {
query = query.filter(saved.eq(true));
};
if unread_only {
query = query.filter(read.eq(false));
};
match type_ {
ListingType::Subscribed => { ListingType::Subscribed => {
query = query.filter(subscribed.eq(true)); query = query.filter(subscribed.eq(true));
} }
_ => {} _ => {}
}; };
// The view lets you pass a null user_id, if you're not logged in
if let Some(my_user_id) = my_user_id {
query = query.filter(user_id.eq(my_user_id));
} else {
query = query.filter(user_id.is_null());
}
if !show_nsfw {
query = query
.filter(nsfw.eq(false))
.filter(community_nsfw.eq(false));
};
query = match sort { query = match sort {
SortType::Hot => query SortType::Hot => query
.then_order_by(hot_rank.desc()) .then_order_by(hot_rank.desc())
@ -161,7 +123,125 @@ impl PostView {
.then_order_by(score.desc()), .then_order_by(score.desc()),
}; };
query = query if !show_nsfw {
query = query
.filter(nsfw.eq(false))
.filter(community_nsfw.eq(false));
};
// TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
if saved_only {
query = query.filter(saved.eq(true));
};
if unread_only {
query = query.filter(read.eq(false));
};
PostViewQuery {
conn,
query,
my_user_id: None,
page: None,
limit: None,
}
}
pub fn for_community_id(mut self, for_community_id: i32) -> Self {
use super::post_view::post_view::dsl::*;
self.query = self.query.filter(community_id.eq(for_community_id));
self.query = self.query.then_order_by(stickied.desc());
self
}
pub fn for_community_id_optional(self, for_community_id: Option<i32>) -> Self {
match for_community_id {
Some(for_community_id) => self.for_community_id(for_community_id),
None => self,
}
}
pub fn for_creator_id(mut self, for_creator_id: i32) -> Self {
use super::post_view::post_view::dsl::*;
self.query = self.query.filter(creator_id.eq(for_creator_id));
self
}
pub fn for_creator_id_optional(self, for_creator_id: Option<i32>) -> Self {
match for_creator_id {
Some(for_creator_id) => self.for_creator_id(for_creator_id),
None => self,
}
}
pub fn search_term(mut self, search_term: String) -> Self {
use super::post_view::post_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 url_search(mut self, url_search: String) -> Self {
use super::post_view::post_view::dsl::*;
self.query = self.query.filter(url.eq(url_search));
self
}
pub fn url_search_optional(self, url_search: Option<String>) -> Self {
match url_search {
Some(url_search) => self.url_search(url_search),
None => 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 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(mut self) -> Result<Vec<PostView>, Error> {
use super::post_view::post_view::dsl::*;
// The view lets you pass a null user_id, if you're not logged in
self.query = if let Some(my_user_id) = self.my_user_id {
self.query.filter(user_id.eq(my_user_id))
} else {
self.query.filter(user_id.is_null())
};
let (limit, offset) = limit_and_offset(self.page, self.limit);
let query = self
.query
.limit(limit) .limit(limit)
.offset(offset) .offset(offset)
.filter(removed.eq(false)) .filter(removed.eq(false))
@ -169,9 +249,11 @@ impl PostView {
.filter(community_removed.eq(false)) .filter(community_removed.eq(false))
.filter(community_deleted.eq(false)); .filter(community_deleted.eq(false));
query.load::<Self>(conn) query.load::<PostView>(self.conn)
} }
}
impl PostView {
pub fn read( pub fn read(
conn: &PgConnection, conn: &PgConnection,
from_post_id: i32, from_post_id: i32,
@ -344,38 +426,31 @@ mod tests {
nsfw: false, nsfw: false,
}; };
let read_post_listings_with_user = PostView::list( let read_post_listings_with_user = PostViewQuery::create(
&conn, &conn,
ListingType::Community, ListingType::Community,
&SortType::New, &SortType::New,
Some(inserted_community.id),
None,
None,
None,
Some(inserted_user.id),
false, false,
false, false,
false, false,
None,
None,
) )
.for_community_id(inserted_community.id)
.my_user_id(inserted_user.id)
.list()
.unwrap(); .unwrap();
let read_post_listings_no_user = PostView::list(
let read_post_listings_no_user = PostViewQuery::create(
&conn, &conn,
ListingType::Community, ListingType::Community,
&SortType::New, &SortType::New,
Some(inserted_community.id),
None,
None,
None,
None,
false, false,
false, false,
false, false,
None,
None,
) )
.for_community_id(inserted_community.id)
.list()
.unwrap(); .unwrap();
let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap(); let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
let read_post_listing_with_user = let read_post_listing_with_user =
PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap(); PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();

View file

@ -4,7 +4,7 @@ extern crate rss;
use super::*; use super::*;
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::PostView; use crate::db::post_view::PostViewQuery;
use crate::db::user::User_; use crate::db::user::User_;
use crate::db::{establish_connection, ListingType, SortType}; use crate::db::{establish_connection, ListingType, SortType};
use crate::Settings; use crate::Settings;
@ -124,21 +124,10 @@ fn get_feed_internal(
} }
} }
let posts = PostView::list( let posts = PostViewQuery::create(&conn, ListingType::All, sort_type, true, false, false)
&conn, .for_community_id_optional(community_id)
ListingType::All, .for_creator_id_optional(creator_id)
sort_type, .list()?;
community_id,
creator_id,
None,
None,
None,
true,
false,
false,
None,
None,
)?;
let mut items: Vec<Item> = Vec::new(); let mut items: Vec<Item> = Vec::new();

View file

@ -134,21 +134,19 @@ impl ChatServer {
use crate::db::post_view::*; use crate::db::post_view::*;
use crate::db::*; use crate::db::*;
let conn = establish_connection(); let conn = establish_connection();
let posts = PostView::list(
let posts = PostViewQuery::create(
&conn, &conn,
ListingType::Community, ListingType::Community,
&SortType::New, &SortType::New,
Some(*community_id),
None,
None,
None,
None,
false, false,
false, false,
false, false,
None, )
Some(9999), .for_community_id(*community_id)
)?; .limit(9999)
.list()?;
for post in posts { for post in posts {
self.send_room_message(&post.id, message, skip_id); self.send_room_message(&post.id, message, skip_id);
} }