]> Untitled Git - lemmy.git/commitdiff
Adding some helper functions.
authorDessalines <tyhou13@gmx.com>
Tue, 21 Jul 2020 17:52:57 +0000 (13:52 -0400)
committerDessalines <tyhou13@gmx.com>
Tue, 21 Jul 2020 17:52:57 +0000 (13:52 -0400)
server/lemmy_db/src/post.rs
server/src/api/community.rs
server/src/api/post.rs

index 35b0feada20099e2a702a52f6a7327758f54ed96..d466778973ee72f15d8f0991691f7aefc2275830 100644 (file)
@@ -148,6 +148,10 @@ impl Post {
       .set(stickied.eq(new_stickied))
       .get_result::<Self>(conn)
   }
+
+  pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
+    user_id == post_creator_id
+  }
 }
 
 impl Crud<PostForm> for Post {
index f4ba15fb31ab44756d5c9bca688f0779e0fe437a..1ae7036f220dc44048f5d81086b95d6bcf3f609e 100644 (file)
@@ -433,19 +433,7 @@ impl Perform for Oper<EditCommunity> {
       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<DeleteCommunity> {
       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<RemoveCommunity> {
       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<BanFromCommunity> {
     }
 
     // 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<TransferCommunity> {
     })
   }
 }
+
+pub fn send_community_websocket(
+  res: &CommunityResponse,
+  websocket_info: Option<WebsocketInfo>,
+  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,
+    });
+  }
+}
index 15c38a3f6126167a5667b490dbe2f324c55c418f..70f46b2a2b69bb214859435241dc34f9c3b3fb05 100644 (file)
@@ -589,7 +589,7 @@ impl Perform for Oper<EditPost> {
     }
 
     // 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<DeletePost> {
     }
 
     // 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());
     }