Exemplo n.º 1
0
        public HttpResponseMessage NewReplyPost(int replyToItemId, string message)
        {
            var success      = false;
            var errorMessage = string.Empty;
            var userId       = -1;
            var moduleId     = this.Request.FindModuleId();
            var tabId        = this.Request.FindTabId();

            if (this.UserInfo != null)
            {
                userId = this.UserInfo.UserID;
            }


            var moduleSettings = new ShoutBoxModuleSettings(moduleId, tabId);

            //validate the post for profanity
            if (ValidatePostForProfanity(message))
            {
                var floodControl = new FloodControl(this.Request.FindModuleId(),
                                                    this.Request.FindTabId(),
                                                    this.Request.GetIPAddress(),
                                                    this.UserInfo);

                if (floodControl.AllowReply(replyToItemId))
                {
                    success = true;

                    _repository.AddPost(new ShoutPost()
                    {
                        ModuleId    = moduleId,
                        CreatedDate = DateTime.Now,
                        VoteDown    = 0,
                        VoteUp      = 0,
                        UserId      = userId > 0 ? (int?)userId : null,
                        Message     = message,
                        ReplyTo     = replyToItemId
                    });


                    Log.DebugFormat("Reply has been saved for the item:{1}",
                                    replyToItemId);
                }
                else
                {
                    Log.WarnFormat("Flood control block the reply. The IP:{0} has already replied on this item:{1} in the time limit window",
                                   this.Request.GetIPAddress(),
                                   replyToItemId);

                    errorMessage = Localization.GetString("FloodControlReply.Text", SharedResource);
                }
            }
            else
            {
                Log.WarnFormat("The reply was not saved due to profanity. The IP:{0}",
                               this.Request.GetIPAddress());

                errorMessage = Localization.GetString("ProfanityBanned.Text", SharedResource);
            }

            var posts = _repository
                        .GetDisplayPosts(moduleId,
                                         moduleSettings.NumberOfPostsToReturn);

            return(Request.CreateResponse(new
            {
                success = success,
                message = errorMessage,
                data = new { posts = posts.ToArray() }
            }));
        }