Example #1
0
        public Task <IEnumerable <MulticastMessage <ChatMessage> > > GetChatMessages(Tag tag)
        {
            if (tag == null)
            {
                throw new ArgumentNullException(nameof(tag));
            }

            var now = DateTime.UtcNow;

            return(Task.Run(() =>
            {
                var results = new List <MulticastMessage <ChatMessage> >();

                var trusts = new List <MulticastMetadata>();
                var untrusts = new List <MulticastMetadata>();

                foreach (var multicastMetadata in _coreManager.GetMulticastMetadatas(tag, "ChatMessage"))
                {
                    if (_searchSignatures.Contains(multicastMetadata.Certificate.GetSignature()))
                    {
                        trusts.Add(multicastMetadata);
                    }
                    else
                    {
                        if ((now - multicastMetadata.CreationTime).TotalDays > 7)
                        {
                            continue;
                        }

                        untrusts.Add(multicastMetadata);
                    }
                }

                trusts.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));
                untrusts.Sort((x, y) =>
                {
                    int c;
                    if (0 != (c = y.Cost.CashAlgorithm.CompareTo(x.Cost.CashAlgorithm)))
                    {
                        return c;
                    }
                    if (0 != (c = y.Cost.Value.CompareTo(x.Cost.Value)))
                    {
                        return c;
                    }

                    return y.CreationTime.CompareTo(x.CreationTime);
                });

                foreach (var multicastMetadata in CollectionUtils.Unite(trusts.Take(1024), untrusts.Take(1024)))
                {
                    var dic = _cache_ChatMessages.GetOrAdd(multicastMetadata.Tag, (_) => new LockedHashDictionary <MulticastMetadata, MulticastMessage <ChatMessage> >());

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

                            continue;
                        }
                    }

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

                        var result = new MulticastMessage <ChatMessage>(
                            multicastMetadata.Tag,
                            multicastMetadata.Certificate.GetSignature(),
                            multicastMetadata.CreationTime,
                            multicastMetadata.Cost,
                            ContentConverter.FromStream <ChatMessage>(stream));

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

                        dic[multicastMetadata] = result;

                        results.Add(result);
                    }
                }

                return (IEnumerable <MulticastMessage <ChatMessage> >)results.ToArray();
            }));
        }
Example #2
0
        public Task <IEnumerable <MulticastCommentMessage> > GetMulticastCommentMessages(Tag tag,
                                                                                         int trustMessageCountUpperLimit, int untrustMessageCountUpperLimit, IEnumerable <MessageCondition> conditions)
        {
            if (tag == null)
            {
                throw new ArgumentNullException(nameof(tag));
            }

            var now = DateTime.UtcNow;

            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 <MulticastMetadata>();
                    var untrustedMetadatas = new List <MulticastMetadata>();
                    {
                        foreach (var multicastMetadata in _coreManager.GetMulticastMetadatas(tag, "ChatMessage"))
                        {
                            if (_searchSignatures.Contains(multicastMetadata.Certificate.GetSignature()))
                            {
                                trustedMetadatas.Add(multicastMetadata);
                            }
                            else
                            {
                                untrustedMetadatas.Add(multicastMetadata);
                            }
                        }

                        trustedMetadatas.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));
                        untrustedMetadatas.Sort((x, y) =>
                        {
                            int c;
                            if (0 != (c = y.Cost.CashAlgorithm.CompareTo(x.Cost.CashAlgorithm)))
                            {
                                return c;
                            }
                            if (0 != (c = y.Cost.Value.CompareTo(x.Cost.Value)))
                            {
                                return c;
                            }

                            return y.CreationTime.CompareTo(x.CreationTime);
                        });
                    }

                    var results = new List <MulticastCommentMessage>();

                    foreach (var multicastMetadata in CollectionUtils.Unite(trustedMetadatas.Take(trustMessageCountUpperLimit), untrustedMetadatas.Take(untrustMessageCountUpperLimit)))
                    {
                        if (filter.TryGetValue(multicastMetadata.Certificate.GetSignature(), out var hashSet) && hashSet.Contains(multicastMetadata.CreationTime))
                        {
                            continue;
                        }

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

                        var content = ContentConverter.FromStream <CommentContent>(stream, 0);
                        if (content == null)
                        {
                            continue;
                        }

                        var result = new MulticastCommentMessage(
                            multicastMetadata.Tag,
                            multicastMetadata.Certificate.GetSignature(),
                            multicastMetadata.CreationTime,
                            multicastMetadata.Cost,
                            content);

                        results.Add(result);
                    }

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

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