diff --git a/Wave/Components/Pages/ArticleView.razor b/Wave/Components/Pages/ArticleView.razor index 19da952..3ac4b70 100644 --- a/Wave/Components/Pages/ArticleView.razor +++ b/Wave/Components/Pages/ArticleView.razor @@ -74,6 +74,14 @@ @Localizer["Delete_Submit"] } + @if (article.AllowedToRejectReview(HttpContext.User)) { +
+ + + + } @if (article.AllowedToSubmitForReview(HttpContext.User)) {
@@ -295,6 +303,47 @@ 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", + $"

{message}

", + 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() { if (Article.AllowedToPublish(HttpContext.User) is false) return; @@ -353,4 +402,5 @@ Navigation.NavigateTo("/"); } + } diff --git a/Wave/Utilities/Permissions.cs b/Wave/Utilities/Permissions.cs index 35ed8e4..6114edc 100644 --- a/Wave/Utilities/Permissions.cs +++ b/Wave/Utilities/Permissions.cs @@ -69,6 +69,11 @@ public static class Permissions { 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) { if (article is null || article.IsDeleted) return false; if (article.Author is null) throw new ArgumentException("Checking permissions without loading related Author.");