-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use MinimalAPI for marking notifications as read (#16175)
- Loading branch information
1 parent
4d10d15
commit 873b493
Showing
10 changed files
with
155 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 0 additions & 75 deletions
75
src/OrchardCore.Modules/OrchardCore.Notifications/Controllers/ApiController.cs
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
...OrchardCore.Modules/OrchardCore.Notifications/Endpoints/Management/MarkAsReadEndpoints.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using OrchardCore.Entities; | ||
using OrchardCore.Modules; | ||
using OrchardCore.Notifications.Indexes; | ||
using OrchardCore.Notifications.Models; | ||
using OrchardCore.Notifications.ViewModels; | ||
using YesSql; | ||
|
||
namespace OrchardCore.Notifications.Endpoints.Management; | ||
|
||
public static class MarkAsReadEndpoints | ||
{ | ||
public const string RouteName = "NotificationsMarkAsRead"; | ||
|
||
public static IEndpointRouteBuilder AddMarkAsReadEndpoint(this IEndpointRouteBuilder builder) | ||
{ | ||
builder.MapPost("Notifications/MarkAsRead", HandleAsync) | ||
.AllowAnonymous() | ||
.WithName(RouteName) | ||
.DisableAntiforgery(); | ||
|
||
return builder; | ||
} | ||
|
||
private static async Task<IResult> HandleAsync( | ||
ReadNotificationViewModel model, | ||
IAuthorizationService authorizationService, | ||
IHttpContextAccessor httpContextAccessor, | ||
YesSql.ISession session, | ||
IClock clock) | ||
{ | ||
if (string.IsNullOrEmpty(model?.MessageId)) | ||
{ | ||
return TypedResults.BadRequest(); | ||
} | ||
|
||
if (!await authorizationService.AuthorizeAsync(httpContextAccessor.HttpContext.User, NotificationPermissions.ManageNotifications)) | ||
{ | ||
return TypedResults.Forbid(); | ||
} | ||
|
||
var userId = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); | ||
|
||
var notification = await session.Query<Notification, NotificationIndex>(x => x.NotificationId == model.MessageId && x.UserId == userId, collection: NotificationConstants.NotificationCollection).FirstOrDefaultAsync(); | ||
|
||
if (notification == null) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
var updated = false; | ||
var readInfo = notification.As<NotificationReadInfo>(); | ||
|
||
if (!readInfo.IsRead) | ||
{ | ||
readInfo.ReadAtUtc = clock.UtcNow; | ||
readInfo.IsRead = true; | ||
notification.Put(readInfo); | ||
|
||
updated = true; | ||
|
||
await session.SaveAsync(notification, collection: NotificationConstants.NotificationCollection); | ||
} | ||
|
||
return TypedResults.Ok(new | ||
{ | ||
messageId = model.MessageId, | ||
updated, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...OrchardCore.Modules/OrchardCore.Notifications/wwwroot/Scripts/notification-manager.min.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters