mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-22 20:31:19 +00:00
Use .map_err in api code (fixes #1573)
This commit is contained in:
parent
03c18ecfbe
commit
19001cf326
22 changed files with 80 additions and 185 deletions
|
@ -46,14 +46,11 @@ impl Perform for MarkCommentAsRead {
|
||||||
|
|
||||||
// Do the mark as read
|
// Do the mark as read
|
||||||
let read = data.read;
|
let read = data.read;
|
||||||
match blocking(context.pool(), move |conn| {
|
blocking(context.pool(), move |conn| {
|
||||||
Comment::update_read(conn, comment_id, read)
|
Comment::update_read(conn, comment_id, read)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
|
||||||
Ok(comment) => comment,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Refetch it
|
// Refetch it
|
||||||
let comment_id = data.comment_id;
|
let comment_id = data.comment_id;
|
||||||
|
|
|
@ -59,14 +59,11 @@ impl Perform for CreateCommentReport {
|
||||||
reason: data.reason.to_owned(),
|
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)
|
CommentReport::report(conn, &report_form)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_create_report"))?;
|
||||||
Ok(report) => report,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_create_report").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let res = CreateCommentReportResponse { success: true };
|
let res = CreateCommentReportResponse { success: true };
|
||||||
|
|
||||||
|
|
|
@ -414,24 +414,18 @@ impl Perform for TransferCommunity {
|
||||||
|
|
||||||
let community_id = data.community_id;
|
let community_id = data.community_id;
|
||||||
let person_id = local_user_view.person.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))
|
CommunityView::read(conn, community_id, Some(person_id))
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_community"))?;
|
||||||
Ok(community) => community,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let community_id = data.community_id;
|
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)
|
CommunityModeratorView::for_community(conn, community_id)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_community"))?;
|
||||||
Ok(moderators) => moderators,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Return the jwt
|
// Return the jwt
|
||||||
Ok(GetCommunityResponse {
|
Ok(GetCommunityResponse {
|
||||||
|
|
|
@ -83,14 +83,11 @@ impl Perform for Login {
|
||||||
|
|
||||||
// Fetch that username / email
|
// Fetch that username / email
|
||||||
let username_or_email = data.username_or_email.clone();
|
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)
|
LocalUserView::find_by_email_or_name(conn, &username_or_email)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?;
|
||||||
Ok(uv) => uv,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Verify the password
|
// Verify the password
|
||||||
let valid: bool = verify(
|
let valid: bool = verify(
|
||||||
|
@ -629,14 +626,11 @@ impl Perform for PasswordReset {
|
||||||
|
|
||||||
// Fetch that email
|
// Fetch that email
|
||||||
let email = data.email.clone();
|
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)
|
LocalUserView::find_by_email(conn, &email)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?;
|
||||||
Ok(lu) => lu,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generate a random token
|
// Generate a random token
|
||||||
let token = generate_random_string();
|
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 subject = &format!("Password reset for {}", local_user_view.person.name);
|
||||||
let hostname = &Settings::get().get_protocol_and_hostname();
|
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);
|
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) {
|
send_email(subject, email, &local_user_view.person.name, html)
|
||||||
Ok(_o) => _o,
|
.map_err(|e| ApiError::err(&e))?;
|
||||||
Err(_e) => return Err(ApiError::err(&_e).into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(PasswordResetResponse {})
|
Ok(PasswordResetResponse {})
|
||||||
}
|
}
|
||||||
|
@ -691,14 +683,11 @@ impl Perform for PasswordChange {
|
||||||
|
|
||||||
// Update the user with the new password
|
// Update the user with the new password
|
||||||
let password = data.password.clone();
|
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)
|
LocalUser::update_password(conn, local_user_id, &password)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_user"))?;
|
||||||
Ok(u) => u,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_user").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Return the jwt
|
// Return the jwt
|
||||||
Ok(LoginResponse {
|
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 local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
||||||
|
|
||||||
let person_id = local_user_view.person.id;
|
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)
|
CommunityFollowerView::for_person(conn, person_id)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("system_err_login"))?;
|
||||||
Ok(communities) => communities,
|
|
||||||
_ => return Err(ApiError::err("system_err_login").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Return the jwt
|
// Return the jwt
|
||||||
Ok(GetFollowedCommunitiesResponse { communities })
|
Ok(GetFollowedCommunitiesResponse { communities })
|
||||||
|
|
|
@ -71,14 +71,11 @@ impl Perform for CreatePostLike {
|
||||||
|
|
||||||
let post_id = data.post_id;
|
let post_id = data.post_id;
|
||||||
let person_id = local_user_view.person.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))
|
PostView::read(conn, post_id, Some(person_id))
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_post"))?;
|
||||||
Ok(post) => post,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let res = PostResponse { post_view };
|
let res = PostResponse { post_view };
|
||||||
|
|
||||||
|
|
|
@ -68,14 +68,11 @@ impl Perform for CreatePostReport {
|
||||||
reason: data.reason.to_owned(),
|
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)
|
PostReport::report(conn, &report_form)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_create_report"))?;
|
||||||
Ok(report) => report,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_create_report").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let res = CreatePostReportResponse { success: true };
|
let res = CreatePostReportResponse { success: true };
|
||||||
|
|
||||||
|
|
|
@ -36,14 +36,11 @@ impl Perform for MarkPrivateMessageAsRead {
|
||||||
// Doing the update
|
// Doing the update
|
||||||
let private_message_id = data.private_message_id;
|
let private_message_id = data.private_message_id;
|
||||||
let read = data.read;
|
let read = data.read;
|
||||||
match blocking(context.pool(), move |conn| {
|
blocking(context.pool(), move |conn| {
|
||||||
PrivateMessage::update_read(conn, private_message_id, read)
|
PrivateMessage::update_read(conn, private_message_id, read)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_private_message"))?;
|
||||||
Ok(private_message) => private_message,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// No need to send an apub update
|
// No need to send an apub update
|
||||||
let private_message_id = data.private_message_id;
|
let private_message_id = data.private_message_id;
|
||||||
|
|
|
@ -388,10 +388,8 @@ impl Perform for SaveSiteConfig {
|
||||||
is_admin(&local_user_view)?;
|
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
|
// 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) {
|
let config_hjson = Settings::save_config_file(&data.config_hjson)
|
||||||
Ok(config_hjson) => config_hjson,
|
.map_err(|_| ApiError::err("couldnt_update_site"))?;
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_site").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(GetSiteConfigResponse { config_hjson })
|
Ok(GetSiteConfigResponse { config_hjson })
|
||||||
}
|
}
|
||||||
|
|
|
@ -247,10 +247,9 @@ pub async fn get_local_user_view_from_jwt(
|
||||||
jwt: &str,
|
jwt: &str,
|
||||||
pool: &DbPool,
|
pool: &DbPool,
|
||||||
) -> Result<LocalUserView, LemmyError> {
|
) -> Result<LocalUserView, LemmyError> {
|
||||||
let claims = match Claims::decode(&jwt) {
|
let claims = Claims::decode(&jwt)
|
||||||
Ok(claims) => claims.claims,
|
.map_err(|_| ApiError::err("not_logged_in"))?
|
||||||
Err(_e) => return Err(ApiError::err("not_logged_in").into()),
|
.claims;
|
||||||
};
|
|
||||||
let local_user_id = LocalUserId(claims.sub);
|
let local_user_id = LocalUserId(claims.sub);
|
||||||
let local_user_view =
|
let local_user_view =
|
||||||
blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
|
blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
|
||||||
|
@ -291,10 +290,9 @@ pub async fn get_local_user_settings_view_from_jwt(
|
||||||
jwt: &str,
|
jwt: &str,
|
||||||
pool: &DbPool,
|
pool: &DbPool,
|
||||||
) -> Result<LocalUserSettingsView, LemmyError> {
|
) -> Result<LocalUserSettingsView, LemmyError> {
|
||||||
let claims = match Claims::decode(&jwt) {
|
let claims = Claims::decode(&jwt)
|
||||||
Ok(claims) => claims.claims,
|
.map_err(|_| ApiError::err("not_logged_in"))?
|
||||||
Err(_e) => return Err(ApiError::err("not_logged_in").into()),
|
.claims;
|
||||||
};
|
|
||||||
let local_user_id = LocalUserId(claims.sub);
|
let local_user_id = LocalUserId(claims.sub);
|
||||||
let local_user_view = blocking(pool, move |conn| {
|
let local_user_view = blocking(pool, move |conn| {
|
||||||
LocalUserSettingsView::read(conn, local_user_id)
|
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 there's a parent_id, check to make sure that comment is in that post
|
||||||
if let Some(parent_id) = data.parent_id {
|
if let Some(parent_id) = data.parent_id {
|
||||||
// Make sure the parent comment exists
|
// Make sure the parent comment exists
|
||||||
let parent =
|
let parent = blocking(context.pool(), move |conn| Comment::read(&conn, parent_id))
|
||||||
match blocking(context.pool(), move |conn| Comment::read(&conn, parent_id)).await? {
|
.await?
|
||||||
Ok(comment) => comment,
|
.map_err(|_| ApiError::err("couldnt_create_comment"))?;
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
|
|
||||||
};
|
|
||||||
if parent.post_id != post_id {
|
if parent.post_id != post_id {
|
||||||
return Err(ApiError::err("couldnt_create_comment").into());
|
return Err(ApiError::err("couldnt_create_comment").into());
|
||||||
}
|
}
|
||||||
|
@ -68,28 +66,22 @@ impl PerformCrud for CreateComment {
|
||||||
|
|
||||||
// Create the comment
|
// Create the comment
|
||||||
let comment_form2 = comment_form.clone();
|
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)
|
Comment::create(&conn, &comment_form2)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_create_comment"))?;
|
||||||
Ok(comment) => comment,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Necessary to update the ap_id
|
// Necessary to update the ap_id
|
||||||
let inserted_comment_id = inserted_comment.id;
|
let inserted_comment_id = inserted_comment.id;
|
||||||
let updated_comment: Comment =
|
let updated_comment: Comment =
|
||||||
match blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
|
blocking(context.pool(), move |conn| -> Result<Comment, LemmyError> {
|
||||||
let apub_id =
|
let apub_id =
|
||||||
generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?;
|
generate_apub_endpoint(EndpointType::Comment, &inserted_comment_id.to_string())?;
|
||||||
Ok(Comment::update_ap_id(&conn, inserted_comment_id, apub_id)?)
|
Ok(Comment::update_ap_id(&conn, inserted_comment_id, apub_id)?)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_create_comment"))?;
|
||||||
Ok(comment) => comment,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
updated_comment
|
updated_comment
|
||||||
.send_create(&local_user_view.person, context)
|
.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
|
// If its a comment to yourself, mark it as read
|
||||||
let comment_id = comment_view.comment.id;
|
let comment_id = comment_view.comment.id;
|
||||||
if local_user_view.person.id == comment_view.get_recipient_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)
|
Comment::update_read(conn, comment_id, true)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
|
||||||
Ok(comment) => comment,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
|
|
||||||
};
|
|
||||||
comment_view.comment.read = true;
|
comment_view.comment.read = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,14 +47,11 @@ impl PerformCrud for DeleteComment {
|
||||||
|
|
||||||
// Do the delete
|
// Do the delete
|
||||||
let deleted = data.deleted;
|
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)
|
Comment::update_deleted(conn, comment_id, deleted)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
|
||||||
Ok(comment) => comment,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send the apub message
|
// Send the apub message
|
||||||
if deleted {
|
if deleted {
|
||||||
|
@ -139,14 +136,11 @@ impl PerformCrud for RemoveComment {
|
||||||
|
|
||||||
// Do the remove
|
// Do the remove
|
||||||
let removed = data.removed;
|
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)
|
Comment::update_removed(conn, comment_id, removed)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
|
||||||
Ok(comment) => comment,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Mod tables
|
// Mod tables
|
||||||
let form = ModRemoveCommentForm {
|
let form = ModRemoveCommentForm {
|
||||||
|
|
|
@ -40,11 +40,8 @@ impl PerformCrud for GetComments {
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.list()
|
.list()
|
||||||
})
|
})
|
||||||
.await?;
|
.await?
|
||||||
let comments = match comments {
|
.map_err(|_| ApiError::err("couldnt_get_comments"))?;
|
||||||
Ok(comments) => comments,
|
|
||||||
Err(_) => return Err(ApiError::err("couldnt_get_comments").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(GetCommentsResponse { comments })
|
Ok(GetCommentsResponse { comments })
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,14 +52,11 @@ impl PerformCrud for EditComment {
|
||||||
// Do the update
|
// Do the update
|
||||||
let content_slurs_removed = remove_slurs(&data.content.to_owned());
|
let content_slurs_removed = remove_slurs(&data.content.to_owned());
|
||||||
let comment_id = data.comment_id;
|
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)
|
Comment::update_content(conn, comment_id, &content_slurs_removed)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
|
||||||
Ok(comment) => comment,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send the apub update
|
// Send the apub update
|
||||||
updated_comment
|
updated_comment
|
||||||
|
|
|
@ -85,14 +85,11 @@ impl PerformCrud for CreateCommunity {
|
||||||
..CommunityForm::default()
|
..CommunityForm::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = match blocking(context.pool(), move |conn| {
|
let inserted_community = blocking(context.pool(), move |conn| {
|
||||||
Community::create(conn, &community_form)
|
Community::create(conn, &community_form)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("community_already_exists"))?;
|
||||||
Ok(community) => community,
|
|
||||||
Err(_e) => return Err(ApiError::err("community_already_exists").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// The community creator becomes a moderator
|
// The community creator becomes a moderator
|
||||||
let community_moderator_form = CommunityModeratorForm {
|
let community_moderator_form = CommunityModeratorForm {
|
||||||
|
|
|
@ -41,14 +41,11 @@ impl PerformCrud for DeleteCommunity {
|
||||||
// Do the delete
|
// Do the delete
|
||||||
let community_id = data.community_id;
|
let community_id = data.community_id;
|
||||||
let deleted = data.deleted;
|
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)
|
Community::update_deleted(conn, community_id, deleted)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_community"))?;
|
||||||
Ok(community) => community,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send apub messages
|
// Send apub messages
|
||||||
if deleted {
|
if deleted {
|
||||||
|
@ -99,14 +96,11 @@ impl PerformCrud for RemoveCommunity {
|
||||||
// Do the remove
|
// Do the remove
|
||||||
let community_id = data.community_id;
|
let community_id = data.community_id;
|
||||||
let removed = data.removed;
|
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)
|
Community::update_removed(conn, community_id, removed)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_community"))?;
|
||||||
Ok(community) => community,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Mod tables
|
// Mod tables
|
||||||
let expires = data.expires.map(naive_from_unix);
|
let expires = data.expires.map(naive_from_unix);
|
||||||
|
|
|
@ -28,35 +28,26 @@ impl PerformCrud for GetCommunity {
|
||||||
Some(id) => id,
|
Some(id) => id,
|
||||||
None => {
|
None => {
|
||||||
let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
|
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)
|
Community::read_from_name(conn, &name)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_community"))?
|
||||||
Ok(community) => community,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
|
|
||||||
}
|
|
||||||
.id
|
.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)
|
CommunityView::read(conn, community_id, person_id)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_community"))?;
|
||||||
Ok(community) => community,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
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)
|
CommunityModeratorView::for_community(conn, community_id)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_community"))?;
|
||||||
Ok(moderators) => moderators,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let online = context
|
let online = context
|
||||||
.chat_server()
|
.chat_server()
|
||||||
|
|
|
@ -69,15 +69,12 @@ impl PerformCrud for CreatePost {
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_post_id = inserted_post.id;
|
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())?;
|
let apub_id = generate_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string())?;
|
||||||
Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
|
Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_create_post"))?;
|
||||||
Ok(post) => post,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_create_post").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
updated_post
|
updated_post
|
||||||
.send_create(&local_user_view.person, context)
|
.send_create(&local_user_view.person, context)
|
||||||
|
@ -101,14 +98,11 @@ impl PerformCrud for CreatePost {
|
||||||
|
|
||||||
// Refetch the view
|
// Refetch the view
|
||||||
let inserted_post_id = inserted_post.id;
|
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))
|
PostView::read(conn, inserted_post_id, Some(local_user_view.person.id))
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_post"))?;
|
||||||
Ok(post) => post,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let res = PostResponse { post_view };
|
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 person_id = local_user_view.map(|u| u.person.id);
|
||||||
|
|
||||||
let id = data.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)
|
PostView::read(conn, id, person_id)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_post"))?;
|
||||||
Ok(post) => post,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let id = data.id;
|
let id = data.id;
|
||||||
let comments = blocking(context.pool(), move |conn| {
|
let comments = blocking(context.pool(), move |conn| {
|
||||||
|
@ -54,14 +51,11 @@ impl PerformCrud for GetPost {
|
||||||
.await??;
|
.await??;
|
||||||
|
|
||||||
// Necessary for the sidebar
|
// 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)
|
CommunityView::read(conn, community_id, person_id)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_find_community"))?;
|
||||||
Ok(community) => community,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let online = context
|
let online = context
|
||||||
.chat_server()
|
.chat_server()
|
||||||
|
@ -108,7 +102,7 @@ impl PerformCrud for GetPosts {
|
||||||
let community_name = data.community_name.to_owned();
|
let community_name = data.community_name.to_owned();
|
||||||
let saved_only = data.saved_only;
|
let saved_only = data.saved_only;
|
||||||
|
|
||||||
let posts = match blocking(context.pool(), move |conn| {
|
let posts = blocking(context.pool(), move |conn| {
|
||||||
PostQueryBuilder::create(conn)
|
PostQueryBuilder::create(conn)
|
||||||
.listing_type(&type_)
|
.listing_type(&type_)
|
||||||
.sort(&sort)
|
.sort(&sort)
|
||||||
|
@ -122,10 +116,7 @@ impl PerformCrud for GetPosts {
|
||||||
.list()
|
.list()
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_get_posts"))?;
|
||||||
Ok(posts) => posts,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_get_posts").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(GetPostsResponse { posts })
|
Ok(GetPostsResponse { posts })
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ impl PerformCrud for CreatePrivateMessage {
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_private_message_id = inserted_private_message.id;
|
let inserted_private_message_id = inserted_private_message.id;
|
||||||
let updated_private_message = match blocking(
|
let updated_private_message = blocking(
|
||||||
context.pool(),
|
context.pool(),
|
||||||
move |conn| -> Result<PrivateMessage, LemmyError> {
|
move |conn| -> Result<PrivateMessage, LemmyError> {
|
||||||
let apub_id = generate_apub_endpoint(
|
let apub_id = generate_apub_endpoint(
|
||||||
|
@ -61,10 +61,7 @@ impl PerformCrud for CreatePrivateMessage {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_create_private_message"))?;
|
||||||
Ok(private_message) => private_message,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_create_private_message").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
updated_private_message
|
updated_private_message
|
||||||
.send_create(&local_user_view.person, context)
|
.send_create(&local_user_view.person, context)
|
||||||
|
|
|
@ -37,14 +37,11 @@ impl PerformCrud for DeletePrivateMessage {
|
||||||
// Doing the update
|
// Doing the update
|
||||||
let private_message_id = data.private_message_id;
|
let private_message_id = data.private_message_id;
|
||||||
let deleted = data.deleted;
|
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)
|
PrivateMessage::update_deleted(conn, private_message_id, deleted)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_private_message"))?;
|
||||||
Ok(private_message) => private_message,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send the apub update
|
// Send the apub update
|
||||||
if data.deleted {
|
if data.deleted {
|
||||||
|
|
|
@ -37,14 +37,11 @@ impl PerformCrud for EditPrivateMessage {
|
||||||
// Doing the update
|
// Doing the update
|
||||||
let content_slurs_removed = remove_slurs(&data.content);
|
let content_slurs_removed = remove_slurs(&data.content);
|
||||||
let private_message_id = data.private_message_id;
|
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)
|
PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed)
|
||||||
})
|
})
|
||||||
.await?
|
.await?
|
||||||
{
|
.map_err(|_| ApiError::err("couldnt_update_private_message"))?;
|
||||||
Ok(private_message) => private_message,
|
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send the apub update
|
// Send the apub update
|
||||||
updated_private_message
|
updated_private_message
|
||||||
|
|
|
@ -43,10 +43,9 @@ impl PerformCrud for GetPersonDetails {
|
||||||
Person::find_by_name(conn, &username)
|
Person::find_by_name(conn, &username)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
match person {
|
person
|
||||||
Ok(p) => p.id,
|
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?
|
||||||
Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
|
.id
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue