Adding support for community and user searching.

- Fixes #130
This commit is contained in:
Dessalines 2019-08-10 10:32:06 -07:00
parent ec4699e9ca
commit d0d429a627
9 changed files with 209 additions and 58 deletions

View file

@ -36,6 +36,7 @@ Front Page|Post
- Can lock, remove, and restore posts and comments. - Can lock, remove, and restore posts and comments.
- Can ban and unban users from communities and the site. - Can ban and unban users from communities and the site.
- Clean, mobile-friendly interface. - Clean, mobile-friendly interface.
- i18n / internationalization support.
- High performance. - High performance.
- Server is written in rust. - Server is written in rust.
- Front end is `~80kB` gzipped. - Front end is `~80kB` gzipped.

View file

@ -348,7 +348,7 @@ 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(&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 // Return the jwt
Ok( Ok(

View file

@ -25,6 +25,8 @@ pub struct SearchResponse {
op: String, op: String,
comments: Vec<CommentView>, comments: Vec<CommentView>,
posts: Vec<PostView>, posts: Vec<PostView>,
communities: Vec<CommunityView>,
users: Vec<UserView>,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -272,53 +274,89 @@ impl Perform<SearchResponse> for Oper<Search> {
let mut posts = Vec::new(); let mut posts = Vec::new();
let mut comments = Vec::new(); let mut comments = Vec::new();
let mut communities = Vec::new();
let mut users = Vec::new();
match type_ { match type_ {
SearchType::Posts => { SearchType::Posts => {
posts = PostView::list(&conn, posts = PostView::list(
PostListingType::All, &conn,
&sort, PostListingType::All,
data.community_id, &sort,
None, data.community_id,
Some(data.q.to_owned()), None,
None, Some(data.q.to_owned()),
false, None,
false, false,
data.page, false,
data.limit)?; data.page,
data.limit)?;
}, },
SearchType::Comments => { SearchType::Comments => {
comments = CommentView::list(&conn, comments = CommentView::list(
&sort, &conn,
None, &sort,
None, None,
Some(data.q.to_owned()), None,
None, Some(data.q.to_owned()),
false, None,
data.page, false,
data.limit)?; data.page,
data.limit)?;
}, },
SearchType::Both => { SearchType::Communities => {
posts = PostView::list(&conn, communities = CommunityView::list(
PostListingType::All, &conn,
&sort, &sort,
data.community_id, None,
None, Some(data.q.to_owned()),
Some(data.q.to_owned()), data.page,
None, data.limit)?;
false, },
false, SearchType::Users => {
data.page, users = UserView::list(
data.limit)?; &conn,
comments = CommentView::list(&conn, &sort,
&sort, Some(data.q.to_owned()),
None, data.page,
None, data.limit)?;
Some(data.q.to_owned()), },
None, SearchType::All => {
false, posts = PostView::list(
data.page, &conn,
data.limit)?; 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(), op: self.op.to_string(),
comments: comments, comments: comments,
posts: posts, posts: posts,
communities: communities,
users: users,
} }
) )
} }

View file

@ -113,8 +113,9 @@ impl CommunityView {
} }
pub fn list(conn: &PgConnection, pub fn list(conn: &PgConnection,
sort: &SortType,
from_user_id: Option<i32>, from_user_id: Option<i32>,
sort: SortType, search_term: Option<String>,
page: Option<i64>, page: Option<i64>,
limit: Option<i64>, limit: Option<i64>,
) -> Result<Vec<Self>, Error> { ) -> Result<Vec<Self>, Error> {
@ -123,6 +124,10 @@ impl CommunityView {
let (limit, offset) = limit_and_offset(page, limit); 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 // The view lets you pass a null user_id, if you're not logged in
match sort { match sort {
SortType::Hot => query = query.order_by(hot_rank.desc()) SortType::Hot => query = query.order_by(hot_rank.desc())

View file

@ -67,7 +67,7 @@ pub enum SortType {
#[derive(EnumString,ToString,Debug, Serialize, Deserialize)] #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
pub enum SearchType { pub enum SearchType {
Both, Comments, Posts All, Comments, Posts, Communities, Users
} }
pub fn fuzzy_search(q: &str) -> String { pub fn fuzzy_search(q: &str) -> String {

View file

@ -31,6 +31,49 @@ pub struct UserView {
} }
impl 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> { 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,7 +1,8 @@
import { Component, linkEvent } from 'inferno'; import { Component, linkEvent } from 'inferno';
import { Link } from 'inferno-router';
import { Subscription } from "rxjs"; import { Subscription } from "rxjs";
import { retryWhen, delay, take } from 'rxjs/operators'; 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 { WebSocketService } from '../services';
import { msgOp, fetchLimit } from '../utils'; import { msgOp, fetchLimit } from '../utils';
import { PostListing } from './post-listing'; import { PostListing } from './post-listing';
@ -23,13 +24,15 @@ export class Search extends Component<any, SearchState> {
private subscription: Subscription; private subscription: Subscription;
private emptyState: SearchState = { private emptyState: SearchState = {
q: undefined, q: undefined,
type_: SearchType.Both, type_: SearchType.All,
sort: SortType.TopAll, sort: SortType.TopAll,
page: 1, page: 1,
searchResponse: { searchResponse: {
op: null, op: null,
posts: [], posts: [],
comments: [], comments: [],
communities: [],
users: [],
}, },
loading: false, loading: false,
} }
@ -65,8 +68,8 @@ export class Search extends Component<any, SearchState> {
<h5><T i18nKey="search">#</T></h5> <h5><T i18nKey="search">#</T></h5>
{this.selects()} {this.selects()}
{this.searchForm()} {this.searchForm()}
{this.state.type_ == SearchType.Both && {this.state.type_ == SearchType.All &&
this.both() this.all()
} }
{this.state.type_ == SearchType.Comments && {this.state.type_ == SearchType.Comments &&
this.comments() this.comments()
@ -74,6 +77,12 @@ export class Search extends Component<any, SearchState> {
{this.state.type_ == SearchType.Posts && {this.state.type_ == SearchType.Posts &&
this.posts() this.posts()
} }
{this.state.type_ == SearchType.Communities &&
this.communities()
}
{this.state.type_ == SearchType.Users &&
this.users()
}
{this.noResults()} {this.noResults()}
{this.paginator()} {this.paginator()}
</div> </div>
@ -101,9 +110,11 @@ export class Search extends Component<any, SearchState> {
<div className="mb-2"> <div className="mb-2">
<select value={this.state.type_} onChange={linkEvent(this, this.handleTypeChange)} class="custom-select custom-select-sm w-auto"> <select value={this.state.type_} onChange={linkEvent(this, this.handleTypeChange)} class="custom-select custom-select-sm w-auto">
<option disabled><T i18nKey="type">#</T></option> <option disabled><T i18nKey="type">#</T></option>
<option value={SearchType.Both}><T i18nKey="both">#</T></option> <option value={SearchType.All}><T i18nKey="all">#</T></option>
<option value={SearchType.Comments}><T i18nKey="comments">#</T></option> <option value={SearchType.Comments}><T i18nKey="comments">#</T></option>
<option value={SearchType.Posts}><T i18nKey="posts">#</T></option> <option value={SearchType.Posts}><T i18nKey="posts">#</T></option>
<option value={SearchType.Communities}><T i18nKey="communities">#</T></option>
<option value={SearchType.Users}><T i18nKey="users">#</T></option>
</select> </select>
<select value={this.state.sort} onChange={linkEvent(this, this.handleSortChange)} class="custom-select custom-select-sm w-auto ml-2"> <select value={this.state.sort} onChange={linkEvent(this, this.handleSortChange)} class="custom-select custom-select-sm w-auto ml-2">
<option disabled><T i18nKey="sort_type">#</T></option> <option disabled><T i18nKey="sort_type">#</T></option>
@ -119,28 +130,51 @@ export class Search extends Component<any, SearchState> {
} }
both() { all() {
let combined: Array<{type_: string, data: Comment | Post}> = []; let combined: Array<{type_: string, data: Comment | Post | Community | UserView}> = [];
let comments = this.state.searchResponse.comments.map(e => {return {type_: "comments", data: e}}); let comments = this.state.searchResponse.comments.map(e => {return {type_: "comments", data: e}});
let posts = this.state.searchResponse.posts.map(e => {return {type_: "posts", data: e}}); let posts = this.state.searchResponse.posts.map(e => {return {type_: "posts", data: e}});
let communities = this.state.searchResponse.communities.map(e => {return {type_: "communities", data: e}});
let users = this.state.searchResponse.users.map(e => {return {type_: "users", data: e}});
combined.push(...comments); combined.push(...comments);
combined.push(...posts); combined.push(...posts);
combined.push(...communities);
combined.push(...users);
// Sort it // Sort it
if (this.state.sort == SortType.New) { if (this.state.sort == SortType.New) {
combined.sort((a, b) => b.data.published.localeCompare(a.data.published)); combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
} else { } else {
combined.sort((a, b) => b.data.score - a.data.score); combined.sort((a, b) => ((b.data as Comment | Post).score
| (b.data as Community).number_of_subscribers
| (b.data as UserView).comment_score)
- ((a.data as Comment | Post).score
| (a.data as Community).number_of_subscribers
| (a.data as UserView).comment_score));
} }
return ( return (
<div> <div>
{combined.map(i => {combined.map(i =>
<div> <div>
{i.type_ == "posts" {i.type_ == "posts" &&
? <PostListing post={i.data as Post} showCommunity viewOnly /> <PostListing post={i.data as Post} showCommunity viewOnly />
: <CommentNodes nodes={[{comment: i.data as Comment}]} viewOnly noIndent /> }
{i.type_ == "comments" &&
<CommentNodes nodes={[{comment: i.data as Comment}]} viewOnly noIndent />
}
{i.type_ == "communities" &&
<div>
<span><Link to={`/c/${(i.data as Community).name}`}>{`/c/${(i.data as Community).name}`}</Link></span>
<span>{` - ${(i.data as Community).title} - ${(i.data as Community).number_of_subscribers} subscribers`}</span>
</div>
}
{i.type_ == "users" &&
<div>
<span><Link className="text-info" to={`/u/${(i.data as UserView).name}`}>{`/u/${(i.data as UserView).name}`}</Link></span>
<span>{` - ${(i.data as UserView).comment_score} comment karma`}</span>
</div>
} }
</div> </div>
) )
@ -169,6 +203,33 @@ export class Search extends Component<any, SearchState> {
); );
} }
// Todo possibly create UserListing and CommunityListing
communities() {
return (
<div>
{this.state.searchResponse.communities.map(community =>
<div>
<span><Link to={`/c/${community.name}`}>{`/c/${community.name}`}</Link></span>
<span>{` - ${community.title} - ${community.number_of_subscribers} subscribers`}</span>
</div>
)}
</div>
);
}
users() {
return (
<div>
{this.state.searchResponse.users.map(user =>
<div>
<span><Link className="text-info" to={`/u/${user.name}`}>{`/u/${user.name}`}</Link></span>
<span>{` - ${user.comment_score} comment karma`}</span>
</div>
)}
</div>
);
}
paginator() { paginator() {
return ( return (
<div class="mt-2"> <div class="mt-2">
@ -220,14 +281,12 @@ export class Search extends Component<any, SearchState> {
i.state.sort = Number(event.target.value); i.state.sort = Number(event.target.value);
i.state.page = 1; i.state.page = 1;
i.setState(i.state); i.setState(i.state);
i.search();
} }
handleTypeChange(i: Search, event: any) { handleTypeChange(i: Search, event: any) {
i.state.type_ = Number(event.target.value); i.state.type_ = Number(event.target.value);
i.state.page = 1; i.state.page = 1;
i.setState(i.state); i.setState(i.state);
i.search();
} }
handleSearchSubmit(i: Search, event: any) { handleSearchSubmit(i: Search, event: any) {

View file

@ -15,7 +15,7 @@ export enum SortType {
} }
export enum SearchType { export enum SearchType {
Both, Comments, Posts All, Comments, Posts, Communities, Users
} }
export interface User { export interface User {
@ -542,4 +542,6 @@ export interface SearchResponse {
op: string; op: string;
posts?: Array<Post>; posts?: Array<Post>;
comments?: Array<Comment>; comments?: Array<Comment>;
communities: Array<Community>;
users: Array<UserView>;
} }

View file

@ -12,6 +12,7 @@ export const en = {
number_of_comments:'{{count}} Comments', number_of_comments:'{{count}} Comments',
remove_comment: 'Remove Comment', remove_comment: 'Remove Comment',
communities: 'Communities', communities: 'Communities',
users: 'Users',
create_a_community: 'Create a community', create_a_community: 'Create a community',
create_community: 'Create Community', create_community: 'Create Community',
remove_community: 'Remove Community', remove_community: 'Remove Community',