@ -16,7 +16,6 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
@@ -16,7 +16,6 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
private readonly INotificationHubPool _ notificationHubPool ;
private readonly IServiceProvider _ serviceProvider ;
private readonly ILogger < NotificationHubPushRegistrationService > _l ogger ;
private Dictionary < NotificationHubType , NotificationHubClient > _ clients = [ ] ;
public NotificationHubPushRegistrationService (
IInstallationDeviceRepository installationDeviceRepository ,
@ -30,25 +29,6 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
@@ -30,25 +29,6 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
_ notificationHubPool = notificationHubPool ;
_ serviceProvider = serviceProvider ;
_l ogger = logger ;
// Is this dirty to do in the ctor?
void addHub ( NotificationHubType type )
{
var hubRegistration = globalSettings . NotificationHubs . FirstOrDefault (
h = > h . HubType = = type & & h . EnableRegistration ) ;
if ( hubRegistration ! = null )
{
var client = NotificationHubClient . CreateClientFromConnectionString (
hubRegistration . ConnectionString ,
hubRegistration . HubName ,
hubRegistration . EnableSendTracing ) ;
_ clients . Add ( type , client ) ;
}
}
addHub ( NotificationHubType . General ) ;
addHub ( NotificationHubType . iOS ) ;
addHub ( NotificationHubType . Android ) ;
}
public async Task CreateOrUpdateRegistrationAsync ( string pushToken , string deviceId , string userId ,
@ -121,7 +101,7 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
@@ -121,7 +101,7 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
BuildInstallationTemplate ( installation , "badgeMessage" , badgeMessageTemplate ? ? messageTemplate ,
userId , identifier ) ;
await GetClient ( type ) . CreateOrUpdateInstallationAsync ( installation ) ;
await ClientFor ( GetComb ( deviceId ) ) . CreateOrUpdateInstallationAsync ( installation ) ;
if ( InstallationDeviceEntity . IsInstallationDeviceId ( deviceId ) )
{
await _ installationDeviceRepository . UpsertAsync ( new InstallationDeviceEntity ( deviceId ) ) ;
@ -156,11 +136,11 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
@@ -156,11 +136,11 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
installation . Templates . Add ( fullTemplateId , template ) ;
}
public async Task DeleteRegistrationAsync ( string deviceId , DeviceType deviceType )
public async Task DeleteRegistrationAsync ( string deviceId )
{
try
{
await Get Client( deviceType ) . DeleteInstallationAsync ( deviceId ) ;
await ClientFor ( GetComb ( deviceId ) ) . DeleteInstallationAsync ( deviceId ) ;
if ( InstallationDeviceEntity . IsInstallationDeviceId ( deviceId ) )
{
await _ installationDeviceRepository . DeleteAsync ( new InstallationDeviceEntity ( deviceId ) ) ;
@ -172,31 +152,31 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
@@ -172,31 +152,31 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
}
}
public async Task AddUserRegistrationOrganizationAsync ( IEnumerable < KeyValuePair < string , DeviceType > > devices , string organizationId )
public async Task AddUserRegistrationOrganizationAsync ( IEnumerable < string > deviceId s , string organizationId )
{
await PatchTagsForUserDevicesAsync ( devices , UpdateOperationType . Add , $"organizationId:{organizationId}" ) ;
if ( devices . Any ( ) & & InstallationDeviceEntity . IsInstallationDeviceId ( devices . First ( ) . Key ) )
await PatchTagsForUserDevicesAsync ( deviceId s , UpdateOperationType . Add , $"organizationId:{organizationId}" ) ;
if ( deviceId s . Any ( ) & & InstallationDeviceEntity . IsInstallationDeviceId ( deviceId s . First ( ) ) )
{
var entities = devices . Select ( e = > new InstallationDeviceEntity ( e . Key ) ) ;
var entities = deviceId s . Select ( e = > new InstallationDeviceEntity ( e ) ) ;
await _ installationDeviceRepository . UpsertManyAsync ( entities . ToList ( ) ) ;
}
}
public async Task DeleteUserRegistrationOrganizationAsync ( IEnumerable < KeyValuePair < string , DeviceType > > devices , string organizationId )
public async Task DeleteUserRegistrationOrganizationAsync ( IEnumerable < string > deviceId s , string organizationId )
{
await PatchTagsForUserDevicesAsync ( devices , UpdateOperationType . Remove ,
await PatchTagsForUserDevicesAsync ( deviceId s , UpdateOperationType . Remove ,
$"organizationId:{organizationId}" ) ;
if ( devices . Any ( ) & & InstallationDeviceEntity . IsInstallationDeviceId ( devices . First ( ) . Key ) )
if ( deviceId s . Any ( ) & & InstallationDeviceEntity . IsInstallationDeviceId ( deviceId s . First ( ) ) )
{
var entities = devices . Select ( e = > new InstallationDeviceEntity ( e . Key ) ) ;
var entities = deviceId s . Select ( e = > new InstallationDeviceEntity ( e ) ) ;
await _ installationDeviceRepository . UpsertManyAsync ( entities . ToList ( ) ) ;
}
}
private async Task PatchTagsForUserDevicesAsync ( IEnumerable < KeyValuePair < string , DeviceType > > devices , UpdateOperationType op ,
private async Task PatchTagsForUserDevicesAsync ( IEnumerable < string > deviceId s , UpdateOperationType op ,
string tag )
{
if ( ! devices . Any ( ) )
if ( ! deviceId s . Any ( ) )
{
return ;
}
@ -216,11 +196,11 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
@@ -216,11 +196,11 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
operation . Path + = $"/{tag}" ;
}
foreach ( var device in devices )
foreach ( var deviceId in deviceId s )
{
try
{
await GetClient ( device . Value ) . PatchInstallationAsync ( device . Key , new List < PartialUpdateOperation > { operation } ) ;
await ClientFor ( GetComb ( deviceId ) ) . PatchInstallationAsync ( deviceId , new List < PartialUpdateOperation > { operation } ) ;
}
catch ( Exception e ) when ( e . InnerException = = null | | ! e . InnerException . Message . Contains ( "(404) Not Found" ) )
{
@ -229,53 +209,24 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
@@ -229,53 +209,24 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
}
}
private NotificationHubClient GetClient ( DeviceType deviceType )
{
var hubType = NotificationHubType . General ;
switch ( deviceType )
private NotificationHubClient ClientFor ( Guid deviceId )
{
case DeviceType . Android :
hubType = NotificationHubType . Android ;
break ;
case DeviceType . iOS :
hubType = NotificationHubType . iOS ;
break ;
case DeviceType . ChromeExtension :
case DeviceType . FirefoxExtension :
case DeviceType . OperaExtension :
case DeviceType . EdgeExtension :
case DeviceType . VivaldiExtension :
case DeviceType . SafariExtension :
hubType = NotificationHubType . GeneralBrowserExtension ;
break ;
case DeviceType . WindowsDesktop :
case DeviceType . MacOsDesktop :
case DeviceType . LinuxDesktop :
hubType = NotificationHubType . GeneralDesktop ;
break ;
case DeviceType . ChromeBrowser :
case DeviceType . FirefoxBrowser :
case DeviceType . OperaBrowser :
case DeviceType . EdgeBrowser :
case DeviceType . IEBrowser :
case DeviceType . UnknownBrowser :
case DeviceType . SafariBrowser :
case DeviceType . VivaldiBrowser :
hubType = NotificationHubType . GeneralWeb ;
break ;
default :
break ;
return _ notificationHubPool . ClientFor ( deviceId ) ;
}
if ( ! _ clients . ContainsKey ( hubType ) )
private Guid GetComb ( string deviceId )
{
Guid deviceIdGuid ;
if ( InstallationDeviceEntity . TrySplit ( deviceId , out _ , out deviceIdGuid ) )
{
_l ogger . LogWarning ( "No hub client for '{0}'. Using general hub instead." , hubType ) ;
hubType = NotificationHubType . General ;
if ( ! _ clients . ContainsKey ( hubType ) )
}
else if ( Guid . TryParse ( deviceId , out deviceIdGuid ) )
{
throw new Exception ( "No general hub client found." ) ;
}
else
{
throw new Exception ( $"Invalid device id {deviceId}." ) ;
}
return _ clients [ hubType ] ;
return deviceIdGuid ;
}
}