private async Task <PrivateConversation> AcceptRequest(Request request, bool accept) { PrivateConversation newConversation = null; request.Completed = true; await semaphoreSlim.WaitAsync(); try { if (accept) { if (await _dbContext.AreFriends(request.CreatorId, request.TargetId)) { await _dbContext.SaveChangesAsync(); throw new AiurAPIModelException(ErrorType.HasSuccessAlready, "You two are already friends."); } newConversation = _dbContext.AddFriend(request.CreatorId, request.TargetId); } await _dbContext.SaveChangesAsync(); } finally { semaphoreSlim.Release(); } await Task.WhenAll( _pusher.FriendsChangedEvent( request.Creator, request, accept, newConversation?.Build(request.CreatorId, _onlineJudger) as PrivateConversation), _pusher.FriendsChangedEvent( request.Target, request, accept, newConversation?.Build(request.TargetId, _onlineJudger) as PrivateConversation)); return(newConversation); }
public async Task <IActionResult> CompleteRequest(CompleteRequestAddressModel model) { var user = await GetKahlaUser(); var request = await _dbContext .Requests .Include(t => t.Creator) .ThenInclude(t => t.HisDevices) .Include(t => t.Target) .ThenInclude(t => t.HisDevices) .SingleOrDefaultAsync(t => t.Id == model.Id); if (request == null) { return(this.Protocol(ErrorType.NotFound, "We can not find target request.")); } if (request.TargetId != user.Id) { return(this.Protocol(ErrorType.Unauthorized, "The target user of this request is not you.")); } if (request.Completed) { return(this.Protocol(ErrorType.HasDoneAlready, "The target request is already completed.")); } request.Completed = true; PrivateConversation newConversation = null; if (model.Accept) { if (await _dbContext.AreFriends(request.CreatorId, request.TargetId)) { await _dbContext.SaveChangesAsync(); return(this.Protocol(ErrorType.RequireAttention, "You two are already friends.")); } newConversation = _dbContext.AddFriend(request.CreatorId, request.TargetId); await _dbContext.SaveChangesAsync(); } else { await _dbContext.SaveChangesAsync(); } await Task.WhenAll( _pusher.FriendsChangedEvent( request.Creator, request, model.Accept, newConversation?.Build(request.CreatorId, _onlineJudger) as PrivateConversation), _pusher.FriendsChangedEvent( request.Target, request, model.Accept, newConversation?.Build(request.TargetId, _onlineJudger) as PrivateConversation) ); return(Json(new AiurValue <int?>(newConversation?.Id) { Code = ErrorType.Success, Message = "You have successfully completed this request." })); }