예제 #1
0
 public async Task AddOnlineUser(string userId)
 {
     if (!await _onlineUsers.ExistsAsync(userId))
     {
         await _onlineUsers.AddAsync(userId, await GetChatUser(userId));
     }
 }
예제 #2
0
        public static async Task <UserApiAuthKey> GetApiAuthKey(string apiKey)
        {
            var existing = await _rediscache.GetValueAsync(apiKey);

            if (existing != null)
            {
                return(existing);
            }

            using (var context = new ApplicationDbContext())
            {
                existing = await context.Users
                           .Where(x => x.ApiKey == apiKey)
                           .Select(x => new UserApiAuthKey
                {
                    UserId    = x.Id,
                    Key       = x.ApiKey,
                    Secret    = x.ApiSecret,
                    IsEnabled = !x.IsDisabled && x.IsApiEnabled
                }).FirstOrDefaultNoLockAsync();
            }

            if (existing != null)
            {
                await _rediscache.AddAsync(apiKey, existing);
            }

            return(existing);
        }
예제 #3
0
        public async Task AddMessage(ChatMessageData message)
        {
            try
            {
                var messageCache = await GetMessages();

                if (messageCache.Count > _historyCount)
                {
                    await _chatMessages.RemoveAsync(messageCache.Min(x => x.Id).ToString());
                }

                using (var context = new ApplicationDbContext())
                {
                    var chatMessage = new ChatMessage
                    {
                        Message   = message.Message,
                        Timestamp = message.Timestamp,
                        UserId    = message.UserId,
                        IsEnabled = true
                    };
                    context.ChatMessages.Add(chatMessage);
                    await context.SaveChangesAsync();

                    message.Id = chatMessage.Id;
                    await _chatMessages.AddAsync(message.Id.ToString(), message);
                }
            }
            catch (Exception)
            {
            }
        }