diff --git a/src/Api/Controllers/PushController.cs b/src/Api/Controllers/PushController.cs index c83eb200b8..6f0274e1a2 100644 --- a/src/Api/Controllers/PushController.cs +++ b/src/Api/Controllers/PushController.cs @@ -72,15 +72,20 @@ public class PushController : Controller { CheckUsage(); - if (!string.IsNullOrWhiteSpace(model.UserId)) + if (model.Global) + { + 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), - model.Type.Value, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId)); + model.Type, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId), model.ClientType); } else if (!string.IsNullOrWhiteSpace(model.OrganizationId)) { await _pushNotificationService.SendPayloadToOrganizationAsync(Prefix(model.OrganizationId), - model.Type.Value, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId)); + model.Type, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId), model.ClientType); } } diff --git a/src/Core/Models/Api/Request/PushSendRequestModel.cs b/src/Core/Models/Api/Request/PushSendRequestModel.cs index b85c8fb555..4b982ed73f 100644 --- a/src/Core/Models/Api/Request/PushSendRequestModel.cs +++ b/src/Core/Models/Api/Request/PushSendRequestModel.cs @@ -1,22 +1,23 @@ -using System.ComponentModel.DataAnnotations; +#nullable enable +using System.ComponentModel.DataAnnotations; using Bit.Core.Enums; namespace Bit.Core.Models.Api; public class PushSendRequestModel : IValidatableObject { - public string UserId { get; set; } - public string OrganizationId { get; set; } - public string DeviceId { get; set; } - public string Identifier { get; set; } - [Required] - public PushType? Type { get; set; } - [Required] - public object Payload { get; set; } + public string? UserId { get; set; } + public string? OrganizationId { get; set; } + public string? DeviceId { get; set; } + public string? Identifier { get; set; } + [Required] public PushType Type { get; set; } + [Required] public object Payload { get; set; } = null!; + public bool Global { get; set; } + public ClientType? ClientType { get; set; } public IEnumerable Validate(ValidationContext validationContext) { - if (string.IsNullOrWhiteSpace(UserId) && string.IsNullOrWhiteSpace(OrganizationId)) + if (string.IsNullOrWhiteSpace(UserId) && string.IsNullOrWhiteSpace(OrganizationId) && !Global) { yield return new ValidationResult($"{nameof(UserId)} or {nameof(OrganizationId)} is required."); } diff --git a/src/Core/Services/Implementations/RelayPushNotificationService.cs b/src/Core/Services/Implementations/RelayPushNotificationService.cs index 99f120a80c..16db74acf6 100644 --- a/src/Core/Services/Implementations/RelayPushNotificationService.cs +++ b/src/Core/Services/Implementations/RelayPushNotificationService.cs @@ -202,40 +202,60 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti if (notification.Global) { - await SendPayloadToEveryoneAsync(PushType.SyncNotification, message, true); + 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); } else if (notification.OrganizationId.HasValue) { await SendPayloadToOrganizationAsync(notification.OrganizationId.Value, PushType.SyncNotification, message, - true); + true, notification.ClientType); } } - private async Task SendPayloadToUserAsync(Guid userId, PushType type, object payload, bool excludeCurrentContext) + private async Task SendPayloadToUserAsync(Guid userId, PushType type, object payload, bool excludeCurrentContext, + ClientType? clientType = null) { - var request = new PushSendRequestModel { UserId = userId.ToString(), Type = type, Payload = payload }; + var request = new PushSendRequestModel + { + UserId = userId.ToString(), + Type = type, + Payload = payload, + ClientType = clientType + }; await AddCurrentContextAsync(request, excludeCurrentContext); await SendAsync(HttpMethod.Post, "push/send", request); } private async Task SendPayloadToOrganizationAsync(Guid orgId, PushType type, object payload, - bool excludeCurrentContext) + bool excludeCurrentContext, ClientType? clientType = null) { - var request = new PushSendRequestModel { OrganizationId = orgId.ToString(), Type = type, Payload = payload }; + var request = new PushSendRequestModel + { + OrganizationId = orgId.ToString(), + Type = type, + Payload = payload, + ClientType = clientType + }; await AddCurrentContextAsync(request, excludeCurrentContext); await SendAsync(HttpMethod.Post, "push/send", request); } - private async Task SendPayloadToEveryoneAsync(PushType type, object payload, bool excludeCurrentContext) + private async Task SendPayloadToEveryoneAsync(PushType type, object payload, bool excludeCurrentContext, + ClientType? clientType = null) { - // TODO global flag prop to be explicit ? - var request = new PushSendRequestModel { Type = type, Payload = payload }; + var request = new PushSendRequestModel + { + Global = true, + Type = type, + Payload = payload, + ClientType = clientType + }; await AddCurrentContextAsync(request, excludeCurrentContext); await SendAsync(HttpMethod.Post, "push/send", request);