-
Notifications
You must be signed in to change notification settings - Fork 0
Automatically recover stale mail IDs and retry once #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using System.Text.Json; | ||
| using Microsoft.Data.Sqlite; | ||
|
|
||
| namespace BetterMail.Core; | ||
|
|
||
| public sealed partial class EncryptedMailStore | ||
| { | ||
| public Task<MailAction?> ClaimAutomaticRecoveryAsync(string id, CancellationToken token = default) => | ||
| WithLockAsync<MailAction?>(async connection => | ||
| { | ||
| var actions = await ReadActionsAsync(connection, null, token).ConfigureAwait(false); | ||
| var action = actions.FirstOrDefault(action => action.Id == id); | ||
| if (action is not { CanAutomaticallyRecover: true } || actions.TakeWhile(item => item.Id != id).Any(item => | ||
| item.MailboxId == action.MailboxId && item.ItemId == action.ItemId && !item.Accepted)) return null; | ||
| action = action with { AutomaticRecoveryAttempted = true, | ||
| AutomaticRecoveryDetails = "Automatic recovery was attempted. Check status if it did not complete." }; | ||
| await WriteActionAsync(connection, null, action, token).ConfigureAwait(false); | ||
| return action; | ||
| }, token); | ||
|
|
||
| public Task RecordAutomaticRecoveryAsync(string id, string details, CancellationToken token = default) => | ||
| WithLockAsync(async connection => | ||
| { | ||
| var action = (await ReadActionsAsync(connection, null, token).ConfigureAwait(false)).FirstOrDefault(action => action.Id == id); | ||
| if (action is { AutomaticRecoveryAttempted: true }) | ||
| await WriteActionAsync(connection, null, action with { AutomaticRecoveryDetails = details }, token).ConfigureAwait(false); | ||
| }, token); | ||
|
|
||
| public Task<bool> RecoverMailActionAsync(MailAction expected, MailMessage verified, string expectedIdentity, | ||
| CancellationToken token = default) => WithLockAsync(async connection => | ||
| { | ||
| await using var transaction = (SqliteTransaction)await connection.BeginTransactionAsync(token).ConfigureAwait(false); | ||
| var actions = await ReadActionsAsync(connection, transaction, token).ConfigureAwait(false); | ||
| var current = actions.FirstOrDefault(action => action.Id == expected.Id); | ||
| // Lookup happens outside the store lock. Reject cancellation, edits or execution since it began. | ||
| if (current is not { CanRecover: true } || JsonSerializer.Serialize(current) != JsonSerializer.Serialize(expected) || | ||
| verified.MailboxId != current.MailboxId || verified.IsDeleted || | ||
| string.IsNullOrWhiteSpace(expectedIdentity) || verified.InternetMessageId != expectedIdentity || | ||
| string.IsNullOrWhiteSpace(verified.ProviderId) || string.IsNullOrWhiteSpace(verified.FolderId)) return false; | ||
| var related = actions.Where(action => action.MailboxId == current.MailboxId && action.ItemId == current.ItemId && | ||
| action.Kind is MailActionKind.Move or MailActionKind.UpdateState).ToArray(); | ||
| if (related.Any(action => action.Running) || related.TakeWhile(action => action.Id != current.Id).Any(action => !action.Accepted)) return false; | ||
| var alreadyMoved = current.Kind == MailActionKind.Move && verified.FolderId == current.DestinationId; | ||
| foreach (var action in related) | ||
| await WriteActionAsync(connection, transaction, action with | ||
| { | ||
| ProviderId = verified.ProviderId, | ||
| PreviousProviderIds = (action.PreviousProviderIds ?? []).Append(current.ProviderId!).Distinct().ToArray(), | ||
| SourceFolderId = action.Kind == MailActionKind.Move && !action.Accepted ? verified.FolderId : action.SourceFolderId, | ||
| SourceWasUnread = action.Kind == MailActionKind.Move && !action.Accepted ? verified.IsUnread : action.SourceWasUnread, | ||
| Accepted = action.Accepted || action.Id == current.Id && alreadyMoved, | ||
| RecoveredAtFailureCount = action.Id == current.Id ? action.FailureCount : action.RecoveredAtFailureCount, | ||
| RetryAuthorizedAtFailureCount = action.Id == current.Id ? action.FailureCount : action.RetryAuthorizedAtFailureCount | ||
| }, token).ConfigureAwait(false); | ||
| var desired = related.LastOrDefault(action => action.Kind == MailActionKind.Move && !action.Accepted && | ||
| !(action.Id == current.Id && alreadyMoved)); | ||
| await UpsertMessageAsync(connection, transaction, verified with | ||
| { | ||
| FolderId = desired?.DestinationId ?? verified.FolderId, | ||
| IsRead = desired is not null || verified.IsRead | ||
| }, token).ConfigureAwait(false); | ||
| foreach (var state in related.Where(action => action.Kind == MailActionKind.UpdateState && !action.Accepted)) | ||
| await SetActionStateAsync(connection, transaction, state with { ProviderId = verified.ProviderId }, false, token).ConfigureAwait(false); | ||
| if (verified.ProviderId != current.ProviderId) | ||
| await DeleteMessageAsync(connection, transaction, current.MailboxId, current.ProviderId!, token).ConfigureAwait(false); | ||
| await transaction.CommitAsync(token).ConfigureAwait(false); | ||
| return true; | ||
| }, token); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a standalone
UpdateStateaction is recovered to a new provider ID, this deletes the old cached row but records no durable tombstone or alias. The repaired state action is deleted byCompleteMessageStateAsync, after which a delayed source-folder page containing the old ID passes throughApplySyncPageAsyncand is blindly upserted, leaving both the valid and obsolete copies visible and allowing subsequent actions to target the dead ID again. Preserve enough recovery metadata to reject stale sync records for the replaced ID.Useful? React with 👍 / 👎.