// 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;
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 };
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 {
// 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(
// 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();
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 {})
}
// 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 {
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 })
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 };
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 };
// 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;
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 })
}
}
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??;
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)
// 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());
}
// 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)
// 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;
}
// 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 {
// 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 {
.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 })
}
// 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
..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 {
// 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 {
// 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);
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()
};
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)
// 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 };
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| {
.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()
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)
.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 })
}
};
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(
},
)
.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)
// 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 {
// 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
};
// 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 {
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
}
};