@@ -15,11 +15,11 @@ namespace Backbone.Modules.Devices.Infrastructure.PushNotifications;
15
15
16
16
public class PushService : IPushNotificationRegistrationService , IPushNotificationSender
17
17
{
18
- private readonly IIdentitiesRepository _identitiesRepository ;
18
+ private readonly IPnsRegistrationsRepository _pnsRegistrationsRepository ;
19
19
private readonly ILogger < PushService > _logger ;
20
- private readonly IPushNotificationTextProvider _notificationTextProvider ;
21
20
private readonly PnsConnectorFactory _pnsConnectorFactory ;
22
- private readonly IPnsRegistrationsRepository _pnsRegistrationsRepository ;
21
+ private readonly IPushNotificationTextProvider _notificationTextProvider ;
22
+ private readonly IIdentitiesRepository _identitiesRepository ;
23
23
24
24
public PushService ( IPnsRegistrationsRepository pnsRegistrationsRepository , PnsConnectorFactory pnsConnectorFactory , ILogger < PushService > logger ,
25
25
IPushNotificationTextProvider notificationTextProvider , IIdentitiesRepository identitiesRepository )
@@ -31,48 +31,6 @@ public PushService(IPnsRegistrationsRepository pnsRegistrationsRepository, PnsCo
31
31
_identitiesRepository = identitiesRepository ;
32
32
}
33
33
34
- public async Task < DevicePushIdentifier > UpdateRegistration ( IdentityAddress address , DeviceId deviceId , PnsHandle handle , string appId , PushEnvironment environment ,
35
- CancellationToken cancellationToken )
36
- {
37
- var registration = await _pnsRegistrationsRepository . FindByDeviceId ( deviceId , cancellationToken , track : true ) ;
38
- var pnsConnector = _pnsConnectorFactory . CreateFor ( handle . Platform ) ;
39
-
40
- if ( registration != null )
41
- {
42
- registration . Update ( handle , appId , environment ) ;
43
- pnsConnector . ValidateRegistration ( registration ) ;
44
-
45
- await _pnsRegistrationsRepository . Update ( registration , cancellationToken ) ;
46
-
47
- _logger . LogTrace ( "Device successfully updated." ) ;
48
- }
49
- else
50
- {
51
- registration = new PnsRegistration ( address , deviceId , handle , appId , environment ) ;
52
- pnsConnector . ValidateRegistration ( registration ) ;
53
-
54
- try
55
- {
56
- await _pnsRegistrationsRepository . Add ( registration , cancellationToken ) ;
57
- _logger . LogTrace ( "New device successfully registered." ) ;
58
- }
59
- catch ( InfrastructureException exception ) when ( exception . Code == InfrastructureErrors . UniqueKeyViolation ( ) . Code )
60
- {
61
- _logger . LogInformation ( exception , "This exception can be ignored. It is only thrown in case of a concurrent registration request from multiple devices." ) ;
62
- }
63
- }
64
-
65
- return registration . DevicePushIdentifier ;
66
- }
67
-
68
- public async Task DeleteRegistration ( DeviceId deviceId , CancellationToken cancellationToken )
69
- {
70
- var numberOfDeletedDevices = await _pnsRegistrationsRepository . Delete ( [ deviceId ] , cancellationToken ) ;
71
-
72
- if ( numberOfDeletedDevices == 1 )
73
- _logger . UnregisteredDevice ( ) ;
74
- }
75
-
76
34
public async Task SendNotification ( IPushNotification notification , SendPushNotificationFilter filter , CancellationToken cancellationToken )
77
35
{
78
36
var devices = await FindDevices ( filter , cancellationToken ) ;
@@ -82,6 +40,11 @@ public async Task SendNotification(IPushNotification notification, SendPushNotif
82
40
await SendNotificationInternal ( notification , devices , notificationTexts , cancellationToken ) ;
83
41
}
84
42
43
+ private static List < CommunicationLanguage > GetDistinctCommunicationLanguages ( IEnumerable < DeviceWithOnlyIdAndCommunicationLanguage > devices )
44
+ {
45
+ return devices . Select ( d => d . CommunicationLanguage ) . Distinct ( ) . ToList ( ) ;
46
+ }
47
+
85
48
public async Task SendNotification ( IPushNotification notification , SendPushNotificationFilter filter , Dictionary < string , NotificationText > notificationTexts , CancellationToken cancellationToken )
86
49
{
87
50
var devices = await FindDevices ( filter , cancellationToken ) ;
@@ -90,11 +53,6 @@ public async Task SendNotification(IPushNotification notification, SendPushNotif
90
53
await SendNotificationInternal ( notification , devices , mappedNotificationTexts , cancellationToken ) ;
91
54
}
92
55
93
- private static List < CommunicationLanguage > GetDistinctCommunicationLanguages ( IEnumerable < DeviceWithOnlyIdAndCommunicationLanguage > devices )
94
- {
95
- return devices . Select ( d => d . CommunicationLanguage ) . Distinct ( ) . ToList ( ) ;
96
- }
97
-
98
56
private async Task < DeviceWithOnlyIdAndCommunicationLanguage [ ] > FindDevices ( SendPushNotificationFilter filter , CancellationToken cancellationToken )
99
57
{
100
58
var result = await _identitiesRepository . FindDevices (
@@ -161,6 +119,48 @@ private async Task HandleSendNotificationResponses(SendResults sendResults)
161
119
_logger . LogTrace ( "Successfully sent push notifications." ) ;
162
120
}
163
121
122
+ public async Task < DevicePushIdentifier > UpdateRegistration ( IdentityAddress address , DeviceId deviceId , PnsHandle handle , string appId , PushEnvironment environment ,
123
+ CancellationToken cancellationToken )
124
+ {
125
+ var registration = await _pnsRegistrationsRepository . FindByDeviceId ( deviceId , cancellationToken , track : true ) ;
126
+ var pnsConnector = _pnsConnectorFactory . CreateFor ( handle . Platform ) ;
127
+
128
+ if ( registration != null )
129
+ {
130
+ registration . Update ( handle , appId , environment ) ;
131
+ pnsConnector . ValidateRegistration ( registration ) ;
132
+
133
+ await _pnsRegistrationsRepository . Update ( registration , cancellationToken ) ;
134
+
135
+ _logger . LogTrace ( "Device successfully updated." ) ;
136
+ }
137
+ else
138
+ {
139
+ registration = new PnsRegistration ( address , deviceId , handle , appId , environment ) ;
140
+ pnsConnector . ValidateRegistration ( registration ) ;
141
+
142
+ try
143
+ {
144
+ await _pnsRegistrationsRepository . Add ( registration , cancellationToken ) ;
145
+ _logger . LogTrace ( "New device successfully registered." ) ;
146
+ }
147
+ catch ( InfrastructureException exception ) when ( exception . Code == InfrastructureErrors . UniqueKeyViolation ( ) . Code )
148
+ {
149
+ _logger . LogInformation ( exception , "This exception can be ignored. It is only thrown in case of a concurrent registration request from multiple devices." ) ;
150
+ }
151
+ }
152
+
153
+ return registration . DevicePushIdentifier ;
154
+ }
155
+
156
+ public async Task DeleteRegistration ( DeviceId deviceId , CancellationToken cancellationToken )
157
+ {
158
+ var numberOfDeletedDevices = await _pnsRegistrationsRepository . Delete ( [ deviceId ] , cancellationToken ) ;
159
+
160
+ if ( numberOfDeletedDevices == 1 )
161
+ _logger . UnregisteredDevice ( ) ;
162
+ }
163
+
164
164
public class DeviceWithOnlyIdAndCommunicationLanguage
165
165
{
166
166
public required DeviceId Id { get ; init ; }
0 commit comments