summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDessalines <happydooby@gmail.com>2019-08-10 10:32:06 -0700
committerDessalines <happydooby@gmail.com>2019-08-10 10:32:06 -0700
commit30f5f97f2fd9703c5a197bd08842de01fe7eb93b (patch)
treedc90721fbad5ce88781db9f091497f508950cc7e /server
parent5ced2cdb4fa6f33f064631f9519025e329b61422 (diff)
Adding support for community and user searching.
- Fixes #130
Diffstat (limited to 'server')
-rw-r--r--server/src/api/community.rs2
-rw-r--r--server/src/api/site.rs122
-rw-r--r--server/src/db/community_view.rs7
-rw-r--r--server/src/db/mod.rs2
-rw-r--r--server/src/db/user_view.rs43
5 files changed, 132 insertions, 44 deletions
diff --git a/server/src/api/community.rs b/server/src/api/community.rs
index fe225794..ca73de49 100644
--- a/server/src/api/community.rs
+++ b/server/src/api/community.rs
@@ -348,7 +348,7 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
let sort = SortType::from_str(&data.sort)?;
- let communities: Vec<CommunityView> = CommunityView::list(&conn, user_id, sort, data.page, data.limit)?;
+ let communities: Vec<CommunityView> = CommunityView::list(&conn, &sort, user_id, None, data.page, data.limit)?;
// Return the jwt
Ok(
diff --git a/server/src/api/site.rs b/server/src/api/site.rs
index 08fefae4..09af742f 100644
--- a/server/src/api/site.rs
+++ b/server/src/api/site.rs
@@ -25,6 +25,8 @@ pub struct SearchResponse {
op: String,
comments: Vec<CommentView>,
posts: Vec<PostView>,
+ communities: Vec<CommunityView>,
+ users: Vec<UserView>,
}
#[derive(Serialize, Deserialize)]
@@ -272,53 +274,89 @@ impl Perform<SearchResponse> for Oper<Search> {
let mut posts = Vec::new();
let mut comments = Vec::new();
+ let mut communities = Vec::new();
+ let mut users = Vec::new();
match type_ {
SearchType::Posts => {
- posts = PostView::list(&conn,
- PostListingType::All,
- &sort,
- data.community_id,
- None,
- Some(data.q.to_owned()),
- None,
- false,
- false,
- data.page,
- data.limit)?;
+ posts = PostView::list(
+ &conn,
+ PostListingType::All,
+ &sort,
+ data.community_id,
+ None,
+ Some(data.q.to_owned()),
+ None,
+ false,
+ false,
+ data.page,
+ data.limit)?;
},
SearchType::Comments => {
- comments = CommentView::list(&conn,
- &sort,
- None,
- None,
- Some(data.q.to_owned()),
- None,
- false,
- data.page,
- data.limit)?;
+ comments = CommentView::list(
+ &conn,
+ &sort,
+ None,
+ None,
+ Some(data.q.to_owned()),
+ None,
+ false,
+ data.page,
+ data.limit)?;
+ },
+ SearchType::Communities => {
+ communities = CommunityView::list(
+ &conn,
+ &sort,
+ None,
+ Some(data.q.to_owned()),
+ data.page,
+ data.limit)?;
+ },
+ SearchType::Users => {
+ users = UserView::list(
+ &conn,
+ &sort,
+ Some(data.q.to_owned()),
+ data.page,
+ data.limit)?;
},
- SearchType::Both => {
- posts = PostView::list(&conn,
- PostListingType::All,
- &sort,
- data.community_id,
- None,
- Some(data.q.to_owned()),
- None,
- false,
- false,
- data.page,
- data.limit)?;
- comments = CommentView::list(&conn,
- &sort,
- None,
- None,
- Some(data.q.to_owned()),
- None,
- false,
- data.page,
- data.limit)?;
+ SearchType::All => {
+ posts = PostView::list(
+ &conn,
+ PostListingType::All,
+ &sort,
+ data.community_id,
+ None,
+ Some(data.q.to_owned()),
+ None,
+ false,
+ false,
+ data.page,
+ data.limit)?;
+ comments = CommentView::list(
+ &conn,
+ &sort,
+ None,
+ None,
+ Some(data.q.to_owned()),
+ None,
+ false,
+ data.page,
+ data.limit)?;
+ communities = CommunityView::list(
+ &conn,
+ &sort,
+ None,
+ Some(data.q.to_owned()),
+ data.page,
+ data.limit)?;
+ users = UserView::list(
+ &conn,
+ &sort,
+ Some(data.q.to_owned()),
+ data.page,
+ data.limit)?;
}
};
@@ -329,6 +367,8 @@ impl Perform<SearchResponse> for Oper<Search> {
op: self.op.to_string(),
comments: comments,
posts: posts,
+ communities: communities,
+ users: users,
}
)
}
diff --git a/server/src/db/community_view.rs b/server/src/db/community_view.rs
index ff0fc89b..6249090d 100644
--- a/server/src/db/community_view.rs
+++ b/server/src/db/community_view.rs
@@ -113,8 +113,9 @@ impl CommunityView {
}
pub fn list(conn: &PgConnection,
+ sort: &SortType,
from_user_id: Option<i32>,
- sort: SortType,
+ search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
) -> Result<Vec<Self>, Error> {
@@ -123,6 +124,10 @@ impl CommunityView {
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())
diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs
index e0b7c856..9f0c79b8 100644
--- a/server/src/db/mod.rs
+++ b/server/src/db/mod.rs
@@ -67,7 +67,7 @@ pub enum SortType {
#[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
pub enum SearchType {
- Both, Comments, Posts
+ All, Comments, Posts, Communities, Users
}
pub fn fuzzy_search(q: &str) -> String {
diff --git a/server/src/db/user_view.rs b/server/src/db/user_view.rs
index 3d78ae1a..897ee23a 100644
--- a/server/src/db/user_view.rs
+++ b/server/src/db/user_view.rs
@@ -31,6 +31,49 @@ pub struct UserView {
}
impl UserView {
+
+ pub fn list(conn: &PgConnection,
+ sort: &SortType,
+ search_term: Option<String>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
+ use super::user_view::user_view::dsl::*;
+
+ let (limit, offset) = limit_and_offset(page, limit);
+
+ let mut query = user_view.into_boxed();
+
+ if let Some(search_term) = search_term {
+ query = query.filter(name.ilike(fuzzy_search(&search_term)));
+ };
+
+ query = match sort {
+ SortType::Hot => query.order_by(comment_score.desc())
+ .then_order_by(published.desc()),
+ SortType::New => query.order_by(published.desc()),
+ SortType::TopAll => query.order_by(comment_score.desc()),
+ SortType::TopYear => query
+ .filter(published.gt(now - 1.years()))
+ .order_by(comment_score.desc()),
+ SortType::TopMonth => query
+ .filter(published.gt(now - 1.months()))
+ .order_by(comment_score.desc()),
+ SortType::TopWeek => query
+ .filter(published.gt(now - 1.weeks()))
+ .order_by(comment_score.desc()),
+ SortType::TopDay => query
+ .filter(published.gt(now - 1.days()))
+ .order_by(comment_score.desc())
+ };
+
+ query = query
+ .limit(limit)
+ .offset(offset);
+
+ query.load::<Self>(conn)
+ }
+
pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> {
use super::user_view::user_view::dsl::*;