public async Task <string> AddNewOperationAsync(long nodeId, long userId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                long currentTime          = DateTime.UtcNow.ToUnixTime();
                var  uncomplededOperation = await context.ChangeUserNodeOperations
                                            .FirstOrDefaultAsync(opt => !opt.Completed && opt.UserId == userId && opt.ExpirationTime > currentTime).ConfigureAwait(false);

                if (uncomplededOperation != null)
                {
                    context.Remove(uncomplededOperation);
                }
                ChangeUserNodeOperation operation = new ChangeUserNodeOperation
                {
                    NodeId         = nodeId,
                    UserId         = userId,
                    OperationId    = RandomExtensions.NextString(64),
                    Completed      = false,
                    RequestTime    = currentTime,
                    ExpirationTime = currentTime + (long)TimeSpan.FromDays(1).TotalSeconds
                };
                await context.AddAsync(operation).ConfigureAwait(false);

                await context.SaveChangesAsync().ConfigureAwait(false);

                return(operation.OperationId);
            }
        }
Exemplo n.º 2
0
        public async Task RemoveUserFavoritesAsync(long?channelId, long?chatId, Guid?contactId, long userId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var query = context.UsersFavorites.Where(opt => opt.UserId == userId);
                if (channelId != null)
                {
                    query = query.Where(opt => opt.ChannelId == channelId);
                }
                else if (chatId != null)
                {
                    query = query.Where(opt => opt.ChatId == chatId);
                }
                else if (contactId != null)
                {
                    query = query.Where(opt => opt.ContactId == contactId);
                }
                else
                {
                    throw new ArgumentNullException();
                }
                var favorite = await query.FirstOrDefaultAsync().ConfigureAwait(false);

                if (favorite != null)
                {
                    context.Remove(favorite);
                    await context.SaveChangesAsync().ConfigureAwait(false);
                }
                else
                {
                    throw new ObjectDoesNotExistsException("Object not found in favorites.");
                }
            }
        }
Exemplo n.º 3
0
        public async Task <TokenVm> CheckTokenAsync(TokenVm targetToken, long nodeId)
        {
            long userId = targetToken.UserId;

            using (MessengerDbContext context = contextFactory.Create())
            {
                Token tokenPair = await context.Tokens
                                  .Include(token => token.User)
                                  .FirstOrDefaultAsync(
                    token => token.UserId == userId && token.AccessToken == targetToken.AccessToken && !token.User.Deleted)
                                  .ConfigureAwait(false);

                if (tokenPair == null)
                {
                    User userInfo = await context.Users.FindAsync(userId).ConfigureAwait(false);

                    if (userInfo != null && userInfo.NodeId != nodeId)
                    {
                        throw new UserFromAnotherNodeException(userInfo.NodeId);
                    }
                    throw new InvalidTokenException();
                }

                if (DateTime.UtcNow <= tokenPair.AccessTokenExpirationTime.ToDateTime())
                {
                    tokenPair.DeviceTokenId = targetToken.DeviceTokenId;
                    context.Update(tokenPair);
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    return(TokenConverter.GetTokenVm(tokenPair));
                }
                if (DateTime.UtcNow >= tokenPair.AccessTokenExpirationTime.ToDateTime() &&
                    DateTime.UtcNow <= tokenPair.RefreshTokenExpirationTime.GetValueOrDefault().ToDateTime())
                {
                    string accessToken  = RandomExtensions.NextString(64);
                    string refreshToken = RandomExtensions.NextString(64);
                    Token  newToken     = new Token
                    {
                        AccessToken                = accessToken,
                        RefreshToken               = refreshToken,
                        UserId                     = userId,
                        DeviceTokenId              = targetToken.DeviceTokenId,
                        AccessTokenExpirationTime  = DateTime.UtcNow.AddHours(ACCESS_LIFETIME_HOUR).ToUnixTime(),
                        RefreshTokenExpirationTime = DateTime.UtcNow.AddHours(REFRESH_LIFETIME_HOUR).ToUnixTime()
                    };
                    await context.Tokens.AddAsync(newToken).ConfigureAwait(false);

                    context.Remove(tokenPair);
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    context.Database.CloseConnection();
                    return(TokenConverter.GetTokenVm(newToken));
                }
                throw new TokensTimeoutException();
            }
        }
Exemplo n.º 4
0
        public void Delete(Chat entity)
        {
            if (context.Entry(entity).State == EntityState.Detached)
            {
                context.Attach(entity);
            }

            context.Remove(entity);
            context.SaveChanges();
        }
        public async Task DeleteChannelAsync(long channelId, long userId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var channelUser = await context.ChannelUsers
                                  .Include(opt => opt.Channel)
                                  .FirstOrDefaultAsync(opt => opt.ChannelId == channelId &&
                                                       opt.UserId == userId &&
                                                       opt.ChannelUserRole == ChannelUserRole.Creator)
                                  .ConfigureAwait(false);

                if (channelUser != null)
                {
                    context.Remove(channelUser.Channel);
                    await context.SaveChangesAsync().ConfigureAwait(false);
                }
                else
                {
                    throw new PermissionDeniedException();
                }
            }
        }
Exemplo n.º 6
0
 public void Delete(Message entity)
 {
     context.Remove(entity);
     context.SaveChanges();
 }