From: Dessalines Date: Sun, 5 May 2019 16:20:30 +0000 (-0700) Subject: Done with reorg X-Git-Url: http://these/git/readmes/static/%7B%60https:/%7B%60%24%7BrepoUrl%7D?a=commitdiff_plain;h=f8615b6178ed2c2863de6cb98988b97d01b46786;p=lemmy.git Done with reorg --- diff --git a/server/src/api/comment.rs b/server/src/api/comment.rs index 36a44b36..65516aca 100644 --- a/server/src/api/comment.rs +++ b/server/src/api/comment.rs @@ -47,13 +47,13 @@ pub struct CreateCommentLike { impl Perform for Oper { fn perform(&self) -> Result { - let data: CreateComment = self.data; + let data: &CreateComment = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -62,12 +62,12 @@ impl Perform for Oper { // Check for a community ban let post = Post::read(&conn, data.post_id)?; if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { - return Err(APIError::err(self.op, "You have been banned from this community"))? + return Err(APIError::err(&self.op, "You have been banned from this community"))? } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(self.op, "You have been banned from the site"))? + return Err(APIError::err(&self.op, "You have been banned from the site"))? } let content_slurs_removed = remove_slurs(&data.content.to_owned()); @@ -86,7 +86,7 @@ impl Perform for Oper { let inserted_comment = match Comment::create(&conn, &comment_form) { Ok(comment) => comment, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't create Comment"))? + return Err(APIError::err(&self.op, "Couldn't create Comment"))? } }; @@ -101,7 +101,7 @@ impl Perform for Oper { let _inserted_like = match CommentLike::like(&conn, &like_form) { Ok(like) => like, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't like comment."))? + return Err(APIError::err(&self.op, "Couldn't like comment."))? } }; @@ -118,13 +118,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: EditComment = self.data; + let data: &EditComment = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -153,17 +153,17 @@ impl Perform for Oper { ); if !editors.contains(&user_id) { - return Err(APIError::err(self.op, "Not allowed to edit comment."))? + return Err(APIError::err(&self.op, "Not allowed to edit comment."))? } // Check for a community ban if CommunityUserBanView::get(&conn, user_id, orig_comment.community_id).is_ok() { - return Err(APIError::err(self.op, "You have been banned from this community"))? + return Err(APIError::err(&self.op, "You have been banned from this community"))? } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(self.op, "You have been banned from the site"))? + return Err(APIError::err(&self.op, "You have been banned from the site"))? } } @@ -184,7 +184,7 @@ impl Perform for Oper { let _updated_comment = match Comment::update(&conn, data.edit_id, &comment_form) { Ok(comment) => comment, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't update Comment"))? + return Err(APIError::err(&self.op, "Couldn't update Comment"))? } }; @@ -214,13 +214,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: SaveComment = self.data; + let data: &SaveComment = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -235,14 +235,14 @@ impl Perform for Oper { match CommentSaved::save(&conn, &comment_saved_form) { Ok(comment) => comment, Err(_e) => { - return Err(APIError::err(self.op, "Couldnt do comment save"))? + return Err(APIError::err(&self.op, "Couldnt do comment save"))? } }; } else { match CommentSaved::unsave(&conn, &comment_saved_form) { Ok(comment) => comment, Err(_e) => { - return Err(APIError::err(self.op, "Couldnt do comment save"))? + return Err(APIError::err(&self.op, "Couldnt do comment save"))? } }; } @@ -260,13 +260,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: CreateCommentLike = self.data; + let data: &CreateCommentLike = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -275,12 +275,12 @@ impl Perform for Oper { // Check for a community ban let post = Post::read(&conn, data.post_id)?; if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { - return Err(APIError::err(self.op, "You have been banned from this community"))? + return Err(APIError::err(&self.op, "You have been banned from this community"))? } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(self.op, "You have been banned from the site"))? + return Err(APIError::err(&self.op, "You have been banned from the site"))? } let like_form = CommentLikeForm { @@ -298,7 +298,7 @@ impl Perform for Oper { let _inserted_like = match CommentLike::like(&conn, &like_form) { Ok(like) => like, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't like comment."))? + return Err(APIError::err(&self.op, "Couldn't like comment."))? } }; } diff --git a/server/src/api/community.rs b/server/src/api/community.rs index 5059b17f..be4bb41a 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -46,7 +46,7 @@ pub struct ListCommunitiesResponse { communities: Vec } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone)] pub struct BanFromCommunity { pub community_id: i32, user_id: i32, @@ -111,7 +111,7 @@ pub struct GetFollowedCommunitiesResponse { impl Perform for Oper { fn perform(&self) -> Result { - let data: GetCommunity = self.data; + let data: &GetCommunity = &self.data; let conn = establish_connection(); let user_id: Option = match &data.auth { @@ -135,14 +135,14 @@ impl Perform for Oper { let community_view = match CommunityView::read(&conn, community_id, user_id) { Ok(community) => community, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't find Community"))? + return Err(APIError::err(&self.op, "Couldn't find Community"))? } }; let moderators = match CommunityModeratorView::for_community(&conn, community_id) { Ok(moderators) => moderators, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't find Community"))? + return Err(APIError::err(&self.op, "Couldn't find Community"))? } }; @@ -162,27 +162,27 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: CreateCommunity = self.data; + let data: &CreateCommunity = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; if has_slurs(&data.name) || has_slurs(&data.title) || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { - return Err(APIError::err(self.op, "No slurs"))? + return Err(APIError::err(&self.op, "No slurs"))? } let user_id = claims.id; // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(self.op, "You have been banned from the site"))? + return Err(APIError::err(&self.op, "You have been banned from the site"))? } // When you create a community, make sure the user becomes a moderator and a follower @@ -200,7 +200,7 @@ impl Perform for Oper { let inserted_community = match Community::create(&conn, &community_form) { Ok(community) => community, Err(_e) => { - return Err(APIError::err(self.op, "Community already exists."))? + return Err(APIError::err(&self.op, "Community already exists."))? } }; @@ -212,7 +212,7 @@ impl Perform for Oper { let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community moderator already exists."))? + return Err(APIError::err(&self.op, "Community moderator already exists."))? } }; @@ -224,7 +224,7 @@ impl Perform for Oper { let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community follower already exists."))? + return Err(APIError::err(&self.op, "Community follower already exists."))? } }; @@ -241,10 +241,10 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: EditCommunity = self.data; + let data: &EditCommunity = &self.data; if has_slurs(&data.name) || has_slurs(&data.title) { - return Err(APIError::err(self.op, "No slurs"))? + return Err(APIError::err(&self.op, "No slurs"))? } let conn = establish_connection(); @@ -252,7 +252,7 @@ impl Perform for Oper { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -260,7 +260,7 @@ impl Perform for Oper { // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(self.op, "You have been banned from the site"))? + return Err(APIError::err(&self.op, "You have been banned from the site"))? } // Verify its a mod @@ -280,7 +280,7 @@ impl Perform for Oper { .collect() ); if !editors.contains(&user_id) { - return Err(APIError::err(self.op, "Not allowed to edit community"))? + return Err(APIError::err(&self.op, "Not allowed to edit community"))? } let community_form = CommunityForm { @@ -297,7 +297,7 @@ impl Perform for Oper { let _updated_community = match Community::update(&conn, data.edit_id, &community_form) { Ok(community) => community, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't update Community"))? + return Err(APIError::err(&self.op, "Couldn't update Community"))? } }; @@ -330,7 +330,7 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: ListCommunities = self.data; + let data: &ListCommunities = &self.data; let conn = establish_connection(); let user_id: Option = match &data.auth { @@ -363,13 +363,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: FollowCommunity = self.data; + let data: &FollowCommunity = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -384,14 +384,14 @@ impl Perform for Oper { match CommunityFollower::follow(&conn, &community_follower_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community follower already exists."))? + return Err(APIError::err(&self.op, "Community follower already exists."))? } }; } else { match CommunityFollower::ignore(&conn, &community_follower_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community follower already exists."))? + return Err(APIError::err(&self.op, "Community follower already exists."))? } }; } @@ -410,13 +410,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: GetFollowedCommunities = self.data; + let data: &GetFollowedCommunities = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -425,7 +425,7 @@ impl Perform for Oper { let communities: Vec = match CommunityFollowerView::for_user(&conn, user_id) { Ok(communities) => communities, Err(_e) => { - return Err(APIError::err(self.op, "System error, try logging out and back in."))? + return Err(APIError::err(&self.op, "System error, try logging out and back in."))? } }; @@ -442,13 +442,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: BanFromCommunity = self.data; + let data: &BanFromCommunity = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -463,14 +463,14 @@ impl Perform for Oper { match CommunityUserBan::ban(&conn, &community_user_ban_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community user ban already exists"))? + return Err(APIError::err(&self.op, "Community user ban already exists"))? } }; } else { match CommunityUserBan::unban(&conn, &community_user_ban_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community user ban already exists"))? + return Err(APIError::err(&self.op, "Community user ban already exists"))? } }; } @@ -505,13 +505,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: AddModToCommunity = self.data; + let data: &AddModToCommunity = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -526,14 +526,14 @@ impl Perform for Oper { match CommunityModerator::join(&conn, &community_moderator_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community moderator already exists."))? + return Err(APIError::err(&self.op, "Community moderator already exists."))? } }; } else { match CommunityModerator::leave(&conn, &community_moderator_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community moderator already exists."))? + return Err(APIError::err(&self.op, "Community moderator already exists."))? } }; } diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 4bf6f7fe..6e3e8269 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -28,12 +28,12 @@ pub enum UserOperation { #[derive(Fail, Debug)] #[fail(display = "{{\"op\":\"{}\", \"error\":\"{}\"}}", op, message)] pub struct APIError { - op: String, - message: String, + pub op: String, + pub message: String, } impl APIError { - pub fn err(op: UserOperation, msg: &str) -> Self { + pub fn err(op: &UserOperation, msg: &str) -> Self { APIError { op: op.to_string(), message: msg.to_string(), diff --git a/server/src/api/post.rs b/server/src/api/post.rs index e7bec69e..39df9546 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -87,32 +87,32 @@ pub struct SavePost { impl Perform for Oper { fn perform(&self) -> Result { - let data: CreatePost = self.data; + let data: &CreatePost = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; if has_slurs(&data.name) || (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) { - return Err(APIError::err(self.op, "No slurs"))? + return Err(APIError::err(&self.op, "No slurs"))? } let user_id = claims.id; // Check for a community ban if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() { - return Err(APIError::err(self.op, "You have been banned from this community"))? + return Err(APIError::err(&self.op, "You have been banned from this community"))? } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(self.op, "You have been banned from the site"))? + return Err(APIError::err(&self.op, "You have been banned from the site"))? } let post_form = PostForm { @@ -130,7 +130,7 @@ impl Perform for Oper { let inserted_post = match Post::create(&conn, &post_form) { Ok(post) => post, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't create Post"))? + return Err(APIError::err(&self.op, "Couldn't create Post"))? } }; @@ -145,7 +145,7 @@ impl Perform for Oper { let _inserted_like = match PostLike::like(&conn, &like_form) { Ok(like) => like, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't like post."))? + return Err(APIError::err(&self.op, "Couldn't like post."))? } }; @@ -153,7 +153,7 @@ impl Perform for Oper { let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) { Ok(post) => post, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't find Post"))? + return Err(APIError::err(&self.op, "Couldn't find Post"))? } }; @@ -168,7 +168,7 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: GetPost = self.data; + let data: &GetPost = &self.data; let conn = establish_connection(); let user_id: Option = match &data.auth { @@ -187,7 +187,7 @@ impl Perform for Oper { let post_view = match PostView::read(&conn, data.id, user_id) { Ok(post) => post, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't find Post"))? + return Err(APIError::err(&self.op, "Couldn't find Post"))? } }; @@ -216,7 +216,7 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: GetPosts = self.data; + let data: &GetPosts = &self.data; let conn = establish_connection(); let user_id: Option = match &data.auth { @@ -248,7 +248,7 @@ impl Perform for Oper { data.limit) { Ok(posts) => posts, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't get posts"))? + return Err(APIError::err(&self.op, "Couldn't get posts"))? } }; @@ -264,13 +264,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: CreatePostLike = self.data; + let data: &CreatePostLike = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -279,12 +279,12 @@ impl Perform for Oper { // Check for a community ban let post = Post::read(&conn, data.post_id)?; if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { - return Err(APIError::err(self.op, "You have been banned from this community"))? + return Err(APIError::err(&self.op, "You have been banned from this community"))? } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(self.op, "You have been banned from the site"))? + return Err(APIError::err(&self.op, "You have been banned from the site"))? } let like_form = PostLikeForm { @@ -301,7 +301,7 @@ impl Perform for Oper { let _inserted_like = match PostLike::like(&conn, &like_form) { Ok(like) => like, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't like post."))? + return Err(APIError::err(&self.op, "Couldn't like post."))? } }; } @@ -309,7 +309,7 @@ impl Perform for Oper { let post_view = match PostView::read(&conn, data.post_id, Some(user_id)) { Ok(post) => post, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't find Post"))? + return Err(APIError::err(&self.op, "Couldn't find Post"))? } }; @@ -325,10 +325,10 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: EditPost = self.data; + let data: &EditPost = &self.data; if has_slurs(&data.name) || (data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) { - return Err(APIError::err(self.op, "No slurs"))? + return Err(APIError::err(&self.op, "No slurs"))? } let conn = establish_connection(); @@ -336,7 +336,7 @@ impl Perform for Oper { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -359,17 +359,17 @@ impl Perform for Oper { .collect() ); if !editors.contains(&user_id) { - return Err(APIError::err(self.op, "Not allowed to edit post."))? + return Err(APIError::err(&self.op, "Not allowed to edit post."))? } // Check for a community ban if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() { - return Err(APIError::err(self.op, "You have been banned from this community"))? + return Err(APIError::err(&self.op, "You have been banned from this community"))? } // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(self.op, "You have been banned from the site"))? + return Err(APIError::err(&self.op, "You have been banned from the site"))? } let post_form = PostForm { @@ -387,7 +387,7 @@ impl Perform for Oper { let _updated_post = match Post::update(&conn, data.edit_id, &post_form) { Ok(post) => post, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't update Post"))? + return Err(APIError::err(&self.op, "Couldn't update Post"))? } }; @@ -424,13 +424,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: SavePost = self.data; + let data: &SavePost = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -445,14 +445,14 @@ impl Perform for Oper { match PostSaved::save(&conn, &post_saved_form) { Ok(post) => post, Err(_e) => { - return Err(APIError::err(self.op, "Couldnt do post save"))? + return Err(APIError::err(&self.op, "Couldnt do post save"))? } }; } else { match PostSaved::unsave(&conn, &post_saved_form) { Ok(post) => post, Err(_e) => { - return Err(APIError::err(self.op, "Couldnt do post save"))? + return Err(APIError::err(&self.op, "Couldnt do post save"))? } }; } diff --git a/server/src/api/site.rs b/server/src/api/site.rs index 3140788d..03ee90ff 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -83,7 +83,7 @@ pub struct GetSiteResponse { impl Perform for Oper { fn perform(&self) -> Result { - let data: ListCategories = self.data; + let _data: &ListCategories = &self.data; let conn = establish_connection(); let categories: Vec = Category::list_all(&conn)?; @@ -100,7 +100,7 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: GetModlog = self.data; + let data: &GetModlog = &self.data; let conn = establish_connection(); let removed_posts = ModRemovePostView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?; @@ -139,26 +139,26 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: CreateSite = self.data; + let data: &CreateSite = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; if has_slurs(&data.name) || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { - return Err(APIError::err(self.op, "No slurs"))? + return Err(APIError::err(&self.op, "No slurs"))? } let user_id = claims.id; // Make sure user is an admin if !UserView::read(&conn, user_id)?.admin { - return Err(APIError::err(self.op, "Not an admin."))? + return Err(APIError::err(&self.op, "Not an admin."))? } let site_form = SiteForm { @@ -171,7 +171,7 @@ impl Perform for Oper { match Site::create(&conn, &site_form) { Ok(site) => site, Err(_e) => { - return Err(APIError::err(self.op, "Site exists already"))? + return Err(APIError::err(&self.op, "Site exists already"))? } }; @@ -189,26 +189,26 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: EditSite = self.data; + let data: &EditSite = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; if has_slurs(&data.name) || (data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { - return Err(APIError::err(self.op, "No slurs"))? + return Err(APIError::err(&self.op, "No slurs"))? } let user_id = claims.id; // Make sure user is an admin if UserView::read(&conn, user_id)?.admin == false { - return Err(APIError::err(self.op, "Not an admin."))? + return Err(APIError::err(&self.op, "Not an admin."))? } let found_site = Site::read(&conn, 1)?; @@ -223,7 +223,7 @@ impl Perform for Oper { match Site::update(&conn, 1, &site_form) { Ok(site) => site, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't update site."))? + return Err(APIError::err(&self.op, "Couldn't update site."))? } }; @@ -240,7 +240,7 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: GetSite = self.data; + let _data: &GetSite = &self.data; let conn = establish_connection(); // It can return a null site in order to redirect @@ -265,7 +265,7 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: Search = self.data; + let data: &Search = &self.data; let conn = establish_connection(); let sort = SortType::from_str(&data.sort)?; diff --git a/server/src/api/user.rs b/server/src/api/user.rs index 82ec11cc..ab0f24a5 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -97,19 +97,19 @@ pub struct GetReplies { impl Perform for Oper { fn perform(&self) -> Result { - let data: Login = self.data; + let data: &Login = &self.data; let conn = establish_connection(); // Fetch that username / email let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) { Ok(user) => user, - Err(_e) => return Err(APIError::err(self.op, "Couldn't find that username or email"))? + Err(_e) => return Err(APIError::err(&self.op, "Couldn't find that username or email"))? }; // Verify the password let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false); if !valid { - return Err(APIError::err(self.op, "Password incorrect"))? + return Err(APIError::err(&self.op, "Password incorrect"))? } // Return the jwt @@ -125,25 +125,25 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: Register = self.data; + let data: &Register = &self.data; let conn = establish_connection(); // Make sure passwords match if &data.password != &data.password_verify { - return Err(APIError::err(self.op, "Passwords do not match."))? + return Err(APIError::err(&self.op, "Passwords do not match."))? } if data.spam_timeri < 1142 { - return Err(APIError::err(self.op, "Too fast"))? + return Err(APIError::err(&self.op, "Too fast"))? } if has_slurs(&data.username) { - return Err(APIError::err(self.op, "No slurs"))? + return Err(APIError::err(&self.op, "No slurs"))? } // Make sure there are no admins if data.admin && UserView::admins(&conn)?.len() > 0 { - return Err(APIError::err(self.op, "Sorry, there's already an admin."))? + return Err(APIError::err(&self.op, "Sorry, there's already an admin."))? } // Register the new user @@ -162,7 +162,7 @@ impl Perform for Oper { let inserted_user = match User_::register(&conn, &user_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "User already exists."))? + return Err(APIError::err(&self.op, "User already exists."))? } }; @@ -175,7 +175,7 @@ impl Perform for Oper { let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community follower already exists."))? + return Err(APIError::err(&self.op, "Community follower already exists."))? } }; @@ -189,7 +189,7 @@ impl Perform for Oper { let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Community moderator already exists."))? + return Err(APIError::err(&self.op, "Community moderator already exists."))? } }; @@ -208,7 +208,7 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: GetUserDetails = self.data; + let data: &GetUserDetails = &self.data; let conn = establish_connection(); let user_id: Option = match &data.auth { @@ -302,13 +302,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: AddAdmin = self.data; + let data: &AddAdmin = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -316,7 +316,7 @@ impl Perform for Oper { // Make sure user is an admin if UserView::read(&conn, user_id)?.admin == false { - return Err(APIError::err(self.op, "Not an admin."))? + return Err(APIError::err(&self.op, "Not an admin."))? } let read_user = User_::read(&conn, data.user_id)?; @@ -335,7 +335,7 @@ impl Perform for Oper { match User_::update(&conn, data.user_id, &user_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't update user"))? + return Err(APIError::err(&self.op, "Couldn't update user"))? } }; @@ -361,13 +361,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: BanUser = self.data; + let data: &BanUser = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -375,7 +375,7 @@ impl Perform for Oper { // Make sure user is an admin if UserView::read(&conn, user_id)?.admin == false { - return Err(APIError::err(self.op, "Not an admin."))? + return Err(APIError::err(&self.op, "Not an admin."))? } let read_user = User_::read(&conn, data.user_id)?; @@ -394,7 +394,7 @@ impl Perform for Oper { match User_::update(&conn, data.user_id, &user_form) { Ok(user) => user, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't update user"))? + return Err(APIError::err(&self.op, "Couldn't update user"))? } }; @@ -429,13 +429,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: GetReplies = self.data; + let data: &GetReplies = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -457,13 +457,13 @@ impl Perform for Oper { impl Perform for Oper { fn perform(&self) -> Result { - let data: MarkAllAsRead = self.data; + let data: &MarkAllAsRead = &self.data; let conn = establish_connection(); let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, Err(_e) => { - return Err(APIError::err(self.op, "Not logged in."))? + return Err(APIError::err(&self.op, "Not logged in."))? } }; @@ -486,7 +486,7 @@ impl Perform for Oper { let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) { Ok(comment) => comment, Err(_e) => { - return Err(APIError::err(self.op, "Couldn't update Comment"))? + return Err(APIError::err(&self.op, "Couldn't update Comment"))? } }; } diff --git a/server/src/db/category.rs b/server/src/db/category.rs index 7835fedd..99f906d4 100644 --- a/server/src/db/category.rs +++ b/server/src/db/category.rs @@ -47,7 +47,6 @@ impl Category { #[cfg(test)] mod tests { - use establish_connection; use super::*; #[test] fn test_crud() { diff --git a/server/src/db/comment.rs b/server/src/db/comment.rs index 4e57aaa4..a924bd4c 100644 --- a/server/src/db/comment.rs +++ b/server/src/db/comment.rs @@ -154,12 +154,10 @@ impl Saveable for CommentSaved { #[cfg(test)] mod tests { - use establish_connection; use super::*; use super::super::post::*; use super::super::community::*; use super::super::user::*; - use Crud; #[test] fn test_crud() { let conn = establish_connection(); diff --git a/server/src/db/comment_view.rs b/server/src/db/comment_view.rs index eb1d302f..9cd61e33 100644 --- a/server/src/db/comment_view.rs +++ b/server/src/db/comment_view.rs @@ -244,13 +244,11 @@ impl ReplyView { #[cfg(test)] mod tests { - use establish_connection; use super::*; use super::super::post::*; use super::super::community::*; use super::super::user::*; use super::super::comment::*; - use {Crud,Likeable}; #[test] fn test_crud() { let conn = establish_connection(); diff --git a/server/src/db/community.rs b/server/src/db/community.rs index a2c300f2..4540f731 100644 --- a/server/src/db/community.rs +++ b/server/src/db/community.rs @@ -215,10 +215,8 @@ impl Crud for Site { #[cfg(test)] mod tests { - use establish_connection; use super::*; use super::super::user::*; - use Crud; #[test] fn test_crud() { let conn = establish_connection(); diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index a3edb1b9..c3587c47 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -81,3 +81,12 @@ pub fn limit_and_offset(page: Option, limit: Option) -> (i64, i64) { let offset = limit * (page - 1); (limit, offset) } +#[cfg(test)] +mod tests { + use super::fuzzy_search; + #[test] fn test_fuzzy_search() { + let test = "This is a fuzzy search"; + assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string()); + } +} + diff --git a/server/src/db/moderator.rs b/server/src/db/moderator.rs index c23cf9e6..8b85e663 100644 --- a/server/src/db/moderator.rs +++ b/server/src/db/moderator.rs @@ -394,7 +394,6 @@ impl Crud for ModAdd { #[cfg(test)] mod tests { - use establish_connection; use super::*; use super::super::user::*; use super::super::post::*; diff --git a/server/src/db/post.rs b/server/src/db/post.rs index 009543d3..f0302271 100644 --- a/server/src/db/post.rs +++ b/server/src/db/post.rs @@ -168,8 +168,6 @@ impl Readable for PostRead { #[cfg(test)] mod tests { - use establish_connection; - use Crud; use super::*; use super::super::community::*; use super::super::user::*; diff --git a/server/src/db/post_view.rs b/server/src/db/post_view.rs index f51eab10..bfe730a2 100644 --- a/server/src/db/post_view.rs +++ b/server/src/db/post_view.rs @@ -174,7 +174,6 @@ impl PostView { #[cfg(test)] mod tests { - use {establish_connection, Crud, Likeable}; use super::*; use super::super::community::*; use super::super::user::*; diff --git a/server/src/db/user.rs b/server/src/db/user.rs index 25403069..a4a7be43 100644 --- a/server/src/db/user.rs +++ b/server/src/db/user.rs @@ -120,9 +120,7 @@ impl User_ { #[cfg(test)] mod tests { - use establish_connection; - use super::{User_, UserForm}; - use Crud; + use super::*; #[test] fn test_crud() { let conn = establish_connection(); diff --git a/server/src/lib.rs b/server/src/lib.rs index 9d87d704..7dd090d6 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -72,7 +72,7 @@ pub fn has_slurs(test: &str) -> bool { #[cfg(test)] mod tests { - use {Settings, is_email_regex, remove_slurs, has_slurs, fuzzy_search}; + use {Settings, is_email_regex, remove_slurs, has_slurs}; #[test] fn test_api() { assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1"); @@ -91,10 +91,6 @@ mod tests { assert!(!has_slurs(slur_free)); } - #[test] fn test_fuzzy_search() { - let test = "This is a fuzzy search"; - assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string()); - } } lazy_static! { diff --git a/server/src/main.rs b/server/src/main.rs index add17cfd..fc27531b 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -7,7 +7,7 @@ use lemmy_server::actix::*; use lemmy_server::actix_web::server::HttpServer; use lemmy_server::actix_web::{ws, App, Error, HttpRequest, HttpResponse, fs::NamedFile, fs}; use lemmy_server::websocket::server::*; -use lemmy_server::establish_connection; +use lemmy_server::db::establish_connection; embed_migrations!(); @@ -117,6 +117,7 @@ impl StreamHandler for WSSession { } ws::Message::Text(text) => { let m = text.trim().to_owned(); + println!("WEBSOCKET MESSAGE: {:?} from id: {}", &m, self.id); ctx.state() .addr @@ -130,79 +131,11 @@ impl StreamHandler for WSSession { Ok(res) => ctx.text(res), Err(e) => { eprintln!("{}", &e); - // ctx.text(e); } } - // Ok(res) => ctx.text(res), - // // something is wrong with chat server - // _ => ctx.stop(), fut::ok(()) }) .wait(ctx); - - // we check for /sss type of messages - // if m.starts_with('/') { - // let v: Vec<&str> = m.splitn(2, ' ').collect(); - // match v[0] { - // "/list" => { - // // Send ListRooms message to chat server and wait for - // // response - // println!("List rooms"); - // ctx.state() - // .addr - // .send(ListRooms) - // .into_actor(self) - // .then(|res, _, ctx| { - // match res { - // Ok(rooms) => { - // for room in rooms { - // ctx.text(room); - // } - // } - // _ => println!("Something is wrong"), - // } - // fut::ok(()) - // }) - // .wait(ctx) - // .wait(ctx) pauses all events in context, - // so actor wont receive any new messages until it get list - // of rooms back - // } - // "/join" => { - // if v.len() == 2 { - // self.room = v[1].to_owned(); - // ctx.state().addr.do_send(Join { - // id: self.id, - // name: self.room.clone(), - // }); - - // ctx.text("joined"); - // } else { - // ctx.text("!!! room name is required"); - // } - // } - // "/name" => { - // if v.len() == 2 { - // self.name = Some(v[1].to_owned()); - // } else { - // ctx.text("!!! name is required"); - // } - // } - // _ => ctx.text(format!("!!! unknown command: {:?}", m)), - // } - // } else { - // let msg = if let Some(ref name) = self.name { - // format!("{}: {}", name, m) - // } else { - // m.to_owned() - // }; - // send message to chat server - // ctx.state().addr.do_send(ClientMessage { - // id: self.id, - // msg: msg, - // room: self.room.clone(), - // }) - // } } ws::Message::Binary(_bin) => println!("Unexpected binary"), ws::Message::Close(_) => { diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 857c626a..cd931e1f 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -104,8 +104,8 @@ impl Default for ChatServer { impl ChatServer { /// Send message to all users in the room - fn send_room_message(&self, room: i32, message: &str, skip_id: usize) { - if let Some(sessions) = self.rooms.get(&room) { + fn send_room_message(&self, room: &i32, message: &str, skip_id: usize) { + if let Some(sessions) = self.rooms.get(room) { for id in sessions { if *id != skip_id { if let Some(info) = self.sessions.get(id) { @@ -116,9 +116,9 @@ impl ChatServer { } } - fn join_room(&self, room_id: i32, id: usize) { + fn join_room(&mut self, room_id: i32, id: usize) { // remove session from all rooms - for (_n, sessions) in &mut self.rooms { + for (_n, mut sessions) in &mut self.rooms { sessions.remove(&id); } @@ -127,17 +127,17 @@ impl ChatServer { self.rooms.insert(room_id, HashSet::new()); } - self.rooms.get_mut(&room_id).unwrap().insert(id); + &self.rooms.get_mut(&room_id).unwrap().insert(id); } - fn send_community_message(&self, community_id: i32, message: &str, skip_id: usize) -> Result<(), Error> { + fn send_community_message(&self, community_id: &i32, message: &str, skip_id: usize) -> Result<(), Error> { use db::*; use db::post_view::*; let conn = establish_connection(); let posts = PostView::list(&conn, PostListingType::Community, &SortType::New, - Some(community_id), + Some(*community_id), None, None, None, @@ -146,7 +146,7 @@ impl ChatServer { None, Some(9999))?; for post in posts { - self.send_room_message(post.id, message, skip_id); + self.send_room_message(&post.id, message, skip_id); } Ok(()) @@ -346,7 +346,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { @@ -361,16 +361,18 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { let ban_from_community: BanFromCommunity = serde_json::from_str(data)?; + let community_id = ban_from_community.community_id; let res = Oper::new(user_operation, ban_from_community).perform()?; let res_str = serde_json::to_string(&res)?; - chat.send_community_message(ban_from_community.community_id, &res_str, msg.id)?; + chat.send_community_message(&community_id, &res_str, msg.id)?; Ok(res_str) }, UserOperation::AddModToCommunity => { let mod_add_to_community: AddModToCommunity = serde_json::from_str(data)?; + let community_id = mod_add_to_community.community_id; let res = Oper::new(user_operation, mod_add_to_community).perform()?; let res_str = serde_json::to_string(&res)?; - chat.send_community_message(mod_add_to_community.community_id, &res_str, msg.id)?; + chat.send_community_message(&community_id, &res_str, msg.id)?; Ok(res_str) }, UserOperation::ListCategories => { @@ -407,7 +409,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { @@ -418,22 +420,24 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { chat.check_rate_limit(msg.id)?; let create_comment: CreateComment = serde_json::from_str(data)?; + let post_id = create_comment.post_id; let res = Oper::new(user_operation, create_comment).perform()?; let mut comment_sent = res.clone(); comment_sent.comment.my_vote = None; comment_sent.comment.user_id = None; let comment_sent_str = serde_json::to_string(&comment_sent)?; - chat.send_room_message(create_comment.post_id, &comment_sent_str, msg.id); + chat.send_room_message(&post_id, &comment_sent_str, msg.id); Ok(serde_json::to_string(&res)?) }, UserOperation::EditComment => { let edit_comment: EditComment = serde_json::from_str(data)?; + let post_id = edit_comment.post_id; let res = Oper::new(user_operation, edit_comment).perform()?; let mut comment_sent = res.clone(); comment_sent.comment.my_vote = None; comment_sent.comment.user_id = None; let comment_sent_str = serde_json::to_string(&comment_sent)?; - chat.send_room_message(edit_comment.post_id, &comment_sent_str, msg.id); + chat.send_room_message(&post_id, &comment_sent_str, msg.id); Ok(serde_json::to_string(&res)?) }, UserOperation::SaveComment => { @@ -444,12 +448,13 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result { chat.check_rate_limit(msg.id)?; let create_comment_like: CreateCommentLike = serde_json::from_str(data)?; + let post_id = create_comment_like.post_id; let res = Oper::new(user_operation, create_comment_like).perform()?; let mut comment_sent = res.clone(); comment_sent.comment.my_vote = None; comment_sent.comment.user_id = None; let comment_sent_str = serde_json::to_string(&comment_sent)?; - chat.send_room_message(create_comment_like.post_id, &comment_sent_str, msg.id); + chat.send_room_message(&post_id, &comment_sent_str, msg.id); Ok(serde_json::to_string(&res)?) }, UserOperation::GetModlog => {