1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-08 14:38:15 -05:00

enabled send and added send sync notifications (#1106)

This commit is contained in:
Addison Beck 2021-01-22 16:16:40 -05:00 committed by GitHub
parent 3555b15b91
commit b8a2158626
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 182 additions and 2 deletions

View File

@ -84,7 +84,6 @@ namespace Bit.Api.Controllers
[HttpPost("")] [HttpPost("")]
public async Task<SendResponseModel> Post([FromBody] SendRequestModel model) public async Task<SendResponseModel> Post([FromBody] SendRequestModel model)
{ {
throw new NotFoundException();
model.ValidateCreation(); model.ValidateCreation();
var userId = _userService.GetProperUserId(User).Value; var userId = _userService.GetProperUserId(User).Value;
var send = model.ToSend(userId, _sendService); var send = model.ToSend(userId, _sendService);
@ -97,7 +96,6 @@ namespace Bit.Api.Controllers
[DisableFormValueModelBinding] [DisableFormValueModelBinding]
public async Task<SendResponseModel> PostFile() public async Task<SendResponseModel> PostFile()
{ {
throw new NotFoundException();
if (!Request?.ContentType.Contains("multipart/") ?? true) if (!Request?.ContentType.Contains("multipart/") ?? true)
{ {
throw new BadRequestException("Invalid content."); throw new BadRequestException("Invalid content.");

View File

@ -16,5 +16,9 @@
SyncSettings = 10, SyncSettings = 10,
LogOut = 11, LogOut = 11,
SyncSendCreate = 12,
SyncSendUpdate = 13,
SyncSendDelete = 14,
} }
} }

View File

@ -39,4 +39,11 @@ namespace Bit.Core.Models
public Guid UserId { get; set; } public Guid UserId { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }
} }
public class SyncSendPushNotification
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public DateTime RevisionDate { get; set; }
}
} }

View File

@ -19,6 +19,9 @@ namespace Bit.Core.Services
Task PushSyncOrgKeysAsync(Guid userId); Task PushSyncOrgKeysAsync(Guid userId);
Task PushSyncSettingsAsync(Guid userId); Task PushSyncSettingsAsync(Guid userId);
Task PushLogOutAsync(Guid userId); Task PushLogOutAsync(Guid userId);
Task PushSyncSendCreateAsync(Send send);
Task PushSyncSendUpdateAsync(Send send);
Task PushSyncSendDeleteAsync(Send send);
Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier, string deviceId = null); Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier, string deviceId = null);
Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier, Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
string deviceId = null); string deviceId = null);

View File

@ -135,6 +135,36 @@ namespace Bit.Core.Services
await SendMessageAsync(type, message, false); 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, true);
}
}
private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext) private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext)
{ {
var contextId = GetContextIdentifier(excludeCurrentContext); var contextId = GetContextIdentifier(excludeCurrentContext);

View File

@ -122,6 +122,24 @@ namespace Bit.Core.Services
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task PushSyncSendCreateAsync(Send send)
{
PushToServices((s) => s.PushSyncSendCreateAsync(send));
return Task.FromResult(0);
}
public Task PushSyncSendUpdateAsync(Send send)
{
PushToServices((s) => s.PushSyncSendUpdateAsync(send));
return Task.FromResult(0);
}
public Task PushSyncSendDeleteAsync(Send send)
{
PushToServices((s) => s.PushSyncSendDeleteAsync(send));
return Task.FromResult(0);
}
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier, public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
string deviceId = null) string deviceId = null)
{ {

View File

@ -136,6 +136,36 @@ namespace Bit.Core.Services
await SendPayloadToUserAsync(userId, type, message, false); await SendPayloadToUserAsync(userId, 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 SendPayloadToUserAsync(message.UserId, type, message, true);
}
}
private async Task SendPayloadToUserAsync(Guid userId, PushType type, object payload, bool excludeCurrentContext) private async Task SendPayloadToUserAsync(Guid userId, PushType type, object payload, bool excludeCurrentContext)
{ {
await SendPayloadToUserAsync(userId.ToString(), type, payload, GetContextIdentifier(excludeCurrentContext)); await SendPayloadToUserAsync(userId.ToString(), type, payload, GetContextIdentifier(excludeCurrentContext));

View File

@ -142,6 +142,36 @@ namespace Bit.Core.Services
await SendMessageAsync(type, message, false); 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);
}
}
private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext) private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext)
{ {
var contextId = GetContextIdentifier(excludeCurrentContext); var contextId = GetContextIdentifier(excludeCurrentContext);

View File

@ -139,6 +139,36 @@ namespace Bit.Core.Services
await SendPayloadToUserAsync(userId, type, message, false); await SendPayloadToUserAsync(userId, 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 SendPayloadToUserAsync(message.UserId, type, message, true);
}
}
private async Task SendPayloadToUserAsync(Guid userId, PushType type, object payload, bool excludeCurrentContext) private async Task SendPayloadToUserAsync(Guid userId, PushType type, object payload, bool excludeCurrentContext)
{ {
var request = new PushSendRequestModel var request = new PushSendRequestModel

View File

@ -18,6 +18,7 @@ namespace Bit.Core.Services
private readonly IOrganizationRepository _organizationRepository; private readonly IOrganizationRepository _organizationRepository;
private readonly ISendFileStorageService _sendFileStorageService; private readonly ISendFileStorageService _sendFileStorageService;
private readonly IPasswordHasher<User> _passwordHasher; private readonly IPasswordHasher<User> _passwordHasher;
private readonly IPushNotificationService _pushService;
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
public SendService( public SendService(
@ -27,6 +28,7 @@ namespace Bit.Core.Services
IOrganizationRepository organizationRepository, IOrganizationRepository organizationRepository,
ISendFileStorageService sendFileStorageService, ISendFileStorageService sendFileStorageService,
IPasswordHasher<User> passwordHasher, IPasswordHasher<User> passwordHasher,
IPushNotificationService pushService,
GlobalSettings globalSettings) GlobalSettings globalSettings)
{ {
_sendRepository = sendRepository; _sendRepository = sendRepository;
@ -35,6 +37,7 @@ namespace Bit.Core.Services
_organizationRepository = organizationRepository; _organizationRepository = organizationRepository;
_sendFileStorageService = sendFileStorageService; _sendFileStorageService = sendFileStorageService;
_passwordHasher = passwordHasher; _passwordHasher = passwordHasher;
_pushService = pushService;
_globalSettings = globalSettings; _globalSettings = globalSettings;
} }
@ -43,11 +46,13 @@ namespace Bit.Core.Services
if (send.Id == default(Guid)) if (send.Id == default(Guid))
{ {
await _sendRepository.CreateAsync(send); await _sendRepository.CreateAsync(send);
await _pushService.PushSyncSendCreateAsync(send);
} }
else else
{ {
send.RevisionDate = DateTime.UtcNow; send.RevisionDate = DateTime.UtcNow;
await _sendRepository.UpsertAsync(send); await _sendRepository.UpsertAsync(send);
await _pushService.PushSyncSendUpdateAsync(send);
} }
} }
@ -129,6 +134,7 @@ namespace Bit.Core.Services
var data = JsonConvert.DeserializeObject<SendFileData>(send.Data); var data = JsonConvert.DeserializeObject<SendFileData>(send.Data);
await _sendFileStorageService.DeleteFileAsync(data.Id); await _sendFileStorageService.DeleteFileAsync(data.Id);
} }
await _pushService.PushSyncSendDeleteAsync(send);
} }
// Response: Send, password required, password invalid // Response: Send, password required, password invalid

View File

@ -63,6 +63,21 @@ namespace Bit.Core.Services
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task PushSyncSendCreateAsync(Send send)
{
return Task.FromResult(0);
}
public Task PushSyncSendDeleteAsync(Send send)
{
return Task.FromResult(0);
}
public Task PushSyncSendUpdateAsync(Send send)
{
return Task.FromResult(0);
}
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier, public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
string deviceId = null) string deviceId = null)
{ {

View File

@ -54,6 +54,15 @@ namespace Bit.Notifications
await hubContext.Clients.User(userNotification.Payload.UserId.ToString()) await hubContext.Clients.User(userNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", userNotification, cancellationToken); .SendAsync("ReceiveMessage", userNotification, cancellationToken);
break; break;
case PushType.SyncSendCreate:
case PushType.SyncSendUpdate:
case PushType.SyncSendDelete:
var sendNotification =
JsonConvert.DeserializeObject<PushNotificationData<SyncSendPushNotification>>(
notificationJson);
await hubContext.Clients.User(sendNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", sendNotification, cancellationToken);
break;
default: default:
break; break;
} }