Files
Wino-Mail/Wino.Services/MailService.cs

1109 lines
43 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
2024-04-18 01:44:37 +02:00
using System.Threading.Tasks;
using MimeKit;
using Serilog;
using SqlKata;
using Wino.Core.Domain;
using Wino.Core.Domain.Entities.Mail;
using Wino.Core.Domain.Entities.Shared;
2024-04-18 01:44:37 +02:00
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Exceptions;
Full trust Wino Server implementation. (#295) * Separation of messages. Introducing Wino.Messages library. * Wino.Server and Wino.Packaging projects. Enabling full trust for UWP and app service connection manager basics. * Remove debug code. * Enable generating assembly info to deal with unsupported os platform warnings. * Fix server-client connection. * UIMessage communication. Single instancing for server and re-connection mechanism on suspension. * Removed IWinoSynchronizerFactory from UWP project. * Removal of background task service from core. * Delegating changes to UI and triggering new background synchronization. * Fix build error. * Moved core lib messages to Messaging project. * Better client-server communication. Handling of requests in the server. New synchronizer factory in the server. * WAM broker and MSAL token caching for OutlookAuthenticator. Handling account creation for Outlook. * WinoServerResponse basics. * Delegating protocol activation for Gmail authenticator. * Adding margin to searchbox to match action bar width. * Move libraries into lib folder. * Storing base64 encoded mime on draft creation instead of MimeMessage object. Fixes serialization/deserialization issue with S.T.Json * Scrollbar adjustments * WınoExpander for thread expander layout ıssue. * Handling synchronizer state changes. * Double init on background activation. * FIxing packaging issues and new Wino Mail launcher protocol for activation from full thrust process. * Remove debug deserialization. * Remove debug code. * Making sure the server connection is established when the app is launched. * Thrust -> Trust string replacement... * Rename package to Wino Mail * Enable translated values in the server. * Fixed an issue where toast activation can't find the clicked mail after the folder is initialized. * Revert debug code. * Change server background sync to every 3 minute and Inbox only synchronization. * Revert google auth changes. * App preferences page. * Changing tray icon visibility on preference change. * Start the server with invisible tray icon if set to invisible. * Reconnect button on the title bar. * Handling of toast actions. * Enable x86 build for server during packaging. * Get rid of old background tasks and v180 migration. * Terminate client when Exit clicked in server. * Introducing SynchronizationSource to prevent notifying UI after server tick synchronization. * Remove confirmAppClose restricted capability and unused debug code in manifest. * Closing the reconnect info popup when reconnect is clicked. * Custom RetryHandler for OutlookSynchronizer and separating client/server logs. * Running server on Windows startup. * Fix startup exe. * Fix for expander list view item paddings. * Force full sync on app launch instead of Inbox. * Fix draft creation. * Fix an issue with custom folder sync logic. * Reporting back account sync progress from server. * Fix sending drafts and missing notifications for imap. * Changing imap folder sync requirements. * Retain file count is set to 3. * Disabled swipe gestures temporarily due to native crash with SwipeControl * Save all attachments implementation. * Localization for save all attachments button. * Fix logging dates for logs. * Fixing ARM64 build. * Add ARM64 build config to packaging project. * Comment out OutOfProcPDB for ARM64. * Hnadling GONE response for Outlook folder synchronization.
2024-08-05 00:36:26 +02:00
using Wino.Core.Domain.Extensions;
2024-04-18 01:44:37 +02:00
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Comparers;
using Wino.Core.Domain.Models.MailItem;
Full trust Wino Server implementation. (#295) * Separation of messages. Introducing Wino.Messages library. * Wino.Server and Wino.Packaging projects. Enabling full trust for UWP and app service connection manager basics. * Remove debug code. * Enable generating assembly info to deal with unsupported os platform warnings. * Fix server-client connection. * UIMessage communication. Single instancing for server and re-connection mechanism on suspension. * Removed IWinoSynchronizerFactory from UWP project. * Removal of background task service from core. * Delegating changes to UI and triggering new background synchronization. * Fix build error. * Moved core lib messages to Messaging project. * Better client-server communication. Handling of requests in the server. New synchronizer factory in the server. * WAM broker and MSAL token caching for OutlookAuthenticator. Handling account creation for Outlook. * WinoServerResponse basics. * Delegating protocol activation for Gmail authenticator. * Adding margin to searchbox to match action bar width. * Move libraries into lib folder. * Storing base64 encoded mime on draft creation instead of MimeMessage object. Fixes serialization/deserialization issue with S.T.Json * Scrollbar adjustments * WınoExpander for thread expander layout ıssue. * Handling synchronizer state changes. * Double init on background activation. * FIxing packaging issues and new Wino Mail launcher protocol for activation from full thrust process. * Remove debug deserialization. * Remove debug code. * Making sure the server connection is established when the app is launched. * Thrust -> Trust string replacement... * Rename package to Wino Mail * Enable translated values in the server. * Fixed an issue where toast activation can't find the clicked mail after the folder is initialized. * Revert debug code. * Change server background sync to every 3 minute and Inbox only synchronization. * Revert google auth changes. * App preferences page. * Changing tray icon visibility on preference change. * Start the server with invisible tray icon if set to invisible. * Reconnect button on the title bar. * Handling of toast actions. * Enable x86 build for server during packaging. * Get rid of old background tasks and v180 migration. * Terminate client when Exit clicked in server. * Introducing SynchronizationSource to prevent notifying UI after server tick synchronization. * Remove confirmAppClose restricted capability and unused debug code in manifest. * Closing the reconnect info popup when reconnect is clicked. * Custom RetryHandler for OutlookSynchronizer and separating client/server logs. * Running server on Windows startup. * Fix startup exe. * Fix for expander list view item paddings. * Force full sync on app launch instead of Inbox. * Fix draft creation. * Fix an issue with custom folder sync logic. * Reporting back account sync progress from server. * Fix sending drafts and missing notifications for imap. * Changing imap folder sync requirements. * Retain file count is set to 3. * Disabled swipe gestures temporarily due to native crash with SwipeControl * Save all attachments implementation. * Localization for save all attachments button. * Fix logging dates for logs. * Fixing ARM64 build. * Add ARM64 build config to packaging project. * Comment out OutOfProcPDB for ARM64. * Hnadling GONE response for Outlook folder synchronization.
2024-08-05 00:36:26 +02:00
using Wino.Messaging.UI;
using Wino.Services.Extensions;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Services;
public class MailService : BaseDatabaseService, IMailService
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
private const int ItemLoadCount = 100;
private readonly IFolderService _folderService;
private readonly IContactService _contactService;
private readonly IAccountService _accountService;
private readonly ISignatureService _signatureService;
private readonly IThreadingStrategyProvider _threadingStrategyProvider;
private readonly IMimeFileService _mimeFileService;
private readonly IPreferencesService _preferencesService;
private readonly ILogger _logger = Log.ForContext<MailService>();
public MailService(IDatabaseService databaseService,
IFolderService folderService,
IContactService contactService,
IAccountService accountService,
ISignatureService signatureService,
IThreadingStrategyProvider threadingStrategyProvider,
IMimeFileService mimeFileService,
IPreferencesService preferencesService) : base(databaseService)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
_folderService = folderService;
_contactService = contactService;
_accountService = accountService;
_signatureService = signatureService;
_threadingStrategyProvider = threadingStrategyProvider;
_mimeFileService = mimeFileService;
_preferencesService = preferencesService;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task<(MailCopy draftMailCopy, string draftBase64MimeMessage)> CreateDraftAsync(Guid accountId, DraftCreationOptions draftCreationOptions)
{
var composerAccount = await _accountService.GetAccountAsync(accountId).ConfigureAwait(false);
var createdDraftMimeMessage = await CreateDraftMimeAsync(composerAccount, draftCreationOptions);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var draftFolder = await _folderService.GetSpecialFolderByAccountIdAsync(composerAccount.Id, SpecialFolderType.Draft);
2024-04-18 01:44:37 +02:00
if (draftFolder == null)
throw new UnavailableSpecialFolderException(SpecialFolderType.Draft, accountId);
2025-02-16 11:54:23 +01:00
// Get locally created unique id from the mime headers.
// This header will be used to map the local draft copy with the remote draft copy.
var mimeUniqueId = createdDraftMimeMessage.Headers[Constants.WinoLocalDraftHeader];
2025-02-16 11:54:23 +01:00
var primaryAlias = await _accountService.GetPrimaryAccountAliasAsync(accountId).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var copy = new MailCopy
{
UniqueId = Guid.Parse(mimeUniqueId),
Id = Guid.NewGuid().ToString(), // This will be replaced after network call with the remote draft id.
CreationDate = DateTime.UtcNow,
FromAddress = primaryAlias?.AliasAddress ?? composerAccount.Address,
FromName = composerAccount.SenderName,
HasAttachments = false,
Importance = MailImportance.Normal,
Subject = createdDraftMimeMessage.Subject,
PreviewText = createdDraftMimeMessage.TextBody,
IsRead = true,
IsDraft = true,
FolderId = draftFolder.Id,
DraftId = $"{Constants.LocalDraftStartPrefix}{Guid.NewGuid()}",
AssignedFolder = draftFolder,
AssignedAccount = composerAccount,
FileId = Guid.NewGuid()
};
// If replying, add In-Reply-To, ThreadId and References.
if (draftCreationOptions.ReferencedMessage != null)
{
if (draftCreationOptions.ReferencedMessage.MimeMessage.References != null)
copy.References = string.Join(",", draftCreationOptions.ReferencedMessage.MimeMessage.References);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (!string.IsNullOrEmpty(draftCreationOptions.ReferencedMessage.MimeMessage.MessageId))
copy.InReplyTo = draftCreationOptions.ReferencedMessage.MimeMessage.MessageId;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (!string.IsNullOrEmpty(draftCreationOptions.ReferencedMessage.MailCopy?.ThreadId))
copy.ThreadId = draftCreationOptions.ReferencedMessage.MailCopy.ThreadId;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await Connection.InsertAsync(copy);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await _mimeFileService.SaveMimeMessageAsync(copy.FileId, createdDraftMimeMessage, composerAccount.Id);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
ReportUIChange(new DraftCreated(copy, composerAccount));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return (copy, createdDraftMimeMessage.GetBase64MimeMessage());
}
Folder operations, Gmail folder sync improvements and rework of menu items. (#273) * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * New rename folder dialog keys. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * Enable empty folder on base sync. * Move updates on event listeners. * Remove folder UI messages. * Reworked folder synchronization for gmail. * Loading folders on the fly as the selected account changed instead of relying on cached menu items. * Merged account folder items, re-navigating to existing rendering page. * - Reworked merged account menu system. - Reworked unread item count loadings. - Fixed back button visibility. - Instant rendering of mails if renderer is active. - Animation fixes. - Menu item re-load crash/hang fixes. * Handle folder renaming on the UI. * Empty folder for all synchronizers. * New execution delay mechanism and handling folder mark as read for all synchronizers. * Revert UI changes on failure for IMAP. * Remove duplicate translation keys. * Cleanup.
2024-07-09 01:05:16 +02:00
2025-02-16 11:54:23 +01:00
public async Task<List<MailCopy>> GetMailsByFolderIdAsync(Guid folderId)
{
var mails = await Connection.QueryAsync<MailCopy>("SELECT * FROM MailCopy WHERE FolderId = ?", folderId);
Folder operations, Gmail folder sync improvements and rework of menu items. (#273) * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * New rename folder dialog keys. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * Enable empty folder on base sync. * Move updates on event listeners. * Remove folder UI messages. * Reworked folder synchronization for gmail. * Loading folders on the fly as the selected account changed instead of relying on cached menu items. * Merged account folder items, re-navigating to existing rendering page. * - Reworked merged account menu system. - Reworked unread item count loadings. - Fixed back button visibility. - Instant rendering of mails if renderer is active. - Animation fixes. - Menu item re-load crash/hang fixes. * Handle folder renaming on the UI. * Empty folder for all synchronizers. * New execution delay mechanism and handling folder mark as read for all synchronizers. * Revert UI changes on failure for IMAP. * Remove duplicate translation keys. * Cleanup.
2024-07-09 01:05:16 +02:00
2025-02-16 11:54:23 +01:00
foreach (var mail in mails)
{
await LoadAssignedPropertiesAsync(mail).ConfigureAwait(false);
Folder operations, Gmail folder sync improvements and rework of menu items. (#273) * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * New rename folder dialog keys. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * Enable empty folder on base sync. * Move updates on event listeners. * Remove folder UI messages. * Reworked folder synchronization for gmail. * Loading folders on the fly as the selected account changed instead of relying on cached menu items. * Merged account folder items, re-navigating to existing rendering page. * - Reworked merged account menu system. - Reworked unread item count loadings. - Fixed back button visibility. - Instant rendering of mails if renderer is active. - Animation fixes. - Menu item re-load crash/hang fixes. * Handle folder renaming on the UI. * Empty folder for all synchronizers. * New execution delay mechanism and handling folder mark as read for all synchronizers. * Revert UI changes on failure for IMAP. * Remove duplicate translation keys. * Cleanup.
2024-07-09 01:05:16 +02:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return mails;
}
Folder operations, Gmail folder sync improvements and rework of menu items. (#273) * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * New rename folder dialog keys. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * Enable empty folder on base sync. * Move updates on event listeners. * Remove folder UI messages. * Reworked folder synchronization for gmail. * Loading folders on the fly as the selected account changed instead of relying on cached menu items. * Merged account folder items, re-navigating to existing rendering page. * - Reworked merged account menu system. - Reworked unread item count loadings. - Fixed back button visibility. - Instant rendering of mails if renderer is active. - Animation fixes. - Menu item re-load crash/hang fixes. * Handle folder renaming on the UI. * Empty folder for all synchronizers. * New execution delay mechanism and handling folder mark as read for all synchronizers. * Revert UI changes on failure for IMAP. * Remove duplicate translation keys. * Cleanup.
2024-07-09 01:05:16 +02:00
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;
}
2025-02-16 11:54:23 +01:00
public async Task<List<MailCopy>> GetUnreadMailsByFolderIdAsync(Guid folderId)
{
var unreadMails = await Connection.QueryAsync<MailCopy>("SELECT * FROM MailCopy WHERE FolderId = ? AND IsRead = 0", folderId);
Folder operations, Gmail folder sync improvements and rework of menu items. (#273) * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Disable vertical scroll for composing page editor items. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * New rename folder dialog keys. * Insfra work for folder operations and rename folder code. * RenameFolder for Gmail. * Fixed input dialog to take custom take for primary button. * Missing rename for DS call. * Outlook to throw exception in case of error. * Implemented rename folder functionality for Outlook. * Remove default primary text from input dialog. * Fixed an issue where outlook folder rename does not work. * Fixing some issues with imap folder sync. * fix copy pasta * TODO folder update/removed overrides for shell. * New rename folder dialog keys. * New rename folder dialog keys. * New rename folder dialog keys. * Fixed an issue where redundant older updates causing pivots to be re-created. * New empty folder request * Enable empty folder on base sync. * Move updates on event listeners. * Remove folder UI messages. * Reworked folder synchronization for gmail. * Loading folders on the fly as the selected account changed instead of relying on cached menu items. * Merged account folder items, re-navigating to existing rendering page. * - Reworked merged account menu system. - Reworked unread item count loadings. - Fixed back button visibility. - Instant rendering of mails if renderer is active. - Animation fixes. - Menu item re-load crash/hang fixes. * Handle folder renaming on the UI. * Empty folder for all synchronizers. * New execution delay mechanism and handling folder mark as read for all synchronizers. * Revert UI changes on failure for IMAP. * Remove duplicate translation keys. * Cleanup.
2024-07-09 01:05:16 +02:00
2025-02-16 11:54:23 +01:00
foreach (var mail in unreadMails)
{
await LoadAssignedPropertiesAsync(mail).ConfigureAwait(false);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return unreadMails;
}
private static string BuildMailFetchQuery(MailListInitializationOptions options)
2025-02-16 11:54:23 +01:00
{
// If the search query is there, we should ignore some properties and trim it.
//if (!string.IsNullOrEmpty(options.SearchQuery))
//{
// options.IsFocusedOnly = null;
// filterType = FilterOptionType.All;
// searchQuery = searchQuery.Trim();
//}
// SQLite PCL doesn't support joins.
// We make the query using SqlKata and execute it directly on SQLite-PCL.
2025-02-16 11:54:23 +01:00
var query = new Query("MailCopy")
.Join("MailItemFolder", "MailCopy.FolderId", "MailItemFolder.Id")
.WhereIn("MailCopy.FolderId", options.Folders.Select(a => a.Id))
.Take(ItemLoadCount)
.SelectRaw("MailCopy.*");
if (options.SortingOptionType == SortingOptionType.ReceiveDate)
query.OrderByDesc("CreationDate");
else if (options.SortingOptionType == SortingOptionType.Sender)
query.OrderBy("FromName");
// Conditional where.
switch (options.FilterType)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
case FilterOptionType.Unread:
query.Where("MailCopy.IsRead", false);
break;
case FilterOptionType.Flagged:
query.Where("MailCopy.IsFlagged", true);
break;
case FilterOptionType.Files:
query.Where("MailCopy.HasAttachments", true);
break;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (options.IsFocusedOnly != null)
query.Where("MailCopy.IsFocused", options.IsFocusedOnly.Value);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (!string.IsNullOrEmpty(options.SearchQuery))
query.Where(a =>
a.OrWhereContains("MailCopy.PreviewText", options.SearchQuery)
.OrWhereContains("MailCopy.Subject", options.SearchQuery)
.OrWhereContains("MailCopy.FromName", options.SearchQuery)
.OrWhereContains("MailCopy.FromAddress", options.SearchQuery));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (options.ExistingUniqueIds?.Any() ?? false)
{
2025-02-16 11:54:23 +01:00
query.WhereNotIn("MailCopy.UniqueId", options.ExistingUniqueIds);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
//if (options.Skip > 0)
//{
// query.Skip(options.Skip);
//}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return query.GetRawQuery();
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task<List<IMailItem>> FetchMailsAsync(MailListInitializationOptions options, CancellationToken cancellationToken = default)
{
List<MailCopy> mails = null;
2024-04-18 01:44:37 +02:00
// If user performs an online search, mail copies are passed to options.
if (options.PreFetchMailCopies != null)
{
mails = options.PreFetchMailCopies;
}
else
{
// If not just do the query.
var query = BuildMailFetchQuery(options);
mails = await Connection.QueryAsync<MailCopy>(query);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Dictionary<Guid, MailItemFolder> folderCache = [];
Dictionary<Guid, MailAccount> accountCache = [];
Dictionary<string, AccountContact> contactCache = [];
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Populate Folder Assignment for each single mail, to be able later group by "MailAccountId".
// This is needed to execute threading strategy by account type.
// Avoid DBs calls as possible, storing info in a dictionary.
foreach (var mail in mails)
{
await LoadAssignedPropertiesWithCacheAsync(mail, folderCache, accountCache, contactCache).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Remove items that has no assigned account or folder.
mails.RemoveAll(a => a.AssignedAccount == null || a.AssignedFolder == null);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (!options.CreateThreads)
{
cancellationToken.ThrowIfCancellationRequested();
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
// Threading is disabled. Just return everything as it is.
mails.Sort(options.SortingOptionType == SortingOptionType.ReceiveDate ? new DateComparer() : new NameComparer());
2025-02-16 11:35:43 +01:00
return [.. mails];
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Populate threaded items.
List<IMailItem> threadedItems = [];
2025-02-16 11:54:23 +01:00
// Each account items must be threaded separately.
foreach (var group in mails.GroupBy(a => a.AssignedAccount.Id))
{
2025-02-16 11:54:23 +01:00
cancellationToken.ThrowIfCancellationRequested();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var accountId = group.Key;
var groupAccount = mails.First(a => a.AssignedAccount.Id == accountId).AssignedAccount;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var threadingStrategy = _threadingStrategyProvider.GetStrategy(groupAccount.ProviderType);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
// Only thread items from Draft and Sent folders must present here.
// Otherwise this strategy will fetch the items that are in Deleted folder as well.
var accountThreadedItems = await threadingStrategy.ThreadItemsAsync([.. group], options.Folders.First());
// Populate threaded items with folder and account assignments.
// Almost everything already should be in cache from initial population.
foreach (var mail in accountThreadedItems)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
cancellationToken.ThrowIfCancellationRequested();
await LoadAssignedPropertiesWithCacheAsync(mail, folderCache, accountCache, contactCache).ConfigureAwait(false);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (accountThreadedItems != null)
{
2025-02-16 11:54:23 +01:00
threadedItems.AddRange(accountThreadedItems);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
threadedItems.Sort(options.SortingOptionType == SortingOptionType.ReceiveDate ? new DateComparer() : new NameComparer());
cancellationToken.ThrowIfCancellationRequested();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return threadedItems;
}
2024-04-18 01:44:37 +02:00
/// <summary>
/// This method should used for operations with multiple mailItems. Don't use this for single mail items.
/// Called method should provide own instances for caches.
/// </summary>
private async Task LoadAssignedPropertiesWithCacheAsync(IMailItem mail, Dictionary<Guid, MailItemFolder> folderCache, Dictionary<Guid, MailAccount> accountCache, Dictionary<string, AccountContact> contactCache)
{
if (mail is ThreadMailItem threadMailItem)
2025-02-16 11:54:23 +01:00
{
foreach (var childMail in threadMailItem.ThreadItems)
2024-04-18 01:44:37 +02:00
{
await LoadAssignedPropertiesWithCacheAsync(childMail, folderCache, accountCache, contactCache).ConfigureAwait(false);
}
}
2024-04-18 01:44:37 +02:00
if (mail is MailCopy mailCopy)
{
var isFolderCached = folderCache.TryGetValue(mailCopy.FolderId, out MailItemFolder folderAssignment);
MailAccount accountAssignment = null;
if (!isFolderCached)
{
folderAssignment = await _folderService.GetFolderAsync(mailCopy.FolderId).ConfigureAwait(false);
folderCache.TryAdd(mailCopy.FolderId, folderAssignment);
}
2025-02-16 11:54:23 +01:00
if (folderAssignment != null)
{
var isAccountCached = accountCache.TryGetValue(folderAssignment.MailAccountId, out accountAssignment);
if (!isAccountCached)
{
accountAssignment = await _accountService.GetAccountAsync(folderAssignment.MailAccountId).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
accountCache.TryAdd(folderAssignment.MailAccountId, accountAssignment);
2025-02-16 11:54:23 +01:00
}
}
AccountContact contactAssignment = null;
bool isContactCached = !string.IsNullOrEmpty(mailCopy.FromAddress) &&
contactCache.TryGetValue(mailCopy.FromAddress, out contactAssignment);
if (!isContactCached && accountAssignment != null)
{
contactAssignment = await GetSenderContactForAccountAsync(accountAssignment, mailCopy.FromAddress).ConfigureAwait(false);
2024-08-23 02:07:50 +02:00
if (contactAssignment != null)
2025-02-16 11:54:23 +01:00
{
contactCache.TryAdd(mailCopy.FromAddress, contactAssignment);
}
2024-04-18 01:44:37 +02:00
}
mailCopy.AssignedFolder = folderAssignment;
mailCopy.AssignedAccount = accountAssignment;
mailCopy.SenderContact = contactAssignment ?? CreateUnknownContact(mailCopy.FromName, mailCopy.FromAddress);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
private static AccountContact CreateUnknownContact(string fromName, string fromAddress)
2025-02-16 11:54:23 +01:00
{
if (string.IsNullOrEmpty(fromName) && string.IsNullOrEmpty(fromAddress))
{
2025-02-16 11:54:23 +01:00
return new AccountContact()
{
2025-02-16 11:54:23 +01:00
Name = Translator.UnknownSender,
Address = Translator.UnknownAddress
};
}
2025-02-16 11:54:23 +01:00
else
{
2025-02-16 11:54:23 +01:00
if (string.IsNullOrEmpty(fromName)) fromName = fromAddress;
2025-02-16 11:54:23 +01:00
return new AccountContact()
{
2025-02-16 11:54:23 +01:00
Name = fromName,
Address = fromAddress
};
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:54:23 +01:00
private async Task<List<MailCopy>> GetMailItemsAsync(string mailCopyId)
{
var mailCopies = await Connection.Table<MailCopy>().Where(a => a.Id == mailCopyId).ToListAsync();
foreach (var mailCopy in mailCopies)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
await LoadAssignedPropertiesAsync(mailCopy).ConfigureAwait(false);
2025-02-16 11:35:43 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return mailCopies;
}
private Task<AccountContact> GetSenderContactForAccountAsync(MailAccount account, string fromAddress)
{
// Make sure to return the latest up to date contact information for the original account.
if (fromAddress == account.Address)
{
return Task.FromResult(new AccountContact() { Address = account.Address, Name = account.SenderName, Base64ContactPicture = account.Base64ProfilePictureData });
}
else
{
2025-02-16 11:54:23 +01:00
return _contactService.GetAddressInformationByAddressAsync(fromAddress);
}
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private async Task LoadAssignedPropertiesAsync(MailCopy mailCopy)
{
if (mailCopy == null) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Load AssignedAccount, AssignedFolder and SenderContact.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var folder = await _folderService.GetFolderAsync(mailCopy.FolderId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (folder == null) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var account = await _accountService.GetAccountAsync(folder.MailAccountId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (account == null) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
mailCopy.AssignedAccount = account;
mailCopy.AssignedFolder = folder;
mailCopy.SenderContact = await GetSenderContactForAccountAsync(account, mailCopy.FromAddress).ConfigureAwait(false);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task<MailCopy> GetSingleMailItemWithoutFolderAssignmentAsync(string mailCopyId)
{
var mailCopy = await Connection.Table<MailCopy>().FirstOrDefaultAsync(a => a.Id == mailCopyId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (mailCopy == null) return null;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await LoadAssignedPropertiesAsync(mailCopy).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return mailCopy;
}
2024-04-18 01:44:37 +02:00
/// <summary>
/// Using this override is dangerous.
/// Gmail stores multiple copies of same mail in different folders.
/// This one will always return the first one. Use with caution.
/// </summary>
/// <param name="mailCopyId">Mail copy id.</param>
public async Task<MailCopy> GetSingleMailItemAsync(string mailCopyId)
{
var query = new Query("MailCopy")
.Where("MailCopy.Id", mailCopyId)
.SelectRaw("MailCopy.*")
.GetRawQuery();
var mailCopy = await Connection.FindWithQueryAsync<MailCopy>(query);
if (mailCopy == null) return null;
await LoadAssignedPropertiesAsync(mailCopy).ConfigureAwait(false);
return mailCopy;
}
2025-02-16 11:54:23 +01:00
public async Task<MailCopy> GetSingleMailItemAsync(string mailCopyId, string remoteFolderId)
{
var query = new Query("MailCopy")
.Join("MailItemFolder", "MailCopy.FolderId", "MailItemFolder.Id")
.Where("MailCopy.Id", mailCopyId)
.Where("MailItemFolder.RemoteFolderId", remoteFolderId)
.SelectRaw("MailCopy.*")
.GetRawQuery();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var mailItem = await Connection.FindWithQueryAsync<MailCopy>(query);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (mailItem == null) return null;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await LoadAssignedPropertiesAsync(mailItem).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return mailItem;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task<MailCopy> GetSingleMailItemAsync(Guid uniqueMailId)
{
var mailItem = await Connection.FindAsync<MailCopy>(uniqueMailId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (mailItem == null) return null;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await LoadAssignedPropertiesAsync(mailItem).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return mailItem;
}
2024-08-29 23:58:39 +02:00
2025-02-16 11:54:23 +01:00
// v2
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
public async Task DeleteMailAsync(Guid accountId, string mailCopyId)
{
var allMails = await GetMailItemsAsync(mailCopyId).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
foreach (var mailItem in allMails)
{
// Delete mime file as well.
// Even though Gmail might have multiple copies for the same mail, we only have one MIME file for all.
// Their FileId is inserted same.
await DeleteMailInternalAsync(mailItem, preserveMimeFile: false).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
#region Repository Calls
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private async Task InsertMailAsync(MailCopy mailCopy)
{
if (mailCopy == null)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
_logger.Warning("Null mail passed to InsertMailAsync call.");
return;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (mailCopy.FolderId == Guid.Empty)
{
_logger.Warning("Invalid FolderId for MailCopyId {Id} for InsertMailAsync", mailCopy.Id);
return;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
_logger.Debug("Inserting mail {MailCopyId} to {FolderName}", mailCopy.Id, mailCopy.AssignedFolder.FolderName);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await Connection.InsertAsync(mailCopy).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
ReportUIChange(new MailAddedMessage(mailCopy));
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task UpdateMailAsync(MailCopy mailCopy)
{
if (mailCopy == null)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
_logger.Warning("Null mail passed to UpdateMailAsync call.");
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
_logger.Debug("Updating mail {MailCopyId} with Folder {FolderId}", mailCopy.Id, mailCopy.FolderId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await Connection.UpdateAsync(mailCopy).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
ReportUIChange(new MailUpdatedMessage(mailCopy));
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private async Task DeleteMailInternalAsync(MailCopy mailCopy, bool preserveMimeFile)
{
if (mailCopy == null)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
_logger.Warning("Null mail passed to DeleteMailAsync call.");
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return;
}
2024-06-11 22:48:18 +02:00
2025-02-16 11:54:23 +01:00
_logger.Debug("Deleting mail {Id} from folder {FolderName}", mailCopy.Id, mailCopy.AssignedFolder.FolderName);
2024-06-11 22:48:18 +02:00
2025-02-16 11:54:23 +01:00
await Connection.DeleteAsync(mailCopy).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// If there are no more copies exists of the same mail, delete the MIME file as well.
var isMailExists = await IsMailExistsAsync(mailCopy.Id).ConfigureAwait(false);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (!isMailExists && !preserveMimeFile)
{
await _mimeFileService.DeleteMimeMessageAsync(mailCopy.AssignedAccount.Id, mailCopy.FileId).ConfigureAwait(false);
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
ReportUIChange(new MailRemovedMessage(mailCopy));
}
#endregion
private async Task UpdateAllMailCopiesAsync(string mailCopyId, Func<MailCopy, bool> action)
{
var mailCopies = await GetMailItemsAsync(mailCopyId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (mailCopies == null || !mailCopies.Any())
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
_logger.Warning("Updating mail copies failed because there are no copies available with Id {MailCopyId}", mailCopyId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
_logger.Debug("Updating {MailCopyCount} mail copies with Id {MailCopyId}", mailCopies.Count, mailCopyId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
foreach (var mailCopy in mailCopies)
{
bool shouldUpdateItem = action(mailCopy);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (shouldUpdateItem)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
await UpdateMailAsync(mailCopy).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
else
_logger.Debug("Skipped updating mail because it is already in the desired state.");
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public Task ChangeReadStatusAsync(string mailCopyId, bool isRead)
=> UpdateAllMailCopiesAsync(mailCopyId, (item) =>
{
if (item.IsRead == isRead) return false;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
item.IsRead = isRead;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return true;
});
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public Task ChangeFlagStatusAsync(string mailCopyId, bool isFlagged)
=> UpdateAllMailCopiesAsync(mailCopyId, (item) =>
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
if (item.IsFlagged == isFlagged) return false;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
item.IsFlagged = isFlagged;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return true;
});
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task CreateAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId)
{
// Note: Folder might not be available at the moment due to user not syncing folders before the delta processing.
// This is a problem, because assignments won't be created.
// Therefore we sync folders every time before the delta processing.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var localFolder = await _folderService.GetFolderAsync(accountId, remoteFolderId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (localFolder == null)
{
_logger.Warning("Local folder not found for remote folder {RemoteFolderId}", remoteFolderId);
_logger.Warning("Skipping assignment creation for the the message {MailCopyId}", mailCopyId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var mailCopy = await GetSingleMailItemWithoutFolderAssignmentAsync(mailCopyId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (mailCopy == null)
{
_logger.Warning("Can't create assignment for mail {MailCopyId} because it does not exist.", mailCopyId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return;
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
if (mailCopy.AssignedFolder.SpecialFolderType == SpecialFolderType.Sent &&
localFolder.SpecialFolderType == SpecialFolderType.Deleted)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// Sent item is deleted.
// Gmail does not delete the sent items, but moves them to the deleted folder.
// API doesn't allow removing Sent label.
// Here we intercept this behavior, removing the Sent copy of the mail and adding the Deleted copy.
// This way item will only be visible in Trash folder as in Gmail Web UI.
// Don't delete MIME file since if exists.
await DeleteMailInternalAsync(mailCopy, preserveMimeFile: true).ConfigureAwait(false);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Copy one of the mail copy and assign it to the new folder.
// We don't need to create a new MIME pack.
// Therefore FileId is not changed for the new MailCopy.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
mailCopy.UniqueId = Guid.NewGuid();
mailCopy.FolderId = localFolder.Id;
mailCopy.AssignedFolder = localFolder;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await InsertMailAsync(mailCopy).ConfigureAwait(false);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task DeleteAssignmentAsync(Guid accountId, string mailCopyId, string remoteFolderId)
{
var mailItem = await GetSingleMailItemAsync(mailCopyId, remoteFolderId).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (mailItem == null)
{
_logger.Warning("Mail not found with id {MailCopyId} with remote folder {RemoteFolderId}", mailCopyId, remoteFolderId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return;
2025-02-16 11:35:43 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var localFolder = await _folderService.GetFolderAsync(accountId, remoteFolderId);
if (localFolder == null)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
_logger.Warning("Local folder not found for remote folder {RemoteFolderId}", remoteFolderId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await DeleteMailInternalAsync(mailItem, preserveMimeFile: false).ConfigureAwait(false);
}
2025-02-16 11:35:43 +01:00
public async Task CreateMailRawAsync(MailAccount account, MailItemFolder mailItemFolder, NewMailItemPackage package)
{
var mailCopy = package.Copy;
var mimeMessage = package.Mime;
mailCopy.UniqueId = Guid.NewGuid();
mailCopy.AssignedAccount = account;
mailCopy.AssignedFolder = mailItemFolder;
mailCopy.SenderContact = await GetSenderContactForAccountAsync(account, mailCopy.FromAddress).ConfigureAwait(false);
mailCopy.FolderId = mailItemFolder.Id;
var mimeSaveTask = _mimeFileService.SaveMimeMessageAsync(mailCopy.FileId, mimeMessage, account.Id);
var contactSaveTask = _contactService.SaveAddressInformationAsync(mimeMessage);
var insertMailTask = InsertMailAsync(mailCopy);
await Task.WhenAll(mimeSaveTask, contactSaveTask, insertMailTask).ConfigureAwait(false);
}
2025-02-16 11:54:23 +01:00
public async Task<bool> CreateMailAsync(Guid accountId, NewMailItemPackage package)
{
var account = await _accountService.GetAccountAsync(accountId).ConfigureAwait(false);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (account == null) return false;
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (string.IsNullOrEmpty(package.AssignedRemoteFolderId))
{
_logger.Warning("Remote folder id is not set for {MailCopyId}.", package.Copy.Id);
_logger.Warning("Ignoring creation of mail.");
2025-02-16 11:54:23 +01:00
return false;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var assignedFolder = await _folderService.GetFolderAsync(accountId, package.AssignedRemoteFolderId).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (assignedFolder == null)
{
_logger.Warning("Assigned folder not found for {MailCopyId}.", package.Copy.Id);
_logger.Warning("Ignoring creation of mail.");
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return false;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var mailCopy = package.Copy;
var mimeMessage = package.Mime;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
mailCopy.UniqueId = Guid.NewGuid();
mailCopy.AssignedAccount = account;
mailCopy.AssignedFolder = assignedFolder;
mailCopy.SenderContact = await GetSenderContactForAccountAsync(account, mailCopy.FromAddress).ConfigureAwait(false);
mailCopy.FolderId = assignedFolder.Id;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Only save MIME files if they don't exists.
// This is because 1 mail may have multiple copies in different folders.
// but only single MIME to represent all.
// Save mime file to disk.
var isMimeExists = await _mimeFileService.IsMimeExistAsync(accountId, mailCopy.FileId).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
if (!isMimeExists)
{
bool isMimeSaved = await _mimeFileService.SaveMimeMessageAsync(mailCopy.FileId, mimeMessage, accountId).ConfigureAwait(false);
if (!isMimeSaved)
{
_logger.Warning("Failed to save mime file for {MailCopyId}.", mailCopy.Id);
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Save contact information.
await _contactService.SaveAddressInformationAsync(mimeMessage).ConfigureAwait(false);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
// Create mail copy in the database.
// Update if exists.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var existingCopyItem = await Connection.Table<MailCopy>()
.FirstOrDefaultAsync(a => a.Id == mailCopy.Id && a.FolderId == assignedFolder.Id);
2025-02-16 11:54:23 +01:00
if (existingCopyItem != null)
{
mailCopy.UniqueId = existingCopyItem.UniqueId;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await UpdateMailAsync(mailCopy).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
return false;
}
else
{
if (account.ProviderType != MailProviderType.Gmail)
{
// Make sure there is only 1 instance left of this mail copy id.
var allMails = await GetMailItemsAsync(mailCopy.Id).ConfigureAwait(false);
await DeleteMailAsync(accountId, mailCopy.Id).ConfigureAwait(false);
}
2025-02-16 11:54:23 +01:00
await InsertMailAsync(mailCopy).ConfigureAwait(false);
2025-02-16 11:54:23 +01:00
return true;
}
2025-02-16 11:54:23 +01:00
}
private async Task<MimeMessage> CreateDraftMimeAsync(MailAccount account, DraftCreationOptions draftCreationOptions)
{
// This unique id is stored in mime headers for Wino to identify remote message with local copy.
// Same unique id will be used for the local copy as well.
// Synchronizer will map this unique id to the local draft copy after synchronization.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var message = new MimeMessage()
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
Headers = { { Constants.WinoLocalDraftHeader, Guid.NewGuid().ToString() } },
};
2025-02-16 11:54:23 +01:00
var primaryAlias = await _accountService.GetPrimaryAccountAliasAsync(account.Id) ?? throw new MissingAliasException();
2025-02-16 11:54:23 +01:00
// Set FromName and FromAddress by alias.
message.From.Add(new MailboxAddress(account.SenderName, primaryAlias.AliasAddress));
2025-02-16 11:54:23 +01:00
var builder = new BodyBuilder();
2025-02-16 11:54:23 +01:00
var signature = await GetSignature(account, draftCreationOptions.Reason);
2025-02-16 11:54:23 +01:00
_ = draftCreationOptions.Reason switch
{
DraftCreationReason.Empty => CreateEmptyDraft(builder, message, draftCreationOptions, signature),
_ => CreateReferencedDraft(builder, message, draftCreationOptions, account, signature),
};
2025-02-16 11:54:23 +01:00
// TODO: Migration
// builder.SetHtmlBody(builder.HtmlBody);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
message.Body = builder.ToMessageBody();
2025-02-16 11:54:23 +01:00
return message;
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
private string CreateHtmlGap()
{
var template = $"""<div style="font-family: '{_preferencesService.ComposerFont}', Arial, sans-serif; font-size: {_preferencesService.ComposerFontSize}px"><br></div>""";
return string.Concat(Enumerable.Repeat(template, 2));
}
2025-02-16 11:54:23 +01:00
private async Task<string> GetSignature(MailAccount account, DraftCreationReason reason)
{
if (account.Preferences.IsSignatureEnabled)
{
2025-02-16 11:54:23 +01:00
var signatureId = reason == DraftCreationReason.Empty ?
account.Preferences.SignatureIdForNewMessages :
account.Preferences.SignatureIdForFollowingMessages;
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
if (signatureId != null)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var signature = await _signatureService.GetSignatureAsync(signatureId.Value);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
return signature.HtmlBody;
}
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
return null;
}
private MimeMessage CreateEmptyDraft(BodyBuilder builder, MimeMessage message, DraftCreationOptions draftCreationOptions, string signature)
{
builder.HtmlBody = CreateHtmlGap();
if (draftCreationOptions.MailToUri != null)
{
2025-02-16 11:54:23 +01:00
if (draftCreationOptions.MailToUri.Subject != null)
message.Subject = draftCreationOptions.MailToUri.Subject;
if (draftCreationOptions.MailToUri.Body != null)
{
2025-02-16 11:54:23 +01:00
// TODO: In .NET 6+ replace with string "ReplaceLineEndings" method.
var escapedBody = draftCreationOptions.MailToUri.Body.Replace("\r\n", "<br>").Replace("\n", "<br>").Replace("\r", "<br>");
builder.HtmlBody = $"""<div style="font-family: '{_preferencesService.ComposerFont}', Arial, sans-serif; font-size: {_preferencesService.ComposerFontSize}px">{escapedBody}</div>""" + builder.HtmlBody;
}
2025-02-16 11:54:23 +01:00
if (draftCreationOptions.MailToUri.To.Any())
message.To.AddRange(draftCreationOptions.MailToUri.To.Select(x => new MailboxAddress(x, x)));
if (draftCreationOptions.MailToUri.Cc.Any())
message.Cc.AddRange(draftCreationOptions.MailToUri.Cc.Select(x => new MailboxAddress(x, x)));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (draftCreationOptions.MailToUri.Bcc.Any())
message.Bcc.AddRange(draftCreationOptions.MailToUri.Bcc.Select(x => new MailboxAddress(x, x)));
}
2025-02-16 11:54:23 +01:00
if (signature != null)
builder.HtmlBody += signature;
2025-02-16 11:54:23 +01:00
return message;
}
2025-02-16 11:54:23 +01:00
private MimeMessage CreateReferencedDraft(BodyBuilder builder, MimeMessage message, DraftCreationOptions draftCreationOptions, MailAccount account, string signature)
{
var reason = draftCreationOptions.Reason;
var referenceMessage = draftCreationOptions.ReferencedMessage.MimeMessage;
2025-02-16 11:54:23 +01:00
var gap = CreateHtmlGap();
builder.HtmlBody = gap + CreateHtmlForReferencingMessage(referenceMessage);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (signature != null)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
builder.HtmlBody = gap + signature + builder.HtmlBody;
}
2025-02-16 11:54:23 +01:00
// Manage "To"
if (reason == DraftCreationReason.Reply || reason == DraftCreationReason.ReplyAll)
{
// Reply to the sender of the message
if (referenceMessage.ReplyTo.Count > 0)
message.To.AddRange(referenceMessage.ReplyTo);
else if (referenceMessage.From.Count > 0)
message.To.AddRange(referenceMessage.From);
else if (referenceMessage.Sender != null)
message.To.Add(referenceMessage.Sender);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (reason == DraftCreationReason.ReplyAll)
{
2025-02-16 11:54:23 +01:00
// Include all of the other original recipients
message.To.AddRange(referenceMessage.To.Where(x => x is MailboxAddress mailboxAddress && !mailboxAddress.Address.Equals(account.Address, StringComparison.OrdinalIgnoreCase)));
message.Cc.AddRange(referenceMessage.Cc.Where(x => x is MailboxAddress mailboxAddress && !mailboxAddress.Address.Equals(account.Address, StringComparison.OrdinalIgnoreCase)));
}
2025-02-16 11:54:23 +01:00
// Self email can be present at this step, when replying to own message. It should be removed only in case there no other recipients.
if (message.To.Count > 1)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var self = message.To.FirstOrDefault(x => x is MailboxAddress mailboxAddress && mailboxAddress.Address.Equals(account.Address, StringComparison.OrdinalIgnoreCase));
if (self != null)
message.To.Remove(self);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Manage "ThreadId-ConversationId"
if (!string.IsNullOrEmpty(referenceMessage.MessageId))
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
message.InReplyTo = referenceMessage.MessageId;
message.References.AddRange(referenceMessage.References);
message.References.Add(referenceMessage.MessageId);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
message.Headers.Add("Thread-Topic", referenceMessage.Subject);
}
// Manage Subject
if (reason == DraftCreationReason.Forward && !referenceMessage.Subject.StartsWith("FW: ", StringComparison.OrdinalIgnoreCase))
message.Subject = $"FW: {referenceMessage.Subject}";
else if ((reason == DraftCreationReason.Reply || reason == DraftCreationReason.ReplyAll) && !referenceMessage.Subject.StartsWith("RE: ", StringComparison.OrdinalIgnoreCase))
message.Subject = $"RE: {referenceMessage.Subject}";
else if (referenceMessage != null)
message.Subject = referenceMessage.Subject;
2025-02-16 11:54:23 +01:00
// Only include attachments if forwarding.
if (reason == DraftCreationReason.Forward && (referenceMessage?.Attachments?.Any() ?? false))
{
foreach (var attachment in referenceMessage.Attachments)
{
2025-02-16 11:54:23 +01:00
builder.Attachments.Add(attachment);
}
2025-02-16 11:35:43 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return message;
// Generates html representation of To/Cc/From/Time and so on from referenced message.
string CreateHtmlForReferencingMessage(MimeMessage referenceMessage)
{
2025-02-16 11:54:23 +01:00
var htmlMimeInfo = string.Empty;
// Separation Line
htmlMimeInfo += "<hr style='display:inline-block;width:100%' tabindex='-1'>";
var visitor = _mimeFileService.CreateHTMLPreviewVisitor(referenceMessage, string.Empty);
visitor.Visit(referenceMessage);
htmlMimeInfo += $"""
<div id="divRplyFwdMsg" dir="ltr">
<font face="Calibri, sans-serif" style="font-size: 11pt;" color="#000000">
<b>From:</b> {ParticipantsToHtml(referenceMessage.From)}<br>
<b>Sent:</b> {referenceMessage.Date.ToLocalTime()}<br>
<b>To:</b> {ParticipantsToHtml(referenceMessage.To)}<br>
{(referenceMessage.Cc.Count > 0 ? $"<b>Cc:</b> {ParticipantsToHtml(referenceMessage.Cc)}<br>" : string.Empty)}
<b>Subject:</b> {referenceMessage.Subject}
</font>
<div>&nbsp;</div>
{visitor.HtmlBody}
</div>
""";
return htmlMimeInfo;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
static string ParticipantsToHtml(InternetAddressList internetAddresses) =>
string.Join("; ", internetAddresses.Mailboxes
.Select(x => $"{x.Name ?? Translator.UnknownSender} &lt;<a href=\"mailto:{x.Address ?? Translator.UnknownAddress}\">{x.Address ?? Translator.UnknownAddress}</a>&gt;"));
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task<bool> MapLocalDraftAsync(Guid accountId, Guid localDraftCopyUniqueId, string newMailCopyId, string newDraftId, string newThreadId)
{
var query = new Query("MailCopy")
.Join("MailItemFolder", "MailCopy.FolderId", "MailItemFolder.Id")
.Where("MailCopy.UniqueId", localDraftCopyUniqueId)
.Where("MailItemFolder.MailAccountId", accountId)
.SelectRaw("MailCopy.*")
.GetRawQuery();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var localDraftCopy = await Connection.FindWithQueryAsync<MailCopy>(query);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (localDraftCopy == null)
{
_logger.Warning("Draft mapping failed because local draft copy with unique id {LocalDraftCopyUniqueId} does not exist.", localDraftCopyUniqueId);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return false;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var oldLocalDraftId = localDraftCopy.Id;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await LoadAssignedPropertiesAsync(localDraftCopy).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
bool isIdChanging = localDraftCopy.Id != newMailCopyId;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
localDraftCopy.Id = newMailCopyId;
localDraftCopy.DraftId = newDraftId;
localDraftCopy.ThreadId = newThreadId;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await UpdateMailAsync(localDraftCopy).ConfigureAwait(false);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
ReportUIChange(new DraftMapped(oldLocalDraftId, newDraftId));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return true;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public Task MapLocalDraftAsync(string mailCopyId, string newDraftId, string newThreadId)
{
return UpdateAllMailCopiesAsync(mailCopyId, (item) =>
{
if (item.ThreadId != newThreadId || item.DraftId != newDraftId)
{
var oldDraftId = item.DraftId;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
item.DraftId = newDraftId;
item.ThreadId = newThreadId;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
ReportUIChange(new DraftMapped(oldDraftId, newDraftId));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return true;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return false;
});
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public Task<List<MailCopy>> GetDownloadedUnreadMailsAsync(Guid accountId, IEnumerable<string> downloadedMailCopyIds)
{
var rawQuery = new Query("MailCopy")
.Join("MailItemFolder", "MailCopy.FolderId", "MailItemFolder.Id")
.WhereIn("MailCopy.Id", downloadedMailCopyIds)
.Where("MailCopy.IsRead", false)
.Where("MailItemFolder.MailAccountId", accountId)
.Where("MailItemFolder.SpecialFolderType", SpecialFolderType.Inbox)
.SelectRaw("MailCopy.*")
.GetRawQuery();
return Connection.QueryAsync<MailCopy>(rawQuery);
}
Full trust Wino Server implementation. (#295) * Separation of messages. Introducing Wino.Messages library. * Wino.Server and Wino.Packaging projects. Enabling full trust for UWP and app service connection manager basics. * Remove debug code. * Enable generating assembly info to deal with unsupported os platform warnings. * Fix server-client connection. * UIMessage communication. Single instancing for server and re-connection mechanism on suspension. * Removed IWinoSynchronizerFactory from UWP project. * Removal of background task service from core. * Delegating changes to UI and triggering new background synchronization. * Fix build error. * Moved core lib messages to Messaging project. * Better client-server communication. Handling of requests in the server. New synchronizer factory in the server. * WAM broker and MSAL token caching for OutlookAuthenticator. Handling account creation for Outlook. * WinoServerResponse basics. * Delegating protocol activation for Gmail authenticator. * Adding margin to searchbox to match action bar width. * Move libraries into lib folder. * Storing base64 encoded mime on draft creation instead of MimeMessage object. Fixes serialization/deserialization issue with S.T.Json * Scrollbar adjustments * WınoExpander for thread expander layout ıssue. * Handling synchronizer state changes. * Double init on background activation. * FIxing packaging issues and new Wino Mail launcher protocol for activation from full thrust process. * Remove debug deserialization. * Remove debug code. * Making sure the server connection is established when the app is launched. * Thrust -> Trust string replacement... * Rename package to Wino Mail * Enable translated values in the server. * Fixed an issue where toast activation can't find the clicked mail after the folder is initialized. * Revert debug code. * Change server background sync to every 3 minute and Inbox only synchronization. * Revert google auth changes. * App preferences page. * Changing tray icon visibility on preference change. * Start the server with invisible tray icon if set to invisible. * Reconnect button on the title bar. * Handling of toast actions. * Enable x86 build for server during packaging. * Get rid of old background tasks and v180 migration. * Terminate client when Exit clicked in server. * Introducing SynchronizationSource to prevent notifying UI after server tick synchronization. * Remove confirmAppClose restricted capability and unused debug code in manifest. * Closing the reconnect info popup when reconnect is clicked. * Custom RetryHandler for OutlookSynchronizer and separating client/server logs. * Running server on Windows startup. * Fix startup exe. * Fix for expander list view item paddings. * Force full sync on app launch instead of Inbox. * Fix draft creation. * Fix an issue with custom folder sync logic. * Reporting back account sync progress from server. * Fix sending drafts and missing notifications for imap. * Changing imap folder sync requirements. * Retain file count is set to 3. * Disabled swipe gestures temporarily due to native crash with SwipeControl * Save all attachments implementation. * Localization for save all attachments button. * Fix logging dates for logs. * Fixing ARM64 build. * Add ARM64 build config to packaging project. * Comment out OutOfProcPDB for ARM64. * Hnadling GONE response for Outlook folder synchronization.
2024-08-05 00:36:26 +02:00
2025-02-16 11:54:23 +01:00
public Task<MailAccount> GetMailAccountByUniqueIdAsync(Guid uniqueMailId)
{
var query = new Query("MailCopy")
.Join("MailItemFolder", "MailCopy.FolderId", "MailItemFolder.Id")
.Join("MailAccount", "MailItemFolder.MailAccountId", "MailAccount.Id")
.Where("MailCopy.UniqueId", uniqueMailId)
.SelectRaw("MailAccount.*")
.GetRawQuery();
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
return Connection.FindWithQueryAsync<MailAccount>(query);
}
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
public Task<bool> IsMailExistsAsync(string mailCopyId)
=> Connection.ExecuteScalarAsync<bool>("SELECT EXISTS(SELECT 1 FROM MailCopy WHERE Id = ?)", mailCopyId);
2025-02-15 12:53:32 +01:00
2025-02-16 11:54:23 +01:00
public async Task<List<MailCopy>> GetExistingMailsAsync(Guid folderId, IEnumerable<MailKit.UniqueId> uniqueIds)
{
var localMailIds = uniqueIds.Where(a => a != null).Select(a => MailkitClientExtensions.CreateUid(folderId, a.Id)).ToArray();
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
var query = new Query(nameof(MailCopy))
.WhereIn("Id", localMailIds)
.GetRawQuery();
2025-02-16 11:54:23 +01:00
return await Connection.QueryAsync<MailCopy>(query);
}
2025-02-16 11:54:23 +01:00
public Task<bool> IsMailExistsAsync(string mailCopyId, Guid folderId)
=> Connection.ExecuteScalarAsync<bool>("SELECT EXISTS(SELECT 1 FROM MailCopy WHERE Id = ? AND FolderId = ?)", mailCopyId, folderId);
public async Task<GmailArchiveComparisonResult> GetGmailArchiveComparisonResultAsync(Guid archiveFolderId, List<string> onlineArchiveMailIds)
{
var localArchiveMails = await Connection.Table<MailCopy>()
.Where(a => a.FolderId == archiveFolderId)
.ToListAsync().ConfigureAwait(false);
var removedMails = localArchiveMails.Where(a => !onlineArchiveMailIds.Contains(a.Id)).Select(a => a.Id).Distinct().ToArray();
var addedMails = onlineArchiveMailIds.Where(a => !localArchiveMails.Select(b => b.Id).Contains(a)).Distinct().ToArray();
return new GmailArchiveComparisonResult(addedMails, removedMails);
}
public async Task<List<MailCopy>> GetMailItemsAsync(IEnumerable<string> mailCopyIds)
{
if (!mailCopyIds.Any()) return [];
var query = new Query("MailCopy")
.WhereIn("MailCopy.Id", mailCopyIds)
.SelectRaw("MailCopy.*")
.GetRawQuery();
var mailCopies = await Connection.QueryAsync<MailCopy>(query);
if (mailCopies?.Count == 0) return [];
Dictionary<Guid, MailItemFolder> folderCache = [];
Dictionary<Guid, MailAccount> accountCache = [];
Dictionary<string, AccountContact> contactCache = [];
foreach (var mail in mailCopies)
{
await LoadAssignedPropertiesWithCacheAsync(mail, folderCache, accountCache, contactCache).ConfigureAwait(false);
}
return mailCopies;
}
public async Task<List<string>> AreMailsExistsAsync(IEnumerable<string> mailCopyIds)
{
var query = new Query(nameof(MailCopy))
.WhereIn("Id", mailCopyIds)
.Select("Id")
.GetRawQuery();
return await Connection.QueryScalarsAsync<string>(query);
}
2024-04-18 01:44:37 +02:00
}