mirror of
https://github.com/bitwarden/mobile
synced 2025-12-05 23:53:33 +00:00
Update send notifications to only sync sends
This commit is contained in:
@@ -133,10 +133,20 @@ namespace Bit.App.Services
|
||||
break;
|
||||
case NotificationType.SyncSendCreate:
|
||||
case NotificationType.SyncSendUpdate:
|
||||
case NotificationType.SyncSendDelete:
|
||||
if (isAuthenticated)
|
||||
var sendCreateUpdateMessage = JsonConvert.DeserializeObject<SyncSendNotification>(
|
||||
notification.Payload);
|
||||
if (isAuthenticated && sendCreateUpdateMessage.UserId == myUserId)
|
||||
{
|
||||
await _syncService.Value.FullSyncAsync(false);
|
||||
await _syncService.Value.SyncUpsertSendAsync(sendCreateUpdateMessage,
|
||||
notification.Type == NotificationType.SyncCipherUpdate);
|
||||
}
|
||||
break;
|
||||
case NotificationType.SyncSendDelete:
|
||||
var sendDeleteMessage = JsonConvert.DeserializeObject<SyncSendNotification>(
|
||||
notification.Payload);
|
||||
if (isAuthenticated && sendDeleteMessage.UserId == myUserId)
|
||||
{
|
||||
await _syncService.Value.SyncDeleteSendAsync(sendDeleteMessage);
|
||||
}
|
||||
break;
|
||||
case NotificationType.AuthRequest:
|
||||
|
||||
@@ -12,8 +12,10 @@ namespace Bit.Core.Abstractions
|
||||
Task SetLastSyncAsync(DateTime date);
|
||||
Task<bool> SyncDeleteCipherAsync(SyncCipherNotification notification);
|
||||
Task<bool> SyncDeleteFolderAsync(SyncFolderNotification notification);
|
||||
Task<bool> SyncDeleteSendAsync(SyncSendNotification notification);
|
||||
Task<bool> SyncUpsertCipherAsync(SyncCipherNotification notification, bool isEdit);
|
||||
Task<bool> SyncUpsertFolderAsync(SyncFolderNotification notification, bool isEdit);
|
||||
Task<bool> SyncUpsertSendAsync(SyncSendNotification notification, bool isEdit);
|
||||
// Passwordless code will be moved to an independent service in future techdept
|
||||
Task SyncPasswordlessLoginRequestsAsync();
|
||||
}
|
||||
|
||||
@@ -33,6 +33,13 @@ namespace Bit.Core.Models.Response
|
||||
public string UserId { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
}
|
||||
|
||||
public class SyncSendNotification
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string UserId { get; set; }
|
||||
public DateTime RevisionDate { get; set; }
|
||||
}
|
||||
|
||||
public class PasswordlessRequestNotification
|
||||
{
|
||||
|
||||
@@ -274,6 +274,78 @@ namespace Bit.Core.Services
|
||||
return SyncCompleted(false);
|
||||
}
|
||||
|
||||
public async Task<bool> SyncUpsertSendAsync(SyncSendNotification notification, bool isEdit)
|
||||
{
|
||||
SyncStarted();
|
||||
if (await _stateService.IsAuthenticatedAsync())
|
||||
{
|
||||
try
|
||||
{
|
||||
var shouldUpdate = true;
|
||||
var localSend = await _sendService.GetAsync(notification.Id);
|
||||
if (localSend != null && localSend.RevisionDate >= notification.RevisionDate)
|
||||
{
|
||||
shouldUpdate = false;
|
||||
}
|
||||
|
||||
if (shouldUpdate)
|
||||
{
|
||||
if (isEdit)
|
||||
{
|
||||
shouldUpdate = localSend != null;
|
||||
}
|
||||
else
|
||||
{
|
||||
shouldUpdate = localSend == null;
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldUpdate)
|
||||
{
|
||||
var remoteSend = await _apiService.GetSendAsync(notification.Id);
|
||||
if (remoteSend != null)
|
||||
{
|
||||
var userId = await _stateService.GetActiveUserIdAsync();
|
||||
await _sendService.UpsertAsync(new SendData(remoteSend, userId));
|
||||
_messagingService.Send("syncedUpsertedSend", new Dictionary<string, string>
|
||||
{
|
||||
["sendId"] = notification.Id
|
||||
});
|
||||
return SyncCompleted(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ApiException e)
|
||||
{
|
||||
if (e.Error != null && e.Error.StatusCode == System.Net.HttpStatusCode.NotFound && isEdit)
|
||||
{
|
||||
await _sendService.DeleteAsync(notification.Id);
|
||||
_messagingService.Send("syncedDeletedSend", new Dictionary<string, string>
|
||||
{
|
||||
["sendId"] = notification.Id
|
||||
});
|
||||
return SyncCompleted(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
return SyncCompleted(false);
|
||||
}
|
||||
|
||||
public async Task<bool> SyncDeleteSendAsync(SyncSendNotification notification)
|
||||
{
|
||||
SyncStarted();
|
||||
if (await _stateService.IsAuthenticatedAsync())
|
||||
{
|
||||
await _sendService.DeleteAsync(notification.Id);
|
||||
_messagingService.Send("syncedDeletedSend", new Dictionary<string, string>
|
||||
{
|
||||
["sendId"] = notification.Id
|
||||
});
|
||||
return SyncCompleted(true);
|
||||
}
|
||||
return SyncCompleted(false);
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
private void SyncStarted()
|
||||
|
||||
Reference in New Issue
Block a user