Files
server/src/Core/Services/Implementations/RelayPushRegistrationService.cs

72 lines
2.4 KiB
C#
Raw Normal View History

2017-08-11 08:57:31 -04:00
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Http;
using Bit.Core.Models.Api;
using Bit.Core.Enums;
using Bit.Core.Settings;
2017-08-11 08:57:31 -04:00
using System.Linq;
2017-08-14 13:06:44 -04:00
using Microsoft.Extensions.Logging;
2017-08-11 08:57:31 -04:00
namespace Bit.Core.Services
{
2018-08-15 18:43:26 -04:00
public class RelayPushRegistrationService : BaseIdentityClientService, IPushRegistrationService
2017-08-11 08:57:31 -04:00
{
2017-08-14 13:06:44 -04:00
private readonly ILogger<RelayPushRegistrationService> _logger;
public RelayPushRegistrationService(
GlobalSettings globalSettings,
ILogger<RelayPushRegistrationService> logger)
2018-08-15 18:43:26 -04:00
: base(
globalSettings.PushRelayBaseUri,
globalSettings.Installation.IdentityUri,
"api.push",
$"installation.{globalSettings.Installation.Id}",
globalSettings.Installation.Key,
logger)
2017-08-14 13:06:44 -04:00
{
_logger = logger;
}
2017-08-11 08:57:31 -04:00
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
string identifier, DeviceType type)
{
var requestModel = new PushRegistrationRequestModel
{
DeviceId = deviceId,
Identifier = identifier,
PushToken = pushToken,
Type = type,
UserId = userId
};
2018-08-21 14:32:09 -04:00
await SendAsync(HttpMethod.Post, "push/register", requestModel);
2017-08-11 08:57:31 -04:00
}
public async Task DeleteRegistrationAsync(string deviceId)
{
2018-08-21 14:32:09 -04:00
await SendAsync(HttpMethod.Delete, string.Concat("push/", deviceId));
2017-08-11 08:57:31 -04:00
}
public async Task AddUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
{
if (!deviceIds.Any())
2017-08-11 08:57:31 -04:00
{
return;
}
var requestModel = new PushUpdateRequestModel(deviceIds, organizationId);
2018-08-21 14:32:09 -04:00
await SendAsync(HttpMethod.Put, "push/add-organization", requestModel);
2017-08-11 08:57:31 -04:00
}
public async Task DeleteUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
{
if (!deviceIds.Any())
2017-08-11 08:57:31 -04:00
{
return;
}
var requestModel = new PushUpdateRequestModel(deviceIds, organizationId);
2018-08-21 14:32:09 -04:00
await SendAsync(HttpMethod.Put, "push/delete-organization", requestModel);
2017-08-11 08:57:31 -04:00
}
}
}