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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

211 lines
6.7 KiB
C#
Raw Normal View History

2018-08-16 12:05:01 -04:00
using System;
using System.Collections.Generic;
2018-08-16 12:05:01 -04:00
using System.Net.Http;
using System.Threading.Tasks;
using Bit.Core.Context;
2018-08-16 12:05:01 -04:00
using Bit.Core.Enums;
using Bit.Core.Models;
using Bit.Core.Models.Table;
using Bit.Core.Settings;
2018-08-16 12:05:01 -04:00
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
2018-08-16 12:05:01 -04:00
namespace Bit.Core.Services
{
2018-08-16 13:52:11 -04:00
public class NotificationsApiPushNotificationService : BaseIdentityClientService, IPushNotificationService
2018-08-16 12:05:01 -04:00
{
private readonly GlobalSettings _globalSettings;
private readonly IHttpContextAccessor _httpContextAccessor;
private JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
2018-08-16 13:52:11 -04:00
public NotificationsApiPushNotificationService(
2018-08-16 12:05:01 -04:00
GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor,
2018-08-16 13:52:11 -04:00
ILogger<NotificationsApiPushNotificationService> logger)
2018-08-16 12:05:01 -04:00
: base(
2018-08-16 13:50:41 -04:00
globalSettings.BaseServiceUri.InternalNotifications,
2018-08-16 12:05:01 -04:00
globalSettings.BaseServiceUri.InternalIdentity,
"internal",
$"internal.{globalSettings.ProjectName}",
globalSettings.InternalIdentityKey,
logger)
{
_globalSettings = globalSettings;
_httpContextAccessor = httpContextAccessor;
}
public async Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
2018-08-16 12:05:01 -04:00
{
await PushCipherAsync(cipher, PushType.SyncCipherCreate, collectionIds);
2018-08-16 12:05:01 -04:00
}
public async Task PushSyncCipherUpdateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
2018-08-16 12:05:01 -04:00
{
await PushCipherAsync(cipher, PushType.SyncCipherUpdate, collectionIds);
2018-08-16 12:05:01 -04:00
}
public async Task PushSyncCipherDeleteAsync(Cipher cipher)
{
await PushCipherAsync(cipher, PushType.SyncLoginDelete, null);
2018-08-16 12:05:01 -04:00
}
private async Task PushCipherAsync(Cipher cipher, PushType type, IEnumerable<Guid> collectionIds)
2018-08-16 12:05:01 -04:00
{
if (cipher.OrganizationId.HasValue)
2018-08-16 12:05:01 -04:00
{
var message = new SyncCipherPushNotification
{
Id = cipher.Id,
OrganizationId = cipher.OrganizationId,
RevisionDate = cipher.RevisionDate,
CollectionIds = collectionIds,
2018-08-16 12:05:01 -04:00
};
await SendMessageAsync(type, message, true);
}
else if (cipher.UserId.HasValue)
2018-08-16 12:05:01 -04:00
{
var message = new SyncCipherPushNotification
{
Id = cipher.Id,
UserId = cipher.UserId,
RevisionDate = cipher.RevisionDate,
};
await SendMessageAsync(type, message, true);
}
}
public async Task PushSyncFolderCreateAsync(Folder folder)
{
await PushFolderAsync(folder, PushType.SyncFolderCreate);
}
public async Task PushSyncFolderUpdateAsync(Folder folder)
{
await PushFolderAsync(folder, PushType.SyncFolderUpdate);
}
public async Task PushSyncFolderDeleteAsync(Folder folder)
{
await PushFolderAsync(folder, PushType.SyncFolderDelete);
}
private async Task PushFolderAsync(Folder folder, PushType type)
{
var message = new SyncFolderPushNotification
{
Id = folder.Id,
UserId = folder.UserId,
RevisionDate = folder.RevisionDate
};
await SendMessageAsync(type, message, true);
}
public async Task PushSyncCiphersAsync(Guid userId)
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.SyncCiphers);
2018-08-16 12:05:01 -04:00
}
public async Task PushSyncVaultAsync(Guid userId)
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.SyncVault);
2018-08-16 12:05:01 -04:00
}
public async Task PushSyncOrgKeysAsync(Guid userId)
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.SyncOrgKeys);
2018-08-16 12:05:01 -04:00
}
public async Task PushSyncSettingsAsync(Guid userId)
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.SyncSettings);
2018-08-16 12:05:01 -04:00
}
2018-08-28 08:22:49 -04:00
public async Task PushLogOutAsync(Guid userId)
2018-08-16 12:05:01 -04:00
{
2018-08-28 08:22:49 -04:00
await PushUserAsync(userId, PushType.LogOut);
}
private async Task PushUserAsync(Guid userId, PushType type)
{
var message = new UserPushNotification
2018-08-16 12:05:01 -04:00
{
UserId = userId,
Date = DateTime.UtcNow
};
await SendMessageAsync(type, message, false);
}
public async Task PushSyncSendCreateAsync(Send send)
{
await PushSendAsync(send, PushType.SyncSendCreate);
}
public async Task PushSyncSendUpdateAsync(Send send)
{
await PushSendAsync(send, PushType.SyncSendUpdate);
}
public async Task PushSyncSendDeleteAsync(Send send)
{
await PushSendAsync(send, PushType.SyncSendDelete);
}
private async Task PushSendAsync(Send send, PushType type)
{
if (send.UserId.HasValue)
{
var message = new SyncSendPushNotification
{
Id = send.Id,
UserId = send.UserId.Value,
RevisionDate = send.RevisionDate
};
await SendMessageAsync(type, message, false);
}
}
2018-08-16 12:05:01 -04:00
private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext)
{
var contextId = GetContextIdentifier(excludeCurrentContext);
var request = new PushNotificationData<T>(type, payload, contextId);
await SendAsync(HttpMethod.Post, "send", request);
2018-08-16 12:05:01 -04:00
}
private string GetContextIdentifier(bool excludeCurrentContext)
{
if (!excludeCurrentContext)
2018-08-16 12:05:01 -04:00
{
return null;
}
var currentContext = _httpContextAccessor?.HttpContext?.
RequestServices.GetService(typeof(ICurrentContext)) as ICurrentContext;
2018-08-16 12:05:01 -04:00
return currentContext?.DeviceIdentifier;
}
2019-03-19 00:39:03 -04:00
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
string deviceId = null)
2018-08-16 12:05:01 -04:00
{
// Noop
return Task.FromResult(0);
}
2019-03-19 00:39:03 -04:00
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
string deviceId = null)
2018-08-16 12:05:01 -04:00
{
// Noop
return Task.FromResult(0);
}
}
}