Skip to content

Commit 98533de

Browse files
authored
Notifications (#75)
Support for browser notifications
1 parent 9318dfc commit 98533de

File tree

80 files changed

+1609
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1609
-229
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Coderr.Server.Api.Core.Users.Commands
2+
{
3+
[Message]
4+
public class DeleteBrowserSubscription
5+
{
6+
public int UserId { get; set; }
7+
public string Endpoint { get; set; }
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Coderr.Server.Api.Core.Users.Commands
2+
{
3+
/// <summary>
4+
/// https://tools.ietf.org/html/draft-ietf-webpush-encryption-08
5+
/// </summary>
6+
[Message]
7+
public class StoreBrowserSubscription
8+
{
9+
public int UserId { get; set; }
10+
public string Endpoint { get; set; }
11+
12+
public string PublicKey { get; set; }
13+
public string AuthenticationSecret { get; set; }
14+
15+
public int? ExpirationTime { get; set; }
16+
}
17+
}

src/Server/Coderr.Server.Api/Core/Users/Commands/UpdateNotifications.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ public class UpdateNotifications
1616
/// </summary>
1717
public NotificationState NotifyOnNewIncidents { get; set; }
1818

19-
/// <summary>
20-
/// How to notify when a new report is created (receive an exception)
21-
/// </summary>
22-
public NotificationState NotifyOnNewReport { get; set; }
23-
2419
/// <summary>
2520
/// How to notify user when a peak is detected
2621
/// </summary>
@@ -36,6 +31,7 @@ public class UpdateNotifications
3631
/// </summary>
3732
public NotificationState NotifyOnUserFeedback { get; set; }
3833

34+
3935
/// <summary>
4036
/// User that configured its settings.
4137
/// </summary>

src/Server/Coderr.Server.Api/Core/Users/NotificationSettings.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ public class NotificationSettings
1212
/// </summary>
1313
public NotificationState NotifyOnNewIncidents { get; set; }
1414

15-
/// <summary>
16-
/// How to notify when a new report is created (receive an exception)
17-
/// </summary>
18-
public NotificationState NotifyOnNewReport { get; set; }
19-
2015
/// <summary>
2116
/// How to notify user when a peak is detected
2217
/// </summary>

src/Server/Coderr.Server.Api/Core/Users/NotificationState.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@ public enum NotificationState
88
/// <summary>
99
/// Use global setting
1010
/// </summary>
11-
UseGlobalSetting,
11+
UseGlobalSetting = 1,
1212

1313
/// <summary>
1414
/// Do not notify
1515
/// </summary>
16-
Disabled,
16+
Disabled = 2,
1717

1818
/// <summary>
1919
/// By cellphone (text message)
2020
/// </summary>
21-
Cellphone,
21+
Cellphone = 3,
2222

2323
/// <summary>
2424
/// By email
2525
/// </summary>
26-
Email
26+
Email = 4,
27+
28+
/// <summary>
29+
/// Use browser/desktop notifications.
30+
/// </summary>
31+
BrowserNotification = 5
2732
}
2833
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading.Tasks;
2+
using Coderr.Server.Api.Core.Users.Commands;
3+
using DotNetCqs;
4+
5+
namespace Coderr.Server.App.Core.Notifications.Commands
6+
{
7+
internal class DeleteBrowserSubscriptionHandler : IMessageHandler<DeleteBrowserSubscription>
8+
{
9+
private readonly INotificationsRepository _repository;
10+
11+
public DeleteBrowserSubscriptionHandler(INotificationsRepository repository)
12+
{
13+
_repository = repository;
14+
}
15+
16+
public async Task HandleAsync(IMessageContext context, DeleteBrowserSubscription message)
17+
{
18+
await _repository.DeleteBrowserSubscription(message.UserId, message.Endpoint);
19+
}
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Coderr.Server.Abstractions.Security;
4+
using Coderr.Server.Api.Core.Users.Commands;
5+
using Coderr.Server.Domain.Modules.UserNotifications;
6+
using DotNetCqs;
7+
8+
namespace Coderr.Server.App.Core.Notifications.Commands
9+
{
10+
internal class StoreBrowserSubscriptionHandler : IMessageHandler<StoreBrowserSubscription>
11+
{
12+
private readonly INotificationsRepository _notificationsRepository;
13+
14+
public StoreBrowserSubscriptionHandler(INotificationsRepository notificationsRepository)
15+
{
16+
_notificationsRepository = notificationsRepository;
17+
}
18+
19+
public async Task HandleAsync(IMessageContext context, StoreBrowserSubscription message)
20+
{
21+
var subscription = new BrowserSubscription
22+
{
23+
AccountId = context.Principal.GetAccountId(),
24+
AuthenticationSecret = message.AuthenticationSecret,
25+
Endpoint = message.Endpoint,
26+
PublicKey = message.PublicKey,
27+
ExpiresAtUtc = message.ExpirationTime == null
28+
? (DateTime?) null
29+
: DateTime.UtcNow.AddMilliseconds(message.ExpirationTime.Value)
30+
};
31+
await _notificationsRepository.Save(subscription);
32+
}
33+
}
34+
}

src/Server/Coderr.Server.App/Core/Notifications/INotificationsRepository.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
44
using System.Threading.Tasks;
5+
using Coderr.Server.Api.Core.Users.Commands;
56
using Coderr.Server.Domain.Modules.UserNotifications;
67

78
namespace Coderr.Server.App.Core.Notifications
89
{
910
/// <summary>
10-
/// Repository for notification settings
11+
/// Repository for managing notification settings
1112
/// </summary>
1213
public interface INotificationsRepository
1314
{
@@ -45,5 +46,8 @@ public interface INotificationsRepository
4546
/// <returns>task</returns>
4647
/// <exception cref="ArgumentNullException">notificationSettings</exception>
4748
Task UpdateAsync(UserNotificationSettings notificationSettings);
49+
50+
Task Save(BrowserSubscription message);
51+
Task DeleteBrowserSubscription(int accountId, string endpoint);
4852
}
4953
}

src/Server/Coderr.Server.App/Core/Reports/Config/ReportConfig.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Collections.Generic;
22
using Coderr.Server.Abstractions.Config;
3-
using Coderr.Server.Infrastructure.Configuration;
4-
//using Coderr.Server.ReportAnalyzer.Abstractions.ErrorReports;
53

64
namespace Coderr.Server.App.Core.Reports.Config
75
{

src/Server/Coderr.Server.App/Core/Reports/Jobs/DeleteReportsBelowReportLimit.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
21
using System.Data;
3-
using System.Diagnostics;
42
using Coderr.Server.Abstractions.Boot;
53
using Coderr.Server.Abstractions.Config;
64
using Coderr.Server.App.Core.Reports.Config;

0 commit comments

Comments
 (0)