-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 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
126 changes: 126 additions & 0 deletions
126
src/server/host/Endpoints/Notifications/SendNotificationEndpoint.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,126 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using FastEndpoints; | ||
using FluentValidation; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure.Internal; | ||
using MinigolfFriday.Data; | ||
using MinigolfFriday.Domain.Models; | ||
using MinigolfFriday.Domain.Models.Push; | ||
using MinigolfFriday.Domain.Models.RealtimeEvents; | ||
using MinigolfFriday.Host.Common; | ||
using MinigolfFriday.Host.Mappers; | ||
using MinigolfFriday.Host.Services; | ||
using MinigolfFriday.Host.Utilities; | ||
|
||
namespace MinigolfFriday.Host.Endpoints.Notifications; | ||
|
||
public record SendNotificationRequest(string? UserId, string? Title, string? Body); | ||
|
||
public class SendNotificationRequestValidator : Validator<SendNotificationRequest> | ||
{ | ||
public SendNotificationRequestValidator(IIdService idService) | ||
{ | ||
// RuleFor(x => x.UserId).NotNull().ValidSqid(idService.User); | ||
} | ||
} | ||
|
||
public class SendNotificationEndpoint( | ||
DatabaseContext databaseContext, | ||
IJwtService jwtService, | ||
IIdService idService, | ||
Check warning on line 30 in src/server/host/Endpoints/Notifications/SendNotificationEndpoint.cs GitHub Actions / Build
Check warning on line 30 in src/server/host/Endpoints/Notifications/SendNotificationEndpoint.cs GitHub Actions / Build
|
||
IUserPushSubscriptionMapper userPushSubscriptionMapper, | ||
Check warning on line 31 in src/server/host/Endpoints/Notifications/SendNotificationEndpoint.cs GitHub Actions / Build
Check warning on line 31 in src/server/host/Endpoints/Notifications/SendNotificationEndpoint.cs GitHub Actions / Build
|
||
IWebPushService webPushService | ||
) : Endpoint<SendNotificationRequest> | ||
{ | ||
public override void Configure() | ||
{ | ||
Post(""); | ||
Group<NotificationsGroup>(); | ||
this.ProducesErrors(EndpointErrors.UserIdNotInClaims); | ||
} | ||
|
||
public override async Task HandleAsync(SendNotificationRequest req, CancellationToken ct) | ||
{ | ||
// var userId = idService.User.DecodeSingle(req.UserId); | ||
if (!jwtService.TryGetUserId(User, out var currendUserId)) | ||
{ | ||
Logger.LogWarning(EndpointErrors.UserIdNotInClaims); | ||
await this.SendErrorAsync(EndpointErrors.UserIdNotInClaims, ct); | ||
return; | ||
} | ||
// Logger.LogWarning("user found. id: {}", userId); | ||
Logger.LogWarning("current User: {} ", currendUserId); | ||
|
||
var user = await databaseContext | ||
.Users.Include(x => x.Settings) | ||
.FirstAsync(x => x.Id == currendUserId, ct); | ||
|
||
Logger.LogWarning(user.Alias); | ||
|
||
var notifications = await databaseContext | ||
.Users.Where(u => u.Id == currendUserId) | ||
.Select(u => new | ||
{ | ||
Subscriptions = u.PushSubscriptions.Select(x => new UserPushSubscription( | ||
x.Id, | ||
x.UserId, | ||
x.Lang, | ||
x.Endpoint, | ||
x.P256DH, | ||
x.Auth | ||
)), | ||
NotificationData = new PushNotificationData.TestNotification( | ||
req.Title != null ? req.Title : "Body", | ||
req.Body != null ? req.Body : "Test" | ||
) | ||
}) | ||
.ToArrayAsync(ct); | ||
foreach (var notification in notifications) | ||
await webPushService.SendAsync( | ||
notification.Subscriptions, | ||
notification.NotificationData, | ||
ct | ||
); | ||
|
||
// var user = await databaseContext | ||
// .Users.Include(x => x.Settings) | ||
// .FirstAsync(x => x.Id == userId, ct); | ||
|
||
// if (user.Settings == null) | ||
// { | ||
// user.Settings = new() | ||
// { | ||
// EnableNotifications = req.EnableNotifications ?? true, | ||
// NotifyOnEventPublish = req.NotifyOnEventPublish ?? true, | ||
// NotifyOnEventStart = req.NotifyOnEventStart ?? true, | ||
// NotifyOnEventUpdated = req.NotifyOnEventUpdated ?? true, | ||
// NotifyOnTimeslotStart = req.NotifyOnTimeslotStart ?? true, | ||
// SecondsToNotifyBeforeTimeslotStart = req.SecondsToNotifyBeforeTimeslotStart ?? 600 | ||
// }; | ||
// } | ||
// else | ||
// { | ||
// user.Settings.EnableNotifications = | ||
// req.EnableNotifications ?? user.Settings.EnableNotifications; | ||
// user.Settings.NotifyOnEventPublish = | ||
// req.NotifyOnEventPublish ?? user.Settings.NotifyOnEventPublish; | ||
// user.Settings.NotifyOnEventStart = | ||
// req.NotifyOnEventStart ?? user.Settings.NotifyOnEventStart; | ||
// user.Settings.NotifyOnEventUpdated = | ||
// req.NotifyOnEventUpdated ?? user.Settings.NotifyOnEventUpdated; | ||
// user.Settings.NotifyOnTimeslotStart = | ||
// req.NotifyOnTimeslotStart ?? user.Settings.NotifyOnTimeslotStart; | ||
// user.Settings.SecondsToNotifyBeforeTimeslotStart = | ||
// req.SecondsToNotifyBeforeTimeslotStart | ||
// ?? user.Settings.SecondsToNotifyBeforeTimeslotStart; | ||
// } | ||
|
||
// await databaseContext.SaveChangesAsync(ct); | ||
await SendOkAsync(ct); | ||
|
||
// await realtimeEventsService.SendEventAsync( | ||
// new RealtimeEvent.UserSettingsChanged(idService.User.Encode(userId)), | ||
// ct | ||
// ); | ||
} | ||
} |