Esempio n. 1
0
        public bool AddMessageAdmin(ChatMessage chatMessage)
        {
            if (chatMessage == null)
            {
                return(false);
            }

            DateTime now            = DateTime.Now;
            string   compareMessage = ChatFilter.CutOff(chatMessage.Content, " ,.-_():;/\\\'\"");

            //trong vòng 300s=5p không được gửi tin nhắn có nội dung giống nhau
            //List<ChatMessage> lst = new List<ChatMessage>(LastMessages.Where(m => m != null && !string.IsNullOrEmpty(m.Content) && ChatFilter.CutOff(m.Content, " ,.-_():;/\\\'\"").Equals(compareMessage) && m.CreatedDate.AddSeconds(ChatController.DUPLICATE_MESSAGE_DURATION) > now));
            //if (lst.Count > 0)
            //    return false;

            if (LastMessages.Count > ChatController.MAX_MESSAGE_IN_CHANNEL)
            {
                if (Monitor.TryEnter(LastMessages, 500))
                {
                    LastMessages.RemoveRange(0, LastMessages.Count - ChatController.MAX_MESSAGE_IN_CHANNEL);
                    Monitor.Exit(LastMessages);
                }
            }

            LastMessages.Add(chatMessage);
            return(true);
        }
Esempio n. 2
0
        public bool AddMessage(string channelId, ChatMessage chatMessage)
        {
            DateTime now = DateTime.Now;

            if (LastMessages.Count > 0)
            {
                if (DateTime.Compare(LastMessageSentTime.AddSeconds(ChatController.TWO_MESSAGE_DURATION), now) > 0)
                {
                    return(false);
                }

                if (LastMessages.Count > 100)
                {
                    int pos = LastMessages.Count - 100;
                    if (LastMessages.ElementAt(pos).CreatedDate.AddSeconds(ChatController.TEN_MESSAGE_DURATION) > now)
                    {
                        return(false);
                    }
                }

                string             compareMessage = ChatFilter.CutOff(chatMessage.Content, " ,.-_():;/\\\'\"");
                List <ChatMessage> lst            = new List <ChatMessage>(LastMessages.Where(m => m != null && !string.IsNullOrEmpty(m.Content) && ChatFilter.CutOff(m.Content, " ,.-_():;/\\\'\"").Equals(compareMessage) && m.CreatedDate.AddSeconds(ChatController.GLOBAL_TEN_SAME_MESSAGE_DURATION) > now));
                if (lst.Count > 120)
                {
                    return(false);
                }

                if (LastMessages.Count > 1200)
                {
                    if (Monitor.TryEnter(LastMessages, 50000))
                    {
                        LastMessages.RemoveRange(0, LastMessages.Count - 1200);
                        Monitor.Exit(LastMessages);
                    }
                }
            }

            LastActivity        = now;
            LastMessageSentTime = now;
            LastMessages.Add(chatMessage);
            return(true);
        }