Browse Source

Adding a front page / fetching subscribed forums.

- Adding subscribed to post view. Fixes #25
pull/269/head
Dessalines 3 years ago
parent
commit
b10592f06d
  1. 2
      server/Cargo.lock
  2. 8
      server/migrations/2019-03-30-212058_create_post_view/up.sql
  3. 22
      server/src/actions/community_view.rs
  4. 9
      server/src/actions/post_view.rs
  5. 50
      server/src/websocket_server/server.rs

2
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"

8
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
;

22
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)
}
}

9
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));

50
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 {

Loading…
Cancel
Save