Example #1
0
        public SignInResponse SignIn(SignInRequest request)
        {
            lock (_access)
            {
                IChatClient currentClient = _chatClientsProvider.GetCurrentClient();
                if (currentClient == null)
                {
                    _logger.Warn("Sign in failure: wrong callback channel.", WarnCategory.InvalidRequest);
                    return(SignInResponse.Failed(Errors.CallbackChannelFailure));
                }

                if (_chatClientsProvider.IsClientActive(currentClient) &&
                    _chatClientsProvider.IsClientSignedIn(currentClient))
                {
                    _logger.Warn("Sign in failure: already signed in.", WarnCategory.InvalidRequest);
                    return(SignInResponse.Failed(Errors.AlreadySignedIn));
                }

                string name = request.Name;
                if (string.IsNullOrWhiteSpace(name))
                {
                    _logger.Info("Sign in failure: empty name.", InfoCategory.Validation);
                    return(SignInResponse.Failed(Errors.EmptyNameNotAllowed));
                }

                if (name.Length > Limits.UsernameMaxLength)
                {
                    _logger.Info("Sign in failure: name length exceeded limits.", InfoCategory.Validation);
                    return(SignInResponse.Failed(Errors.NameLengthExceededLimits));
                }

                if (_chatClientsProvider.IsNameActive(name))
                {
                    _logger.Info("Sign in failure: name already taken.", InfoCategory.Validation);
                    return(SignInResponse.Failed(Errors.NameAlreadyTaken));
                }

                _chatClientsProvider.AddCurrent(name);

                BroadcastUpdateOnlineCount();
                BroadcastMessage($"{name} has joined the chat.");

                SignInResponse response = SignInResponse.Successful();
                response.OnlineCount = _chatClientsProvider.GetClientsCount();

                return(response);
            }
        }
Example #2
0
        public SignInResponse SignIn(SignInRequest request)
        {
            IChatClient currentClient = _chatClientProvider.GetCurrent();

            if (currentClient == null)
            {
                _logger.Warn("Sign in failure: wrong callback channel.", WarnCategory.InvalidRequest);
                return(SignInResponse.Failed(Errors.CallbackChannelFailure));
            }

            if (_clients.ContainsKey(currentClient))
            {
                _logger.Warn("Sign in failure: already signed in.", WarnCategory.InvalidRequest);
                return(SignInResponse.Failed(Errors.AlreadySignedIn));
            }

            string name = request.Name;

            if (string.IsNullOrWhiteSpace(name))
            {
                _logger.Info("Sign in failure: empty name.", InfoCategory.Validation);
                return(SignInResponse.Failed(Errors.EmptyNameNotAllowed));
            }

            if (name.Length > Limits.UsernameMaxLength)
            {
                _logger.Info("Sign in failure: name length exceeded limits.", InfoCategory.Validation);
                return(SignInResponse.Failed(Errors.NameLengthExceededLimits));
            }

            if (_clients.Values.Contains(name))
            {
                _logger.Info("Sign in failure: name already taken.", InfoCategory.Validation);
                return(SignInResponse.Failed(Errors.NameAlreadyTaken));
            }

            _clients[currentClient] = name;

            Broadcast($"{name} has joined the chat.");

            return(SignInResponse.Successful());
        }