From b304b482eafd8ab87bd9fb8c263773a038ea1e6f Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 1 May 2019 22:26:31 -0700 Subject: [PATCH] Reworking some UI. Adding proper trending communities with hot rank. - Breaking out subscribed and all into radios. Fixes #142 --- README.md | 3 +- .../down.sql | 28 +++++++++++++++ .../up.sql | 29 ++++++++++++++++ server/src/actions/community_view.rs | 3 ++ server/src/websocket_server/server.rs | 3 +- ui/src/components/communities.tsx | 18 +++++----- ui/src/components/community.tsx | 2 +- ui/src/components/inbox.tsx | 4 +-- ui/src/components/login.tsx | 11 +++--- ui/src/components/main.tsx | 34 +++++++++++++------ ui/src/components/search.tsx | 4 +-- ui/src/components/user.tsx | 4 +-- ui/src/index.tsx | 2 +- 13 files changed, 110 insertions(+), 35 deletions(-) create mode 100644 server/migrations/2019-05-02-051656_community_view_hot_rank/down.sql create mode 100644 server/migrations/2019-05-02-051656_community_view_hot_rank/up.sql diff --git a/README.md b/README.md index 94238136f..957462fc1 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ Each lemmy server can set its own moderation policy; appointing site-wide admins ## Why's it called Lemmy? - Lead singer from [motorhead](https://invidio.us/watch?v=pWB5JZRGl0U). -- The old school [video game](https://en.wikipedia.org/wiki/Lemmings_(video_game)). +- The old school [video game](). +- The [Koopa from Super Mario](https://www.mariowiki.com/Lemmy_Koopa). - The [furry rodents](http://sunchild.fpwc.org/lemming-the-little-giant-of-the-north/). Made with [Rust](https://www.rust-lang.org), [Actix](https://actix.rs/), [Inferno](https://www.infernojs.org), [Typescript](https://www.typescriptlang.org/) and [Diesel](http://diesel.rs/). diff --git a/server/migrations/2019-05-02-051656_community_view_hot_rank/down.sql b/server/migrations/2019-05-02-051656_community_view_hot_rank/down.sql new file mode 100644 index 000000000..0f3a58a8b --- /dev/null +++ b/server/migrations/2019-05-02-051656_community_view_hot_rank/down.sql @@ -0,0 +1,28 @@ +drop view community_view; +create view community_view as +with all_community as +( + select *, + (select name from user_ u where c.creator_id = u.id) as creator_name, + (select name from category ct where c.category_id = ct.id) as category_name, + (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers, + (select count(*) from post p where p.community_id = c.id) as number_of_posts, + (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments + from community c +) + +select +ac.*, +u.id as user_id, +(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed +from user_ u +cross join all_community ac + +union all + +select +ac.*, +null as user_id, +null as subscribed +from all_community ac +; diff --git a/server/migrations/2019-05-02-051656_community_view_hot_rank/up.sql b/server/migrations/2019-05-02-051656_community_view_hot_rank/up.sql new file mode 100644 index 000000000..e7e753665 --- /dev/null +++ b/server/migrations/2019-05-02-051656_community_view_hot_rank/up.sql @@ -0,0 +1,29 @@ +drop view community_view; +create view community_view as +with all_community as +( + select *, + (select name from user_ u where c.creator_id = u.id) as creator_name, + (select name from category ct where c.category_id = ct.id) as category_name, + (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers, + (select count(*) from post p where p.community_id = c.id) as number_of_posts, + (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments, + hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank + from community c +) + +select +ac.*, +u.id as user_id, +(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed +from user_ u +cross join all_community ac + +union all + +select +ac.*, +null as user_id, +null as subscribed +from all_community ac +; diff --git a/server/src/actions/community_view.rs b/server/src/actions/community_view.rs index a52897ff6..9a162746a 100644 --- a/server/src/actions/community_view.rs +++ b/server/src/actions/community_view.rs @@ -21,6 +21,7 @@ table! { number_of_subscribers -> BigInt, number_of_posts -> BigInt, number_of_comments -> BigInt, + hot_rank -> Int4, user_id -> Nullable, subscribed -> Nullable, } @@ -92,6 +93,7 @@ pub struct CommunityView { pub number_of_subscribers: i64, pub number_of_posts: i64, pub number_of_comments: i64, + pub hot_rank: i32, pub user_id: Option, pub subscribed: Option, } @@ -127,6 +129,7 @@ impl CommunityView { // 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()).filter(user_id.is_null()), SortType::New => query = query.order_by(published.desc()).filter(user_id.is_null()), SortType::TopAll => { match from_user_id { diff --git a/server/src/websocket_server/server.rs b/server/src/websocket_server/server.rs index b24169513..aaeae132b 100644 --- a/server/src/websocket_server/server.rs +++ b/server/src/websocket_server/server.rs @@ -573,6 +573,7 @@ impl ChatServer { fn check_rate_limit_full(&mut self, addr: usize, rate: i32, per: i32) -> Result<(), Error> { if let Some(info) = self.sessions.get(&addr) { if let Some(rate_limit) = self.rate_limits.get_mut(&info.ip) { + // The initial value if rate_limit.allowance == -2f64 { rate_limit.allowance = rate as f64; }; @@ -625,7 +626,7 @@ impl Handler for ChatServer { // register session with random id let id = self.rng.gen::(); - println!("{} Joined", &msg.ip); + println!("{} joined", &msg.ip); self.sessions.insert(id, SessionInfo { addr: msg.addr, diff --git a/ui/src/components/communities.tsx b/ui/src/components/communities.tsx index 03e124f17..190f8e3d6 100644 --- a/ui/src/components/communities.tsx +++ b/ui/src/components/communities.tsx @@ -66,17 +66,17 @@ export class Communities extends Component { {this.state.loading ?
:
-
Communities
+
List of communities
- + - - - + + + @@ -84,11 +84,11 @@ export class Communities extends Component { {this.state.communities.map(community => - + - - - + + +
NameTitleTitle CategorySubscribersPostsCommentsSubscribersPostsComments
{community.name}{community.title}{community.title} {community.category_name}{community.number_of_subscribers}{community.number_of_posts}{community.number_of_comments}{community.number_of_subscribers}{community.number_of_posts}{community.number_of_comments} {community.subscribed ? Unsubscribe : diff --git a/ui/src/components/community.tsx b/ui/src/components/community.tsx index fa8ae43b5..93cdbd92f 100644 --- a/ui/src/components/community.tsx +++ b/ui/src/components/community.tsx @@ -124,7 +124,7 @@ export class Community extends Component { selects() { return (
- diff --git a/ui/src/components/inbox.tsx b/ui/src/components/inbox.tsx index 48a9d3664..659a4a20d 100644 --- a/ui/src/components/inbox.tsx +++ b/ui/src/components/inbox.tsx @@ -80,12 +80,12 @@ export class Inbox extends Component { selects() { return (
- - diff --git a/ui/src/components/login.tsx b/ui/src/components/login.tsx index 5c0b8dd1d..b2ad70a16 100644 --- a/ui/src/components/login.tsx +++ b/ui/src/components/login.tsx @@ -95,7 +95,7 @@ export class Login extends Component {
- Forgot your password or deleted your account? Reset your password. TODO + {/* Forgot your password or deleted your account? Reset your password. TODO */} ); } @@ -161,7 +161,6 @@ export class Login extends Component { event.preventDefault(); i.state.registerLoading = true; i.setState(i.state); - event.preventDefault(); let endTimer = new Date().getTime(); let elapsed = endTimer - i.state.registerForm.spam_timeri; @@ -209,14 +208,14 @@ export class Login extends Component { return; } else { if (op == UserOperation.Login) { - this.state.loginLoading = false; - this.state.registerLoading = false; + this.state = this.emptyState; + this.setState(this.state); let res: LoginResponse = msg; UserService.Instance.login(res); this.props.history.push('/'); } else if (op == UserOperation.Register) { - this.state.loginLoading = false; - this.state.registerLoading = false; + this.state = this.emptyState; + this.setState(this.state); let res: LoginResponse = msg; UserService.Instance.login(res); this.props.history.push('/communities'); diff --git a/ui/src/components/main.tsx b/ui/src/components/main.tsx index 44025fef6..cd679ecb9 100644 --- a/ui/src/components/main.tsx +++ b/ui/src/components/main.tsx @@ -87,7 +87,7 @@ export class Main extends Component { } let listCommunitiesForm: ListCommunitiesForm = { - sort: SortType[SortType.New], + sort: SortType[SortType.Hot], limit: 6 } @@ -247,7 +247,29 @@ export class Main extends Component { selects() { return (
- + Subscribed + + +
+ - { UserService.Instance.user && - - - } ) } diff --git a/ui/src/components/search.tsx b/ui/src/components/search.tsx index a4b389e7a..f066f6ed6 100644 --- a/ui/src/components/search.tsx +++ b/ui/src/components/search.tsx @@ -97,13 +97,13 @@ export class Search extends Component { selects() { return (
- - diff --git a/ui/src/components/user.tsx b/ui/src/components/user.tsx index b55bf41cd..92ad53359 100644 --- a/ui/src/components/user.tsx +++ b/ui/src/components/user.tsx @@ -141,14 +141,14 @@ export class User extends Component { selects() { return (
- - diff --git a/ui/src/index.tsx b/ui/src/index.tsx index 76605d8b8..b744ac7d2 100644 --- a/ui/src/index.tsx +++ b/ui/src/index.tsx @@ -36,7 +36,7 @@ class Index extends Component { return ( -
+