1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 00:22:50 -05:00

PM-10600: Sending to specific client types for relay push notifications

This commit is contained in:
Maciej Zieniuk
2024-10-22 10:29:11 +01:00
parent 3a604af0a4
commit 7020565770
3 changed files with 49 additions and 23 deletions

View File

@ -72,15 +72,20 @@ public class PushController : Controller
{ {
CheckUsage(); 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), 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)) else if (!string.IsNullOrWhiteSpace(model.OrganizationId))
{ {
await _pushNotificationService.SendPayloadToOrganizationAsync(Prefix(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);
} }
} }

View File

@ -1,22 +1,23 @@
using System.ComponentModel.DataAnnotations; #nullable enable
using System.ComponentModel.DataAnnotations;
using Bit.Core.Enums; using Bit.Core.Enums;
namespace Bit.Core.Models.Api; namespace Bit.Core.Models.Api;
public class PushSendRequestModel : IValidatableObject public class PushSendRequestModel : IValidatableObject
{ {
public string UserId { get; set; } public string? UserId { get; set; }
public string OrganizationId { get; set; } public string? OrganizationId { get; set; }
public string DeviceId { get; set; } public string? DeviceId { get; set; }
public string Identifier { get; set; } public string? Identifier { get; set; }
[Required] [Required] public PushType Type { get; set; }
public PushType? Type { get; set; } [Required] public object Payload { get; set; } = null!;
[Required] public bool Global { get; set; }
public object Payload { 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)) if (string.IsNullOrWhiteSpace(UserId) && string.IsNullOrWhiteSpace(OrganizationId) && !Global)
{ {
yield return new ValidationResult($"{nameof(UserId)} or {nameof(OrganizationId)} is required."); yield return new ValidationResult($"{nameof(UserId)} or {nameof(OrganizationId)} is required.");
} }

View File

@ -202,40 +202,60 @@ public class RelayPushNotificationService : BaseIdentityClientService, IPushNoti
if (notification.Global) if (notification.Global)
{ {
await SendPayloadToEveryoneAsync(PushType.SyncNotification, message, true); await SendPayloadToEveryoneAsync(PushType.SyncNotification, message, true, notification.ClientType);
} }
else if (notification.UserId.HasValue) 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) else if (notification.OrganizationId.HasValue)
{ {
await SendPayloadToOrganizationAsync(notification.OrganizationId.Value, PushType.SyncNotification, message, 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 AddCurrentContextAsync(request, excludeCurrentContext);
await SendAsync(HttpMethod.Post, "push/send", request); await SendAsync(HttpMethod.Post, "push/send", request);
} }
private async Task SendPayloadToOrganizationAsync(Guid orgId, PushType type, object payload, 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 AddCurrentContextAsync(request, excludeCurrentContext);
await SendAsync(HttpMethod.Post, "push/send", request); 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
var request = new PushSendRequestModel { Type = type, Payload = payload }; {
Global = true,
Type = type,
Payload = payload,
ClientType = clientType
};
await AddCurrentContextAsync(request, excludeCurrentContext); await AddCurrentContextAsync(request, excludeCurrentContext);
await SendAsync(HttpMethod.Post, "push/send", request); await SendAsync(HttpMethod.Post, "push/send", request);