From 96a7e99e57f8c79eead599e6de5d94016f7021e6 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 2 Feb 2020 12:45:41 -0500 Subject: [PATCH] Removing PostLikeResponse in favor of PostResponse. Consolidating comment and post_sends. --- server/src/api/post.rs | 11 +-- server/src/routes/api.rs | 2 +- server/src/websocket/server.rs | 164 ++++++++++++++------------------ ui/src/components/community.tsx | 3 +- ui/src/components/main.tsx | 3 +- ui/src/components/post.tsx | 7 +- ui/src/components/search.tsx | 4 +- ui/src/interfaces.ts | 6 +- 8 files changed, 84 insertions(+), 116 deletions(-) diff --git a/server/src/api/post.rs b/server/src/api/post.rs index 642521eba..086705bce 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -55,11 +55,6 @@ pub struct CreatePostLike { auth: String, } -#[derive(Serialize, Deserialize, Clone)] -pub struct CreatePostLikeResponse { - pub post: PostView, -} - #[derive(Serialize, Deserialize)] pub struct EditPost { pub edit_id: i32, @@ -242,8 +237,8 @@ impl Perform for Oper { } } -impl Perform for Oper { - fn perform(&self, conn: &PgConnection) -> Result { +impl Perform for Oper { + fn perform(&self, conn: &PgConnection) -> Result { let data: &CreatePostLike = &self.data; let claims = match Claims::decode(&data.auth) { @@ -296,7 +291,7 @@ impl Perform for Oper { }; // just output the score - Ok(CreatePostLikeResponse { post: post_view }) + Ok(PostResponse { post: post_view }) } } diff --git a/server/src/routes/api.rs b/server/src/routes/api.rs index 5121d2401..29a360e4e 100644 --- a/server/src/routes/api.rs +++ b/server/src/routes/api.rs @@ -31,7 +31,7 @@ pub fn config(cfg: &mut web::ServiceConfig) { .route("/api/v1/post", web::put().to(route_post::)) .route("/api/v1/post", web::get().to(route_get::)) .route("/api/v1/post/list", web::get().to(route_get::)) - .route("/api/v1/post/like", web::post().to(route_post::)) + .route("/api/v1/post/like", web::post().to(route_post::)) .route("/api/v1/post/save", web::put().to(route_post::)) // Comment .route("/api/v1/comment", web::post().to(route_post::)) diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 369f50f75..a26c8144e 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -204,6 +204,61 @@ impl ChatServer { } } + fn comment_sends( + &self, + user_operation: UserOperation, + comment: CommentResponse, + id: ConnectionId, + ) -> Result { + let mut comment_reply_sent = comment.clone(); + comment_reply_sent.comment.my_vote = None; + comment_reply_sent.comment.user_id = None; + + // For the post room ones, and the directs back to the user + // strip out the recipient_ids, so that + // users don't get double notifs + let mut comment_user_sent = comment.clone(); + comment_user_sent.recipient_ids = Vec::new(); + + let mut comment_post_sent = comment_reply_sent.clone(); + comment_post_sent.recipient_ids = Vec::new(); + + let comment_reply_sent_str = to_json_string(&user_operation, &comment_reply_sent)?; + let comment_post_sent_str = to_json_string(&user_operation, &comment_post_sent)?; + let comment_user_sent_str = to_json_string(&user_operation, &comment_user_sent)?; + + // Send it to the post room + self.send_post_room_message(comment.comment.post_id, &comment_post_sent_str, id); + + // Send it to the recipient(s) including the mentioned users + for recipient_id in comment_reply_sent.recipient_ids { + self.send_user_room_message(recipient_id, &comment_reply_sent_str, id); + } + + Ok(comment_user_sent_str) + } + + fn post_sends( + &self, + user_operation: UserOperation, + post: PostResponse, + id: ConnectionId, + ) -> Result { + let community_id = post.post.community_id; + + // Don't send my data with it + let mut post_sent = post.clone(); + post_sent.post.my_vote = None; + post_sent.post.user_id = None; + let post_sent_str = to_json_string(&user_operation, &post_sent)?; + + // Send it to /c/all and that community + self.send_community_room_message(0, &post_sent_str, id); + self.send_community_room_message(community_id, &post_sent_str, id); + + to_json_string(&user_operation, post) + } + fn check_rate_limit_register(&mut self, id: usize) -> Result<(), Error> { self.check_rate_limit_full( id, @@ -487,25 +542,6 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { do_user_operation::(user_operation, data, &conn) } - UserOperation::CreatePost => { - chat.check_rate_limit_post(msg.id)?; - let create_post: CreatePost = serde_json::from_str(data)?; - let community_id = create_post.community_id; - let res = Oper::new(create_post).perform(&conn)?; - let res_str = to_json_string(&user_operation, &res)?; - - // Don't send my data with it - let mut post_sent = res; - post_sent.post.my_vote = None; - post_sent.post.user_id = None; - let post_sent_str = to_json_string(&user_operation, &post_sent)?; - - // Send it to /c/all and that community - chat.send_community_room_message(0, &post_sent_str, msg.id); - chat.send_community_room_message(community_id, &post_sent_str, msg.id); - - Ok(res_str) - } UserOperation::GetPost => { let get_post: GetPost = serde_json::from_str(data)?; let post_id = get_post.id; @@ -529,43 +565,25 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { - let user_join: UserJoin = serde_json::from_str(data)?; - let res = Oper::new(user_join).perform(&conn)?; - chat.join_user_room(res.user_id, msg.id); - to_json_string(&user_operation, &res) + UserOperation::CreatePost => { + chat.check_rate_limit_post(msg.id)?; + let create_post: CreatePost = serde_json::from_str(data)?; + let res = Oper::new(create_post).perform(&conn)?; + + chat.post_sends(UserOperation::CreatePost, res, msg.id) } UserOperation::CreatePostLike => { chat.check_rate_limit_message(msg.id)?; let create_post_like: CreatePostLike = serde_json::from_str(data)?; let res = Oper::new(create_post_like).perform(&conn)?; - let community_id = res.post.community_id; - let res_str = to_json_string(&user_operation, &res)?; - // Don't send my data with it - let mut post_sent = res; - post_sent.post.my_vote = None; - post_sent.post.user_id = None; - let post_sent_str = to_json_string(&user_operation, &post_sent)?; - - // Send it to /c/all and that community - chat.send_community_room_message(0, &post_sent_str, msg.id); - chat.send_community_room_message(community_id, &post_sent_str, msg.id); - - Ok(res_str) + chat.post_sends(UserOperation::CreatePostLike, res, msg.id) } UserOperation::EditPost => { let edit_post: EditPost = serde_json::from_str(data)?; let res = Oper::new(edit_post).perform(&conn)?; - let mut post_sent = res.clone(); - post_sent.post.my_vote = None; - post_sent.post.user_id = None; - let post_sent_str = to_json_string(&user_operation, &post_sent)?; - // Send it to /c/all and that community - chat.send_community_room_message(0, &post_sent_str, msg.id); - chat.send_community_room_message(post_sent.post.community_id, &post_sent_str, msg.id); - to_json_string(&user_operation, &res) + chat.post_sends(UserOperation::EditPost, res, msg.id) } UserOperation::SavePost => { do_user_operation::(user_operation, data, &conn) @@ -573,48 +591,15 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { chat.check_rate_limit_message(msg.id)?; let create_comment: CreateComment = serde_json::from_str(data)?; - let post_id = create_comment.post_id; let res = Oper::new(create_comment).perform(&conn)?; - let mut comment_user_sent = res.clone(); - comment_user_sent.comment.my_vote = None; - comment_user_sent.comment.user_id = None; - - // For the post room ones, strip out the recipient_ids, so that - // users don't get double notifs - let mut comment_post_sent = comment_user_sent.clone(); - comment_post_sent.recipient_ids = Vec::new(); - - let comment_user_sent_str = to_json_string(&user_operation, &comment_user_sent)?; - let comment_post_sent_str = to_json_string(&user_operation, &comment_post_sent)?; - - // Send it to the post room - chat.send_post_room_message(post_id, &comment_post_sent_str, msg.id); - - // Send it to the recipient(s) including the mentioned users - for recipient_id in comment_user_sent.recipient_ids { - chat.send_user_room_message(recipient_id, &comment_user_sent_str, msg.id); - } - - to_json_string(&user_operation, &res) + chat.comment_sends(UserOperation::CreateComment, res, msg.id) } UserOperation::EditComment => { let edit_comment: EditComment = serde_json::from_str(data)?; - let post_id = edit_comment.post_id; let res = Oper::new(edit_comment).perform(&conn)?; - let mut comment_sent = res.clone(); - comment_sent.comment.my_vote = None; - comment_sent.comment.user_id = None; - let comment_sent_str = to_json_string(&user_operation, &comment_sent)?; - chat.send_post_room_message(post_id, &comment_sent_str, msg.id); - - // Send it to the recipient(s) including the mentioned users - for recipient_id in comment_sent.recipient_ids { - chat.send_user_room_message(recipient_id, &comment_sent_str, msg.id); - } - - to_json_string(&user_operation, &res) + chat.comment_sends(UserOperation::EditComment, res, msg.id) } UserOperation::SaveComment => { do_user_operation::(user_operation, data, &conn) @@ -622,20 +607,9 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { chat.check_rate_limit_message(msg.id)?; let create_comment_like: CreateCommentLike = serde_json::from_str(data)?; - let post_id = create_comment_like.post_id; let res = Oper::new(create_comment_like).perform(&conn)?; - let mut comment_sent = res.clone(); - comment_sent.comment.my_vote = None; - comment_sent.comment.user_id = None; - let comment_sent_str = to_json_string(&user_operation, &comment_sent)?; - chat.send_post_room_message(post_id, &comment_sent_str, msg.id); - - // Send it to the recipient(s) including the mentioned users - for recipient_id in comment_sent.recipient_ids { - chat.send_user_room_message(recipient_id, &comment_sent_str, msg.id); - } - to_json_string(&user_operation, &res) + chat.comment_sends(UserOperation::CreateCommentLike, res, msg.id) } UserOperation::GetModlog => { do_user_operation::(user_operation, data, &conn) @@ -690,5 +664,11 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { do_user_operation::(user_operation, data, &conn) } + UserOperation::UserJoin => { + let user_join: UserJoin = serde_json::from_str(data)?; + let res = Oper::new(user_join).perform(&conn)?; + chat.join_user_room(res.user_id, msg.id); + to_json_string(&user_operation, &res) + } } } diff --git a/ui/src/components/community.tsx b/ui/src/components/community.tsx index 866b9eec8..18767c51c 100644 --- a/ui/src/components/community.tsx +++ b/ui/src/components/community.tsx @@ -15,7 +15,6 @@ import { ListingType, GetPostsResponse, PostResponse, - CreatePostLikeResponse, AddModToCommunityResponse, BanFromCommunityResponse, WebSocketJsonResponse, @@ -294,7 +293,7 @@ export class Community extends Component { this.state.posts.unshift(data.post); this.setState(this.state); } else if (res.op == UserOperation.CreatePostLike) { - let data = res.data as CreatePostLikeResponse; + let data = res.data as PostResponse; let found = this.state.posts.find(c => c.id == data.post.id); found.score = data.post.score; diff --git a/ui/src/components/main.tsx b/ui/src/components/main.tsx index 9ff6af444..ec801f902 100644 --- a/ui/src/components/main.tsx +++ b/ui/src/components/main.tsx @@ -14,7 +14,6 @@ import { ListingType, SiteResponse, GetPostsResponse, - CreatePostLikeResponse, PostResponse, Post, GetPostsForm, @@ -622,7 +621,7 @@ export class Main extends Component { this.setState(this.state); } else if (res.op == UserOperation.CreatePostLike) { - let data = res.data as CreatePostLikeResponse; + let data = res.data as PostResponse; let found = this.state.posts.find(c => c.id == data.post.id); found.score = data.post.score; diff --git a/ui/src/components/post.tsx b/ui/src/components/post.tsx index fcf794389..b53d7d4f1 100644 --- a/ui/src/components/post.tsx +++ b/ui/src/components/post.tsx @@ -11,7 +11,6 @@ import { CommentForm as CommentFormI, CommentResponse, CommentSortType, - CreatePostLikeResponse, CommunityUser, CommunityResponse, CommentNode as CommentNodeI, @@ -401,8 +400,8 @@ export class Post extends Component { } else if (res.op == UserOperation.CreateComment) { let data = res.data as CommentResponse; - // Necessary since a user might receive a comment reply on another thread - if (data.comment.post_id == this.state.post.id) { + // Necessary since it might be a user reply + if (data.recipient_ids.length == 0) { this.state.comments.unshift(data.comment); this.setState(this.state); } @@ -439,7 +438,7 @@ export class Post extends Component { } this.setState(this.state); } else if (res.op == UserOperation.CreatePostLike) { - let data = res.data as CreatePostLikeResponse; + let data = res.data as PostResponse; this.state.post.score = data.post.score; this.state.post.upvotes = data.post.upvotes; this.state.post.downvotes = data.post.downvotes; diff --git a/ui/src/components/search.tsx b/ui/src/components/search.tsx index 604c26173..f376f609c 100644 --- a/ui/src/components/search.tsx +++ b/ui/src/components/search.tsx @@ -12,7 +12,7 @@ import { SearchForm, SearchResponse, SearchType, - CreatePostLikeResponse, + PostResponse, CommentResponse, WebSocketJsonResponse, } from '../interfaces'; @@ -506,7 +506,7 @@ export class Search extends Component { } this.setState(this.state); } else if (res.op == UserOperation.CreatePostLike) { - let data = res.data as CreatePostLikeResponse; + let data = res.data as PostResponse; let found = this.state.searchResponse.posts.find( c => c.id == data.post.id ); diff --git a/ui/src/interfaces.ts b/ui/src/interfaces.ts index 4036f7e6f..98cdc7630 100644 --- a/ui/src/interfaces.ts +++ b/ui/src/interfaces.ts @@ -664,10 +664,6 @@ export interface CreatePostLikeForm { auth?: string; } -export interface CreatePostLikeResponse { - post: Post; -} - export interface SiteForm { name: string; description?: string; @@ -831,7 +827,7 @@ type ResponseType = | GetFollowedCommunitiesResponse | ListCommunitiesResponse | GetPostsResponse - | CreatePostLikeResponse + | PostResponse | GetRepliesResponse | GetUserMentionsResponse | ListCategoriesResponse