diff options
author | Dessalines <tyhou13@gmx.com> | 2020-01-23 19:17:42 -0500 |
---|---|---|
committer | Dessalines <tyhou13@gmx.com> | 2020-01-23 19:17:42 -0500 |
commit | ac1d5f2b86b0816cc9b5794dc6c38a38365ed839 (patch) | |
tree | bedaab04ca797113990e5702cad36f5e34e9e6e8 /server/src/api/user.rs | |
parent | 39d1fb4450cb418d9c11b31ea99484c2a177fab6 (diff) | |
parent | b45c83682c187737b092080e83ba741c39cfa695 (diff) |
Done merging http-api and private_message
Diffstat (limited to 'server/src/api/user.rs')
-rw-r--r-- | server/src/api/user.rs | 164 |
1 files changed, 58 insertions, 106 deletions
diff --git a/server/src/api/user.rs b/server/src/api/user.rs index 046da6fb..8d2db104 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -41,7 +41,6 @@ pub struct SaveUserSettings { #[derive(Serialize, Deserialize)] pub struct LoginResponse { - op: String, jwt: String, } @@ -59,7 +58,6 @@ pub struct GetUserDetails { #[derive(Serialize, Deserialize)] pub struct GetUserDetailsResponse { - op: String, user: UserView, follows: Vec<CommunityFollowerView>, moderates: Vec<CommunityModeratorView>, @@ -70,13 +68,11 @@ pub struct GetUserDetailsResponse { #[derive(Serialize, Deserialize)] pub struct GetRepliesResponse { - op: String, replies: Vec<ReplyView>, } #[derive(Serialize, Deserialize)] pub struct GetUserMentionsResponse { - op: String, mentions: Vec<UserMentionView>, } @@ -94,7 +90,6 @@ pub struct AddAdmin { #[derive(Serialize, Deserialize)] pub struct AddAdminResponse { - op: String, admins: Vec<UserView>, } @@ -109,7 +104,6 @@ pub struct BanUser { #[derive(Serialize, Deserialize)] pub struct BanUserResponse { - op: String, user: UserView, banned: bool, } @@ -141,7 +135,6 @@ pub struct EditUserMention { #[derive(Serialize, Deserialize, Clone)] pub struct UserMentionResponse { - op: String, mention: UserMentionView, } @@ -157,9 +150,7 @@ pub struct PasswordReset { } #[derive(Serialize, Deserialize, Clone)] -pub struct PasswordResetResponse { - op: String, -} +pub struct PasswordResetResponse {} #[derive(Serialize, Deserialize)] pub struct PasswordChange { @@ -194,13 +185,11 @@ pub struct GetPrivateMessages { #[derive(Serialize, Deserialize, Clone)] pub struct PrivateMessagesResponse { - op: String, messages: Vec<PrivateMessageView>, } #[derive(Serialize, Deserialize, Clone)] pub struct PrivateMessageResponse { - op: String, message: PrivateMessageView, } @@ -211,20 +200,17 @@ impl Perform<LoginResponse> for Oper<Login> { // 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, "couldnt_find_that_username_or_email").into()), + Err(_e) => return Err(APIError::err("couldnt_find_that_username_or_email").into()), }; // 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").into()); + return Err(APIError::err("password_incorrect").into()); } // Return the jwt - Ok(LoginResponse { - op: self.op.to_string(), - jwt: user.jwt(), - }) + Ok(LoginResponse { jwt: user.jwt() }) } } @@ -235,22 +221,22 @@ impl Perform<LoginResponse> for Oper<Register> { // Make sure site has open registration if let Ok(site) = SiteView::read(&conn) { if !site.open_registration { - return Err(APIError::err(&self.op, "registration_closed").into()); + return Err(APIError::err("registration_closed").into()); } } // Make sure passwords match if data.password != data.password_verify { - return Err(APIError::err(&self.op, "passwords_dont_match").into()); + return Err(APIError::err("passwords_dont_match").into()); } if has_slurs(&data.username) { - return Err(APIError::err(&self.op, "no_slurs").into()); + return Err(APIError::err("no_slurs").into()); } // Make sure there are no admins if data.admin && !UserView::admins(&conn)?.is_empty() { - return Err(APIError::err(&self.op, "admin_already_created").into()); + return Err(APIError::err("admin_already_created").into()); } // Register the new user @@ -286,7 +272,7 @@ impl Perform<LoginResponse> for Oper<Register> { "user_already_exists" }; - return Err(APIError::err(&self.op, err_type).into()); + return Err(APIError::err(err_type).into()); } }; @@ -318,7 +304,7 @@ impl Perform<LoginResponse> for Oper<Register> { 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").into()), + Err(_e) => return Err(APIError::err("community_follower_already_exists").into()), }; // If its an admin, add them as a mod and follower to main @@ -331,15 +317,12 @@ impl Perform<LoginResponse> for Oper<Register> { 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").into()) - } + Err(_e) => return Err(APIError::err("community_moderator_already_exists").into()), }; } // Return the jwt Ok(LoginResponse { - op: self.op.to_string(), jwt: inserted_user.jwt(), }) } @@ -351,7 +334,7 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -369,7 +352,7 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> { Some(new_password_verify) => { // Make sure passwords match if new_password != new_password_verify { - return Err(APIError::err(&self.op, "passwords_dont_match").into()); + return Err(APIError::err("passwords_dont_match").into()); } // Check the old password @@ -378,14 +361,14 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> { let valid: bool = verify(old_password, &read_user.password_encrypted).unwrap_or(false); if !valid { - return Err(APIError::err(&self.op, "password_incorrect").into()); + return Err(APIError::err("password_incorrect").into()); } User_::update_password(&conn, user_id, &new_password)?.password_encrypted } - None => return Err(APIError::err(&self.op, "password_incorrect").into()), + None => return Err(APIError::err("password_incorrect").into()), } } - None => return Err(APIError::err(&self.op, "passwords_dont_match").into()), + None => return Err(APIError::err("passwords_dont_match").into()), } } None => read_user.password_encrypted, @@ -422,13 +405,12 @@ impl Perform<LoginResponse> for Oper<SaveUserSettings> { "user_already_exists" }; - return Err(APIError::err(&self.op, err_type).into()); + return Err(APIError::err(err_type).into()); } }; // Return the jwt Ok(LoginResponse { - op: self.op.to_string(), jwt: updated_user.jwt(), }) } @@ -469,9 +451,7 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> { .unwrap_or_else(|| "admin".to_string()), ) { Ok(user) => user.id, - Err(_e) => { - return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email").into()) - } + Err(_e) => return Err(APIError::err("couldnt_find_that_username_or_email").into()), } } }; @@ -514,7 +494,6 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> { // Return the jwt Ok(GetUserDetailsResponse { - op: self.op.to_string(), user: user_view, follows, moderates, @@ -531,14 +510,14 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; 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").into()); + return Err(APIError::err("not_an_admin").into()); } let read_user = User_::read(&conn, data.user_id)?; @@ -566,7 +545,7 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> { match User_::update(&conn, data.user_id, &user_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()), + Err(_e) => return Err(APIError::err("couldnt_update_user").into()), }; // Mod tables @@ -584,10 +563,7 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> { let creator_user = admins.remove(creator_index); admins.insert(0, creator_user); - Ok(AddAdminResponse { - op: self.op.to_string(), - admins, - }) + Ok(AddAdminResponse { admins }) } } @@ -597,14 +573,14 @@ impl Perform<BanUserResponse> for Oper<BanUser> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; 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").into()); + return Err(APIError::err("not_an_admin").into()); } let read_user = User_::read(&conn, data.user_id)?; @@ -632,7 +608,7 @@ impl Perform<BanUserResponse> for Oper<BanUser> { match User_::update(&conn, data.user_id, &user_form) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()), + Err(_e) => return Err(APIError::err("couldnt_update_user").into()), }; // Mod tables @@ -654,7 +630,6 @@ impl Perform<BanUserResponse> for Oper<BanUser> { let user_view = UserView::read(&conn, data.user_id)?; Ok(BanUserResponse { - op: self.op.to_string(), user: user_view, banned: data.ban, }) @@ -667,7 +642,7 @@ impl Perform<GetRepliesResponse> for Oper<GetReplies> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -681,10 +656,7 @@ impl Perform<GetRepliesResponse> for Oper<GetReplies> { .limit(data.limit) .list()?; - Ok(GetRepliesResponse { - op: self.op.to_string(), - replies, - }) + Ok(GetRepliesResponse { replies }) } } @@ -694,7 +666,7 @@ impl Perform<GetUserMentionsResponse> for Oper<GetUserMentions> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -708,10 +680,7 @@ impl Perform<GetUserMentionsResponse> for Oper<GetUserMentions> { .limit(data.limit) .list()?; - Ok(GetUserMentionsResponse { - op: self.op.to_string(), - mentions, - }) + Ok(GetUserMentionsResponse { mentions }) } } @@ -721,7 +690,7 @@ impl Perform<UserMentionResponse> for Oper<EditUserMention> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -737,13 +706,12 @@ impl Perform<UserMentionResponse> for Oper<EditUserMention> { let _updated_user_mention = match UserMention::update(&conn, user_mention.id, &user_mention_form) { Ok(comment) => comment, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()), + Err(_e) => return Err(APIError::err("couldnt_update_comment").into()), }; let user_mention_view = UserMentionView::read(&conn, user_mention.id, user_id)?; Ok(UserMentionResponse { - op: self.op.to_string(), mention: user_mention_view, }) } @@ -755,7 +723,7 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -780,7 +748,7 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> { let _updated_comment = match Comment::update(&conn, reply.id, &comment_form) { Ok(comment) => comment, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()), + Err(_e) => return Err(APIError::err("couldnt_update_comment").into()), }; } @@ -801,7 +769,7 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> { let _updated_mention = match UserMention::update(&conn, mention.user_mention_id, &mention_form) { Ok(mention) => mention, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()), + Err(_e) => return Err(APIError::err("couldnt_update_comment").into()), }; } @@ -825,14 +793,11 @@ impl Perform<GetRepliesResponse> for Oper<MarkAllAsRead> { let _updated_message = match PrivateMessage::update(&conn, message.id, &private_message_form) { Ok(message) => message, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_private_message").into()), + Err(_e) => return Err(APIError::err("couldnt_update_private_message").into()), }; } - Ok(GetRepliesResponse { - op: self.op.to_string(), - replies: vec![], - }) + Ok(GetRepliesResponse { replies: vec![] }) } } @@ -842,7 +807,7 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -852,7 +817,7 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> { // 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").into()); + return Err(APIError::err("password_incorrect").into()); } // Comments @@ -875,7 +840,7 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> { let _updated_comment = match Comment::update(&conn, comment.id, &comment_form) { Ok(comment) => comment, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_comment").into()), + Err(_e) => return Err(APIError::err("couldnt_update_comment").into()), }; } @@ -903,12 +868,11 @@ impl Perform<LoginResponse> for Oper<DeleteAccount> { let _updated_post = match Post::update(&conn, post.id, &post_form) { Ok(post) => post, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_post").into()), + Err(_e) => return Err(APIError::err("couldnt_update_post").into()), }; } Ok(LoginResponse { - op: self.op.to_string(), jwt: data.auth.to_owned(), }) } @@ -921,7 +885,7 @@ impl Perform<PasswordResetResponse> for Oper<PasswordReset> { // Fetch that email let user: User_ = match User_::find_by_email(&conn, &data.email) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_find_that_username_or_email").into()), + Err(_e) => return Err(APIError::err("couldnt_find_that_username_or_email").into()), }; // Generate a random token @@ -938,12 +902,10 @@ impl Perform<PasswordResetResponse> for Oper<PasswordReset> { let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", user.name, hostname, &token); match send_email(subject, user_email, &user.name, html) { Ok(_o) => _o, - Err(_e) => return Err(APIError::err(&self.op, &_e).into()), + Err(_e) => return Err(APIError::err(&_e).into()), }; - Ok(PasswordResetResponse { - op: self.op.to_string(), - }) + Ok(PasswordResetResponse {}) } } @@ -956,18 +918,17 @@ impl Perform<LoginResponse> for Oper<PasswordChange> { // Make sure passwords match if data.password != data.password_verify { - return Err(APIError::err(&self.op, "passwords_dont_match").into()); + return Err(APIError::err("passwords_dont_match").into()); } // Update the user with the new password let updated_user = match User_::update_password(&conn, user_id, &data.password) { Ok(user) => user, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_user").into()), + Err(_e) => return Err(APIError::err("couldnt_update_user").into()), }; // Return the jwt Ok(LoginResponse { - op: self.op.to_string(), jwt: updated_user.jwt(), }) } @@ -979,7 +940,7 @@ impl Perform<PrivateMessageResponse> for Oper<CreatePrivateMessage> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -988,7 +949,7 @@ impl Perform<PrivateMessageResponse> for Oper<CreatePrivateMessage> { // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(&self.op, "site_ban").into()); + return Err(APIError::err("site_ban").into()); } let content_slurs_removed = remove_slurs(&data.content.to_owned()); @@ -1005,7 +966,7 @@ impl Perform<PrivateMessageResponse> for Oper<CreatePrivateMessage> { let inserted_private_message = match PrivateMessage::create(&conn, &private_message_form) { Ok(private_message) => private_message, Err(_e) => { - return Err(APIError::err(&self.op, "couldnt_create_private_message").into()); + return Err(APIError::err("couldnt_create_private_message").into()); } }; @@ -1029,12 +990,9 @@ impl Perform<PrivateMessageResponse> for Oper<CreatePrivateMessage> { } } - let private_message_view = PrivateMessageView::read(&conn, inserted_private_message.id)?; + let message = PrivateMessageView::read(&conn, inserted_private_message.id)?; - Ok(PrivateMessageResponse { - op: self.op.to_string(), - message: private_message_view, - }) + Ok(PrivateMessageResponse { message }) } } @@ -1044,7 +1002,7 @@ impl Perform<PrivateMessageResponse> for Oper<EditPrivateMessage> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -1053,14 +1011,14 @@ impl Perform<PrivateMessageResponse> for Oper<EditPrivateMessage> { // Check for a site ban if UserView::read(&conn, user_id)?.banned { - return Err(APIError::err(&self.op, "site_ban").into()); + return Err(APIError::err("site_ban").into()); } // Check to make sure they are the creator (or the recipient marking as read if !(data.read.is_some() && orig_private_message.recipient_id.eq(&user_id) || orig_private_message.creator_id.eq(&user_id)) { - return Err(APIError::err(&self.op, "no_private_message_edit_allowed").into()); + return Err(APIError::err("no_private_message_edit_allowed").into()); } let content_slurs_removed = match &data.content { @@ -1084,15 +1042,12 @@ impl Perform<PrivateMessageResponse> for Oper<EditPrivateMessage> { let _updated_private_message = match PrivateMessage::update(&conn, data.edit_id, &private_message_form) { Ok(private_message) => private_message, - Err(_e) => return Err(APIError::err(&self.op, "couldnt_update_private_message").into()), + Err(_e) => return Err(APIError::err("couldnt_update_private_message").into()), }; - let private_message_view = PrivateMessageView::read(&conn, data.edit_id)?; + let message = PrivateMessageView::read(&conn, data.edit_id)?; - Ok(PrivateMessageResponse { - op: self.op.to_string(), - message: private_message_view, - }) + Ok(PrivateMessageResponse { message }) } } @@ -1102,7 +1057,7 @@ impl Perform<PrivateMessagesResponse> for Oper<GetPrivateMessages> { let claims = match Claims::decode(&data.auth) { Ok(claims) => claims.claims, - Err(_e) => return Err(APIError::err(&self.op, "not_logged_in").into()), + Err(_e) => return Err(APIError::err("not_logged_in").into()), }; let user_id = claims.id; @@ -1113,9 +1068,6 @@ impl Perform<PrivateMessagesResponse> for Oper<GetPrivateMessages> { .unread_only(data.unread_only) .list()?; - Ok(PrivateMessagesResponse { - op: self.op.to_string(), - messages, - }) + Ok(PrivateMessagesResponse { messages }) } } |