mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 00:22:50 -05:00
PM-10600: Revert global push notifications
This commit is contained in:
@ -72,12 +72,7 @@ public class PushController : Controller
|
|||||||
{
|
{
|
||||||
CheckUsage();
|
CheckUsage();
|
||||||
|
|
||||||
if (model.Global)
|
if (!string.IsNullOrWhiteSpace(model.UserId))
|
||||||
{
|
|
||||||
await _pushNotificationService.SendPayloadToEveryoneAsync(model.Type, model.Payload,
|
|
||||||
Prefix(model.Identifier), Prefix(model.DeviceId), model.ClientType);
|
|
||||||
}
|
|
||||||
else if (!string.IsNullOrWhiteSpace(model.UserId))
|
|
||||||
{
|
{
|
||||||
await _pushNotificationService.SendPayloadToUserAsync(Prefix(model.UserId),
|
await _pushNotificationService.SendPayloadToUserAsync(Prefix(model.UserId),
|
||||||
model.Type, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId), model.ClientType);
|
model.Type, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId), model.ClientType);
|
||||||
|
@ -12,12 +12,11 @@ public class PushSendRequestModel : IValidatableObject
|
|||||||
public string? Identifier { get; set; }
|
public string? Identifier { get; set; }
|
||||||
[Required] public PushType Type { get; set; }
|
[Required] public PushType Type { get; set; }
|
||||||
[Required] public object Payload { get; set; } = null!;
|
[Required] public object Payload { get; set; } = null!;
|
||||||
public bool Global { get; set; }
|
|
||||||
public ClientType? ClientType { get; set; }
|
public ClientType? ClientType { get; set; }
|
||||||
|
|
||||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(UserId) && string.IsNullOrWhiteSpace(OrganizationId) && !Global)
|
if (string.IsNullOrWhiteSpace(UserId) && string.IsNullOrWhiteSpace(OrganizationId))
|
||||||
{
|
{
|
||||||
yield return new ValidationResult($"{nameof(UserId)} or {nameof(OrganizationId)} is required.");
|
yield return new ValidationResult($"{nameof(UserId)} or {nameof(OrganizationId)} is required.");
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,6 @@ public class SyncSendPushNotification
|
|||||||
public class SyncNotificationPushNotification
|
public class SyncNotificationPushNotification
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public bool Global { get; set; }
|
|
||||||
public Guid? UserId { get; set; }
|
public Guid? UserId { get; set; }
|
||||||
public Guid? OrganizationId { get; set; }
|
public Guid? OrganizationId { get; set; }
|
||||||
public ClientType ClientType { get; set; }
|
public ClientType ClientType { get; set; }
|
||||||
|
@ -32,7 +32,4 @@ public interface IPushNotificationService
|
|||||||
|
|
||||||
Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
|
Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
|
||||||
string deviceId = null, ClientType? clientType = null);
|
string deviceId = null, ClientType? clientType = null);
|
||||||
|
|
||||||
Task SendPayloadToEveryoneAsync(PushType type, object payload, string identifier, string deviceId = null,
|
|
||||||
ClientType? clientType = null);
|
|
||||||
}
|
}
|
||||||
|
@ -171,7 +171,6 @@ public class AzureQueuePushNotificationService : IPushNotificationService
|
|||||||
var message = new SyncNotificationPushNotification
|
var message = new SyncNotificationPushNotification
|
||||||
{
|
{
|
||||||
Id = notification.Id,
|
Id = notification.Id,
|
||||||
Global = notification.Global,
|
|
||||||
UserId = notification.UserId,
|
UserId = notification.UserId,
|
||||||
OrganizationId = notification.OrganizationId,
|
OrganizationId = notification.OrganizationId,
|
||||||
ClientType = notification.ClientType,
|
ClientType = notification.ClientType,
|
||||||
@ -229,7 +228,4 @@ public class AzureQueuePushNotificationService : IPushNotificationService
|
|||||||
// Noop
|
// Noop
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SendPayloadToEveryoneAsync(PushType type, object payload, string identifier, string deviceId = null,
|
|
||||||
ClientType? clientType = null) => Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
@ -184,13 +184,6 @@ public class MultiServicePushNotificationService : IPushNotificationService
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SendPayloadToEveryoneAsync(PushType type, object payload, string identifier, string deviceId = null,
|
|
||||||
ClientType? clientType = null)
|
|
||||||
{
|
|
||||||
PushToServices((s) => s.SendPayloadToEveryoneAsync(type, payload, identifier, deviceId, clientType));
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void PushToServices(Func<IPushNotificationService, Task> pushFunc)
|
private void PushToServices(Func<IPushNotificationService, Task> pushFunc)
|
||||||
{
|
{
|
||||||
if (_services != null)
|
if (_services != null)
|
||||||
|
@ -199,18 +199,13 @@ public class NotificationHubPushNotificationService : IPushNotificationService
|
|||||||
var message = new SyncNotificationPushNotification
|
var message = new SyncNotificationPushNotification
|
||||||
{
|
{
|
||||||
Id = notification.Id,
|
Id = notification.Id,
|
||||||
Global = notification.Global,
|
|
||||||
UserId = notification.UserId,
|
UserId = notification.UserId,
|
||||||
OrganizationId = notification.OrganizationId,
|
OrganizationId = notification.OrganizationId,
|
||||||
ClientType = notification.ClientType,
|
ClientType = notification.ClientType,
|
||||||
RevisionDate = notification.RevisionDate
|
RevisionDate = notification.RevisionDate
|
||||||
};
|
};
|
||||||
|
|
||||||
if (notification.Global)
|
if (notification.UserId.HasValue)
|
||||||
{
|
|
||||||
await SendPayloadToEveryoneAsync(PushType.SyncNotification, message, true, notification.ClientType);
|
|
||||||
}
|
|
||||||
else if (notification.UserId.HasValue)
|
|
||||||
{
|
{
|
||||||
await SendPayloadToUserAsync(notification.UserId.Value, PushType.SyncNotification, message, true,
|
await SendPayloadToUserAsync(notification.UserId.Value, PushType.SyncNotification, message, true,
|
||||||
notification.ClientType);
|
notification.ClientType);
|
||||||
@ -243,13 +238,6 @@ public class NotificationHubPushNotificationService : IPushNotificationService
|
|||||||
clientType: clientType);
|
clientType: clientType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendPayloadToEveryoneAsync(PushType type, object payload, bool excludeCurrentContext,
|
|
||||||
ClientType? clientType = null)
|
|
||||||
{
|
|
||||||
await SendPayloadToEveryoneAsync(type, payload, GetContextIdentifier(excludeCurrentContext),
|
|
||||||
clientType: clientType);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
|
public async Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
|
||||||
string deviceId = null, ClientType? clientType = null)
|
string deviceId = null, ClientType? clientType = null)
|
||||||
{
|
{
|
||||||
@ -272,17 +260,6 @@ public class NotificationHubPushNotificationService : IPushNotificationService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendPayloadToEveryoneAsync(PushType type, object payload, string identifier,
|
|
||||||
string deviceId = null, ClientType? clientType = null)
|
|
||||||
{
|
|
||||||
var tag = BuildTag($"template:payload", identifier, clientType);
|
|
||||||
await SendPayloadAsync(tag, type, payload);
|
|
||||||
if (InstallationDeviceEntity.IsInstallationDeviceId(deviceId))
|
|
||||||
{
|
|
||||||
await _installationDeviceRepository.UpsertAsync(new InstallationDeviceEntity(deviceId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetContextIdentifier(bool excludeCurrentContext)
|
private string GetContextIdentifier(bool excludeCurrentContext)
|
||||||
{
|
{
|
||||||
if (!excludeCurrentContext)
|
if (!excludeCurrentContext)
|
||||||
|
@ -178,7 +178,6 @@ public class NotificationsApiPushNotificationService : BaseIdentityClientService
|
|||||||
var message = new SyncNotificationPushNotification
|
var message = new SyncNotificationPushNotification
|
||||||
{
|
{
|
||||||
Id = notification.Id,
|
Id = notification.Id,
|
||||||
Global = notification.Global,
|
|
||||||
UserId = notification.UserId,
|
UserId = notification.UserId,
|
||||||
OrganizationId = notification.OrganizationId,
|
OrganizationId = notification.OrganizationId,
|
||||||
ClientType = notification.ClientType,
|
ClientType = notification.ClientType,
|
||||||
@ -235,7 +234,4 @@ public class NotificationsApiPushNotificationService : BaseIdentityClientService
|
|||||||
// Noop
|
// Noop
|
||||||
return Task.FromResult(0);
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SendPayloadToEveryoneAsync(PushType type, object payload, string identifier, string deviceId = null,
|
|
||||||
ClientType? clientType = null) => Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
@ -194,18 +194,13 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti
|
|||||||
var message = new SyncNotificationPushNotification
|
var message = new SyncNotificationPushNotification
|
||||||
{
|
{
|
||||||
Id = notification.Id,
|
Id = notification.Id,
|
||||||
Global = notification.Global,
|
|
||||||
UserId = notification.UserId,
|
UserId = notification.UserId,
|
||||||
OrganizationId = notification.OrganizationId,
|
OrganizationId = notification.OrganizationId,
|
||||||
ClientType = notification.ClientType,
|
ClientType = notification.ClientType,
|
||||||
RevisionDate = notification.RevisionDate
|
RevisionDate = notification.RevisionDate
|
||||||
};
|
};
|
||||||
|
|
||||||
if (notification.Global)
|
if (notification.UserId.HasValue)
|
||||||
{
|
|
||||||
await SendPayloadToEveryoneAsync(PushType.SyncNotification, message, true, notification.ClientType);
|
|
||||||
}
|
|
||||||
else if (notification.UserId.HasValue)
|
|
||||||
{
|
{
|
||||||
await SendPayloadToUserAsync(notification.UserId.Value, PushType.SyncNotification, message, true,
|
await SendPayloadToUserAsync(notification.UserId.Value, PushType.SyncNotification, message, true,
|
||||||
notification.ClientType);
|
notification.ClientType);
|
||||||
@ -247,21 +242,6 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti
|
|||||||
await SendAsync(HttpMethod.Post, "push/send", request);
|
await SendAsync(HttpMethod.Post, "push/send", request);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendPayloadToEveryoneAsync(PushType type, object payload, bool excludeCurrentContext,
|
|
||||||
ClientType? clientType = null)
|
|
||||||
{
|
|
||||||
var request = new PushSendRequestModel
|
|
||||||
{
|
|
||||||
Global = true,
|
|
||||||
Type = type,
|
|
||||||
Payload = payload,
|
|
||||||
ClientType = clientType
|
|
||||||
};
|
|
||||||
|
|
||||||
await AddCurrentContextAsync(request, excludeCurrentContext);
|
|
||||||
await SendAsync(HttpMethod.Post, "push/send", request);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task AddCurrentContextAsync(PushSendRequestModel request, bool addIdentifier)
|
private async Task AddCurrentContextAsync(PushSendRequestModel request, bool addIdentifier)
|
||||||
{
|
{
|
||||||
var currentContext =
|
var currentContext =
|
||||||
@ -292,10 +272,4 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti
|
|||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SendPayloadToEveryoneAsync(PushType type, object payload, string identifier, string deviceId = null,
|
|
||||||
ClientType? clientType = null)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,4 @@ public class NoopPushNotificationService : IPushNotificationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Task PushSyncNotificationAsync(Notification notification) => Task.CompletedTask;
|
public Task PushSyncNotificationAsync(Notification notification) => Task.CompletedTask;
|
||||||
|
|
||||||
public Task SendPayloadToEveryoneAsync(PushType type, object payload, string identifier, string deviceId = null,
|
|
||||||
ClientType? clientType = null) => Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
@ -93,21 +93,7 @@ public static class HubHelpers
|
|||||||
var syncNotification =
|
var syncNotification =
|
||||||
JsonSerializer.Deserialize<PushNotificationData<SyncNotificationPushNotification>>(
|
JsonSerializer.Deserialize<PushNotificationData<SyncNotificationPushNotification>>(
|
||||||
notificationJson, _deserializerOptions);
|
notificationJson, _deserializerOptions);
|
||||||
if (syncNotification.Payload.Global)
|
if (syncNotification.Payload.UserId.HasValue)
|
||||||
{
|
|
||||||
if (syncNotification.Payload.ClientType == ClientType.All)
|
|
||||||
{
|
|
||||||
await hubContext.Clients.All.SendAsync(_receiveMessageMethod, syncNotification,
|
|
||||||
cancellationToken);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
await hubContext.Clients
|
|
||||||
.Group(NotificationsHub.GetGlobalGroup(syncNotification.Payload.ClientType))
|
|
||||||
.SendAsync(_receiveMessageMethod, syncNotification, cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (syncNotification.Payload.UserId.HasValue)
|
|
||||||
{
|
{
|
||||||
if (syncNotification.Payload.ClientType == ClientType.All)
|
if (syncNotification.Payload.ClientType == ClientType.All)
|
||||||
{
|
{
|
||||||
|
@ -24,14 +24,9 @@ public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub
|
|||||||
await currentContext.BuildAsync(Context.User, _globalSettings);
|
await currentContext.BuildAsync(Context.User, _globalSettings);
|
||||||
|
|
||||||
var clientType = DeviceTypes.ToClientType(currentContext.DeviceType);
|
var clientType = DeviceTypes.ToClientType(currentContext.DeviceType);
|
||||||
if (clientType != ClientType.All)
|
if (clientType != ClientType.All && currentContext.UserId.HasValue)
|
||||||
{
|
{
|
||||||
await Groups.AddToGroupAsync(Context.ConnectionId, GetGlobalGroup(clientType));
|
await Groups.AddToGroupAsync(Context.ConnectionId, GetUserGroup(currentContext.UserId.Value, clientType));
|
||||||
if (currentContext.UserId.HasValue)
|
|
||||||
{
|
|
||||||
await Groups.AddToGroupAsync(Context.ConnectionId,
|
|
||||||
GetUserGroup(currentContext.UserId.Value, clientType));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentContext.Organizations != null)
|
if (currentContext.Organizations != null)
|
||||||
@ -56,15 +51,11 @@ public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub
|
|||||||
await currentContext.BuildAsync(Context.User, _globalSettings);
|
await currentContext.BuildAsync(Context.User, _globalSettings);
|
||||||
|
|
||||||
var clientType = DeviceTypes.ToClientType(currentContext.DeviceType);
|
var clientType = DeviceTypes.ToClientType(currentContext.DeviceType);
|
||||||
if (clientType != ClientType.All)
|
if (clientType != ClientType.All && currentContext.UserId.HasValue)
|
||||||
{
|
|
||||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, GetGlobalGroup(clientType));
|
|
||||||
if (currentContext.UserId.HasValue)
|
|
||||||
{
|
{
|
||||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId,
|
await Groups.RemoveFromGroupAsync(Context.ConnectionId,
|
||||||
GetUserGroup(currentContext.UserId.Value, clientType));
|
GetUserGroup(currentContext.UserId.Value, clientType));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (currentContext.Organizations != null)
|
if (currentContext.Organizations != null)
|
||||||
{
|
{
|
||||||
@ -82,11 +73,6 @@ public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub
|
|||||||
await base.OnDisconnectedAsync(exception);
|
await base.OnDisconnectedAsync(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetGlobalGroup(ClientType clientType)
|
|
||||||
{
|
|
||||||
return $"ClientType_{clientType}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string GetUserGroup(Guid userId, ClientType clientType)
|
public static string GetUserGroup(Guid userId, ClientType clientType)
|
||||||
{
|
{
|
||||||
return $"UserClientType_{userId}_{clientType}";
|
return $"UserClientType_{userId}_{clientType}";
|
||||||
|
Reference in New Issue
Block a user