From: Dessalines Date: Tue, 21 Jul 2020 17:52:57 +0000 (-0400) Subject: Adding some helper functions. X-Git-Url: http://these/git/%7B%24%7B%60data:application/%22%7Burl%7D/readmes/%24%7BgetStaticDir%28%29%7D/js/%7B?a=commitdiff_plain;h=5e5063cbddaedd53c6573c17a9f320096ce35d60;p=lemmy.git Adding some helper functions. --- diff --git a/server/lemmy_db/src/post.rs b/server/lemmy_db/src/post.rs index 35b0fead..d4667789 100644 --- a/server/lemmy_db/src/post.rs +++ b/server/lemmy_db/src/post.rs @@ -148,6 +148,10 @@ impl Post { .set(stickied.eq(new_stickied)) .get_result::(conn) } + + pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool { + user_id == post_creator_id + } } impl Crud for Post { diff --git a/server/src/api/community.rs b/server/src/api/community.rs index f4ba15fb..1ae7036f 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -433,19 +433,7 @@ impl Perform for Oper { community: community_view, }; - if let Some(ws) = websocket_info { - // Strip out the user id and subscribed when sending to others - let mut res_sent = res.clone(); - res_sent.community.user_id = None; - res_sent.community.subscribed = None; - - ws.chatserver.do_send(SendCommunityRoomMessage { - op: UserOperation::EditCommunity, - response: res_sent, - community_id: data.edit_id, - my_id: ws.id, - }); - } + send_community_websocket(&res, websocket_info, UserOperation::EditCommunity); Ok(res) } @@ -515,19 +503,7 @@ impl Perform for Oper { community: community_view, }; - if let Some(ws) = websocket_info { - // Strip out the user id and subscribed when sending to others - let mut res_sent = res.clone(); - res_sent.community.user_id = None; - res_sent.community.subscribed = None; - - ws.chatserver.do_send(SendCommunityRoomMessage { - op: UserOperation::DeleteCommunity, - response: res_sent, - community_id: data.edit_id, - my_id: ws.id, - }); - } + send_community_websocket(&res, websocket_info, UserOperation::DeleteCommunity); Ok(res) } @@ -613,19 +589,7 @@ impl Perform for Oper { community: community_view, }; - if let Some(ws) = websocket_info { - // Strip out the user id and subscribed when sending to others - let mut res_sent = res.clone(); - res_sent.community.user_id = None; - res_sent.community.subscribed = None; - - ws.chatserver.do_send(SendCommunityRoomMessage { - op: UserOperation::RemoveCommunity, - response: res_sent, - community_id: data.edit_id, - my_id: ws.id, - }); - } + send_community_websocket(&res, websocket_info, UserOperation::RemoveCommunity); Ok(res) } @@ -831,6 +795,7 @@ impl Perform for Oper { } // Mod tables + // TODO eventually do correct expires let expires = match data.expires { Some(time) => Some(naive_from_unix(time)), None => None, @@ -1055,3 +1020,23 @@ impl Perform for Oper { }) } } + +pub fn send_community_websocket( + res: &CommunityResponse, + websocket_info: Option, + op: UserOperation, +) { + if let Some(ws) = websocket_info { + // Strip out the user id and subscribed when sending to others + let mut res_sent = res.clone(); + res_sent.community.user_id = None; + res_sent.community.subscribed = None; + + ws.chatserver.do_send(SendCommunityRoomMessage { + op, + response: res_sent, + community_id: res.community.id, + my_id: ws.id, + }); + } +} diff --git a/server/src/api/post.rs b/server/src/api/post.rs index 15c38a3f..70f46b2a 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -589,7 +589,7 @@ impl Perform for Oper { } // Verify that only the creator can edit - if user_id != orig_post.creator_id { + if !Post::is_post_creator(user_id, orig_post.creator_id) { return Err(APIError::err("no_post_edit_allowed").into()); } @@ -692,7 +692,7 @@ impl Perform for Oper { } // Verify that only the creator can delete - if user_id != orig_post.creator_id { + if !Post::is_post_creator(user_id, orig_post.creator_id) { return Err(APIError::err("no_post_edit_allowed").into()); }