mirror of
https://github.com/bitwarden/mobile
synced 2026-01-03 09:03:35 +00:00
[PM-2901] Synchronize sends on send creation/update/deletion notification (#2606)
* Add sync on send create/update/delete notification * Update send notifications to only sync sends * Fix incorrect notification type in PushNotificationListenerService Co-authored-by: aj-rosado <109146700+aj-rosado@users.noreply.github.com> * Invert if to improve readability * Simplify shouldUpdate logic in SyncUpsertSendAsync * Further simplify SyncService code * Fix if condition in SyncService Co-authored-by: aj-rosado <109146700+aj-rosado@users.noreply.github.com> * Fixed whitespace formatting --------- Co-authored-by: aj-rosado <109146700+aj-rosado@users.noreply.github.com> Co-authored-by: Andre Rosado <arosado@bitwarden.com>
This commit is contained in:
@@ -274,6 +274,66 @@ namespace Bit.Core.Services
|
||||
return SyncCompleted(false);
|
||||
}
|
||||
|
||||
public async Task<bool> SyncUpsertSendAsync(SyncSendNotification notification, bool isEdit)
|
||||
{
|
||||
SyncStarted();
|
||||
if (!await _stateService.IsAuthenticatedAsync())
|
||||
{
|
||||
return SyncCompleted(false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var localSend = await _sendService.GetAsync(notification.Id);
|
||||
if ((localSend != null && localSend.RevisionDate >= notification.RevisionDate)
|
||||
|| (isEdit && localSend == null) || (!isEdit && localSend != null))
|
||||
{
|
||||
return SyncCompleted(false);
|
||||
}
|
||||
|
||||
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