Exemplo n.º 1
0
        public Task <IEnumerable <UnicastMessage <MailMessage> > > GetMailMessages(Signature signature, ExchangePrivateKey exchangePrivateKey)
        {
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }
            if (exchangePrivateKey == null)
            {
                throw new ArgumentNullException(nameof(exchangePrivateKey));
            }

            return(Task.Run(() =>
            {
                var results = new List <UnicastMessage <MailMessage> >();

                var trusts = new List <UnicastMetadata>();

                foreach (var unicastMetadata in _coreManager.GetUnicastMetadatas(signature, "MailMessage"))
                {
                    if (_searchSignatures.Contains(unicastMetadata.Certificate.GetSignature()))
                    {
                        trusts.Add(unicastMetadata);
                    }
                }

                trusts.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));

                foreach (var unicastMetadata in trusts.Take(1024))
                {
                    var dic = _cache_MailMessages.GetOrAdd(unicastMetadata.Signature, (_) => new LockedHashDictionary <UnicastMetadata, UnicastMessage <MailMessage> >());

                    // Cache
                    {
                        if (dic.TryGetValue(unicastMetadata, out var result))
                        {
                            results.Add(result);

                            continue;
                        }
                    }

                    {
                        var stream = _coreManager.VolatileGetStream(unicastMetadata.Metadata, 1024 * 1024 * 1);
                        if (stream == null)
                        {
                            continue;
                        }

                        var result = new UnicastMessage <MailMessage>(
                            unicastMetadata.Signature,
                            unicastMetadata.Certificate.GetSignature(),
                            unicastMetadata.CreationTime,
                            ContentConverter.FromCryptoStream <MailMessage>(stream, exchangePrivateKey));

                        if (result.Value == null)
                        {
                            continue;
                        }

                        dic[unicastMetadata] = result;

                        results.Add(result);
                    }
                }

                return (IEnumerable <UnicastMessage <MailMessage> >)results.ToArray();
            }));
        }
Exemplo n.º 2
0
        public Task <IEnumerable <UnicastCommentMessage> > GetUnicastCommentMessages(Signature signature, AgreementPrivateKey agreementPrivateKey,
                                                                                     int messageCountUpperLimit, IEnumerable <MessageCondition> conditions)
        {
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }
            if (agreementPrivateKey == null)
            {
                throw new ArgumentNullException(nameof(agreementPrivateKey));
            }

            return(Task.Run(() =>
            {
                try
                {
                    var filter = new Dictionary <Signature, HashSet <DateTime> >();
                    {
                        foreach (var item in conditions)
                        {
                            filter.GetOrAdd(item.AuthorSignature, (_) => new HashSet <DateTime>()).Add(item.CreationTime);
                        }
                    }

                    var trustedMetadatas = new List <UnicastMetadata>();
                    {
                        foreach (var unicastMetadata in _coreManager.GetUnicastMetadatas(signature, "MailMessage"))
                        {
                            if (!_searchSignatures.Contains(unicastMetadata.Certificate.GetSignature()))
                            {
                                continue;
                            }

                            trustedMetadatas.Add(unicastMetadata);
                        }

                        trustedMetadatas.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));
                    }

                    var results = new List <UnicastCommentMessage>();

                    foreach (var unicastMetadata in trustedMetadatas.Take(messageCountUpperLimit))
                    {
                        if (filter.TryGetValue(unicastMetadata.Certificate.GetSignature(), out var hashSet) && hashSet.Contains(unicastMetadata.CreationTime))
                        {
                            continue;
                        }

                        var stream = _coreManager.VolatileGetStream(unicastMetadata.Metadata, 1024 * 1024 * 1);
                        if (stream == null)
                        {
                            continue;
                        }

                        var result = new UnicastCommentMessage(
                            unicastMetadata.Signature,
                            unicastMetadata.Certificate.GetSignature(),
                            unicastMetadata.CreationTime,
                            ContentConverter.FromCryptoStream <CommentContent>(stream, agreementPrivateKey, 0));

                        if (result.Value == null)
                        {
                            continue;
                        }

                        results.Add(result);
                    }

                    return (IEnumerable <UnicastCommentMessage>)results.ToArray();
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }

                return Array.Empty <UnicastCommentMessage>();
            }));
        }