private dynamic OnFriendRequestReply(dynamic arg) { var helper = new SerializeHelper(); var requestReply = helper.DeserializeObject <FriendRequestReply>(Request.Body); $"Got a friend request reply from { requestReply.Request.TargetAccount.Name } to { requestReply.Request.RequesterAccount.Name }" .WriteToConsole(); Guid senderId = requestReply.Id; SessionAuth clientAuth = Request.Headers.GetSessionAuth(); if (!AccountStateService.Instance.IsAuthorized(senderId, clientAuth.ClientToken)) { throw new ClientNotAuthorizedException(senderId); } AccountStateService stateService = AccountStateService.Instance; UserInformerService informerService = UserInformerService.Instance; FriendRequestService friendRequestService = FriendRequestService.Instance; ComplexFriendRequest processedRequest = friendRequestService.ReplyToRequest(requestReply); Task.Run(() => { informerService.NotifyUserAboutFriendReply(requestReply.Request.RequesterAccount.Id, requestReply); }); requestReply.EstablishedDate = processedRequest.DecisionDate; var serialized = helper.Serialize(requestReply); return(serialized); }
private dynamic OnAddFriendRequest(dynamic arg) { var friendRequest = new SerializeHelper().DeserializeObject <FriendRequest>(Request.Body); $"Got a friend request from { friendRequest.RequesterAccount.Name } to { friendRequest.TargetAccount.Name }".WriteToConsole(); RegistrationService registrationService = RegistrationService.Instance; FriendRequestService friendRequestService = FriendRequestService.Instance; if (registrationService.FindByName(friendRequest.RequesterAccount.Name) != null) { ComplexFriendRequest result = friendRequestService.Save(friendRequest); friendRequest.RequestState = result.RequestState; $"Saved request with result '{ friendRequest.RequestState }'".WriteToConsole(); string serialized = new SerializeHelper().Serialize(friendRequest); Task.Run(() => { UserInformerService.Instance.NotifyUserAboutFriendRequest(friendRequest.TargetAccount.Id, friendRequest); }); return(serialized); } $"Target acount { friendRequest.TargetAccount.Name } doesnt exist".WriteToConsole(); friendRequest.RequestState = FriendRequestState.DoesntExist; return(friendRequest); }
/// <summary> /// Save friendrequest reply state. /// </summary> /// <param name="reply">User reply for a friend request.</param> public ComplexFriendRequest ReplyToRequest(FriendRequestReply reply) { ComplexFriendRequest updatedRequest = SaveReplyState(reply); var registrationService = RegistrationService.Instance; var targetId = reply.Request.TargetAccount.Id; var requesterId = reply.Request.RequesterAccount.Id; var targetAcc = registrationService.FindById(targetId); var requesterAcc = registrationService.FindById(requesterId); if (targetAcc == null) { throw new InvalidOperationException($"Target account '{ targetId }' not found"); } if (requesterAcc == null) { throw new InvalidOperationException($"Requester account '{ requesterId }' not found"); } var friendship = new FriendshipModel { ParentId = reply.Request.RequesterAccount.Id, NavigationParent = reply.Request.RequesterAccount.ToComplex(), AcceptorAccountId = reply.Request.TargetAccount.Id, AcceptorAccount = reply.Request.TargetAccount.ToComplex(), Established = updatedRequest.DecisionDate, }; friendshipService.SaveFriendship(friendship); return(updatedRequest); }
/// <summary> /// Save a friend request to database. /// </summary> /// <param name="request">Incoming friend request model.</param> /// <returns>Saved complex request model.</returns> public ComplexFriendRequest Save(FriendRequest request) { RegistrationService service = RegistrationService.Instance; ComplexUserAccount requester = service.FindById(request.RequesterAccount.Id); ComplexUserAccount target = service.FindById(request.TargetAccount.Id); using (var context = new ComplexFriendRequestContext()) { ComplexFriendRequest savedRequest = context.Objects.FirstOrDefault(req => req.Id == request.Id); if (savedRequest == null) { var complexRequest = new ComplexFriendRequest { Id = request.Id, ParentId = requester.Id, TargetId = target.Id, CreationDate = request.CreationDate, RequestState = FriendRequestState.AwaitingUserAnswer }; context.Objects.Add(complexRequest); context.SaveChanges(); return(complexRequest); } return(savedRequest); } }
public static FriendRequest ToSimple(this ComplexFriendRequest complexRequest) { var complex = new FriendRequest { CreationDate = complexRequest.CreationDate, Id = complexRequest.Id, RequesterAccount = complexRequest.NavigationParent, TargetAccount = complexRequest.TargetAccount, RequestState = complexRequest.RequestState }; return(complex); }