using LiteDB; using MailSharp.MailClient.Models; namespace MailSharp.MailClient.Services; public interface IFolderSettingsStore { Dictionary GetAll(int accountId); void Save(int accountId, string folderFullName, FolderSyncMode syncMode, bool subscribed); void Remove(int accountId, string folderFullName); // Display-only ordering of folders in the main menu (Settings > Mappen beheren) - purely // cosmetic, never touches the actual IMAP folder structure. Folders not mentioned here (new // ones, or ones never reordered) are appended after the ordered ones in their natural IMAP // discovery order - see IImapService.GetFoldersAsync. List GetOrder(int accountId); void SaveOrder(int accountId, List folderFullNamesInOrder); } // Stores the user's per-folder sync mode/subscription choices (Settings > Mappen beheren), keyed // by (account, folder full name) - separate from the folder list itself, which is always // re-fetched live from IMAP (see IImapService.GetFoldersAsync) and has no durable identity of its // own beyond its full name. public class LiteDbFolderSettingsStore : IFolderSettingsStore, IDisposable { private readonly LiteDatabase _db; public LiteDbFolderSettingsStore(IWebHostEnvironment env) { var dir = Path.Combine(env.ContentRootPath, "App_Data"); Directory.CreateDirectory(dir); _db = new LiteDatabase(Path.Combine(dir, "foldersettings.db")); } private ILiteCollection Settings => _db.GetCollection("folderSettings"); private ILiteCollection Orders => _db.GetCollection("folderOrders"); private static string Key(int accountId, string folderFullName) => $"{accountId}:{folderFullName}"; public Dictionary GetAll(int accountId) { var prefix = accountId + ":"; return Settings.Find(x => x.Id.StartsWith(prefix)) .ToDictionary(x => x.Id[prefix.Length..], x => x); } public void Save(int accountId, string folderFullName, FolderSyncMode syncMode, bool subscribed) => Settings.Upsert(new FolderSetting { Id = Key(accountId, folderFullName), SyncMode = syncMode, Subscribed = subscribed }); public void Remove(int accountId, string folderFullName) => Settings.Delete(Key(accountId, folderFullName)); public List GetOrder(int accountId) => Orders.FindById(accountId)?.Names ?? []; public void SaveOrder(int accountId, List folderFullNamesInOrder) => Orders.Upsert(new FolderOrder { Id = accountId, Names = folderFullNamesInOrder }); public void Dispose() => _db.Dispose(); }