diff options
author | Dessalines <happydooby@gmail.com> | 2019-04-05 12:14:54 -0700 |
---|---|---|
committer | Dessalines <happydooby@gmail.com> | 2019-04-05 12:14:54 -0700 |
commit | 1c95f4efcdfaf5900540389d1c86d89d6132f4f9 (patch) | |
tree | 2de5f93884ff6b315dcdab48a43662b85f1a0d85 /server | |
parent | 4b6b456446abd493c5712a19532d0539153bb283 (diff) |
Adding a front page / fetching subscribed forums.
- Adding subscribed to post view. Fixes #25
Diffstat (limited to 'server')
-rw-r--r-- | server/Cargo.lock | 2 | ||||
-rw-r--r-- | server/migrations/2019-03-30-212058_create_post_view/up.sql | 8 | ||||
-rw-r--r-- | server/src/actions/community_view.rs | 22 | ||||
-rw-r--r-- | server/src/actions/post_view.rs | 9 | ||||
-rw-r--r-- | server/src/websocket_server/server.rs | 50 |
5 files changed, 85 insertions, 6 deletions
diff --git a/server/Cargo.lock b/server/Cargo.lock index 21594ccf..0527eae7 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "activitypub" version = "0.1.4" diff --git a/server/migrations/2019-03-30-212058_create_post_view/up.sql b/server/migrations/2019-03-30-212058_create_post_view/up.sql index c1848631..79084a47 100644 --- a/server/migrations/2019-03-30-212058_create_post_view/up.sql +++ b/server/migrations/2019-03-30-212058_create_post_view/up.sql @@ -14,7 +14,7 @@ with all_post as ( select p.*, - (select name from user_ where p.creator_id = user_.id) creator_name, + (select name from user_ where p.creator_id = user_.id) as creator_name, (select name from community where p.community_id = community.id) as community_name, (select count(*) from comment where comment.post_id = p.id) as number_of_comments, coalesce(sum(pl.score), 0) as score, @@ -29,7 +29,8 @@ with all_post as select ap.*, u.id as user_id, -coalesce(pl.score, 0) as my_vote +coalesce(pl.score, 0) as my_vote, +(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed from user_ u cross join all_post ap left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id @@ -39,7 +40,8 @@ union all select ap.*, null as user_id, -null as my_vote +null as my_vote, +null as subscribed from all_post ap ; diff --git a/server/src/actions/community_view.rs b/server/src/actions/community_view.rs index 7eb07a16..185484bf 100644 --- a/server/src/actions/community_view.rs +++ b/server/src/actions/community_view.rs @@ -124,3 +124,25 @@ impl CommunityModeratorView { } } +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)] +#[table_name="community_follower_view"] +pub struct CommunityFollowerView { + pub id: i32, + pub community_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, + pub user_name : String, + pub community_name: String, +} + +impl CommunityFollowerView { + pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> { + use actions::community_view::community_follower_view::dsl::*; + community_follower_view.filter(community_id.eq(from_community_id)).load::<Self>(conn) + } + + pub fn for_user(conn: &PgConnection, from_user_id: i32) -> Result<Vec<Self>, Error> { + use actions::community_view::community_follower_view::dsl::*; + community_follower_view.filter(user_id.eq(from_user_id)).load::<Self>(conn) + } +} diff --git a/server/src/actions/post_view.rs b/server/src/actions/post_view.rs index c48c651e..b41a77ae 100644 --- a/server/src/actions/post_view.rs +++ b/server/src/actions/post_view.rs @@ -33,6 +33,7 @@ table! { hot_rank -> Int4, user_id -> Nullable<Int4>, my_vote -> Nullable<Int4>, + subscribed -> Nullable<Bool>, } } @@ -57,6 +58,7 @@ pub struct PostView { pub hot_rank: i32, pub user_id: Option<i32>, pub my_vote: Option<i32>, + pub subscribed: Option<bool>, } impl PostView { @@ -71,6 +73,13 @@ impl PostView { query = query.filter(community_id.eq(from_community_id)); }; + match type_ { + ListingType::Subscribed => { + query = query.filter(subscribed.eq(true)); + }, + _ => {} + }; + // 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)); diff --git a/server/src/websocket_server/server.rs b/server/src/websocket_server/server.rs index fe7cd0e6..6aae4f2f 100644 --- a/server/src/websocket_server/server.rs +++ b/server/src/websocket_server/server.rs @@ -22,7 +22,7 @@ use actions::community_view::*; #[derive(EnumString,ToString,Debug)] pub enum UserOperation { - Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, EditCommunity, FollowCommunity + Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, EditCommunity, FollowCommunity, GetFollowedCommunities } #[derive(Serialize, Deserialize)] @@ -261,6 +261,18 @@ pub struct FollowCommunity { auth: String } +#[derive(Serialize, Deserialize)] +pub struct GetFollowedCommunities { + auth: String +} + +#[derive(Serialize, Deserialize)] +pub struct GetFollowedCommunitiesResponse { + op: String, + communities: Vec<CommunityFollowerView> +} + + /// `ChatServer` manages chat rooms and responsible for coordinating chat /// session. implementation is super primitive pub struct ChatServer { @@ -450,6 +462,10 @@ impl Handler<StandardMessage> for ChatServer { let follow_community: FollowCommunity = serde_json::from_str(&data.to_string()).unwrap(); follow_community.perform(self, msg.id) }, + UserOperation::GetFollowedCommunities => { + let followed_communities: GetFollowedCommunities = serde_json::from_str(&data.to_string()).unwrap(); + followed_communities.perform(self, msg.id) + }, _ => { let e = ErrorMessage { op: "Unknown".to_string(), @@ -1081,8 +1097,6 @@ impl Perform for GetPosts { let conn = establish_connection(); - println!("{:?}", self.auth); - let user_id: Option<i32> = match &self.auth { Some(auth) => { match Claims::decode(&auth) { @@ -1367,6 +1381,36 @@ impl Perform for FollowCommunity { } } +impl Perform for GetFollowedCommunities { + fn op_type(&self) -> UserOperation { + UserOperation::GetFollowedCommunities + } + + fn perform(&self, _chat: &mut ChatServer, _addr: usize) -> String { + + let conn = establish_connection(); + + let claims = match Claims::decode(&self.auth) { + Ok(claims) => claims.claims, + Err(_e) => { + return self.error("Not logged in."); + } + }; + + let user_id = claims.id; + + let communities: Vec<CommunityFollowerView> = CommunityFollowerView::for_user(&conn, user_id).unwrap(); + + // Return the jwt + serde_json::to_string( + &GetFollowedCommunitiesResponse { + op: self.op_type().to_string(), + communities: communities + } + ) + .unwrap() + } +} // impl Handler<Login> for ChatServer { |