Пример #1
0
        public async Task <IActionResult> CreateRequest([Required] string id)
        {
            var user = await GetKahlaUser();

            await _dbContext.Entry(user)
            .Collection(t => t.HisDevices)
            .LoadAsync();

            var target = await _dbContext.Users.Include(t => t.HisDevices).SingleOrDefaultAsync(t => t.Id == id);

            if (target == null)
            {
                return(this.Protocol(ErrorType.NotFound, "We can not find your target user!"));
            }
            if (target.Id == user.Id)
            {
                return(this.Protocol(ErrorType.RequireAttention, "You can't request yourself!"));
            }
            var areFriends = await _dbContext.AreFriends(user.Id, target.Id);

            if (areFriends)
            {
                return(this.Protocol(ErrorType.HasDoneAlready, "You two are already friends!"));
            }
            Request request;

            lock (Obj)
            {
                var pending = _dbContext.Requests
                              .Where(t =>
                                     t.CreatorId == user.Id && t.TargetId == target.Id ||
                                     t.CreatorId == target.Id && t.TargetId == user.Id)
                              .Any(t => !t.Completed);
                if (pending)
                {
                    return(this.Protocol(ErrorType.HasDoneAlready, "There are some pending request hasn't been completed!"));
                }
                request = new Request
                {
                    CreatorId = user.Id,
                    Creator   = user,
                    TargetId  = id,
                };
                _dbContext.Requests.Add(request);
                _dbContext.SaveChanges();
            }
            await Task.WhenAll(
                _pusher.NewFriendRequestEvent(target, request),
                _pusher.NewFriendRequestEvent(user, request)
                );

            return(Json(new AiurValue <int>(request.Id)
            {
                Code = ErrorType.Success,
                Message = "Successfully created your request!"
            }));
        }
Пример #2
0
        public async Task <IActionResult> CreateRequest([Required] string id)
        {
            var user = await GetKahlaUser();

            if (!user.EmailConfirmed)
            {
                return(this.Protocol(ErrorType.Unauthorized, "You are not allowed to create friend requests without confirming your email!"));
            }
            var target = await _dbContext.Users.FindAsync(id);

            if (target == null)
            {
                return(this.Protocol(ErrorType.NotFound, "We can not find your target user!"));
            }
            if (target.Id == user.Id)
            {
                return(this.Protocol(ErrorType.RequireAttention, "You can't request yourself!"));
            }
            var areFriends = await _dbContext.AreFriends(user.Id, target.Id);

            if (areFriends)
            {
                return(this.Protocol(ErrorType.HasDoneAlready, "You two are already friends!"));
            }
            Request request;

            lock (_obj)
            {
                var pending = _dbContext.Requests
                              .Where(t =>
                                     t.CreatorId == user.Id && t.TargetId == target.Id ||
                                     t.CreatorId == target.Id && t.TargetId == user.Id)
                              .Any(t => !t.Completed);
                if (pending)
                {
                    return(this.Protocol(ErrorType.HasDoneAlready, "There are some pending request hasn't been completed!"));
                }
                request = new Request {
                    CreatorId = user.Id, TargetId = id
                };
                _dbContext.Requests.Add(request);
                _dbContext.SaveChanges();
            }
            await _pusher.NewFriendRequestEvent(target, user);

            return(Json(new AiurValue <int>(request.Id)
            {
                Code = ErrorType.Success,
                Message = "Successfully created your request!"
            }));
        }
Пример #3
0
        public async Task <IActionResult> CreateRequest([Required] string id)
        {
            var user = await GetKahlaUser();

            await _dbContext.Entry(user)
            .Collection(t => t.HisDevices)
            .LoadAsync();

            var target = await _dbContext.Users.Include(t => t.HisDevices).SingleOrDefaultAsync(t => t.Id == id);

            if (target == null)
            {
                return(this.Protocol(ErrorType.NotFound, "We can not find your target user!"));
            }
            if (target.Id == user.Id)
            {
                return(this.Protocol(ErrorType.Conflict, "You can't request yourself!"));
            }
            var areFriends = await _dbContext.AreFriends(user.Id, target.Id);

            if (areFriends)
            {
                return(this.Protocol(ErrorType.Conflict, "You two are already friends!"));
            }
            Request request;
            await semaphoreSlim.WaitAsync();

            try
            {
                var pending = await _dbContext.Requests
                              .Where(t =>
                                     t.CreatorId == user.Id && t.TargetId == target.Id ||
                                     t.CreatorId == target.Id && t.TargetId == user.Id)
                              .AnyAsync(t => !t.Completed);

                if (pending)
                {
                    return(this.Protocol(ErrorType.Conflict, "There are some pending request hasn't been completed!"));
                }
                request = new Request
                {
                    CreatorId = user.Id,
                    Creator   = user,
                    TargetId  = id,
                };
                await _dbContext.Requests.AddAsync(request);

                await _dbContext.SaveChangesAsync();
            }
            finally
            {
                semaphoreSlim.Release();
            }
            await Task.WhenAll(
                _pusher.NewFriendRequestEvent(target, request),
                _pusher.NewFriendRequestEvent(user, request)
                );

            if (_configuration["AutoAcceptRequests"] == true.ToString().ToLower())
            {
                await AcceptRequest(request, true);
            }
            return(this.Protocol(new AiurValue <int>(request.Id)
            {
                Code = ErrorType.Success,
                Message = "Successfully created your request!"
            }));
        }