From: Dessalines Date: Sat, 10 Aug 2019 17:32:06 +0000 (-0700) Subject: Adding support for community and user searching. X-Git-Url: http://these/git/%7Bthis.props.banner%7D?a=commitdiff_plain;h=d0d429a627e5adfa36326bd6641dc633112b07b2;p=lemmy.git Adding support for community and user searching. - Fixes #130 --- diff --git a/README.md b/README.md index ed21cfe3..b0259579 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Front Page|Post - Can lock, remove, and restore posts and comments. - Can ban and unban users from communities and the site. - Clean, mobile-friendly interface. +- i18n / internationalization support. - High performance. - Server is written in rust. - Front end is `~80kB` gzipped. 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 for Oper { let sort = SortType::from_str(&data.sort)?; - let communities: Vec = CommunityView::list(&conn, user_id, sort, data.page, data.limit)?; + let communities: Vec = 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, posts: Vec, + communities: Vec, + users: Vec, } #[derive(Serialize, Deserialize)] @@ -272,53 +274,89 @@ impl Perform for Oper { 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 for Oper { 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, - sort: SortType, + search_term: Option, page: Option, limit: Option, ) -> Result, 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, + page: Option, + limit: Option, + ) -> Result, 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::(conn) + } + pub fn read(conn: &PgConnection, from_user_id: i32) -> Result { use super::user_view::user_view::dsl::*; diff --git a/ui/src/components/search.tsx b/ui/src/components/search.tsx index 01122fd4..0f8727cb 100644 --- a/ui/src/components/search.tsx +++ b/ui/src/components/search.tsx @@ -1,7 +1,8 @@ import { Component, linkEvent } from 'inferno'; +import { Link } from 'inferno-router'; import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; -import { UserOperation, Post, Comment, SortType, SearchForm, SearchResponse, SearchType } from '../interfaces'; +import { UserOperation, Post, Comment, Community, UserView, SortType, SearchForm, SearchResponse, SearchType } from '../interfaces'; import { WebSocketService } from '../services'; import { msgOp, fetchLimit } from '../utils'; import { PostListing } from './post-listing'; @@ -23,13 +24,15 @@ export class Search extends Component { private subscription: Subscription; private emptyState: SearchState = { q: undefined, - type_: SearchType.Both, + type_: SearchType.All, sort: SortType.TopAll, page: 1, searchResponse: { op: null, posts: [], comments: [], + communities: [], + users: [], }, loading: false, } @@ -65,8 +68,8 @@ export class Search extends Component {
#
{this.selects()} {this.searchForm()} - {this.state.type_ == SearchType.Both && - this.both() + {this.state.type_ == SearchType.All && + this.all() } {this.state.type_ == SearchType.Comments && this.comments() @@ -74,6 +77,12 @@ export class Search extends Component { {this.state.type_ == SearchType.Posts && this.posts() } + {this.state.type_ == SearchType.Communities && + this.communities() + } + {this.state.type_ == SearchType.Users && + this.users() + } {this.noResults()} {this.paginator()} @@ -101,9 +110,11 @@ export class Search extends Component {