public void SubscribeToNewChatMessages(int chatId, EventHandler <IEnumerable <Message> > handler)
 {
     lock (ChatsStorage)
     {
         if (!ChatsStorage.ContainsKey(chatId))
         {
             ChatsStorage.Add(chatId, new Chat {
                 Id = chatId
             });
             ChatMessagesDictionary.Add(chatId, new MessageWithState {
                 IsPolled = false, MessageId = -1
             });
         }
     }
     lock (_newChatMessagesHandlers)
     {
         if (_newChatMessagesHandlers.ContainsKey(chatId))
         {
             _newChatMessagesHandlers[chatId] += handler;
         }
         else
         {
             _newChatMessagesHandlers.Add(chatId, handler);
         }
     }
 }
        private void ProcessNewChats(IEnumerable <Chat> chats)
        {
            if (chats == null)
            {
                return;
            }

            var chatsArray = chats as Chat[] ?? chats.ToArray();

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

            lock (ChatsStorage)
                foreach (var chat in chatsArray)
                {
                    if (!ChatsStorage.ContainsKey(chat.Id))
                    {
                        ChatsStorage.Add(chat.Id, chat);
                    }
                }
            NewChatsEvent?.Invoke(this, chatsArray);
            AreChatsPolled = true;
        }
 public void SetChatInfo(int chatId, ChatInfo chatInfo)
 {
     lock (ChatsStorage)
     {
         if (!ChatsStorage.ContainsKey(chatId))
         {
             throw new ArgumentException("Could not set chat info");
         }
         ChatsStorage[chatId]      = ChatsStorage[chatId].CloneJson();
         ChatsStorage[chatId].Info = chatInfo;
         lock (_newChatInfoHandlers)
             if (_newChatInfoHandlers.ContainsKey(chatId))
             {
                 _newChatInfoHandlers[chatId]?.Invoke(this, ChatsStorage[chatId]);
             }
     }
 }
 public void SetChat(Chat chat)
 {
     lock (ChatsStorage)
     {
         if (ChatsStorage.ContainsKey(chat.Id))
         {
             ChatsStorage[chat.Id] = chat;
             lock (_newChatInfoHandlers)
                 if (_newChatInfoHandlers.ContainsKey(chat.Id))
                 {
                     _newChatInfoHandlers[chat.Id]?.Invoke(this, chat);
                 }
         }
         else
         {
             ChatsStorage.Add(chat.Id, chat);
             NewChatsEvent?.Invoke(this, new[] { chat });
         }
     }
 }
        private void ProcessLostChats(IEnumerable <int> chatIds)
        {
            if (chatIds == null)
            {
                return;
            }

            var chatIdsArray = chatIds as int[] ?? chatIds.ToArray();

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

            lock (ChatsStorage)
                foreach (var chatId in chatIdsArray)
                {
                    if (ChatsStorage.ContainsKey(chatId))
                    {
                        ChatsStorage.Remove(chatId);
                    }
                }
            LostChatsEvent?.Invoke(this, chatIdsArray);
        }