mirror of
https://github.com/bitwarden/android.git
synced 2025-03-28 00:29:24 +03:00
sync attachment removals
This commit is contained in:
parent
ac3fdbc2cd
commit
0d672c4f99
3 changed files with 52 additions and 0 deletions
|
@ -8,5 +8,6 @@ namespace Bit.App.Abstractions
|
||||||
public interface IAttachmentRepository : IRepository<AttachmentData, string>
|
public interface IAttachmentRepository : IRepository<AttachmentData, string>
|
||||||
{
|
{
|
||||||
Task<IEnumerable<AttachmentData>> GetAllByLoginIdAsync(string loginId);
|
Task<IEnumerable<AttachmentData>> GetAllByLoginIdAsync(string loginId);
|
||||||
|
Task<IEnumerable<AttachmentData>> GetAllByUserIdAsync(string userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,5 +18,19 @@ namespace Bit.App.Repositories
|
||||||
var attachments = Connection.Table<AttachmentData>().Where(a => a.LoginId == loginId).Cast<AttachmentData>();
|
var attachments = Connection.Table<AttachmentData>().Where(a => a.LoginId == loginId).Cast<AttachmentData>();
|
||||||
return Task.FromResult(attachments);
|
return Task.FromResult(attachments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task<IEnumerable<AttachmentData>> GetAllByUserIdAsync(string userId)
|
||||||
|
{
|
||||||
|
var attachments = Connection.Query<AttachmentData>(@"
|
||||||
|
SELECT
|
||||||
|
A.*
|
||||||
|
FROM
|
||||||
|
Attachment AS A
|
||||||
|
INNER JOIN
|
||||||
|
Site AS S ON S.Id = A.LoginId
|
||||||
|
WHERE
|
||||||
|
S.UserId = ?", userId);
|
||||||
|
return Task.FromResult<IEnumerable<AttachmentData>>(attachments);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,6 +82,10 @@ namespace Bit.App.Services
|
||||||
case Enums.CipherType.Login:
|
case Enums.CipherType.Login:
|
||||||
var loginData = new LoginData(cipher.Result, _authService.UserId);
|
var loginData = new LoginData(cipher.Result, _authService.UserId);
|
||||||
await _loginRepository.UpsertAsync(loginData).ConfigureAwait(false);
|
await _loginRepository.UpsertAsync(loginData).ConfigureAwait(false);
|
||||||
|
|
||||||
|
var localAttachments = (await _attachmentRepository.GetAllByLoginIdAsync(loginData.Id)
|
||||||
|
.ConfigureAwait(false));
|
||||||
|
|
||||||
if(cipher.Result.Attachments != null)
|
if(cipher.Result.Attachments != null)
|
||||||
{
|
{
|
||||||
foreach(var attachment in cipher.Result.Attachments)
|
foreach(var attachment in cipher.Result.Attachments)
|
||||||
|
@ -90,6 +94,19 @@ namespace Bit.App.Services
|
||||||
await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false);
|
await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(localAttachments != null)
|
||||||
|
{
|
||||||
|
foreach(var attachment in localAttachments
|
||||||
|
.Where(a => !cipher.Result.Attachments.Any(sa => sa.Id == a.Id)))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _attachmentRepository.DeleteAsync(attachment.Id).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch(SQLite.SQLiteException) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
SyncCompleted(false);
|
SyncCompleted(false);
|
||||||
|
@ -363,6 +380,11 @@ namespace Bit.App.Services
|
||||||
.Select(s => s.First())
|
.Select(s => s.First())
|
||||||
.ToDictionary(s => s.Id);
|
.ToDictionary(s => s.Id);
|
||||||
|
|
||||||
|
var localAttachments = (await _attachmentRepository.GetAllByUserIdAsync(_authService.UserId)
|
||||||
|
.ConfigureAwait(false))
|
||||||
|
.GroupBy(a => a.LoginId)
|
||||||
|
.ToDictionary(g => g.Key);
|
||||||
|
|
||||||
foreach(var serverLogin in serverLogins)
|
foreach(var serverLogin in serverLogins)
|
||||||
{
|
{
|
||||||
if(!_authService.IsAuthenticated)
|
if(!_authService.IsAuthenticated)
|
||||||
|
@ -372,6 +394,8 @@ namespace Bit.App.Services
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var localLogin = localLogins.ContainsKey(serverLogin.Value.Id) ? localLogins[serverLogin.Value.Id] : null;
|
||||||
|
|
||||||
var data = new LoginData(serverLogin.Value, _authService.UserId);
|
var data = new LoginData(serverLogin.Value, _authService.UserId);
|
||||||
await _loginRepository.UpsertAsync(data).ConfigureAwait(false);
|
await _loginRepository.UpsertAsync(data).ConfigureAwait(false);
|
||||||
|
|
||||||
|
@ -383,6 +407,19 @@ namespace Bit.App.Services
|
||||||
await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false);
|
await _attachmentRepository.UpsertAsync(attachmentData).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(localLogin != null && localAttachments != null && localAttachments.ContainsKey(localLogin.Id))
|
||||||
|
{
|
||||||
|
foreach(var attachment in localAttachments[localLogin.Id]
|
||||||
|
.Where(a => !serverLogin.Value.Attachments.Any(sa => sa.Id == a.Id)))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _attachmentRepository.DeleteAsync(attachment.Id).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch(SQLite.SQLiteException) { }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(SQLite.SQLiteException) { }
|
catch(SQLite.SQLiteException) { }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue