From 29a01ddaf130b228d1daa48c35ab3078a0f85ffb Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 13 Sep 2020 10:45:07 -0500 Subject: [PATCH] Adding CommunityJoin, PostJoin instead of joins from GetComments, etc. - Fixes #1122 --- docker/dev/docker-compose.yml | 2 +- docker/prod/docker-compose.yml | 2 +- lemmy_api_structs/src/community.rs | 10 +++++++ lemmy_api_structs/src/post.rs | 10 +++++++ lemmy_api_structs/src/user.rs | 2 +- src/api/comment.rs | 16 ++-------- src/api/community.rs | 30 ++++++++++++++----- src/api/post.rs | 47 ++++++++++++++++-------------- src/api/user.rs | 2 +- src/routes/api.rs | 6 ++-- src/websocket/chat_server.rs | 2 ++ src/websocket/mod.rs | 2 ++ 12 files changed, 82 insertions(+), 49 deletions(-) diff --git a/docker/dev/docker-compose.yml b/docker/dev/docker-compose.yml index 2a81861f4..5668bc512 100644 --- a/docker/dev/docker-compose.yml +++ b/docker/dev/docker-compose.yml @@ -16,7 +16,7 @@ services: - postgres - iframely lemmy-isomorphic-ui: - image: dessalines/lemmy-isomorphic-ui:v0.0.5 + image: dessalines/lemmy-isomorphic-ui:v0.0.6 # image: lemmy-isomorphic-ui:latest ports: - "1235:1234" diff --git a/docker/prod/docker-compose.yml b/docker/prod/docker-compose.yml index 9e9add246..8fc70ba29 100644 --- a/docker/prod/docker-compose.yml +++ b/docker/prod/docker-compose.yml @@ -26,7 +26,7 @@ services: - iframely lemmy-isomorphic-ui: - image: dessalines/lemmy-isomorphic-ui:v0.0.5 + image: dessalines/lemmy-isomorphic-ui:v0.0.6 # image: lemmy-isomorphic-ui:latest ports: - "1235:1234" diff --git a/lemmy_api_structs/src/community.rs b/lemmy_api_structs/src/community.rs index 52c2ec557..6c543eac2 100644 --- a/lemmy_api_structs/src/community.rs +++ b/lemmy_api_structs/src/community.rs @@ -129,3 +129,13 @@ pub struct TransferCommunity { pub user_id: i32, pub auth: String, } + +#[derive(Deserialize, Debug)] +pub struct CommunityJoin { + pub community_id: i32, +} + +#[derive(Serialize, Clone)] +pub struct CommunityJoinResponse { + pub joined: bool, +} diff --git a/lemmy_api_structs/src/post.rs b/lemmy_api_structs/src/post.rs index d1adf1bd1..1ccbe7e32 100644 --- a/lemmy_api_structs/src/post.rs +++ b/lemmy_api_structs/src/post.rs @@ -103,3 +103,13 @@ pub struct SavePost { pub save: bool, pub auth: String, } + +#[derive(Deserialize, Debug)] +pub struct PostJoin { + pub post_id: i32, +} + +#[derive(Serialize, Clone)] +pub struct PostJoinResponse { + pub joined: bool, +} diff --git a/lemmy_api_structs/src/user.rs b/lemmy_api_structs/src/user.rs index 43eadb115..8e4ca5bd0 100644 --- a/lemmy_api_structs/src/user.rs +++ b/lemmy_api_structs/src/user.rs @@ -235,5 +235,5 @@ pub struct UserJoin { #[derive(Serialize, Clone)] pub struct UserJoinResponse { - pub user_id: i32, + pub joined: bool, } diff --git a/src/api/comment.rs b/src/api/comment.rs index e7b4080eb..d4bb35387 100644 --- a/src/api/comment.rs +++ b/src/api/comment.rs @@ -10,7 +10,7 @@ use crate::{ apub::{ApubLikeableType, ApubObjectType}, blocking, websocket::{ - messages::{JoinCommunityRoom, SendComment}, + messages::SendComment, UserOperation, }, DbPool, @@ -664,7 +664,7 @@ impl Perform for GetComments { async fn perform( &self, context: &Data, - websocket_id: Option, + _websocket_id: Option, ) -> Result { let data: &GetComments = &self; let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?; @@ -694,18 +694,6 @@ impl Perform for GetComments { Err(_) => return Err(APIError::err("couldnt_get_comments").into()), }; - if let Some(id) = websocket_id { - // You don't need to join the specific community room, bc this is already handled by - // GetCommunity - if data.community_id.is_none() { - // 0 is the "all" community - context.chat_server().do_send(JoinCommunityRoom { - community_id: 0, - id, - }); - } - } - Ok(GetCommentsResponse { comments }) } } diff --git a/src/api/community.rs b/src/api/community.rs index 6fb67b330..9375323bb 100644 --- a/src/api/community.rs +++ b/src/api/community.rs @@ -56,7 +56,7 @@ impl Perform for GetCommunity { async fn perform( &self, context: &Data, - websocket_id: Option, + _websocket_id: Option, ) -> Result { let data: &GetCommunity = &self; let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?; @@ -95,12 +95,6 @@ impl Perform for GetCommunity { Err(_e) => return Err(APIError::err("couldnt_find_community").into()), }; - if let Some(id) = websocket_id { - context - .chat_server() - .do_send(JoinCommunityRoom { community_id, id }); - } - let online = context .chat_server() .send(GetCommunityUsersOnline { community_id }) @@ -856,3 +850,25 @@ pub fn send_community_websocket( websocket_id, }); } + +#[async_trait::async_trait(?Send)] +impl Perform for CommunityJoin { + type Response = CommunityJoinResponse; + + async fn perform( + &self, + context: &Data, + websocket_id: Option, + ) -> Result { + let data: &CommunityJoin = &self; + + if let Some(ws_id) = websocket_id { + context.chat_server().do_send(JoinCommunityRoom { + community_id: data.community_id, + id: ws_id, + }); + } + + Ok(CommunityJoinResponse { joined: true }) + } +} diff --git a/src/api/post.rs b/src/api/post.rs index 9e1e97d51..0a4002cfc 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -12,7 +12,7 @@ use crate::{ blocking, fetch_iframely_and_pictrs_data, websocket::{ - messages::{GetPostUsersOnline, JoinCommunityRoom, JoinPostRoom, SendPost}, + messages::{GetPostUsersOnline, JoinPostRoom, SendPost}, UserOperation, }, LemmyContext, @@ -169,7 +169,7 @@ impl Perform for GetPost { async fn perform( &self, context: &Data, - websocket_id: Option, + _websocket_id: Option, ) -> Result { let data: &GetPost = &self; let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?; @@ -207,13 +207,6 @@ impl Perform for GetPost { }) .await??; - if let Some(id) = websocket_id { - context.chat_server().do_send(JoinPostRoom { - post_id: data.id, - id, - }); - } - let online = context .chat_server() .send(GetPostUsersOnline { post_id: data.id }) @@ -238,7 +231,7 @@ impl Perform for GetPosts { async fn perform( &self, context: &Data, - websocket_id: Option, + _websocket_id: Option, ) -> Result { let data: &GetPosts = &self; let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?; @@ -278,18 +271,6 @@ impl Perform for GetPosts { Err(_e) => return Err(APIError::err("couldnt_get_posts").into()), }; - if let Some(id) = websocket_id { - // You don't need to join the specific community room, bc this is already handled by - // GetCommunity - if data.community_id.is_none() { - // 0 is the "all" community - context.chat_server().do_send(JoinCommunityRoom { - community_id: 0, - id, - }); - } - } - Ok(GetPostsResponse { posts }) } } @@ -749,3 +730,25 @@ impl Perform for SavePost { Ok(PostResponse { post: post_view }) } } + +#[async_trait::async_trait(?Send)] +impl Perform for PostJoin { + type Response = PostJoinResponse; + + async fn perform( + &self, + context: &Data, + websocket_id: Option, + ) -> Result { + let data: &PostJoin = &self; + + if let Some(ws_id) = websocket_id { + context.chat_server().do_send(JoinPostRoom { + post_id: data.post_id, + id: ws_id, + }); + } + + Ok(PostJoinResponse { joined: true }) + } +} diff --git a/src/api/user.rs b/src/api/user.rs index 1a71b3aa0..9c0c7260c 100644 --- a/src/api/user.rs +++ b/src/api/user.rs @@ -1279,6 +1279,6 @@ impl Perform for UserJoin { }); } - Ok(UserJoinResponse { user_id: user.id }) + Ok(UserJoinResponse { joined: true }) } } diff --git a/src/routes/api.rs b/src/routes/api.rs index dd727df97..92159535a 100644 --- a/src/routes/api.rs +++ b/src/routes/api.rs @@ -55,7 +55,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { .route("/remove", web::post().to(route_post::)) .route("/transfer", web::post().to(route_post::)) .route("/ban_user", web::post().to(route_post::)) - .route("/mod", web::post().to(route_post::)), + .route("/mod", web::post().to(route_post::)) + .route("/join", web::post().to(route_post::)), ) // Post .service( @@ -76,7 +77,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { .route("/sticky", web::post().to(route_post::)) .route("/list", web::get().to(route_get::)) .route("/like", web::post().to(route_post::)) - .route("/save", web::put().to(route_post::)), + .route("/save", web::put().to(route_post::)) + .route("/join", web::post().to(route_post::)), ) // Comment .service( diff --git a/src/websocket/chat_server.rs b/src/websocket/chat_server.rs index b8066eece..2da107217 100644 --- a/src/websocket/chat_server.rs +++ b/src/websocket/chat_server.rs @@ -398,6 +398,8 @@ impl ChatServer { UserOperation::PasswordReset => do_user_operation::(args).await, UserOperation::PasswordChange => do_user_operation::(args).await, UserOperation::UserJoin => do_user_operation::(args).await, + UserOperation::PostJoin => do_user_operation::(args).await, + UserOperation::CommunityJoin => do_user_operation::(args).await, UserOperation::SaveUserSettings => do_user_operation::(args).await, // Private Message ops diff --git a/src/websocket/mod.rs b/src/websocket/mod.rs index b0441819e..42793a786 100644 --- a/src/websocket/mod.rs +++ b/src/websocket/mod.rs @@ -62,4 +62,6 @@ pub enum UserOperation { GetComments, GetSiteConfig, SaveSiteConfig, + PostJoin, + CommunityJoin, }