use diesel::*;
use diesel::result::Error;
use serde::{Deserialize, Serialize};
+use {SortType};
table! {
community_view (id) {
query.first::<Self>(conn)
}
- pub fn list_all(conn: &PgConnection, from_user_id: Option<i32>) -> Result<Vec<Self>, Error> {
+ pub fn list(conn: &PgConnection, from_user_id: Option<i32>, sort: SortType, limit: Option<i64>) -> Result<Vec<Self>, Error> {
use actions::community_view::community_view::dsl::*;
let mut query = community_view.into_boxed();
+
+
// The view lets you pass a null user_id, if you're not logged in
- if let Some(from_user_id) = from_user_id {
- query = query.filter(user_id.eq(from_user_id))
- .order_by((subscribed.desc(), number_of_subscribers.desc()));
- } else {
- query = query.filter(user_id.is_null())
- .order_by(number_of_subscribers.desc());
+
+ match sort {
+ 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.desc(), number_of_subscribers.desc())),
+ None => query = query.order_by(number_of_subscribers.desc()).filter(user_id.is_null())
+ }
+ }
+ _ => ()
+ };
+
+ if let Some(limit) = limit {
+ query = query.limit(limit);
};
query.load::<Self>(conn)
#[derive(Serialize, Deserialize)]
pub struct ListCommunities {
+ sort: String,
+ limit: Option<i64>,
auth: Option<String>
}
None => None
};
- let communities: Vec<CommunityView> = CommunityView::list_all(&conn, user_id).unwrap();
+ let sort = SortType::from_str(&self.sort).expect("listing sort");
+
+ let communities: Vec<CommunityView> = CommunityView::list(&conn, user_id, sort, self.limit).unwrap();
// Return the jwt
serde_json::to_string(
import { Link } from 'inferno-router';
import { Subscription } from "rxjs";
import { retryWhen, delay, take } from 'rxjs/operators';
-import { UserOperation, Community, ListCommunitiesResponse, CommunityResponse, FollowCommunityForm } from '../interfaces';
+import { UserOperation, Community, ListCommunitiesResponse, CommunityResponse, FollowCommunityForm, ListCommunitiesForm, SortType } from '../interfaces';
import { WebSocketService } from '../services';
import { msgOp } from '../utils';
(err) => console.error(err),
() => console.log('complete')
);
- WebSocketService.Instance.listCommunities();
+
+ let listCommunitiesForm: ListCommunitiesForm = {
+ sort: SortType[SortType.TopAll]
+ }
+
+ WebSocketService.Instance.listCommunities(listCommunitiesForm);
}
<div class="form-group row">
<label class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
- <input type="email" class="form-control" value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
+ <input type="email" class="form-control" placeholder="Optional" value={this.state.registerForm.email} onInput={linkEvent(this, this.handleRegisterEmailChange)} minLength={3} />
</div>
</div>
<div class="form-group row">
import { Link } from 'inferno-router';
import { Subscription } from "rxjs";
import { retryWhen, delay, take } from 'rxjs/operators';
-import { UserOperation, CommunityUser, GetFollowedCommunitiesResponse } from '../interfaces';
+import { UserOperation, CommunityUser, GetFollowedCommunitiesResponse, ListCommunitiesForm, ListCommunitiesResponse, Community, SortType } from '../interfaces';
import { WebSocketService, UserService } from '../services';
import { PostListings } from './post-listings';
import { msgOp, repoUrl } from '../utils';
interface State {
subscribedCommunities: Array<CommunityUser>;
+ trendingCommunities: Array<Community>;
loading: boolean;
}
private subscription: Subscription;
private emptyState: State = {
subscribedCommunities: [],
+ trendingCommunities: [],
loading: true
}
if (UserService.Instance.loggedIn) {
WebSocketService.Instance.getFollowedCommunities();
}
+
+ let listCommunitiesForm: ListCommunitiesForm = {
+ sort: SortType[SortType.New],
+ limit: 8
+ }
+
+ WebSocketService.Instance.listCommunities(listCommunitiesForm);
}
componentWillUnmount() {
<PostListings />
</div>
<div class="col-12 col-md-4">
- {UserService.Instance.loggedIn ?
+ {this.state.loading ?
+ <h4><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h4> :
+ <div>
+ {this.trendingCommunities()}
+ {UserService.Instance.loggedIn ?
<div>
- {this.state.loading ?
- <h4><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h4> :
- <div>
- <h4>Subscribed forums</h4>
- <ul class="list-unstyled">
- {this.state.subscribedCommunities.map(community =>
- <li><Link to={`/community/${community.community_id}`}>{community.community_name}</Link></li>
- )}
- </ul>
- </div>
- }
+ <h4>Subscribed forums</h4>
+ <ul class="list-inline">
+ {this.state.subscribedCommunities.map(community =>
+ <li class="list-inline-item"><Link to={`/community/${community.community_id}`}>{community.community_name}</Link></li>
+ )}
+ </ul>
</div> :
- this.landing()
+ this.landing()
+ }
+ </div>
}
</div>
</div>
)
}
+ trendingCommunities() {
+ return (
+ <div>
+ <h4>Trending forums</h4>
+ <ul class="list-inline">
+ {this.state.trendingCommunities.map(community =>
+ <li class="list-inline-item"><Link to={`/community/${community.id}`}>{community.name}</Link></li>
+ )}
+ </ul>
+ </div>
+ )
+ }
+
landing() {
return (
<div>
this.state.subscribedCommunities = res.communities;
this.state.loading = false;
this.setState(this.state);
+ } else if (op == UserOperation.ListCommunities) {
+ let res: ListCommunitiesResponse = msg;
+ this.state.trendingCommunities = res.communities;
+ this.state.loading = false;
+ this.setState(this.state);
}
}
}
import { Component, linkEvent } from 'inferno';
import { Subscription } from "rxjs";
import { retryWhen, delay, take } from 'rxjs/operators';
-import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse } from '../interfaces';
+import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType } from '../interfaces';
import { WebSocketService } from '../services';
import { msgOp } from '../utils';
import * as autosize from 'autosize';
() => console.log('complete')
);
- WebSocketService.Instance.listCommunities();
+ let listCommunitiesForm: ListCommunitiesForm = {
+ sort: SortType[SortType.TopAll]
+ }
+
+ WebSocketService.Instance.listCommunities(listCommunitiesForm);
}
componentDidMount() {
community: Community;
}
+export interface ListCommunitiesForm {
+ sort: string;
+ limit?: number;
+ auth?: string;
+}
+
export interface ListCommunitiesResponse {
op: string;
communities: Array<Community>;
import { wsUri } from '../env';
-import { LoginForm, RegisterForm, UserOperation, CommunityForm, PostForm, CommentForm, CommentLikeForm, GetPostsForm, CreatePostLikeForm, FollowCommunityForm, GetUserDetailsForm } from '../interfaces';
+import { LoginForm, RegisterForm, UserOperation, CommunityForm, PostForm, CommentForm, CommentLikeForm, GetPostsForm, CreatePostLikeForm, FollowCommunityForm, GetUserDetailsForm, ListCommunitiesForm } from '../interfaces';
import { webSocket } from 'rxjs/webSocket';
import { Subject } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators';
this.subject.next(this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm));
}
- public listCommunities() {
- let data = {auth: UserService.Instance.auth };
- this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, data));
+ public listCommunities(form: ListCommunitiesForm) {
+ this.setAuth(form, false);
+ this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
}
public getFollowedCommunities() {