Files
AI-Studio/app/MindWork AI Studio/Tools/Mail/MailRemovalGuard.cs
T
Thorsten Sommer c4400c0ff6
Build and Release / Determine run mode (push) Waiting to run
Build and Release / Read metadata (push) Blocked by required conditions
Build and Release / Sync Flatpak repo (push) Blocked by required conditions
Build and Release / Collect Flatpak artifacts (push) Blocked by required conditions
Build and Release / Verify (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Blocked by required conditions
Added mailboxes as local data sources (#1022)
2026-10-04 12:26:26 +02:00

59 lines
2.7 KiB
C#

using AIStudio.Tools.Databases.IndexStore;
namespace AIStudio.Tools.Mail;
/// <summary>
/// Keeps a sync from removing a large part of a mailbox from the index without asking.
/// </summary>
/// <remarks>
/// Nothing on the server is touched either way. But a mail removed from the index has to be
/// embedded again should it come back, and much of a mailbox drops out at once for reasons the user
/// may not have meant: a smaller folder or period was picked, a folder was renamed on the server,
/// or the server lists a folder as empty for a while. Embedding all of that again costs money with
/// a cloud provider and hours with a large mailbox, so the sync asks first.
///
/// A few mails are not worth a question, however large their share: a mailbox of fifty mails is
/// embedded again in no time. Nor is a small share of a large mailbox, such as the mails of one day
/// leaving the period.
/// </remarks>
public static class MailRemovalGuard
{
/// <summary>
/// How many mails a removal has to reach at least before anybody is asked.
/// </summary>
public const int MIN_REMOVAL_COUNT = 100;
/// <summary>
/// Which share of the indexed mails a removal has to reach at least before anybody is asked.
/// </summary>
public const double MIN_REMOVAL_SHARE = 0.2;
/// <summary>
/// Whether a removal is large enough to ask about.
/// </summary>
/// <param name="indexedCount">How many the index holds.</param>
/// <param name="removalCount">How many would go.</param>
/// <returns>True when the user has to agree first.</returns>
public static bool IsMassRemoval(int indexedCount, int removalCount) => removalCount >= MIN_REMOVAL_COUNT && removalCount >= indexedCount * MIN_REMOVAL_SHARE;
/// <summary>
/// Decides whether a sync may remove what it found gone.
/// </summary>
/// <remarks>
/// The user agrees to a number, not to whatever a sync comes up with: an approval counts only
/// for the very count which was held back and shown, cf. ApprovePendingMailRemovalAsync.
/// </remarks>
/// <param name="indexedCount">How many the index holds.</param>
/// <param name="removalCount">How many would go.</param>
/// <param name="syncState">How the sync of the mailbox stands, with any removal held back before.</param>
/// <returns>The decision.</returns>
public static MailRemovalDecision Decide(int indexedCount, int removalCount, MailboxSyncState syncState)
{
if (!IsMassRemoval(indexedCount, removalCount))
return MailRemovalDecision.PROCEED;
return syncState is { PendingRemovalApprovedUtc: not null } && syncState.PendingRemovalCount == removalCount
? MailRemovalDecision.PROCEED
: MailRemovalDecision.HOLD_BACK;
}
}