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

104 lines
3.3 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 System;
using Bit.Core.Models.Api;
using Bit.Core.Enums;
using System.Linq;
namespace Bit.Core.Services
{
public class RelayPushRegistrationService : BaseRelayPushNotificationService, IPushRegistrationService
2017-08-11 08:57:31 -04:00
{
public RelayPushRegistrationService(GlobalSettings globalSettings)
: base(globalSettings)
{ }
2017-08-11 08:57:31 -04:00
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
string identifier, DeviceType type)
{
var tokenStateResponse = await HandleTokenStateAsync();
if(!tokenStateResponse)
{
return;
}
var requestModel = new PushRegistrationRequestModel
{
DeviceId = deviceId,
Identifier = identifier,
PushToken = pushToken,
Type = type,
UserId = userId
};
var message = new TokenHttpRequestMessage(requestModel, AccessToken)
2017-08-11 08:57:31 -04:00
{
Method = HttpMethod.Post,
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/register"))
2017-08-11 08:57:31 -04:00
};
await PushClient.SendAsync(message);
2017-08-11 08:57:31 -04:00
}
public async Task DeleteRegistrationAsync(string deviceId)
{
var tokenStateResponse = await HandleTokenStateAsync();
if(!tokenStateResponse)
{
return;
}
var message = new TokenHttpRequestMessage(AccessToken)
2017-08-11 08:57:31 -04:00
{
Method = HttpMethod.Delete,
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/", deviceId))
2017-08-11 08:57:31 -04:00
};
await PushClient.SendAsync(message);
2017-08-11 08:57:31 -04:00
}
public async Task AddUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
{
if(!deviceIds.Any())
{
return;
}
var tokenStateResponse = await HandleTokenStateAsync();
if(!tokenStateResponse)
{
return;
}
var requestModel = new PushUpdateRequestModel(deviceIds, organizationId);
var message = new TokenHttpRequestMessage(requestModel, AccessToken)
2017-08-11 08:57:31 -04:00
{
Method = HttpMethod.Put,
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/add-organization"))
2017-08-11 08:57:31 -04:00
};
await PushClient.SendAsync(message);
2017-08-11 08:57:31 -04:00
}
public async Task DeleteUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
{
if(!deviceIds.Any())
{
return;
}
var tokenStateResponse = await HandleTokenStateAsync();
if(!tokenStateResponse)
{
return;
}
var requestModel = new PushUpdateRequestModel(deviceIds, organizationId);
var message = new TokenHttpRequestMessage(requestModel, AccessToken)
2017-08-11 08:57:31 -04:00
{
Method = HttpMethod.Put,
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/delete-organization"))
2017-08-11 08:57:31 -04:00
};
await PushClient.SendAsync(message);
2017-08-11 08:57:31 -04:00
}
}
}