public List <Chain> GetChainsById(string id) { using (var daoFactory = new DaoFactory()) { var daoChain = daoFactory.CreateChainDao(Tenant, User); var exp = SimpleConversationsExp.CreateBuilder(Tenant, User) .SetChainIds(new List <string> { id }) .Build(); return(daoChain.GetChains(exp)); } }
public void SetConversationsImportanceFlags(int tenant, string user, bool important, List <int> ids) { List <MailInfo> mailInfos; using (var daoFactory = new DaoFactory()) { mailInfos = GetChainedMessagesInfo(daoFactory, ids); var chainsInfo = mailInfos .Select(m => new { m.ChainId, m.MailboxId, m.Folder }) .Distinct().ToList(); if (!chainsInfo.Any()) { throw new Exception("no chain messages belong to current user"); } using (var tx = daoFactory.DbManager.BeginTransaction(IsolationLevel.ReadUncommitted)) { var daoMailInfo = daoFactory.CreateMailInfoDao(tenant, user); var exp = Exp.Empty; var сhains = new List <Tuple <int, string> >(); foreach (var chain in chainsInfo) { var key = new Tuple <int, string>(chain.MailboxId, chain.ChainId); if (сhains.Any() && сhains.Contains(key) && (chain.Folder == FolderType.Inbox || chain.Folder == FolderType.Sent)) { continue; } var innerWhere = Exp.And( Exp.Eq(MailTable.Columns.ChainId.Prefix(MM_ALIAS), chain.ChainId), Exp.Eq(MailTable.Columns.MailboxId.Prefix(MM_ALIAS), chain.MailboxId)); if (chain.Folder == FolderType.Inbox || chain.Folder == FolderType.Sent) { innerWhere &= Exp.Or( Exp.Eq(MailTable.Columns.Folder.Prefix(MM_ALIAS), (int)FolderType.Inbox), Exp.Eq(MailTable.Columns.Folder.Prefix(MM_ALIAS), (int)FolderType.Sent)); сhains.Add(key); } else { innerWhere &= Exp.Eq(MailTable.Columns.Folder.Prefix(MM_ALIAS), (int)chain.Folder); } exp |= innerWhere; } daoMailInfo.SetFieldValue( SimpleMessagesExp.CreateBuilder(tenant, user) .SetExp(exp) .Build(), MailTable.Columns.Importance, important); var daoChain = daoFactory.CreateChainDao(tenant, user); foreach (var chain in chainsInfo) { daoChain.SetFieldValue( SimpleConversationsExp.CreateBuilder(tenant, user) .SetChainId(chain.ChainId) .SetMailboxId(chain.MailboxId) .SetFolder((int)chain.Folder) .Build(), ChainTable.Columns.Importance, important); } tx.Commit(); } } var factory = new EngineFactory(Tenant, User); var data = new MailWrapper { Importance = important }; factory.IndexEngine.Update(data, s => s.In(m => m.Id, mailInfos.Select(o => o.Id).ToArray()), wrapper => wrapper.Importance); }
public List <MailMessageData> GetConversations(MailSearchFilterData filterData, out bool hasMore) { if (filterData == null) { throw new ArgumentNullException("filterData"); } var filter = (MailSearchFilterData)filterData.Clone(); if (filter.UserFolderId.HasValue && Factory.UserFolderEngine.Get((uint)filter.UserFolderId.Value) == null) { throw new ArgumentException("Folder not found"); } using (var daoFactory = new DaoFactory()) { var filteredConversations = GetFilteredConversations(daoFactory, filter, out hasMore); if (!filteredConversations.Any()) { return(filteredConversations); } var chainIds = new List <string>(); filteredConversations.ForEach(x => chainIds.Add(x.ChainId)); var daoChain = daoFactory.CreateChainDao(Tenant, User); var exp = SimpleConversationsExp.CreateBuilder(Tenant, User) .SetChainIds(chainIds) .SetFoldersIds( filter.PrimaryFolder == FolderType.Inbox || filter.PrimaryFolder == FolderType.Sent ? new List <int> { (int)FolderType.Inbox, (int)FolderType.Sent } : new List <int> { (int)filter.PrimaryFolder }) .Build(); var extendedInfo = daoChain.GetChains(exp); foreach (var chain in filteredConversations) { var chainMessages = extendedInfo.FindAll(x => x.MailboxId == chain.MailboxId && x.Id == chain.ChainId); if (!chainMessages.Any()) { continue; } chain.IsNew = chainMessages.Any(x => x.Unread); chain.HasAttachments = chainMessages.Any(x => x.HasAttachments); chain.Important = chainMessages.Any(x => x.Importance); chain.ChainLength = chainMessages.Sum(x => x.Length); var firstOrDefault = chainMessages.FirstOrDefault(x => !string.IsNullOrEmpty(x.Tags)); chain.LabelsString = firstOrDefault != null ? firstOrDefault.Tags : ""; } return(filteredConversations); } }
public List <MailMessageData> GetConversationMessages(int tenant, string user, int messageId, bool loadAllContent, bool needProxyHttp, bool needMailSanitazer, bool markRead = false) { var engine = new EngineFactory(tenant, user); using (var daoFactory = new DaoFactory()) { var db = daoFactory.DbManager; var daoMailInfo = daoFactory.CreateMailInfoDao(tenant, user); var messageInfo = daoMailInfo.GetMailInfoList( SimpleMessagesExp.CreateBuilder(tenant, user) .SetMessageId(messageId) .Build()) .SingleOrDefault(); if (messageInfo == null) { throw new ArgumentException("Message Id not found"); } var searchFolders = new List <int>(); if (messageInfo.Folder == FolderType.Inbox || messageInfo.Folder == FolderType.Sent) { searchFolders.AddRange(new[] { (int)FolderType.Inbox, (int)FolderType.Sent }); } else { searchFolders.Add((int)messageInfo.Folder); } var exp = SimpleMessagesExp.CreateBuilder(tenant, user) .SetMailboxId(messageInfo.MailboxId) .SetChainId(messageInfo.ChainId) .SetFoldersIds(searchFolders) .Build(); var mailInfoList = daoMailInfo.GetMailInfoList(exp); var ids = mailInfoList.Select(m => m.Id).ToList(); var messages = ids.Select( (id, i) => engine.MessageEngine.GetMessage(daoFactory, id, new MailMessageData.Options { LoadImages = false, LoadBody = loadAllContent || (id == messageId), NeedProxyHttp = needProxyHttp, NeedSanitizer = needMailSanitazer })) .Where(mailInfo => mailInfo != null) .OrderBy(m => m.Date) .ToList(); if (!markRead) { return(messages); } var unreadMessages = messages.Where(message => message.WasNew).ToList(); if (!unreadMessages.Any()) { return(messages); } var unreadMessagesCountByFolder = new Dictionary <FolderType, int>(); foreach (var message in unreadMessages) { if (unreadMessagesCountByFolder.ContainsKey(message.Folder)) { unreadMessagesCountByFolder[message.Folder] += 1; } else { unreadMessagesCountByFolder.Add(message.Folder, 1); } } var userFolderXmailDao = daoFactory.CreateUserFolderXMailDao(tenant, user); uint?userFolder = null; if (unreadMessagesCountByFolder.Keys.Any(k => k == FolderType.UserFolder)) { var item = userFolderXmailDao.Get(ids.First()); userFolder = item == null ? (uint?)null : item.FolderId; } List <int> ids2Update; using (var tx = db.BeginTransaction()) { ids2Update = unreadMessages.Select(x => x.Id).ToList(); daoMailInfo.SetFieldValue( SimpleMessagesExp.CreateBuilder(tenant, user) .SetMessageIds(ids2Update) .Build(), MailTable.Columns.Unread, false); var daoChain = daoFactory.CreateChainDao(tenant, user); foreach (var keyPair in unreadMessagesCountByFolder) { var folderType = keyPair.Key; var unreadMessDiff = keyPair.Value != 0 ? keyPair.Value * (-1) : (int?)null; engine.FolderEngine.ChangeFolderCounters(daoFactory, folderType, userFolder, unreadMessDiff, unreadConvDiff: -1); daoChain.SetFieldValue( SimpleConversationsExp.CreateBuilder(tenant, user) .SetChainId(messageInfo.ChainId) .SetMailboxId(messageInfo.MailboxId) .SetFolder((int)keyPair.Key) .Build(), ChainTable.Columns.Unread, false); } if (userFolder.HasValue) { var userFoldersIds = userFolderXmailDao.GetList(mailIds: ids) .Select(ufxm => (int)ufxm.FolderId) .Distinct() .ToList(); engine.UserFolderEngine.RecalculateCounters(daoFactory, userFoldersIds); } tx.Commit(); } var data = new MailWrapper { Unread = false }; engine.IndexEngine.Update(data, s => s.In(m => m.Id, ids2Update.ToArray()), wrapper => wrapper.Unread); return(messages); } }
public void RecalculateFolders(Action <MailOperationRecalculateMailboxProgress> callback = null) { using (var db = new DbManager(Defines.CONNECTION_STRING_NAME, Defines.RecalculateFoldersTimeout)) { var daoFactory = new DaoFactory(db); var daoFolder = daoFactory.CreateFolderDao(Tenant, User); using (var tx = db.BeginTransaction(IsolationLevel.ReadUncommitted)) { var folderTypes = Enum.GetValues(typeof(FolderType)).Cast <int>(); var daoMailInfo = daoFactory.CreateMailInfoDao(Tenant, User); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.CountUnreadMessages); } var unreadMessagesCountByFolder = daoMailInfo.GetMailCount( SimpleMessagesExp.CreateBuilder(Tenant, User) .SetUnread(true) .Build()); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.CountTotalMessages); } var totalMessagesCountByFolder = daoMailInfo.GetMailCount( SimpleMessagesExp.CreateBuilder(Tenant, User) .Build()); var daoChain = daoFactory.CreateChainDao(Tenant, User); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.CountUreadConversation); } var unreadConversationsCountByFolder = daoChain.GetChainCount( SimpleConversationsExp.CreateBuilder(Tenant, User) .SetUnread(true) .Build()); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.CountTotalConversation); } var totalConversationsCountByFolder = daoChain.GetChainCount( SimpleConversationsExp.CreateBuilder(Tenant, User) .Build()); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.UpdateFoldersCounters); } var now = DateTime.UtcNow; var folders = (from folderId in folderTypes let unreadMessCount = unreadMessagesCountByFolder.ContainsKey(folderId) ? unreadMessagesCountByFolder[folderId] : 0 let totalMessCount = totalMessagesCountByFolder.ContainsKey(folderId) ? totalMessagesCountByFolder[folderId] : 0 let unreadConvCount = unreadConversationsCountByFolder.ContainsKey(folderId) ? unreadConversationsCountByFolder[folderId] : 0 let totalConvCount = totalConversationsCountByFolder.ContainsKey(folderId) ? totalConversationsCountByFolder[folderId] : 0 select new Folder { FolderType = (FolderType)folderId, Tenant = Tenant, UserId = User, UnreadCount = unreadMessCount, UnreadChainCount = unreadConvCount, TotalCount = totalMessCount, TotalChainCount = totalConvCount, TimeModified = now }) .ToList(); foreach (var folder in folders) { daoFolder.Save(folder); } var userFolder = folders.FirstOrDefault(f => f.FolderType == FolderType.UserFolder); if (userFolder != null) { var daoUserFolder = daoFactory.CreateUserFolderDao(Tenant, User); var userFolders = daoUserFolder.GetList( SimpleUserFoldersExp.CreateBuilder(Tenant, User) .Build()); if (userFolders.Any()) { var totalMessagesCountByUserFolder = daoMailInfo.GetMailUserFolderCount(); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.CountTotalUserFolderMessages); } var unreadMessagesCountByUserFolder = daoMailInfo.GetMailUserFolderCount(true); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.CountUnreadUserFolderMessages); } var totalConversationsCountByUserFolder = daoChain.GetChainUserFolderCount(); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.CountTotalUserFolderConversation); } var unreadConversationsCountByUserFolder = daoChain.GetChainUserFolderCount(true); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.CountUreadUserFolderConversation); } var newUserFolders = (from folder in userFolders let unreadMessCount = unreadMessagesCountByUserFolder.ContainsKey(folder.Id) ? unreadMessagesCountByUserFolder[folder.Id] : 0 let totalMessCount = totalMessagesCountByUserFolder.ContainsKey(folder.Id) ? totalMessagesCountByUserFolder[folder.Id] : 0 let unreadConvCount = unreadConversationsCountByUserFolder.ContainsKey(folder.Id) ? unreadConversationsCountByUserFolder[folder.Id] : 0 let totalConvCount = totalConversationsCountByUserFolder.ContainsKey(folder.Id) ? totalConversationsCountByUserFolder[folder.Id] : 0 select new UserFolder { Id = folder.Id, ParentId = folder.ParentId, Name = folder.Name, FolderCount = folder.FolderCount, Tenant = Tenant, User = User, UnreadCount = unreadMessCount, UnreadChainCount = unreadConvCount, TotalCount = totalMessCount, TotalChainCount = totalConvCount, TimeModified = now }) .ToList(); if (callback != null) { callback(MailOperationRecalculateMailboxProgress.UpdateUserFoldersCounters); } foreach (var folder in newUserFolders) { daoUserFolder.Save(folder); } } } tx.Commit(); } } }