Skip to content

Commit 7cc6204

Browse files
author
Timothy Dodd
committed
Refactor email storage logic and update default behavior
Updated `EmailSettings` to set `StoreMovedMessages` default to `false`, changing the default behavior for storing moved messages. Refactored `ProcessSingleMoveOperationAsync` to remove the `dbConnection` parameter and conditionally save emails to the database based on the `StoreMovedMessages` configuration. Encapsulated database connection logic within `SaveEmailsToDatabase`, removing the need to pass external connections and ensuring proper disposal of connections. These changes simplify database handling, improve modularity, and introduce a feature toggle for storing moved messages.
1 parent ee155a5 commit 7cc6204

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/MailZort/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class EmailSettings
9090
public string? Password { get; set; }
9191
public int Port { get; set; }
9292
public bool UseSsl { get; set; }
93-
public bool StoreMovedMessages { get; set; } = true;
93+
public bool StoreMovedMessages { get; set; } = false;
9494
public int BatchProcessingIntervalSeconds { get; set; } = 60; // Default to 60 seconds
9595
}
9696
public class RuleTrigger

src/MailZort/Services/EmailMonitoringService.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,11 @@ private async Task ProcessQueuedMoveOperationsAsync()
366366
var processedCount = 0;
367367
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
368368

369-
using var dbConnection = _mailDb.GetConnection();
370-
371369
while (_moveQueue.TryDequeue(out var moveOperation) && moveOperation != null)
372370
{
373371
try
374372
{
375-
await ProcessSingleMoveOperationAsync(dbConnection, moveOperation);
373+
await ProcessSingleMoveOperationAsync(moveOperation);
376374
processedCount++;
377375
}
378376
catch (Exception ex)
@@ -394,7 +392,7 @@ private async Task ProcessQueuedMoveOperationsAsync()
394392
}
395393
}
396394

397-
private async Task ProcessSingleMoveOperationAsync(IDbConnection dbConnection, EmailMoveOperation moveOperation)
395+
private async Task ProcessSingleMoveOperationAsync(EmailMoveOperation moveOperation)
398396
{
399397
if (!moveOperation.Emails.Any())
400398
return;
@@ -409,12 +407,13 @@ private async Task ProcessSingleMoveOperationAsync(IDbConnection dbConnection, E
409407
var destinationFolder = GetDestinationFolder(moveOperation.DestinationFolder);
410408

411409
// Perform the move
412-
413410
await sourceFolder.MoveToAsync(moveOperation.EmailIds, destinationFolder);
414411

415-
// Save to database
416-
SaveEmailsToDatabase(dbConnection, moveOperation.Emails);
417-
412+
// Save to database if enabled
413+
if (_config.StoreMovedMessages)
414+
{
415+
SaveEmailsToDatabase(moveOperation.Emails);
416+
}
418417

419418
_logger.LogInformation("📁 Moved {Count} emails from {Source} to {Destination}",
420419
moveOperation.Emails.Count, moveOperation.SourceFolder, moveOperation.DestinationFolder);
@@ -439,8 +438,9 @@ private IMailFolder GetDestinationFolder(string folderName)
439438
return _client!.GetFolder(folderName);
440439
}
441440

442-
private void SaveEmailsToDatabase(IDbConnection dbConnection, List<Email> emails)
441+
private void SaveEmailsToDatabase(List<Email> emails)
443442
{
443+
using var dbConnection = _mailDb.GetConnection();
444444
foreach (var email in emails)
445445
{
446446
try

0 commit comments

Comments
 (0)