Implemented cache reset for Gmail history id expiration. (#581)

This commit is contained in:
Burak Kaan Köse
2025-02-22 23:09:53 +01:00
committed by GitHub
parent bd5b51c62f
commit 7e05d05f94
16 changed files with 204 additions and 36 deletions

View File

@@ -22,15 +22,18 @@ public class AccountService : BaseDatabaseService, IAccountService
public IAuthenticator ExternalAuthenticationAuthenticator { get; set; }
private readonly ISignatureService _signatureService;
private readonly IMimeFileService _mimeFileService;
private readonly IPreferencesService _preferencesService;
private readonly ILogger _logger = Log.ForContext<AccountService>();
public AccountService(IDatabaseService databaseService,
ISignatureService signatureService,
IMimeFileService mimeFileService,
IPreferencesService preferencesService) : base(databaseService)
{
_signatureService = signatureService;
_mimeFileService = mimeFileService;
_preferencesService = preferencesService;
}
@@ -262,12 +265,26 @@ public class AccountService : BaseDatabaseService, IAccountService
private Task<MergedInbox> GetMergedInboxInformationAsync(Guid mergedInboxId)
=> Connection.Table<MergedInbox>().FirstOrDefaultAsync(a => a.Id == mergedInboxId);
public async Task DeleteAccountMailCacheAsync(Guid accountId, AccountCacheResetReason accountCacheResetReason)
{
var deleteQuery = new Query("MailCopy")
.WhereIn("Id", q => q
.From("MailCopy")
.Select("Id")
.WhereIn("FolderId", q2 => q2
.From("MailItemFolder")
.Select("Id")
.Where("MailAccountId", accountId)
)).AsDelete();
await Connection.ExecuteAsync(deleteQuery.GetRawQuery());
WeakReferenceMessenger.Default.Send(new AccountCacheResetMessage(accountId, accountCacheResetReason));
}
public async Task DeleteAccountAsync(MailAccount account)
{
// TODO: Delete mime messages and attachments.
// TODO: Delete token cache by underlying provider.
await Connection.ExecuteAsync("DELETE FROM MailCopy WHERE Id IN(SELECT Id FROM MailCopy WHERE FolderId IN (SELECT Id from MailItemFolder WHERE MailAccountId == ?))", account.Id);
await DeleteAccountMailCacheAsync(account.Id, AccountCacheResetReason.AccountRemoval);
await Connection.Table<MailItemFolder>().DeleteAsync(a => a.MailAccountId == account.Id);
await Connection.Table<AccountSignature>().DeleteAsync(a => a.MailAccountId == account.Id);
@@ -302,6 +319,8 @@ public class AccountService : BaseDatabaseService, IAccountService
await Connection.DeleteAsync(account);
await _mimeFileService.DeleteUserMimeCacheAsync(account.Id).ConfigureAwait(false);
// Clear out or set up a new startup entity id.
// Next account after the deleted one will be the startup account.
@@ -319,8 +338,6 @@ public class AccountService : BaseDatabaseService, IAccountService
}
}
ReportUIChange(new AccountRemovedMessage(account));
}

View File

@@ -119,6 +119,18 @@ public class MailService : BaseDatabaseService, IMailService
return mails;
}
public async Task<bool> HasAccountAnyDraftAsync(Guid accountId)
{
// Get the draft folder.
var draftFolder = await _folderService.GetSpecialFolderByAccountIdAsync(accountId, SpecialFolderType.Draft);
if (draftFolder == null) return false;
var draftCount = await Connection.Table<MailCopy>().Where(a => a.FolderId == draftFolder.Id).CountAsync();
return draftCount > 0;
}
public async Task<List<MailCopy>> GetUnreadMailsByFolderIdAsync(Guid folderId)
{
var unreadMails = await Connection.QueryAsync<MailCopy>("SELECT * FROM MailCopy WHERE FolderId = ? AND IsRead = 0", folderId);
@@ -143,7 +155,7 @@ public class MailService : BaseDatabaseService, IMailService
//}
// SQLite PCL doesn't support joins.
// We make the query using SqlKatka and execute it directly on SQLite-PCL.
// We make the query using SqlKata and execute it directly on SQLite-PCL.
var query = new Query("MailCopy")
.Join("MailItemFolder", "MailCopy.FolderId", "MailItemFolder.Id")

View File

@@ -176,4 +176,22 @@ public class MimeFileService : IMimeFileService
return renderingModel;
}
public async Task DeleteUserMimeCacheAsync(Guid accountId)
{
var mimeFolderPath = await _nativeAppService.GetMimeMessageStoragePath().ConfigureAwait(false);
var mimeDirectory = Path.Combine(mimeFolderPath, accountId.ToString());
try
{
if (Directory.Exists(mimeDirectory))
{
Directory.Delete(mimeDirectory, true);
}
}
catch (Exception ex)
{
Log.Error(ex, "Failed to remove user's mime cache folder.");
}
}
}