Done with reorg

This commit is contained in:
Dessalines 2019-05-05 09:20:30 -07:00
parent 7fb6a0b138
commit f8615b6178
18 changed files with 158 additions and 228 deletions

View file

@ -47,13 +47,13 @@ pub struct CreateCommentLike {
impl Perform<CommentResponse> for Oper<CreateComment> { impl Perform<CommentResponse> for Oper<CreateComment> {
fn perform(&self) -> Result<CommentResponse, Error> { fn perform(&self) -> Result<CommentResponse, Error> {
let data: CreateComment = self.data; let data: &CreateComment = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<CommentResponse> for Oper<CreateComment> {
// Check for a community ban // Check for a community ban
let post = Post::read(&conn, data.post_id)?; let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { 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 // Check for a site ban
if UserView::read(&conn, user_id)?.banned { 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()); let content_slurs_removed = remove_slurs(&data.content.to_owned());
@ -86,7 +86,7 @@ impl Perform<CommentResponse> for Oper<CreateComment> {
let inserted_comment = match Comment::create(&conn, &comment_form) { let inserted_comment = match Comment::create(&conn, &comment_form) {
Ok(comment) => comment, Ok(comment) => comment,
Err(_e) => { 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<CommentResponse> for Oper<CreateComment> {
let _inserted_like = match CommentLike::like(&conn, &like_form) { let _inserted_like = match CommentLike::like(&conn, &like_form) {
Ok(like) => like, Ok(like) => like,
Err(_e) => { 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<CommentResponse> for Oper<CreateComment> {
impl Perform<CommentResponse> for Oper<EditComment> { impl Perform<CommentResponse> for Oper<EditComment> {
fn perform(&self) -> Result<CommentResponse, Error> { fn perform(&self) -> Result<CommentResponse, Error> {
let data: EditComment = self.data; let data: &EditComment = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<CommentResponse> for Oper<EditComment> {
); );
if !editors.contains(&user_id) { 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 // Check for a community ban
if CommunityUserBanView::get(&conn, user_id, orig_comment.community_id).is_ok() { 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 // Check for a site ban
if UserView::read(&conn, user_id)?.banned { 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<CommentResponse> for Oper<EditComment> {
let _updated_comment = match Comment::update(&conn, data.edit_id, &comment_form) { let _updated_comment = match Comment::update(&conn, data.edit_id, &comment_form) {
Ok(comment) => comment, Ok(comment) => comment,
Err(_e) => { 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<CommentResponse> for Oper<EditComment> {
impl Perform<CommentResponse> for Oper<SaveComment> { impl Perform<CommentResponse> for Oper<SaveComment> {
fn perform(&self) -> Result<CommentResponse, Error> { fn perform(&self) -> Result<CommentResponse, Error> {
let data: SaveComment = self.data; let data: &SaveComment = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<CommentResponse> for Oper<SaveComment> {
match CommentSaved::save(&conn, &comment_saved_form) { match CommentSaved::save(&conn, &comment_saved_form) {
Ok(comment) => comment, Ok(comment) => comment,
Err(_e) => { Err(_e) => {
return Err(APIError::err(self.op, "Couldnt do comment save"))? return Err(APIError::err(&self.op, "Couldnt do comment save"))?
} }
}; };
} else { } else {
match CommentSaved::unsave(&conn, &comment_saved_form) { match CommentSaved::unsave(&conn, &comment_saved_form) {
Ok(comment) => comment, Ok(comment) => comment,
Err(_e) => { 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<CommentResponse> for Oper<SaveComment> {
impl Perform<CommentResponse> for Oper<CreateCommentLike> { impl Perform<CommentResponse> for Oper<CreateCommentLike> {
fn perform(&self) -> Result<CommentResponse, Error> { fn perform(&self) -> Result<CommentResponse, Error> {
let data: CreateCommentLike = self.data; let data: &CreateCommentLike = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<CommentResponse> for Oper<CreateCommentLike> {
// Check for a community ban // Check for a community ban
let post = Post::read(&conn, data.post_id)?; let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { 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 // Check for a site ban
if UserView::read(&conn, user_id)?.banned { 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 { let like_form = CommentLikeForm {
@ -298,7 +298,7 @@ impl Perform<CommentResponse> for Oper<CreateCommentLike> {
let _inserted_like = match CommentLike::like(&conn, &like_form) { let _inserted_like = match CommentLike::like(&conn, &like_form) {
Ok(like) => like, Ok(like) => like,
Err(_e) => { Err(_e) => {
return Err(APIError::err(self.op, "Couldn't like comment."))? return Err(APIError::err(&self.op, "Couldn't like comment."))?
} }
}; };
} }

View file

@ -46,7 +46,7 @@ pub struct ListCommunitiesResponse {
communities: Vec<CommunityView> communities: Vec<CommunityView>
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Clone)]
pub struct BanFromCommunity { pub struct BanFromCommunity {
pub community_id: i32, pub community_id: i32,
user_id: i32, user_id: i32,
@ -111,7 +111,7 @@ pub struct GetFollowedCommunitiesResponse {
impl Perform<GetCommunityResponse> for Oper<GetCommunity> { impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
fn perform(&self) -> Result<GetCommunityResponse, Error> { fn perform(&self) -> Result<GetCommunityResponse, Error> {
let data: GetCommunity = self.data; let data: &GetCommunity = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let user_id: Option<i32> = match &data.auth { let user_id: Option<i32> = match &data.auth {
@ -135,14 +135,14 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
let community_view = match CommunityView::read(&conn, community_id, user_id) { let community_view = match CommunityView::read(&conn, community_id, user_id) {
Ok(community) => community, Ok(community) => community,
Err(_e) => { 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) { let moderators = match CommunityModeratorView::for_community(&conn, community_id) {
Ok(moderators) => moderators, Ok(moderators) => moderators,
Err(_e) => { 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<GetCommunityResponse> for Oper<GetCommunity> {
impl Perform<CommunityResponse> for Oper<CreateCommunity> { impl Perform<CommunityResponse> for Oper<CreateCommunity> {
fn perform(&self) -> Result<CommunityResponse, Error> { fn perform(&self) -> Result<CommunityResponse, Error> {
let data: CreateCommunity = self.data; let data: &CreateCommunity = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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) || if has_slurs(&data.name) ||
has_slurs(&data.title) || has_slurs(&data.title) ||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { (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; let user_id = claims.id;
// Check for a site ban // Check for a site ban
if UserView::read(&conn, user_id)?.banned { 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 // When you create a community, make sure the user becomes a moderator and a follower
@ -200,7 +200,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
let inserted_community = match Community::create(&conn, &community_form) { let inserted_community = match Community::create(&conn, &community_form) {
Ok(community) => community, Ok(community) => community,
Err(_e) => { 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<CommunityResponse> for Oper<CreateCommunity> {
let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) { let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<CommunityResponse> for Oper<CreateCommunity> {
let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) { let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<CommunityResponse> for Oper<CreateCommunity> {
impl Perform<CommunityResponse> for Oper<EditCommunity> { impl Perform<CommunityResponse> for Oper<EditCommunity> {
fn perform(&self) -> Result<CommunityResponse, Error> { fn perform(&self) -> Result<CommunityResponse, Error> {
let data: EditCommunity = self.data; let data: &EditCommunity = &self.data;
if has_slurs(&data.name) || has_slurs(&data.title) { 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(); let conn = establish_connection();
@ -252,7 +252,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<CommunityResponse> for Oper<EditCommunity> {
// Check for a site ban // Check for a site ban
if UserView::read(&conn, user_id)?.banned { 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 // Verify its a mod
@ -280,7 +280,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
.collect() .collect()
); );
if !editors.contains(&user_id) { 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 { let community_form = CommunityForm {
@ -297,7 +297,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
let _updated_community = match Community::update(&conn, data.edit_id, &community_form) { let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
Ok(community) => community, Ok(community) => community,
Err(_e) => { 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<CommunityResponse> for Oper<EditCommunity> {
impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> { impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
fn perform(&self) -> Result<ListCommunitiesResponse, Error> { fn perform(&self) -> Result<ListCommunitiesResponse, Error> {
let data: ListCommunities = self.data; let data: &ListCommunities = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let user_id: Option<i32> = match &data.auth { let user_id: Option<i32> = match &data.auth {
@ -363,13 +363,13 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
impl Perform<CommunityResponse> for Oper<FollowCommunity> { impl Perform<CommunityResponse> for Oper<FollowCommunity> {
fn perform(&self) -> Result<CommunityResponse, Error> { fn perform(&self) -> Result<CommunityResponse, Error> {
let data: FollowCommunity = self.data; let data: &FollowCommunity = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<CommunityResponse> for Oper<FollowCommunity> {
match CommunityFollower::follow(&conn, &community_follower_form) { match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { Err(_e) => {
return Err(APIError::err(self.op, "Community follower already exists."))? return Err(APIError::err(&self.op, "Community follower already exists."))?
} }
}; };
} else { } else {
match CommunityFollower::ignore(&conn, &community_follower_form) { match CommunityFollower::ignore(&conn, &community_follower_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<CommunityResponse> for Oper<FollowCommunity> {
impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> { impl Perform<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
fn perform(&self) -> Result<GetFollowedCommunitiesResponse, Error> { fn perform(&self) -> Result<GetFollowedCommunitiesResponse, Error> {
let data: GetFollowedCommunities = self.data; let data: &GetFollowedCommunities = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
let communities: Vec<CommunityFollowerView> = match CommunityFollowerView::for_user(&conn, user_id) { let communities: Vec<CommunityFollowerView> = match CommunityFollowerView::for_user(&conn, user_id) {
Ok(communities) => communities, Ok(communities) => communities,
Err(_e) => { 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<GetFollowedCommunitiesResponse> for Oper<GetFollowedCommunities> {
impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> { impl Perform<BanFromCommunityResponse> for Oper<BanFromCommunity> {
fn perform(&self) -> Result<BanFromCommunityResponse, Error> { fn perform(&self) -> Result<BanFromCommunityResponse, Error> {
let data: BanFromCommunity = self.data; let data: &BanFromCommunity = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<BanFromCommunityResponse> for Oper<BanFromCommunity> {
match CommunityUserBan::ban(&conn, &community_user_ban_form) { match CommunityUserBan::ban(&conn, &community_user_ban_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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 { } else {
match CommunityUserBan::unban(&conn, &community_user_ban_form) { match CommunityUserBan::unban(&conn, &community_user_ban_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<BanFromCommunityResponse> for Oper<BanFromCommunity> {
impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> { impl Perform<AddModToCommunityResponse> for Oper<AddModToCommunity> {
fn perform(&self) -> Result<AddModToCommunityResponse, Error> { fn perform(&self) -> Result<AddModToCommunityResponse, Error> {
let data: AddModToCommunity = self.data; let data: &AddModToCommunity = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<AddModToCommunityResponse> for Oper<AddModToCommunity> {
match CommunityModerator::join(&conn, &community_moderator_form) { match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { Err(_e) => {
return Err(APIError::err(self.op, "Community moderator already exists."))? return Err(APIError::err(&self.op, "Community moderator already exists."))?
} }
}; };
} else { } else {
match CommunityModerator::leave(&conn, &community_moderator_form) { match CommunityModerator::leave(&conn, &community_moderator_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { Err(_e) => {
return Err(APIError::err(self.op, "Community moderator already exists."))? return Err(APIError::err(&self.op, "Community moderator already exists."))?
} }
}; };
} }

View file

@ -28,12 +28,12 @@ pub enum UserOperation {
#[derive(Fail, Debug)] #[derive(Fail, Debug)]
#[fail(display = "{{\"op\":\"{}\", \"error\":\"{}\"}}", op, message)] #[fail(display = "{{\"op\":\"{}\", \"error\":\"{}\"}}", op, message)]
pub struct APIError { pub struct APIError {
op: String, pub op: String,
message: String, pub message: String,
} }
impl APIError { impl APIError {
pub fn err(op: UserOperation, msg: &str) -> Self { pub fn err(op: &UserOperation, msg: &str) -> Self {
APIError { APIError {
op: op.to_string(), op: op.to_string(),
message: msg.to_string(), message: msg.to_string(),

View file

@ -87,32 +87,32 @@ pub struct SavePost {
impl Perform<PostResponse> for Oper<CreatePost> { impl Perform<PostResponse> for Oper<CreatePost> {
fn perform(&self) -> Result<PostResponse, Error> { fn perform(&self) -> Result<PostResponse, Error> {
let data: CreatePost = self.data; let data: &CreatePost = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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) || if has_slurs(&data.name) ||
(data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) { (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; let user_id = claims.id;
// Check for a community ban // Check for a community ban
if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() { 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 // Check for a site ban
if UserView::read(&conn, user_id)?.banned { 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 { let post_form = PostForm {
@ -130,7 +130,7 @@ impl Perform<PostResponse> for Oper<CreatePost> {
let inserted_post = match Post::create(&conn, &post_form) { let inserted_post = match Post::create(&conn, &post_form) {
Ok(post) => post, Ok(post) => post,
Err(_e) => { 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<PostResponse> for Oper<CreatePost> {
let _inserted_like = match PostLike::like(&conn, &like_form) { let _inserted_like = match PostLike::like(&conn, &like_form) {
Ok(like) => like, Ok(like) => like,
Err(_e) => { 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<PostResponse> for Oper<CreatePost> {
let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) { let post_view = match PostView::read(&conn, inserted_post.id, Some(user_id)) {
Ok(post) => post, Ok(post) => post,
Err(_e) => { 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<PostResponse> for Oper<CreatePost> {
impl Perform<GetPostResponse> for Oper<GetPost> { impl Perform<GetPostResponse> for Oper<GetPost> {
fn perform(&self) -> Result<GetPostResponse, Error> { fn perform(&self) -> Result<GetPostResponse, Error> {
let data: GetPost = self.data; let data: &GetPost = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let user_id: Option<i32> = match &data.auth { let user_id: Option<i32> = match &data.auth {
@ -187,7 +187,7 @@ impl Perform<GetPostResponse> for Oper<GetPost> {
let post_view = match PostView::read(&conn, data.id, user_id) { let post_view = match PostView::read(&conn, data.id, user_id) {
Ok(post) => post, Ok(post) => post,
Err(_e) => { 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<GetPostResponse> for Oper<GetPost> {
impl Perform<GetPostsResponse> for Oper<GetPosts> { impl Perform<GetPostsResponse> for Oper<GetPosts> {
fn perform(&self) -> Result<GetPostsResponse, Error> { fn perform(&self) -> Result<GetPostsResponse, Error> {
let data: GetPosts = self.data; let data: &GetPosts = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let user_id: Option<i32> = match &data.auth { let user_id: Option<i32> = match &data.auth {
@ -248,7 +248,7 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
data.limit) { data.limit) {
Ok(posts) => posts, Ok(posts) => posts,
Err(_e) => { 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<GetPostsResponse> for Oper<GetPosts> {
impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> { impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> {
fn perform(&self) -> Result<CreatePostLikeResponse, Error> { fn perform(&self) -> Result<CreatePostLikeResponse, Error> {
let data: CreatePostLike = self.data; let data: &CreatePostLike = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<CreatePostLikeResponse> for Oper<CreatePostLike> {
// Check for a community ban // Check for a community ban
let post = Post::read(&conn, data.post_id)?; let post = Post::read(&conn, data.post_id)?;
if CommunityUserBanView::get(&conn, user_id, post.community_id).is_ok() { 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 // Check for a site ban
if UserView::read(&conn, user_id)?.banned { 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 { let like_form = PostLikeForm {
@ -301,7 +301,7 @@ impl Perform<CreatePostLikeResponse> for Oper<CreatePostLike> {
let _inserted_like = match PostLike::like(&conn, &like_form) { let _inserted_like = match PostLike::like(&conn, &like_form) {
Ok(like) => like, Ok(like) => like,
Err(_e) => { 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<CreatePostLikeResponse> for Oper<CreatePostLike> {
let post_view = match PostView::read(&conn, data.post_id, Some(user_id)) { let post_view = match PostView::read(&conn, data.post_id, Some(user_id)) {
Ok(post) => post, Ok(post) => post,
Err(_e) => { 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<CreatePostLikeResponse> for Oper<CreatePostLike> {
impl Perform<PostResponse> for Oper<EditPost> { impl Perform<PostResponse> for Oper<EditPost> {
fn perform(&self) -> Result<PostResponse, Error> { fn perform(&self) -> Result<PostResponse, Error> {
let data: EditPost = self.data; let data: &EditPost = &self.data;
if has_slurs(&data.name) || if has_slurs(&data.name) ||
(data.body.is_some() && has_slurs(&data.body.to_owned().unwrap())) { (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(); let conn = establish_connection();
@ -336,7 +336,7 @@ impl Perform<PostResponse> for Oper<EditPost> {
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<PostResponse> for Oper<EditPost> {
.collect() .collect()
); );
if !editors.contains(&user_id) { 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 // Check for a community ban
if CommunityUserBanView::get(&conn, user_id, data.community_id).is_ok() { 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 // Check for a site ban
if UserView::read(&conn, user_id)?.banned { 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 { let post_form = PostForm {
@ -387,7 +387,7 @@ impl Perform<PostResponse> for Oper<EditPost> {
let _updated_post = match Post::update(&conn, data.edit_id, &post_form) { let _updated_post = match Post::update(&conn, data.edit_id, &post_form) {
Ok(post) => post, Ok(post) => post,
Err(_e) => { 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<PostResponse> for Oper<EditPost> {
impl Perform<PostResponse> for Oper<SavePost> { impl Perform<PostResponse> for Oper<SavePost> {
fn perform(&self) -> Result<PostResponse, Error> { fn perform(&self) -> Result<PostResponse, Error> {
let data: SavePost = self.data; let data: &SavePost = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<PostResponse> for Oper<SavePost> {
match PostSaved::save(&conn, &post_saved_form) { match PostSaved::save(&conn, &post_saved_form) {
Ok(post) => post, Ok(post) => post,
Err(_e) => { Err(_e) => {
return Err(APIError::err(self.op, "Couldnt do post save"))? return Err(APIError::err(&self.op, "Couldnt do post save"))?
} }
}; };
} else { } else {
match PostSaved::unsave(&conn, &post_saved_form) { match PostSaved::unsave(&conn, &post_saved_form) {
Ok(post) => post, Ok(post) => post,
Err(_e) => { Err(_e) => {
return Err(APIError::err(self.op, "Couldnt do post save"))? return Err(APIError::err(&self.op, "Couldnt do post save"))?
} }
}; };
} }

View file

@ -83,7 +83,7 @@ pub struct GetSiteResponse {
impl Perform<ListCategoriesResponse> for Oper<ListCategories> { impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
fn perform(&self) -> Result<ListCategoriesResponse, Error> { fn perform(&self) -> Result<ListCategoriesResponse, Error> {
let data: ListCategories = self.data; let _data: &ListCategories = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let categories: Vec<Category> = Category::list_all(&conn)?; let categories: Vec<Category> = Category::list_all(&conn)?;
@ -100,7 +100,7 @@ impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
impl Perform<GetModlogResponse> for Oper<GetModlog> { impl Perform<GetModlogResponse> for Oper<GetModlog> {
fn perform(&self) -> Result<GetModlogResponse, Error> { fn perform(&self) -> Result<GetModlogResponse, Error> {
let data: GetModlog = self.data; let data: &GetModlog = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let removed_posts = ModRemovePostView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?; let removed_posts = ModRemovePostView::list(&conn, data.community_id, data.mod_user_id, data.page, data.limit)?;
@ -139,26 +139,26 @@ impl Perform<GetModlogResponse> for Oper<GetModlog> {
impl Perform<SiteResponse> for Oper<CreateSite> { impl Perform<SiteResponse> for Oper<CreateSite> {
fn perform(&self) -> Result<SiteResponse, Error> { fn perform(&self) -> Result<SiteResponse, Error> {
let data: CreateSite = self.data; let data: &CreateSite = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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) || if has_slurs(&data.name) ||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { (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; let user_id = claims.id;
// Make sure user is an admin // Make sure user is an admin
if !UserView::read(&conn, user_id)?.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 { let site_form = SiteForm {
@ -171,7 +171,7 @@ impl Perform<SiteResponse> for Oper<CreateSite> {
match Site::create(&conn, &site_form) { match Site::create(&conn, &site_form) {
Ok(site) => site, Ok(site) => site,
Err(_e) => { 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<SiteResponse> for Oper<CreateSite> {
impl Perform<SiteResponse> for Oper<EditSite> { impl Perform<SiteResponse> for Oper<EditSite> {
fn perform(&self) -> Result<SiteResponse, Error> { fn perform(&self) -> Result<SiteResponse, Error> {
let data: EditSite = self.data; let data: &EditSite = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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) || if has_slurs(&data.name) ||
(data.description.is_some() && has_slurs(&data.description.to_owned().unwrap())) { (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; let user_id = claims.id;
// Make sure user is an admin // Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false { 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)?; let found_site = Site::read(&conn, 1)?;
@ -223,7 +223,7 @@ impl Perform<SiteResponse> for Oper<EditSite> {
match Site::update(&conn, 1, &site_form) { match Site::update(&conn, 1, &site_form) {
Ok(site) => site, Ok(site) => site,
Err(_e) => { 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<SiteResponse> for Oper<EditSite> {
impl Perform<GetSiteResponse> for Oper<GetSite> { impl Perform<GetSiteResponse> for Oper<GetSite> {
fn perform(&self) -> Result<GetSiteResponse, Error> { fn perform(&self) -> Result<GetSiteResponse, Error> {
let data: GetSite = self.data; let _data: &GetSite = &self.data;
let conn = establish_connection(); let conn = establish_connection();
// It can return a null site in order to redirect // It can return a null site in order to redirect
@ -265,7 +265,7 @@ impl Perform<GetSiteResponse> for Oper<GetSite> {
impl Perform<SearchResponse> for Oper<Search> { impl Perform<SearchResponse> for Oper<Search> {
fn perform(&self) -> Result<SearchResponse, Error> { fn perform(&self) -> Result<SearchResponse, Error> {
let data: Search = self.data; let data: &Search = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let sort = SortType::from_str(&data.sort)?; let sort = SortType::from_str(&data.sort)?;

View file

@ -97,19 +97,19 @@ pub struct GetReplies {
impl Perform<LoginResponse> for Oper<Login> { impl Perform<LoginResponse> for Oper<Login> {
fn perform(&self) -> Result<LoginResponse, Error> { fn perform(&self) -> Result<LoginResponse, Error> {
let data: Login = self.data; let data: &Login = &self.data;
let conn = establish_connection(); let conn = establish_connection();
// Fetch that username / email // Fetch that username / email
let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) { let user: User_ = match User_::find_by_email_or_username(&conn, &data.username_or_email) {
Ok(user) => user, 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 // Verify the password
let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false); let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
if !valid { if !valid {
return Err(APIError::err(self.op, "Password incorrect"))? return Err(APIError::err(&self.op, "Password incorrect"))?
} }
// Return the jwt // Return the jwt
@ -125,25 +125,25 @@ impl Perform<LoginResponse> for Oper<Login> {
impl Perform<LoginResponse> for Oper<Register> { impl Perform<LoginResponse> for Oper<Register> {
fn perform(&self) -> Result<LoginResponse, Error> { fn perform(&self) -> Result<LoginResponse, Error> {
let data: Register = self.data; let data: &Register = &self.data;
let conn = establish_connection(); let conn = establish_connection();
// Make sure passwords match // Make sure passwords match
if &data.password != &data.password_verify { 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 { 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) { 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 // Make sure there are no admins
if data.admin && UserView::admins(&conn)?.len() > 0 { 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 // Register the new user
@ -162,7 +162,7 @@ impl Perform<LoginResponse> for Oper<Register> {
let inserted_user = match User_::register(&conn, &user_form) { let inserted_user = match User_::register(&conn, &user_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<LoginResponse> for Oper<Register> {
let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) { let _inserted_community_follower = match CommunityFollower::follow(&conn, &community_follower_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<LoginResponse> for Oper<Register> {
let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) { let _inserted_community_moderator = match CommunityModerator::join(&conn, &community_moderator_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<LoginResponse> for Oper<Register> {
impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> { impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
fn perform(&self) -> Result<GetUserDetailsResponse, Error> { fn perform(&self) -> Result<GetUserDetailsResponse, Error> {
let data: GetUserDetails = self.data; let data: &GetUserDetails = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let user_id: Option<i32> = match &data.auth { let user_id: Option<i32> = match &data.auth {
@ -302,13 +302,13 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> {
impl Perform<AddAdminResponse> for Oper<AddAdmin> { impl Perform<AddAdminResponse> for Oper<AddAdmin> {
fn perform(&self) -> Result<AddAdminResponse, Error> { fn perform(&self) -> Result<AddAdminResponse, Error> {
let data: AddAdmin = self.data; let data: &AddAdmin = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<AddAdminResponse> for Oper<AddAdmin> {
// Make sure user is an admin // Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false { 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)?; let read_user = User_::read(&conn, data.user_id)?;
@ -335,7 +335,7 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> {
match User_::update(&conn, data.user_id, &user_form) { match User_::update(&conn, data.user_id, &user_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<AddAdminResponse> for Oper<AddAdmin> {
impl Perform<BanUserResponse> for Oper<BanUser> { impl Perform<BanUserResponse> for Oper<BanUser> {
fn perform(&self) -> Result<BanUserResponse, Error> { fn perform(&self) -> Result<BanUserResponse, Error> {
let data: BanUser = self.data; let data: &BanUser = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<BanUserResponse> for Oper<BanUser> {
// Make sure user is an admin // Make sure user is an admin
if UserView::read(&conn, user_id)?.admin == false { 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)?; let read_user = User_::read(&conn, data.user_id)?;
@ -394,7 +394,7 @@ impl Perform<BanUserResponse> for Oper<BanUser> {
match User_::update(&conn, data.user_id, &user_form) { match User_::update(&conn, data.user_id, &user_form) {
Ok(user) => user, Ok(user) => user,
Err(_e) => { 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<BanUserResponse> for Oper<BanUser> {
impl Perform<GetRepliesResponse> for Oper<GetReplies> { impl Perform<GetRepliesResponse> for Oper<GetReplies> {
fn perform(&self) -> Result<GetRepliesResponse, Error> { fn perform(&self) -> Result<GetRepliesResponse, Error> {
let data: GetReplies = self.data; let data: &GetReplies = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<GetRepliesResponse> for Oper<GetReplies> {
impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> { impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> {
fn perform(&self) -> Result<GetRepliesResponse, Error> { fn perform(&self) -> Result<GetRepliesResponse, Error> {
let data: MarkAllAsRead = self.data; let data: &MarkAllAsRead = &self.data;
let conn = establish_connection(); let conn = establish_connection();
let claims = match Claims::decode(&data.auth) { let claims = match Claims::decode(&data.auth) {
Ok(claims) => claims.claims, Ok(claims) => claims.claims,
Err(_e) => { 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<GetRepliesResponse> for Oper<MarkAllAsRead> {
let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) { let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) {
Ok(comment) => comment, Ok(comment) => comment,
Err(_e) => { Err(_e) => {
return Err(APIError::err(self.op, "Couldn't update Comment"))? return Err(APIError::err(&self.op, "Couldn't update Comment"))?
} }
}; };
} }

View file

@ -47,7 +47,6 @@ impl Category {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use establish_connection;
use super::*; use super::*;
#[test] #[test]
fn test_crud() { fn test_crud() {

View file

@ -154,12 +154,10 @@ impl Saveable <CommentSavedForm> for CommentSaved {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use establish_connection;
use super::*; use super::*;
use super::super::post::*; use super::super::post::*;
use super::super::community::*; use super::super::community::*;
use super::super::user::*; use super::super::user::*;
use Crud;
#[test] #[test]
fn test_crud() { fn test_crud() {
let conn = establish_connection(); let conn = establish_connection();

View file

@ -244,13 +244,11 @@ impl ReplyView {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use establish_connection;
use super::*; use super::*;
use super::super::post::*; use super::super::post::*;
use super::super::community::*; use super::super::community::*;
use super::super::user::*; use super::super::user::*;
use super::super::comment::*; use super::super::comment::*;
use {Crud,Likeable};
#[test] #[test]
fn test_crud() { fn test_crud() {
let conn = establish_connection(); let conn = establish_connection();

View file

@ -215,10 +215,8 @@ impl Crud<SiteForm> for Site {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use establish_connection;
use super::*; use super::*;
use super::super::user::*; use super::super::user::*;
use Crud;
#[test] #[test]
fn test_crud() { fn test_crud() {
let conn = establish_connection(); let conn = establish_connection();

View file

@ -81,3 +81,12 @@ pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
let offset = limit * (page - 1); let offset = limit * (page - 1);
(limit, offset) (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());
}
}

View file

@ -394,7 +394,6 @@ impl Crud<ModAddForm> for ModAdd {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use establish_connection;
use super::*; use super::*;
use super::super::user::*; use super::super::user::*;
use super::super::post::*; use super::super::post::*;

View file

@ -168,8 +168,6 @@ impl Readable <PostReadForm> for PostRead {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use establish_connection;
use Crud;
use super::*; use super::*;
use super::super::community::*; use super::super::community::*;
use super::super::user::*; use super::super::user::*;

View file

@ -174,7 +174,6 @@ impl PostView {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use {establish_connection, Crud, Likeable};
use super::*; use super::*;
use super::super::community::*; use super::super::community::*;
use super::super::user::*; use super::super::user::*;

View file

@ -120,9 +120,7 @@ impl User_ {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use establish_connection; use super::*;
use super::{User_, UserForm};
use Crud;
#[test] #[test]
fn test_crud() { fn test_crud() {
let conn = establish_connection(); let conn = establish_connection();

View file

@ -72,7 +72,7 @@ pub fn has_slurs(test: &str) -> bool {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use {Settings, is_email_regex, remove_slurs, has_slurs, fuzzy_search}; use {Settings, is_email_regex, remove_slurs, has_slurs};
#[test] #[test]
fn test_api() { fn test_api() {
assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1"); assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1");
@ -91,10 +91,6 @@ mod tests {
assert!(!has_slurs(slur_free)); 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! { lazy_static! {

View file

@ -7,7 +7,7 @@ use lemmy_server::actix::*;
use lemmy_server::actix_web::server::HttpServer; use lemmy_server::actix_web::server::HttpServer;
use lemmy_server::actix_web::{ws, App, Error, HttpRequest, HttpResponse, fs::NamedFile, fs}; use lemmy_server::actix_web::{ws, App, Error, HttpRequest, HttpResponse, fs::NamedFile, fs};
use lemmy_server::websocket::server::*; use lemmy_server::websocket::server::*;
use lemmy_server::establish_connection; use lemmy_server::db::establish_connection;
embed_migrations!(); embed_migrations!();
@ -117,6 +117,7 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for WSSession {
} }
ws::Message::Text(text) => { ws::Message::Text(text) => {
let m = text.trim().to_owned(); let m = text.trim().to_owned();
println!("WEBSOCKET MESSAGE: {:?} from id: {}", &m, self.id);
ctx.state() ctx.state()
.addr .addr
@ -130,79 +131,11 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for WSSession {
Ok(res) => ctx.text(res), Ok(res) => ctx.text(res),
Err(e) => { Err(e) => {
eprintln!("{}", &e); eprintln!("{}", &e);
// ctx.text(e);
} }
} }
// Ok(res) => ctx.text(res),
// // something is wrong with chat server
// _ => ctx.stop(),
fut::ok(()) fut::ok(())
}) })
.wait(ctx); .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::Binary(_bin) => println!("Unexpected binary"),
ws::Message::Close(_) => { ws::Message::Close(_) => {

View file

@ -104,8 +104,8 @@ impl Default for ChatServer {
impl ChatServer { impl ChatServer {
/// Send message to all users in the room /// Send message to all users in the room
fn send_room_message(&self, room: i32, message: &str, skip_id: usize) { fn send_room_message(&self, room: &i32, message: &str, skip_id: usize) {
if let Some(sessions) = self.rooms.get(&room) { if let Some(sessions) = self.rooms.get(room) {
for id in sessions { for id in sessions {
if *id != skip_id { if *id != skip_id {
if let Some(info) = self.sessions.get(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 // remove session from all rooms
for (_n, sessions) in &mut self.rooms { for (_n, mut sessions) in &mut self.rooms {
sessions.remove(&id); sessions.remove(&id);
} }
@ -127,17 +127,17 @@ impl ChatServer {
self.rooms.insert(room_id, HashSet::new()); 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::*;
use db::post_view::*; use db::post_view::*;
let conn = establish_connection(); let conn = establish_connection();
let posts = PostView::list(&conn, let posts = PostView::list(&conn,
PostListingType::Community, PostListingType::Community,
&SortType::New, &SortType::New,
Some(community_id), Some(*community_id),
None, None,
None, None,
None, None,
@ -146,7 +146,7 @@ impl ChatServer {
None, None,
Some(9999))?; Some(9999))?;
for post in posts { for post in posts {
self.send_room_message(post.id, message, skip_id); self.send_room_message(&post.id, message, skip_id);
} }
Ok(()) Ok(())
@ -346,7 +346,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
community_sent.community.user_id = None; community_sent.community.user_id = None;
community_sent.community.subscribed = None; community_sent.community.subscribed = None;
let community_sent_str = serde_json::to_string(&community_sent)?; let community_sent_str = serde_json::to_string(&community_sent)?;
chat.send_community_message(edit_community.edit_id, &community_sent_str, msg.id)?; chat.send_community_message(&community_sent.community.id, &community_sent_str, msg.id)?;
Ok(serde_json::to_string(&res)?) Ok(serde_json::to_string(&res)?)
}, },
UserOperation::FollowCommunity => { UserOperation::FollowCommunity => {
@ -361,16 +361,18 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
}, },
UserOperation::BanFromCommunity => { UserOperation::BanFromCommunity => {
let ban_from_community: BanFromCommunity = serde_json::from_str(data)?; 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 = Oper::new(user_operation, ban_from_community).perform()?;
let res_str = serde_json::to_string(&res)?; 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) Ok(res_str)
}, },
UserOperation::AddModToCommunity => { UserOperation::AddModToCommunity => {
let mod_add_to_community: AddModToCommunity = serde_json::from_str(data)?; 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 = Oper::new(user_operation, mod_add_to_community).perform()?;
let res_str = serde_json::to_string(&res)?; 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) Ok(res_str)
}, },
UserOperation::ListCategories => { UserOperation::ListCategories => {
@ -407,7 +409,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
let mut post_sent = res.clone(); let mut post_sent = res.clone();
post_sent.post.my_vote = None; post_sent.post.my_vote = None;
let post_sent_str = serde_json::to_string(&post_sent)?; let post_sent_str = serde_json::to_string(&post_sent)?;
chat.send_room_message(edit_post.edit_id, &post_sent_str, msg.id); chat.send_room_message(&post_sent.post.id, &post_sent_str, msg.id);
Ok(serde_json::to_string(&res)?) Ok(serde_json::to_string(&res)?)
}, },
UserOperation::SavePost => { UserOperation::SavePost => {
@ -418,22 +420,24 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
UserOperation::CreateComment => { UserOperation::CreateComment => {
chat.check_rate_limit(msg.id)?; chat.check_rate_limit(msg.id)?;
let create_comment: CreateComment = serde_json::from_str(data)?; 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 res = Oper::new(user_operation, create_comment).perform()?;
let mut comment_sent = res.clone(); let mut comment_sent = res.clone();
comment_sent.comment.my_vote = None; comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None; comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?; 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)?) Ok(serde_json::to_string(&res)?)
}, },
UserOperation::EditComment => { UserOperation::EditComment => {
let edit_comment: EditComment = serde_json::from_str(data)?; 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 res = Oper::new(user_operation, edit_comment).perform()?;
let mut comment_sent = res.clone(); let mut comment_sent = res.clone();
comment_sent.comment.my_vote = None; comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None; comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?; 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)?) Ok(serde_json::to_string(&res)?)
}, },
UserOperation::SaveComment => { UserOperation::SaveComment => {
@ -444,12 +448,13 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
UserOperation::CreateCommentLike => { UserOperation::CreateCommentLike => {
chat.check_rate_limit(msg.id)?; chat.check_rate_limit(msg.id)?;
let create_comment_like: CreateCommentLike = serde_json::from_str(data)?; 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 res = Oper::new(user_operation, create_comment_like).perform()?;
let mut comment_sent = res.clone(); let mut comment_sent = res.clone();
comment_sent.comment.my_vote = None; comment_sent.comment.my_vote = None;
comment_sent.comment.user_id = None; comment_sent.comment.user_id = None;
let comment_sent_str = serde_json::to_string(&comment_sent)?; 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)?) Ok(serde_json::to_string(&res)?)
}, },
UserOperation::GetModlog => { UserOperation::GetModlog => {