Example #1
0
        /// <summary>
        /// Notify user about appeared reaction for his friendrequest.
        /// </summary>
        /// <param name="requesterId"></param>
        /// <param name="requestReply"></param>
        public void NotifyUserAboutFriendReply(Guid requesterId, FriendRequestReply requestReply)
        {
            ComplexAccountState requesterState = AccountStateService.Instance.GetStatusOf(requesterId);

            if (requesterState == null)
            {
                throw new InvalidOperationException($"Got null requester state");
            }

            if (requesterState.OnlineStatus == AccountOnlineStatus.Online)
            {
                using (IHttpActor actor = new HttpActor())
                {
                    string requesterAddress = $"{requesterState.Address}{Subroutes.NotifyClient.NotifyFriendRequestState}";
                    actor.SetRequestAddress(requesterAddress);
                    actor.AddHeader(FreengyHeaders.Server.ServerSessionTokenHeaderName, requesterState.ClientAuth.ServerToken);

                    var result = actor.PostAsync <FriendRequestReply, FriendRequestReply>(requestReply).Result;

                    string resultMessage = result.Success ? "Success" : result.Error.Message;

                    $"Sent friendrequest reply '{ requestReply.Reaction }' to { requesterState.ComplexAccount.Name }:{ Environment.NewLine }    { resultMessage }"
                    .WriteToConsole(ConsoleColor.Blue);
                }
            }
        }
Example #2
0
        /// <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);
        }
Example #3
0
        private static ComplexFriendRequest SaveReplyState(FriendRequestReply reply)
        {
            using (var context = new ComplexFriendRequestContext())
            {
                var targetRequest = context.Objects.FirstOrDefault(request => request.Id == reply.Request.Id);

                if (targetRequest == null)
                {
                    throw new InvalidOperationException($"Friend request for reply with id {reply.Request.Id} not found");
                }

                if (reply.Reaction == FriendRequestReaction.Accept)
                {
                    targetRequest.RequestState = FriendRequestState.Accepted;
                }
                else if (reply.Reaction == FriendRequestReaction.Decline)
                {
                    targetRequest.RequestState = FriendRequestState.Declined;
                }
                else if (reply.Reaction == FriendRequestReaction.Ban)
                {
                    //TODO wut?
                }
                else
                {
                    // not ready yet
                    throw new NotImplementedException();
                }

                targetRequest.DecisionDate = DateTime.Now;

                context.SaveChanges();

                return(targetRequest);
            }
        }