コード例 #1
0
        public async Task <Chat> GetDialogChatAsync(int otherId)
        {
            if (_dialogCache.ContainsKey(otherId))
            {
                var chatId = _dialogCache[otherId];
                lock (_lock)
                    if (_poller != null)
                    {
                        lock (_poller.ChatsStorage)
                            if (_poller.ChatsStorage.ContainsKey(chatId))
                            {
                                return(_poller.ChatsStorage[chatId]);
                            }
                    }
            }

            var response = await _client.GetAsync($"chats/dialog/{UserId}/{otherId}").ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var ret = await response.Content.ReadAsAsync <Chat>();

                if (!_dialogCache.ContainsKey(otherId))
                {
                    _dialogCache.Add(otherId, ret.Id);
                }
                return(ret);
            }
            return(null);
        }
コード例 #2
0
        public async Task <IEnumerable <Message> > GetChatMessagesAsync(int id)
        {
            await _semaphore.WaitAsync();

            try
            {
                if (_messageCache.ContainsKey(id))
                {
                    _messageCache[id].RemoveAll(x => x.ExpirationDate != null && x.ExpirationDate < DateTime.Now);
                    return(_messageCache[id]);
                }

                var response = await _client.GetAsync($"messages/chats/{id}").ConfigureAwait(false);

                if (!response.IsSuccessStatusCode)
                {
                    return(null);
                }

                var ret = await response.Content.ReadAsAsync <List <Message> >();

                _messageCache.Add(id, new List <Message>(ret));
                return(ret);
            }
            catch (Exception e)
            {
                Trace.Write(e.ToString());
                throw;
            }
            finally
            {
                _semaphore.Release();
            }
        }
コード例 #3
0
 public void SetChatUserInfo(int userId, int chatId, ChatUserInfo chatUserInfo)
 {
     lock (ChatUserInfosCache)
     {
         var key = new ChatUserId(userId, chatId);
         if (ChatUserInfosCache.ContainsKey(key))
         {
             ChatUserInfosCache[key] = chatUserInfo;
         }
         else
         {
             ChatUserInfosCache.Add(key, chatUserInfo);
         }
         if (_newChatUserInfosHandlers.ContainsKey(key))
         {
             _newChatUserInfosHandlers[key]?.Invoke(this, chatUserInfo);
         }
     }
 }