Implemented rejecting review articles (will be returned to authors drafts)

This commit is contained in:
Mia Rose Winter 2024-05-28 12:08:06 +02:00
parent a78a420a61
commit 4a7da5518b
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 55 additions and 0 deletions

View file

@ -74,6 +74,14 @@
@Localizer["Delete_Submit"] @Localizer["Delete_Submit"]
</a> </a>
} }
@if (article.AllowedToRejectReview(HttpContext.User)) {
<form @formname="reject-review" method="post" @onsubmit="RejectReview" class="max-sm:w-full">
<AntiforgeryToken />
<button type="submit" class="btn btn-error w-full sm:btn-wide">
@Localizer["Review_Reject"]
</button>
</form>
}
@if (article.AllowedToSubmitForReview(HttpContext.User)) { @if (article.AllowedToSubmitForReview(HttpContext.User)) {
<form @formname="submit-for-review" method="post" @onsubmit="SubmitForReview" class="max-sm:w-full"> <form @formname="submit-for-review" method="post" @onsubmit="SubmitForReview" class="max-sm:w-full">
<AntiforgeryToken/> <AntiforgeryToken/>
@ -295,6 +303,47 @@
Navigation.NavigateTo("/"); Navigation.NavigateTo("/");
} }
private async Task RejectReview() {
if (Article.AllowedToRejectReview(HttpContext.User) is false) return;
await using var context = await ContextFactory.CreateDbContextAsync();
Article!.Status = ArticleStatus.Draft;
string userId = HttpContext.User.FindFirst("Id")!.Value;
if (Article.Author.Id != userId) {
Article.Reviewer = await context.Users.FindAsync(userId);
}
context.Update(Article);
await context.SaveChangesAsync();
try {
var author = Article.Author;
string message =
$"The Article '{Article.Title}' has been rejected by a Reviewer, you will find it in your drafts.\n" +
$"Please make appropriate changes before submitting it again.";
if (author.Id != HttpContext.User.FindFirst("Id")!.Value) {
await EmailService.ConnectAsync(CancellationToken.None);
var email = await Email.CreateDefaultEmail(
author.Email!,
author.Name,
"Review Rejected",
"Your Article has been reject",
$"<p>{message}</p>",
message);
// TODO check if they enabled email notifications (property currently not implemented)
await EmailService.SendEmailAsync(email);
await EmailService.DisconnectAsync(CancellationToken.None);
}
} catch (Exception ex) {
Logger.LogError(ex, "Failed to send mail to author about article '{title}' being rejected.", Article.Title);
}
Navigation.NavigateTo("/");
}
private async Task SubmitForPublish() { private async Task SubmitForPublish() {
if (Article.AllowedToPublish(HttpContext.User) is false) return; if (Article.AllowedToPublish(HttpContext.User) is false) return;
@ -353,4 +402,5 @@
Navigation.NavigateTo("/"); Navigation.NavigateTo("/");
} }
} }

View file

@ -69,6 +69,11 @@ public static class Permissions {
return false; return false;
} }
public static bool AllowedToRejectReview(this Article? article, ClaimsPrincipal principal) {
// if you can publish it, you can reject it
return article?.Status is ArticleStatus.InReview && article.AllowedToPublish(principal);
}
public static bool AllowedToSubmitForReview(this Article? article, ClaimsPrincipal principal) { public static bool AllowedToSubmitForReview(this Article? article, ClaimsPrincipal principal) {
if (article is null || article.IsDeleted) return false; if (article is null || article.IsDeleted) return false;
if (article.Author is null) throw new ArgumentException("Checking permissions without loading related Author."); if (article.Author is null) throw new ArgumentException("Checking permissions without loading related Author.");