Beispiel #1
0
        private dynamic OnLoginRequest(dynamic arg)
        {
            //logInRequest = new SerializeHelper().DeserializeObject<LoginModel>(Request.Body);
            bool   isLoggingIn = logInRequest.IsLoggingIn;
            string direction   = isLoggingIn ? "in" : "out";

            $"Got '{ logInRequest.Account.Name }' log { direction } request".WriteToConsole();

            string userAddress = GetUserAddress(Request);

            if (string.IsNullOrEmpty(userAddress))
            {
                $"Client { logInRequest.Account.Name } didnt attach his address".WriteToConsole();
                throw new InvalidOperationException("Client address must be set in headers");
            }

            ComplexAccountState accountState = LogInOrOut(logInRequest, userAddress);

            $"'{ logInRequest.Account.Name } [{ userAddress }]' log { direction } result: { accountState.OnlineStatus }".WriteToConsole();

            // сложный аккаунт нужно упростить, иначе при дефолтной сериализации получается бесконечная рекурсия ссылающихся друг на друга свойств

            var jsonResponse = new JsonResponse <AccountStateModel>(accountState.ToSimple(), new DefaultJsonSerializer());

            SetAuthHeaders(jsonResponse.Headers, accountState.ClientAuth);

            SetNextPasswordData(logInRequest);

            $"Logged '{ logInRequest.Account.Name }' { direction }".WriteToConsole(isLoggingIn ? ConsoleColor.Green : ConsoleColor.Magenta);

            return(jsonResponse);
        }
Beispiel #2
0
        private static void NotifyFriendAboutUser(ComplexAccountState userState, ComplexAccountState friendAccountState)
        {
            using (IHttpActor actor = new HttpActor())
            {
                string targetFriendAddress = $"{friendAccountState.Address}{Subroutes.NotifyClient.NotifyFriendState}";
                actor.SetRequestAddress(targetFriendAddress);
                actor.AddHeader(FreengyHeaders.Server.ServerSessionTokenHeaderName, friendAccountState.ClientAuth.ServerToken);

                var result = actor.PostAsync <AccountStateModel, AccountStateModel>(userState.ToSimple()).Result;
            }
        }
        private dynamic OnSyncAccountRequest(dynamic arg)
        {
            var stateService = AccountStateService.Instance;

            Guid        senderId   = Request.Headers.GetClientId();
            SessionAuth clientAuth = Request.Headers.GetSessionAuth();

            if (!stateService.IsAuthorized(senderId, clientAuth.ClientToken))
            {
                throw new ClientNotAuthorizedException(senderId);
            }

            ComplexAccountState currentState = AccountStateService.Instance.GetStatusOf(senderId);

            var responce = new JsonResponse <AccountStateModel>(currentState.ToSimple(), new DefaultJsonSerializer());

            responce.Headers.Add(FreengyHeaders.Server.ServerSessionTokenHeaderName, currentState.ClientAuth.ServerToken);

            return(responce);
        }
Beispiel #4
0
        private dynamic ProcessSearchFriends(SearchRequest searchRequest, SerializeHelper helper)
        {
            var registrationService = RegistrationService.Instance;
            var friendService       = FriendshipService.Instance;

            ComplexUserAccount senderAcc = registrationService.FindById(searchRequest.SenderId);

            if (senderAcc == null)
            {
                return($"Account id {searchRequest.SenderId} is not registered");
            }

            friendService.UpdateFriendships(senderAcc);

            var stateService = AccountStateService.Instance;

            AccountStateModel AccountStateSelector(FriendshipModel friendship)
            {
                Guid friendId =
                    friendship.AcceptorAccountId == searchRequest.SenderId ?
                    friendship.ParentId :
                    friendship.AcceptorAccountId;

                ComplexAccountState complexAccountState = stateService.GetStatusOf(friendId);

                if (complexAccountState == null)
                {
                    throw new InvalidOperationException($"Found null state pair for account id { friendId }");
                }

                return(complexAccountState.ToSimple());
            }

            IEnumerable <AccountStateModel> friendStateModels = senderAcc.Friendships.Select(AccountStateSelector);

            var serialized = helper.Serialize(friendStateModels);

            return(serialized);
        }