diff options
author | Dessalines <tyhou13@gmx.com> | 2019-04-04 13:53:32 -0700 |
---|---|---|
committer | Dessalines <tyhou13@gmx.com> | 2019-04-04 13:53:32 -0700 |
commit | f3cbe9e6cee4a03d8677414ae29b81a59d31b71c (patch) | |
tree | e55c24133e335fd55be926e49d4d500fc019e93b /server | |
parent | d07a1b4563b829232040c9b1156a6598f493f469 (diff) |
Getting community moderators
- Getting back mods with a fetch. Fixes #28
Diffstat (limited to 'server')
-rw-r--r-- | server/migrations/2019-04-03-155205_create_community_view/up.sql | 6 | ||||
-rw-r--r-- | server/src/actions/community_view.rs | 46 | ||||
-rw-r--r-- | server/src/websocket_server/server.rs | 22 |
3 files changed, 68 insertions, 6 deletions
diff --git a/server/migrations/2019-04-03-155205_create_community_view/up.sql b/server/migrations/2019-04-03-155205_create_community_view/up.sql index d26a313e..f2f4a766 100644 --- a/server/migrations/2019-04-03-155205_create_community_view/up.sql +++ b/server/migrations/2019-04-03-155205_create_community_view/up.sql @@ -9,10 +9,12 @@ from community c; create view community_moderator_view as select *, -(select name from user_ u where cm.user_id = u.id) as user_name +(select name from user_ u where cm.user_id = u.id) as user_name, +(select name from community c where cm.community_id = c.id) as community_name from community_moderator cm; create view community_follower_view as select *, -(select name from user_ u where cf.user_id = u.id) as user_name +(select name from user_ u where cf.user_id = u.id) as user_name, +(select name from community c where cf.community_id = c.id) as community_name from community_follower cf; diff --git a/server/src/actions/community_view.rs b/server/src/actions/community_view.rs index 03d822ab..eafda161 100644 --- a/server/src/actions/community_view.rs +++ b/server/src/actions/community_view.rs @@ -21,6 +21,28 @@ table! { } } +table! { + community_moderator_view (id) { + id -> Int4, + community_id -> Int4, + user_id -> Int4, + published -> Timestamp, + user_name -> Varchar, + community_name -> Varchar, + } +} + +table! { + community_follower_view (id) { + id -> Int4, + community_id -> Int4, + user_id -> Int4, + published -> Timestamp, + user_name -> Varchar, + community_name -> Varchar, + } +} + #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)] #[table_name="community_view"] pub struct CommunityView { @@ -51,3 +73,27 @@ impl CommunityView { } } + +#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)] +#[table_name="community_moderator_view"] +pub struct CommunityModeratorView { + pub id: i32, + pub community_id: i32, + pub user_id: i32, + pub published: chrono::NaiveDateTime, + pub user_name : String, + pub community_name: String, +} + +impl CommunityModeratorView { + pub fn for_community(conn: &PgConnection, from_community_id: i32) -> Result<Vec<Self>, Error> { + use actions::community_view::community_moderator_view::dsl::*; + community_moderator_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_moderator_view::dsl::*; + community_moderator_view.filter(user_id.eq(from_user_id)).load::<Self>(conn) + } +} + diff --git a/server/src/websocket_server/server.rs b/server/src/websocket_server/server.rs index e5e117ef..4c13aade 100644 --- a/server/src/websocket_server/server.rs +++ b/server/src/websocket_server/server.rs @@ -153,7 +153,8 @@ pub struct GetPostResponse { op: String, post: PostView, comments: Vec<CommentView>, - community: CommunityView + community: CommunityView, + moderators: Vec<CommunityModeratorView> } #[derive(Serialize, Deserialize)] @@ -179,7 +180,8 @@ pub struct GetCommunity { #[derive(Serialize, Deserialize)] pub struct GetCommunityResponse { op: String, - community: CommunityView + community: CommunityView, + moderators: Vec<CommunityModeratorView> } #[derive(Serialize, Deserialize)] @@ -762,13 +764,16 @@ impl Perform for GetPost { let community = CommunityView::read(&conn, post_view.community_id).unwrap(); + let moderators = CommunityModeratorView::for_community(&conn, post_view.community_id).unwrap(); + // Return the jwt serde_json::to_string( &GetPostResponse { op: self.op_type().to_string(), post: post_view, comments: comments, - community: community + community: community, + moderators: moderators } ) .unwrap() @@ -791,11 +796,20 @@ impl Perform for GetCommunity { } }; + + let moderators = match CommunityModeratorView::for_community(&conn, self.id) { + Ok(moderators) => moderators, + Err(_e) => { + return self.error("Couldn't find Community"); + } + }; + // Return the jwt serde_json::to_string( &GetCommunityResponse { op: self.op_type().to_string(), - community: community_view + community: community_view, + moderators: moderators } ) .unwrap() |