Use .map_err in api code (fixes #1573) (#1575)

* Use .map_err in api code (fixes #1573)

* forgot some
pull/1583/head
Nutomic 3 years ago committed by GitHub
parent 03c18ecfbe
commit 65a11a7239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -46,14 +46,11 @@ impl Perform for MarkCommentAsRead {
// Do the mark as read
let read = data.read;
match blocking(context.pool(), move |conn| {
blocking(context.pool(), move |conn| {
Comment::update_read(conn, comment_id, read)
})
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
};
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
// Refetch it
let comment_id = data.comment_id;

@ -59,14 +59,11 @@ impl Perform for CreateCommentReport {
reason: data.reason.to_owned(),
};
let report = match blocking(context.pool(), move |conn| {
let report = blocking(context.pool(), move |conn| {
CommentReport::report(conn, &report_form)
})
.await?
{
Ok(report) => report,
Err(_e) => return Err(ApiError::err("couldnt_create_report").into()),
};
.map_err(|_| ApiError::err("couldnt_create_report"))?;
let res = CreateCommentReportResponse { success: true };

@ -414,24 +414,18 @@ impl Perform for TransferCommunity {
let community_id = data.community_id;
let person_id = local_user_view.person.id;
let community_view = match blocking(context.pool(), move |conn| {
let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(person_id))
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
};
.map_err(|_| ApiError::err("couldnt_find_community"))?;
let community_id = data.community_id;
let moderators = match blocking(context.pool(), move |conn| {
let moderators = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
})
.await?
{
Ok(moderators) => moderators,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
};
.map_err(|_| ApiError::err("couldnt_find_community"))?;
// Return the jwt
Ok(GetCommunityResponse {

@ -83,14 +83,11 @@ impl Perform for Login {
// Fetch that username / email
let username_or_email = data.username_or_email.clone();
let local_user_view = match blocking(context.pool(), move |conn| {
let local_user_view = blocking(context.pool(), move |conn| {
LocalUserView::find_by_email_or_name(conn, &username_or_email)
})
.await?
{
Ok(uv) => uv,
Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
};
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?;
// Verify the password
let valid: bool = verify(
@ -629,14 +626,11 @@ impl Perform for PasswordReset {
// Fetch that email
let email = data.email.clone();
let local_user_view = match blocking(context.pool(), move |conn| {
let local_user_view = blocking(context.pool(), move |conn| {
LocalUserView::find_by_email(conn, &email)
})
.await?
{
Ok(lu) => lu,
Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
};
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?;
// Generate a random token
let token = generate_random_string();
@ -655,10 +649,8 @@ impl Perform for PasswordReset {
let subject = &format!("Password reset for {}", local_user_view.person.name);
let hostname = &Settings::get().get_protocol_and_hostname();
let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", local_user_view.person.name, hostname, &token);
match send_email(subject, email, &local_user_view.person.name, html) {
Ok(_o) => _o,
Err(_e) => return Err(ApiError::err(&_e).into()),
};
send_email(subject, email, &local_user_view.person.name, html)
.map_err(|e| ApiError::err(&e))?;
Ok(PasswordResetResponse {})
}
@ -691,14 +683,11 @@ impl Perform for PasswordChange {
// Update the user with the new password
let password = data.password.clone();
let updated_local_user = match blocking(context.pool(), move |conn| {
let updated_local_user = blocking(context.pool(), move |conn| {
LocalUser::update_password(conn, local_user_id, &password)
})
.await?
{
Ok(u) => u,
Err(_e) => return Err(ApiError::err("couldnt_update_user").into()),
};
.map_err(|_| ApiError::err("couldnt_update_user"))?;
// Return the jwt
Ok(LoginResponse {
@ -776,14 +765,11 @@ impl Perform for GetFollowedCommunities {
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let person_id = local_user_view.person.id;
let communities = match blocking(context.pool(), move |conn| {
let communities = blocking(context.pool(), move |conn| {
CommunityFollowerView::for_person(conn, person_id)
})
.await?
{
Ok(communities) => communities,
_ => return Err(ApiError::err("system_err_login").into()),
};
.map_err(|_| ApiError::err("system_err_login"))?;
// Return the jwt
Ok(GetFollowedCommunitiesResponse { communities })

@ -71,14 +71,11 @@ impl Perform for CreatePostLike {
let post_id = data.post_id;
let person_id = local_user_view.person.id;
let post_view = match blocking(context.pool(), move |conn| {
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(person_id))
})
.await?
{
Ok(post) => post,
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
};
.map_err(|_| ApiError::err("couldnt_find_post"))?;
let res = PostResponse { post_view };

@ -68,14 +68,11 @@ impl Perform for CreatePostReport {
reason: data.reason.to_owned(),
};
let report = match blocking(context.pool(), move |conn| {
let report = blocking(context.pool(), move |conn| {
PostReport::report(conn, &report_form)
})
.await?
{
Ok(report) => report,
Err(_e) => return Err(ApiError::err("couldnt_create_report").into()),
};
.map_err(|_| ApiError::err("couldnt_create_report"))?;
let res = CreatePostReportResponse { success: true };

@ -36,14 +36,11 @@ impl Perform for MarkPrivateMessageAsRead {
// Doing the update
let private_message_id = data.private_message_id;
let read = data.read;
match blocking(context.pool(), move |conn| {
blocking(context.pool(), move |conn| {
PrivateMessage::update_read(conn, private_message_id, read)
})
.await?
{
Ok(private_message) => private_message,
Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
};
.map_err(|_| ApiError::err("couldnt_update_private_message"))?;
// No need to send an apub update
let private_message_id = data.private_message_id;

@ -388,10 +388,8 @@ impl Perform for SaveSiteConfig {
is_admin(&local_user_view)?;
// Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
let config_hjson = match Settings::save_config_file(&data.config_hjson) {
Ok(config_hjson) => config_hjson,
Err(_e) => return Err(ApiError::err("couldnt_update_site").into()),
};
let config_hjson = Settings::save_config_file(&data.config_hjson)
.map_err(|_| ApiError::err("couldnt_update_site"))?;
Ok(GetSiteConfigResponse { config_hjson })
}

@ -237,20 +237,18 @@ pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
}
pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
match blocking(pool, move |conn| Post::read(conn, post_id)).await? {
Ok(post) => Ok(post),
Err(_e) => Err(ApiError::err("couldnt_find_post").into()),
}
blocking(pool, move |conn| Post::read(conn, post_id))
.await?
.map_err(|_| ApiError::err("couldnt_find_post").into())
}
pub async fn get_local_user_view_from_jwt(
jwt: &str,
pool: &DbPool,
) -> Result<LocalUserView, LemmyError> {
let claims = match Claims::decode(&jwt) {
Ok(claims) => claims.claims,
Err(_e) => return Err(ApiError::err("not_logged_in").into()),
};
let claims = Claims::decode(&jwt)
.map_err(|_| ApiError::err("not_logged_in"))?
.claims;
let local_user_id = LocalUserId(claims.sub);
let local_user_view =
blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
@ -291,10 +289,9 @@ pub async fn get_local_user_settings_view_from_jwt(
jwt: &str,
pool: &DbPool,
) -> Result<LocalUserSettingsView, LemmyError> {
let claims = match Claims::decode(&jwt) {
Ok(claims) => claims.claims,
Err(_e) => return Err(ApiError::err("not_logged_in").into()),
};
let claims = Claims::decode(&jwt)
.map_err(|_| ApiError::err("not_logged_in"))?
.claims;
let local_user_id = LocalUserId(claims.sub);
let local_user_view = blocking(pool, move |conn| {
LocalUserSettingsView::read(conn, local_user_id)

@ -48,11 +48,9 @@ impl PerformCrud for CreateComment {
// If there's a parent_id, check to make sure that comment is in that post
if let Some(parent_id) = data.parent_id {
// Make sure the parent comment exists
let parent =
match blocking(context.pool(), move |conn| Comment::read(&conn, parent_id)).await? {
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
};
let parent = blocking(context.pool(), move |conn| Comment::read(&conn, parent_id))
.await?
.map_err(|_| ApiError::err("couldnt_create_comment"))?;
if parent.post_id != post_id {
return Err(ApiError::err("couldnt_create_comment").into());
}
@ -68,28 +66,22 @@ impl PerformCrud for CreateComment {
// Create the comment
let comment_form2 = comment_form.clone();
let inserted_comment = match blocking(context.pool(), move |conn| {
let inserted_comment = blocking(context.pool(), move |conn| {
Comment::create(&conn, &comment_form2)
})
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
};
.map_err(|_| ApiError::err("couldnt_create_comment"))?;
// Necessary to update the ap_id
let inserted_comment_id = inserted_comment.id;
let updated_comment: Comment =
match blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
let apub_id =
generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?;
Ok(Comment::update_ap_id(&conn, inserted_comment_id, apub_id)?)
})
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
};
.map_err(|_| ApiError::err("couldnt_create_comment"))?;
updated_comment
.send_create(&local_user_view.person, context)
@ -134,14 +126,11 @@ impl PerformCrud for CreateComment {
// If its a comment to yourself, mark it as read
let comment_id = comment_view.comment.id;
if local_user_view.person.id == comment_view.get_recipient_id() {
match blocking(context.pool(), move |conn| {
blocking(context.pool(), move |conn| {
Comment::update_read(conn, comment_id, true)
})
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
};
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
comment_view.comment.read = true;
}

@ -47,14 +47,11 @@ impl PerformCrud for DeleteComment {
// Do the delete
let deleted = data.deleted;
let updated_comment = match blocking(context.pool(), move |conn| {
let updated_comment = blocking(context.pool(), move |conn| {
Comment::update_deleted(conn, comment_id, deleted)
})
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
};
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
// Send the apub message
if deleted {
@ -139,14 +136,11 @@ impl PerformCrud for RemoveComment {
// Do the remove
let removed = data.removed;
let updated_comment = match blocking(context.pool(), move |conn| {
let updated_comment = blocking(context.pool(), move |conn| {
Comment::update_removed(conn, comment_id, removed)
})
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
};
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
// Mod tables
let form = ModRemoveCommentForm {

@ -40,11 +40,8 @@ impl PerformCrud for GetComments {
.limit(limit)
.list()
})
.await?;
let comments = match comments {
Ok(comments) => comments,
Err(_) => return Err(ApiError::err("couldnt_get_comments").into()),
};
.await?
.map_err(|_| ApiError::err("couldnt_get_comments"))?;
Ok(GetCommentsResponse { comments })
}

@ -52,14 +52,11 @@ impl PerformCrud for EditComment {
// Do the update
let content_slurs_removed = remove_slurs(&data.content.to_owned());
let comment_id = data.comment_id;
let updated_comment = match blocking(context.pool(), move |conn| {
let updated_comment = blocking(context.pool(), move |conn| {
Comment::update_content(conn, comment_id, &content_slurs_removed)
})
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
};
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
// Send the apub update
updated_comment

@ -85,14 +85,11 @@ impl PerformCrud for CreateCommunity {
..CommunityForm::default()
};
let inserted_community = match blocking(context.pool(), move |conn| {
let inserted_community = blocking(context.pool(), move |conn| {
Community::create(conn, &community_form)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("community_already_exists").into()),
};
.map_err(|_| ApiError::err("community_already_exists"))?;
// The community creator becomes a moderator
let community_moderator_form = CommunityModeratorForm {

@ -41,14 +41,11 @@ impl PerformCrud for DeleteCommunity {
// Do the delete
let community_id = data.community_id;
let deleted = data.deleted;
let updated_community = match blocking(context.pool(), move |conn| {
let updated_community = blocking(context.pool(), move |conn| {
Community::update_deleted(conn, community_id, deleted)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
};
.map_err(|_| ApiError::err("couldnt_update_community"))?;
// Send apub messages
if deleted {
@ -99,14 +96,11 @@ impl PerformCrud for RemoveCommunity {
// Do the remove
let community_id = data.community_id;
let removed = data.removed;
let updated_community = match blocking(context.pool(), move |conn| {
let updated_community = blocking(context.pool(), move |conn| {
Community::update_removed(conn, community_id, removed)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
};
.map_err(|_| ApiError::err("couldnt_update_community"))?;
// Mod tables
let expires = data.expires.map(naive_from_unix);

@ -28,35 +28,26 @@ impl PerformCrud for GetCommunity {
Some(id) => id,
None => {
let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
match blocking(context.pool(), move |conn| {
blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &name)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
}
.map_err(|_| ApiError::err("couldnt_find_community"))?
.id
}
};
let community_view = match blocking(context.pool(), move |conn| {
let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, person_id)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
};
.map_err(|_| ApiError::err("couldnt_find_community"))?;
let moderators: Vec<CommunityModeratorView> = match blocking(context.pool(), move |conn| {
let moderators: Vec<CommunityModeratorView> = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
})
.await?
{
Ok(moderators) => moderators,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
};
.map_err(|_| ApiError::err("couldnt_find_community"))?;
let online = context
.chat_server()

@ -69,15 +69,12 @@ impl PerformCrud for CreatePost {
};
let inserted_post_id = inserted_post.id;
let updated_post = match blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
let updated_post = blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
let apub_id = generate_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string())?;
Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
})
.await?
{
Ok(post) => post,
Err(_e) => return Err(ApiError::err("couldnt_create_post").into()),
};
.map_err(|_| ApiError::err("couldnt_create_post"))?;
updated_post
.send_create(&local_user_view.person, context)
@ -101,14 +98,11 @@ impl PerformCrud for CreatePost {
// Refetch the view
let inserted_post_id = inserted_post.id;
let post_view = match blocking(context.pool(), move |conn| {
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, inserted_post_id, Some(local_user_view.person.id))
})
.await?
{
Ok(post) => post,
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
};
.map_err(|_| ApiError::err("couldnt_find_post"))?;
let res = PostResponse { post_view };

@ -28,14 +28,11 @@ impl PerformCrud for GetPost {
let person_id = local_user_view.map(|u| u.person.id);
let id = data.id;
let post_view = match blocking(context.pool(), move |conn| {
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, id, person_id)
})
.await?
{
Ok(post) => post,
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
};
.map_err(|_| ApiError::err("couldnt_find_post"))?;
let id = data.id;
let comments = blocking(context.pool(), move |conn| {
@ -54,14 +51,11 @@ impl PerformCrud for GetPost {
.await??;
// Necessary for the sidebar
let community_view = match blocking(context.pool(), move |conn| {
let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, person_id)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
};
.map_err(|_| ApiError::err("couldnt_find_community"))?;
let online = context
.chat_server()
@ -108,7 +102,7 @@ impl PerformCrud for GetPosts {
let community_name = data.community_name.to_owned();
let saved_only = data.saved_only;
let posts = match blocking(context.pool(), move |conn| {
let posts = blocking(context.pool(), move |conn| {
PostQueryBuilder::create(conn)
.listing_type(&type_)
.sort(&sort)
@ -122,10 +116,7 @@ impl PerformCrud for GetPosts {
.list()
})
.await?
{
Ok(posts) => posts,
Err(_e) => return Err(ApiError::err("couldnt_get_posts").into()),
};
.map_err(|_| ApiError::err("couldnt_get_posts"))?;
Ok(GetPostsResponse { posts })
}

@ -46,7 +46,7 @@ impl PerformCrud for CreatePrivateMessage {
};
let inserted_private_message_id = inserted_private_message.id;
let updated_private_message = match blocking(
let updated_private_message = blocking(
context.pool(),
move |conn| -> Result<PrivateMessage, LemmyError> {
let apub_id = generate_apub_endpoint(
@ -61,10 +61,7 @@ impl PerformCrud for CreatePrivateMessage {
},
)
.await?
{
Ok(private_message) => private_message,
Err(_e) => return Err(ApiError::err("couldnt_create_private_message").into()),
};
.map_err(|_| ApiError::err("couldnt_create_private_message"))?;
updated_private_message
.send_create(&local_user_view.person, context)

@ -37,14 +37,11 @@ impl PerformCrud for DeletePrivateMessage {
// Doing the update
let private_message_id = data.private_message_id;
let deleted = data.deleted;
let updated_private_message = match blocking(context.pool(), move |conn| {
let updated_private_message = blocking(context.pool(), move |conn| {
PrivateMessage::update_deleted(conn, private_message_id, deleted)
})
.await?
{
Ok(private_message) => private_message,
Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
};
.map_err(|_| ApiError::err("couldnt_update_private_message"))?;
// Send the apub update
if data.deleted {

@ -37,14 +37,11 @@ impl PerformCrud for EditPrivateMessage {
// Doing the update
let content_slurs_removed = remove_slurs(&data.content);
let private_message_id = data.private_message_id;
let updated_private_message = match blocking(context.pool(), move |conn| {
let updated_private_message = blocking(context.pool(), move |conn| {
PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed)
})
.await?
{
Ok(private_message) => private_message,
Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
};
.map_err(|_| ApiError::err("couldnt_update_private_message"))?;
// Send the apub update
updated_private_message

@ -111,16 +111,11 @@ impl PerformCrud for Register {
};
// insert the person
let inserted_person = match blocking(context.pool(), move |conn| {
let inserted_person = blocking(context.pool(), move |conn| {
Person::create(conn, &person_form)
})
.await?
{
Ok(u) => u,
Err(_) => {
return Err(ApiError::err("user_already_exists").into());
}
};
.map_err(|_| ApiError::err("user_already_exists"))?;
// Create the local user
let local_user_form = LocalUserForm {

@ -43,10 +43,9 @@ impl PerformCrud for GetPersonDetails {
Person::find_by_name(conn, &username)
})
.await?;
match person {
Ok(p) => p.id,
Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
}
person
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?
.id
}
};

Loading…
Cancel
Save