Ejemplo n.º 1
0
        public Response <Comment> NewComment(string username, string commentContent, Guid postId,
                                             string email, string ipAddress, string userAgent)
        {
            try
            {
                // 1. Check comment enabled or not
                if (!_blogConfig.EnableComments)
                {
                    return(new FailedResponse <Comment>((int)ResponseFailureCode.CommentDisabled));
                }

                // 2. Check user email domain
                var bannedDomains = _blogConfig.EmailConfiguration.BannedMailDomain;
                if (bannedDomains.Any())
                {
                    var address = new MailAddress(email);
                    if (bannedDomains.Contains(address.Host))
                    {
                        return(new FailedResponse <Comment>((int)ResponseFailureCode.EmailDomainBlocked));
                    }
                }

                // 3. Encode HTML
                username = HttpUtility.HtmlEncode(username);

                // 4. Harmonize banned keywords
                if (AppSettings.EnableHarmonizor)
                {
                    var dw             = _blogConfig.DisharmonyWords;
                    var maskWordFilter = new MaskWordFilter(new StringWordSource(dw));
                    username       = maskWordFilter.FilterContent(username);
                    commentContent = maskWordFilter.FilterContent(commentContent);
                }

                var model = new Comment
                {
                    Id             = Guid.NewGuid(),
                    Username       = username,
                    CommentContent = commentContent,
                    PostId         = postId,
                    CreateOnUtc    = DateTime.UtcNow,
                    Email          = email,
                    IPAddress      = ipAddress,
                    IsApproved     = false,
                    UserAgent      = userAgent
                };

                _commentRepository.Add(model);
                return(new SuccessResponse <Comment>(model));
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Error {nameof(NewComment)}");
                return(new FailedResponse <Comment>((int)ResponseFailureCode.GeneralException));
            }
        }
Ejemplo n.º 2
0
        public Task <Response <CommentListItem> > CreateAsync(NewCommentRequest request)
        {
            return(TryExecuteAsync <CommentListItem>(async() =>
            {
                // 1. Check comment enabled or not
                if (!_blogConfig.ContentSettings.EnableComments)
                {
                    return new FailedResponse <CommentListItem>((int)ResponseFailureCode.CommentDisabled);
                }

                // 2. Harmonize banned keywords
                if (_blogConfig.ContentSettings.EnableWordFilter)
                {
                    var dw = _blogConfig.ContentSettings.DisharmonyWords;
                    var maskWordFilter = new MaskWordFilter(new StringWordSource(dw));
                    request.Username = maskWordFilter.FilterContent(request.Username);
                    request.Content = maskWordFilter.FilterContent(request.Content);
                }

                var model = new CommentEntity
                {
                    Id = Guid.NewGuid(),
                    Username = request.Username,
                    CommentContent = request.Content,
                    PostId = request.PostId,
                    CreateOnUtc = DateTime.UtcNow,
                    Email = request.Email,
                    IPAddress = request.IpAddress,
                    IsApproved = !_blogConfig.ContentSettings.RequireCommentReview,
                    UserAgent = request.UserAgent
                };

                await _commentRepository.AddAsync(model);

                var spec = new PostSpec(request.PostId, false);
                var postTitle = _postRepository.SelectFirstOrDefault(spec, p => p.Title);

                var item = new CommentListItem
                {
                    Id = model.Id,
                    CommentContent = model.CommentContent,
                    CreateOnUtc = model.CreateOnUtc,
                    Email = model.Email,
                    IpAddress = model.IPAddress,
                    IsApproved = model.IsApproved,
                    PostTitle = postTitle,
                    Username = model.Username
                };

                return new SuccessResponse <CommentListItem>(item);
            }));
        }
Ejemplo n.º 3
0
        public async Task<CommentDetailedItem> CreateAsync(NewCommentRequest request)
        {
            if (_blogConfig.ContentSettings.EnableWordFilter)
            {
                var dw = _blogConfig.ContentSettings.DisharmonyWords;
                var maskWordFilter = new MaskWordFilter(new StringWordSource(dw));
                request.Username = maskWordFilter.FilterContent(request.Username);
                request.Content = maskWordFilter.FilterContent(request.Content);
            }

            var model = new CommentEntity
            {
                Id = Guid.NewGuid(),
                Username = request.Username,
                CommentContent = request.Content,
                PostId = request.PostId,
                CreateOnUtc = DateTime.UtcNow,
                Email = request.Email,
                IPAddress = request.IpAddress,
                IsApproved = !_blogConfig.ContentSettings.RequireCommentReview
            };

            await _commentRepository.AddAsync(model);

            var spec = new PostSpec(request.PostId, false);
            var postTitle = _postRepository.SelectFirstOrDefault(spec, p => p.Title);

            var item = new CommentDetailedItem
            {
                Id = model.Id,
                CommentContent = model.CommentContent,
                CreateOnUtc = model.CreateOnUtc,
                Email = model.Email,
                IpAddress = model.IPAddress,
                IsApproved = model.IsApproved,
                PostTitle = postTitle,
                Username = model.Username
            };

            return item;
        }
Ejemplo n.º 4
0
        public Task <Response <CommentListItem> > AddCommentAsync(NewCommentRequest request)
        {
            return(TryExecuteAsync <CommentListItem>(async() =>
            {
                // 1. Check comment enabled or not
                if (!_blogConfig.ContentSettings.EnableComments)
                {
                    return new FailedResponse <CommentListItem>((int)ResponseFailureCode.CommentDisabled);
                }

                // 2. Check user email domain
                var bannedDomains = _blogConfig.EmailSettings.BannedMailDomain?.Split(",");
                if (null != bannedDomains && bannedDomains.Any())
                {
                    var address = new MailAddress(request.Email);
                    if (bannedDomains.Contains(address.Host))
                    {
                        Logger.LogWarning($"Email host '{address.Host}' is found in ban list, rejecting comments.");
                        return new FailedResponse <CommentListItem>((int)ResponseFailureCode.EmailDomainBlocked);
                    }
                }

                // 3. Harmonize banned keywords
                if (_blogConfig.ContentSettings.EnableWordFilter)
                {
                    var dw = _blogConfig.ContentSettings.DisharmonyWords;
                    var maskWordFilter = new MaskWordFilter(new StringWordSource(dw));
                    request.Username = maskWordFilter.FilterContent(request.Username);
                    request.Content = maskWordFilter.FilterContent(request.Content);
                }

                var model = new CommentEntity
                {
                    Id = Guid.NewGuid(),
                    Username = request.Username,
                    CommentContent = request.Content,
                    PostId = request.PostId,
                    CreateOnUtc = DateTime.UtcNow,
                    Email = request.Email,
                    IPAddress = request.IpAddress,
                    IsApproved = !_blogConfig.ContentSettings.RequireCommentReview,
                    UserAgent = request.UserAgent
                };

                await _commentRepository.AddAsync(model);

                var spec = new PostSpec(request.PostId, false);
                var postTitle = _postRepository.SelectFirstOrDefault(spec, p => p.Title);

                var item = new CommentListItem
                {
                    Id = model.Id,
                    CommentContent = model.CommentContent,
                    CreateOnUtc = model.CreateOnUtc,
                    Email = model.Email,
                    IpAddress = model.IPAddress,
                    IsApproved = model.IsApproved,
                    PostTitle = postTitle,
                    Username = model.Username
                };

                return new SuccessResponse <CommentListItem>(item);
            }));
        }