Пример #1
0
        public void UpdateChainFields(IDaoFactory daoFactory, int tenant, string user, List <int> ids)
        {
            var daoMailInfo = daoFactory.CreateMailInfoDao(tenant, user);

            var mailInfoList = daoMailInfo.GetMailInfoList(
                SimpleMessagesExp.CreateBuilder(tenant, user, null)
                .SetMessageIds(ids)
                .Build())
                               .ConvertAll(x => new
            {
                id_mailbox = x.MailboxId,
                chain_id   = x.ChainId,
                folder     = x.Folder
            });

            if (!mailInfoList.Any())
            {
                return;
            }

            foreach (var info in mailInfoList.GroupBy(t => new { t.id_mailbox, t.chain_id, t.folder }))
            {
                uint?userFolder = null;

                if (info.Key.folder == FolderType.UserFolder)
                {
                    var userFolderXmailDao = daoFactory.CreateUserFolderXMailDao(Tenant, User);
                    var item = userFolderXmailDao.Get(ids.First());
                    userFolder = item == null ? (uint?)null : item.FolderId;
                }

                UpdateChain(daoFactory, info.Key.chain_id, info.Key.folder, userFolder, info.Key.id_mailbox, tenant, user);
            }
        }
Пример #2
0
        private static void UpdateMessageChainFlag(IDaoFactory daoFactory, int tenant, string user, int messageId, string fieldFrom, string fieldTo)
        {
            var daoMail = daoFactory.CreateMailDao(tenant, user);

            var mail = daoMail.GetMail(new ConcreteUserMessageExp(messageId, tenant, user));

            if (mail == null)
            {
                return;
            }

            var daoMailInfo = daoFactory.CreateMailInfoDao(tenant, user);

            var maxValue = daoMailInfo.GetFieldMaxValue <bool>(
                SimpleMessagesExp.CreateBuilder(tenant, user)
                .SetChainId(mail.ChainId)
                .SetMailboxId(mail.MailboxId)
                .SetFolder((int)mail.Folder)
                .Build(),
                fieldFrom);

            var daoChain = daoFactory.CreateChainDao(tenant, user);

            daoChain.SetFieldValue(
                SimpleConversationsExp.CreateBuilder(tenant, user)
                .SetChainId(mail.ChainId)
                .SetMailboxId(mail.MailboxId)
                .SetFolder((int)mail.Folder)
                .Build(),
                fieldTo,
                maxValue);
        }
Пример #3
0
        public void RecalculateCounters(IDaoFactory daoFactory, List <int> userFolderIds)
        {
            var chainDao    = daoFactory.CreateChainDao(Tenant, User);
            var mailInfoDao = daoFactory.CreateMailInfoDao(Tenant, User);

            var totalUfMessList    = mailInfoDao.GetMailUserFolderCount(userFolderIds);
            var unreadUfMessUfList = mailInfoDao.GetMailUserFolderCount(userFolderIds, true);
            var totalUfConvList    = chainDao.GetChainUserFolderCount(userFolderIds);
            var unreadUfConvUfList = chainDao.GetChainUserFolderCount(userFolderIds, true);

            foreach (var id in userFolderIds.Select(id => (uint)id))
            {
                int totalMess;
                totalUfMessList.TryGetValue(id, out totalMess);

                int unreadMess;
                unreadUfMessUfList.TryGetValue(id, out unreadMess);

                int totalConv;
                totalUfConvList.TryGetValue(id, out totalConv);

                int unreadConv;
                unreadUfConvUfList.TryGetValue(id, out unreadConv);

                SetFolderCounters(daoFactory, id,
                                  unreadMess, totalMess, unreadConv, totalConv);
            }
        }
Пример #4
0
        private void GetValidForUserMessages(IDaoFactory daoFactory, List <int> messagesIds, out List <int> validIds,
                                             out List <ChainInfo> chains)
        {
            var daoMailInfo = daoFactory.CreateMailInfoDao(Tenant, User);

            var mailInfoList = daoMailInfo.GetMailInfoList(
                SimpleMessagesExp.CreateBuilder(Tenant, User)
                .SetMessageIds(messagesIds)
                .Build());

            validIds = new List <int>();
            chains   = new List <ChainInfo>();

            foreach (var mailInfo in mailInfoList)
            {
                validIds.Add(mailInfo.Id);
                chains.Add(new ChainInfo
                {
                    Id        = mailInfo.ChainId,
                    Folder    = mailInfo.Folder,
                    MailboxId = mailInfo.MailboxId
                });
            }
        }
Пример #5
0
        /// <summary>
        /// Set mailbox removed
        /// </summary>
        /// <param name="daoFactory"></param>
        /// <param name="mailBoxData"></param>
        /// <returns>Return freed quota value</returns>
        private static long RemoveMailBoxInfo(IDaoFactory daoFactory, MailBoxData mailBoxData)
        {
            if (mailBoxData.MailBoxId <= 0)
            {
                throw new Exception("MailBox id is 0");
            }

            var daoMailbox = daoFactory.CreateMailboxDao();

            var mailbox = daoMailbox.GetMailBox(
                new СoncreteUserMailboxExp(mailBoxData.MailBoxId, mailBoxData.TenantId, mailBoxData.UserId, null));

            if (mailbox == null)
            {
                throw new Exception(string.Format("MailBox with id = {0} (Tenant={1}, User='******') not found",
                                                  mailBoxData.MailBoxId, mailBoxData.TenantId, mailBoxData.UserId));
            }

            daoMailbox.SetMailboxRemoved(mailbox);

            var daoChain = daoFactory.CreateChainDao(mailBoxData.TenantId, mailBoxData.UserId);

            var folderTypes = Enum.GetValues(typeof(FolderType)).Cast <int>().ToList();

            daoChain.Delete(
                SimpleConversationsExp.CreateBuilder(mailBoxData.TenantId, mailBoxData.UserId)
                .SetFoldersIds(folderTypes)
                .SetMailboxId(mailBoxData.MailBoxId)
                .Build());

            var daoCrmLink = daoFactory.CreateCrmLinkDao(mailBoxData.TenantId, mailBoxData.UserId);

            daoCrmLink.RemoveCrmLinks(mailBoxData.MailBoxId);

            var daoMailInfo = daoFactory.CreateMailInfoDao(mailBoxData.TenantId, mailBoxData.UserId);

            daoMailInfo.SetFieldValue(
                SimpleMessagesExp.CreateBuilder(mailBoxData.TenantId, mailBoxData.UserId)
                .SetMailboxId(mailBoxData.MailBoxId)
                .Build(),
                MailTable.Columns.IsRemoved,
                true);

            var exp = new ConcreteMailboxAttachmentsExp(mailBoxData.MailBoxId, mailBoxData.TenantId, mailBoxData.UserId,
                                                        onlyEmbedded: null);

            var daoAttachment = daoFactory.CreateAttachmentDao(mailBoxData.TenantId, mailBoxData.UserId);

            var totalAttachmentsSize = daoAttachment.GetAttachmentsSize(exp);

            daoAttachment.SetAttachmnetsRemoved(exp);

            var tagDao = daoFactory.CreateTagDao(mailBoxData.TenantId, mailBoxData.UserId);

            var tagMailDao = daoFactory.CreateTagMailDao(mailBoxData.TenantId, mailBoxData.UserId);

            var tagIds = tagMailDao.GetTagIds(mailBoxData.MailBoxId);

            tagMailDao.DeleteByMailboxId(mailBoxData.MailBoxId);

            foreach (var tagId in tagIds)
            {
                var tag = tagDao.GetTag(tagId);

                if (tag == null)
                {
                    continue;
                }

                var count = tagMailDao.CalculateTagCount(tag.Id);

                tag.Count = count;

                tagDao.SaveTag(tag);
            }

            daoFactory.CreateMailboxSignatureDao(mailBoxData.TenantId, mailBoxData.UserId).DeleteSignature(mailBoxData.MailBoxId);

            daoFactory.CreateMailboxAutoreplyDao(mailBoxData.TenantId, mailBoxData.UserId)
            .DeleteAutoreply(mailBoxData.MailBoxId);

            daoFactory.CreateMailboxAutoreplyHistoryDao(mailBoxData.TenantId, mailBoxData.UserId).DeleteAutoreplyHistory(mailBoxData.MailBoxId);

            daoFactory.CreateAlertDao(mailBoxData.TenantId, mailBoxData.UserId).DeleteAlerts(mailBoxData.MailBoxId);

            daoFactory.CreateUserFolderXMailDao(mailBoxData.TenantId, mailBoxData.UserId)
            .RemoveByMailbox(mailBoxData.MailBoxId);

            return(totalAttachmentsSize);
        }
Пример #6
0
        private List <MailMessageData> GetFilteredConversations(IDaoFactory daoFactory, MailSearchFilterData filter, out bool hasMore)
        {
            var conversations = new List <MailMessageData>();
            var skipFlag      = false;
            var chunkIndex    = 0;

            if (filter.FromDate.HasValue && filter.FromMessage.HasValue && filter.FromMessage.Value > 0)
            {
                skipFlag = true;
            }

            var prevFlag   = filter.PrevFlag.GetValueOrDefault(false);
            var tenantInfo = CoreContext.TenantManager.GetTenant(Tenant);
            var utcNow     = DateTime.UtcNow;
            var pageSize   = filter.PageSize.GetValueOrDefault(25);

            var daoMailInfo = daoFactory.CreateMailInfoDao(Tenant, User);

            while (conversations.Count < pageSize + 1)
            {
                filter.PageSize = CHUNK_SIZE * pageSize;

                IMessagesExp exp = null;

                if (!filter.IsDefault() && FactoryIndexer <MailWrapper> .Support && FactoryIndexer.CheckState(false))
                {
                    filter.Page = chunkIndex * CHUNK_SIZE * pageSize; // Elastic Limit from {index of last message} to {count of messages}

                    List <MailWrapper> mailWrappers;
                    if (FilterChainMessagesExp.TryGetFullTextSearchChains(filter, User, out mailWrappers))
                    {
                        if (!mailWrappers.Any())
                        {
                            break;
                        }

                        var ids = mailWrappers.Select(c => c.Id).ToList();

                        var query = SimpleMessagesExp.CreateBuilder(Tenant, User)
                                    .SetMessageIds(ids)
                                    .SetOrderBy(filter.Sort);

                        if (prevFlag)
                        {
                            query.SetOrderAsc(!(filter.SortOrder == Defines.ASCENDING));
                        }
                        else
                        {
                            query.SetOrderAsc(filter.SortOrder == Defines.ASCENDING);
                        }

                        exp = query
                              .Build();
                    }
                }
                else
                {
                    filter.Page = chunkIndex; // MySQL Limit from {page by size} to {size}

                    exp = new FilterChainMessagesExp(filter, Tenant, User);
                }

                chunkIndex++;

                var listMessages = daoMailInfo.GetMailInfoList(exp, true)
                                   .ConvertAll(m => MessageEngine.ToMailMessage(m, tenantInfo, utcNow));

                if (0 == listMessages.Count)
                {
                    break;
                }

                if (skipFlag && filter.FromMessage.HasValue)
                {
                    var messageData = listMessages.FirstOrDefault(m => m.Id == filter.FromMessage.Value);

                    if (messageData != null)
                    {
                        // Skip chain messages by FromMessage.
                        listMessages =
                            listMessages.Where(
                                m => !(m.ChainId.Equals(messageData.ChainId) && m.MailboxId == messageData.MailboxId))
                            .ToList();
                    }

                    skipFlag = false;
                }

                foreach (var messageData in listMessages)
                {
                    var existingChainIndex =
                        conversations.FindIndex(
                            c => c.ChainId == messageData.ChainId && c.MailboxId == messageData.MailboxId);

                    if (existingChainIndex > -1)
                    {
                        if (conversations[existingChainIndex].Date < messageData.Date)
                        {
                            conversations[existingChainIndex] = messageData;
                        }
                    }
                    else
                    {
                        conversations.Add(messageData);
                    }
                }

                if (conversations.Count > pageSize)
                {
                    break;
                }
            }

            hasMore = conversations.Count > pageSize;

            if (hasMore)
            {
                conversations = conversations.Take(pageSize).ToList();
            }

            if (prevFlag)
            {
                conversations.Reverse();
            }

            return(conversations);
        }
Пример #7
0
        // Method for updating chain flags, date and length.
        public void UpdateChain(IDaoFactory daoFactory, string chainId, FolderType folder, uint?userFolderId, int mailboxId,
                                int tenant, string user)
        {
            if (string.IsNullOrEmpty(chainId))
            {
                return;
            }

            var db     = daoFactory.DbManager;
            var engine = new EngineFactory(tenant, user, Log);

            var daoMailInfo = daoFactory.CreateMailInfoDao(tenant, user);

            const string m_alias = "m";

            var getChainQuery = new SqlQuery(MailTable.TABLE_NAME.Alias(m_alias))
                                .SelectCount()
                                .SelectMax(MailTable.Columns.DateSent.Prefix(m_alias))
                                .SelectMax(MailTable.Columns.Unread.Prefix(m_alias))
                                .SelectMax(MailTable.Columns.AttachCount.Prefix(m_alias))
                                .SelectMax(MailTable.Columns.Importance.Prefix(m_alias))
                                .Where(MailTable.Columns.Tenant.Prefix(m_alias), tenant)
                                .Where(MailTable.Columns.User.Prefix(m_alias), user)
                                .Where(MailTable.Columns.IsRemoved.Prefix(m_alias), 0)
                                .Where(MailTable.Columns.ChainId.Prefix(m_alias), chainId)
                                .Where(MailTable.Columns.MailboxId.Prefix(m_alias), mailboxId)
                                .Where(MailTable.Columns.Folder.Prefix(m_alias), (int)folder);

            var chainInfo = db.ExecuteList(getChainQuery)
                            .ConvertAll(x => new
            {
                length       = Convert.ToInt32(x[0]),
                date         = Convert.ToDateTime(x[1]),
                unread       = Convert.ToBoolean(x[2]),
                attach_count = Convert.ToInt32(x[3]),
                importance   = Convert.ToBoolean(x[4])
            })
                            .FirstOrDefault();

            if (chainInfo == null)
            {
                throw new InvalidDataException("Conversation is absent in MAIL_MAIL");
            }

            var daoChain = daoFactory.CreateChainDao(tenant, user);

            var query = SimpleConversationsExp.CreateBuilder(tenant, user)
                        .SetMailboxId(mailboxId)
                        .SetChainId(chainId)
                        .SetFolder((int)folder)
                        .Build();

            var storedChainInfo = daoChain.GetChains(query);

            var chainUnreadFlag = storedChainInfo.Any(c => c.Unread);

            if (0 == chainInfo.length)
            {
                var deletQuery = SimpleConversationsExp.CreateBuilder(tenant, user)
                                 .SetFolder((int)folder)
                                 .SetMailboxId(mailboxId)
                                 .SetChainId(chainId)
                                 .Build();

                var result = daoChain.Delete(deletQuery);

                Log.DebugFormat(
                    "UpdateChain() row deleted from chain table tenant='{0}', user_id='{1}', id_mailbox='{2}', folder='{3}', chain_id='{4}' result={5}",
                    tenant, user, mailboxId, folder, chainId, result);

                var unreadConvDiff = chainUnreadFlag ? -1 : (int?)null;

                engine.FolderEngine.ChangeFolderCounters(daoFactory, folder, userFolderId,
                                                         unreadConvDiff: unreadConvDiff, totalConvDiff: -1);
            }
            else
            {
                //var updateMailQuery = new SqlUpdate(MailTable.TABLE_NAME)
                //    .Where(MailTable.Columns.Tenant, tenant)
                //    .Where(MailTable.Columns.User, user)
                //    .Where(MailTable.Columns.IsRemoved, 0)
                //    .Where(MailTable.Columns.ChainId, chainId)
                //    .Where(MailTable.Columns.MailboxId, mailboxId)
                //    .Where(MailTable.Columns.Folder, (int) folder)
                //    // Folder condition important because chain has different dates in different folders(Ex: Sent and Inbox).
                //    .Set(MailTable.Columns.ChainDate, chainInfo.date);

                //db.ExecuteNonQuery(updateMailQuery);

                var updateQuery = SimpleMessagesExp.CreateBuilder(tenant, user)
                                  .SetChainId(chainId)
                                  .SetMailboxId(mailboxId)
                                  .SetFolder((int)folder)
                                  .Build();

                daoMailInfo.SetFieldValue(updateQuery,
                                          MailTable.Columns.ChainDate,
                                          chainInfo.date);

                var tags = GetChainTags(daoFactory, chainId, folder, mailboxId, tenant, user);

                var chain = new Chain
                {
                    Id             = chainId,
                    Tenant         = tenant,
                    User           = user,
                    MailboxId      = mailboxId,
                    Folder         = folder,
                    Length         = chainInfo.length,
                    Unread         = chainInfo.unread,
                    HasAttachments = chainInfo.attach_count > 0,
                    Importance     = chainInfo.importance,
                    Tags           = tags
                };

                var result = daoChain.SaveChain(chain);

                if (result <= 0)
                {
                    throw new InvalidOperationException(string.Format("Invalid insert into {0}", ChainTable.TABLE_NAME));
                }

                Log.DebugFormat(
                    "UpdateChain() row inserted to chain table tenant='{0}', user_id='{1}', id_mailbox='{2}', folder='{3}', chain_id='{4}'",
                    tenant, user, mailboxId, folder, chainId);

                var unreadConvDiff = (int?)null;
                var totalConvDiff  = (int?)null;

                if (!storedChainInfo.Any())
                {
                    totalConvDiff  = 1;
                    unreadConvDiff = chainInfo.unread ? 1 : (int?)null;
                }
                else
                {
                    if (chainUnreadFlag != chainInfo.unread)
                    {
                        unreadConvDiff = chainInfo.unread ? 1 : -1;
                    }
                }

                engine.FolderEngine.ChangeFolderCounters(daoFactory, folder, userFolderId,
                                                         unreadConvDiff: unreadConvDiff, totalConvDiff: totalConvDiff);
            }
        }