public void GetPostAbstract()
        {
            var html     = @"<p>Microsoft</p> <p>Rocks!</p><p>Azure <br /><img src=""a.jpg"" /> The best <span>cloud</span>!</p>";
            var result   = ContentProcessor.GetPostAbstract(html, 16);
            var expected = "Microsoft Rocks!" + "\u00A0\u2026";

            Assert.IsTrue(result == expected);
        }
Beispiel #2
0
    public async Task <PostEntity> Handle(CreatePostCommand request, CancellationToken cancellationToken)
    {
        var abs = ContentProcessor.GetPostAbstract(
            string.IsNullOrEmpty(request.Payload.Abstract) ? request.Payload.EditorContent : request.Payload.Abstract.Trim(),
            _blogConfig.ContentSettings.PostAbstractWords,
            _settings.Editor == EditorChoice.Markdown);

        var post = new PostEntity
        {
            CommentEnabled      = request.Payload.EnableComment,
            Id                  = Guid.NewGuid(),
            PostContent         = request.Payload.EditorContent,
            ContentAbstract     = abs,
            CreateTimeUtc       = DateTime.UtcNow,
            LastModifiedUtc     = DateTime.UtcNow, // Fix draft orders
            Slug                = request.Payload.Slug.ToLower().Trim(),
            Author              = request.Payload.Author?.Trim(),
            Title               = request.Payload.Title.Trim(),
            ContentLanguageCode = request.Payload.LanguageCode,
            IsFeedIncluded      = request.Payload.FeedIncluded,
            PubDateUtc          = request.Payload.IsPublished ? DateTime.UtcNow : null,
            IsDeleted           = false,
            IsPublished         = request.Payload.IsPublished,
            IsFeatured          = request.Payload.Featured,
            IsOriginal          = request.Payload.IsOriginal,
            OriginLink          = string.IsNullOrWhiteSpace(request.Payload.OriginLink) ? null : Helper.SterilizeLink(request.Payload.OriginLink),
            HeroImageUrl        = string.IsNullOrWhiteSpace(request.Payload.HeroImageUrl) ? null : Helper.SterilizeLink(request.Payload.HeroImageUrl),
            InlineCss           = request.Payload.InlineCss,
            PostExtension       = new()
            {
                Hits  = 0,
                Likes = 0
            }
        };

        // check if exist same slug under the same day
        var todayUtc = DateTime.UtcNow.Date;

        if (_postRepo.Any(new PostSpec(post.Slug, todayUtc)))
        {
            var uid = Guid.NewGuid();
            post.Slug += $"-{uid.ToString().ToLower()[..8]}";
    public async Task <PostEntity> Handle(UpdatePostCommand request, CancellationToken cancellationToken)
    {
        var post = await _postRepo.GetAsync(request.Id);

        if (null == post)
        {
            throw new InvalidOperationException($"Post {request.Id} is not found.");
        }

        post.CommentEnabled  = request.Payload.EnableComment;
        post.PostContent     = request.Payload.EditorContent;
        post.ContentAbstract = ContentProcessor.GetPostAbstract(
            string.IsNullOrEmpty(request.Payload.Abstract) ? request.Payload.EditorContent : request.Payload.Abstract.Trim(),
            _blogConfig.ContentSettings.PostAbstractWords,
            _settings.Editor == EditorChoice.Markdown);

        // Address #221: Do not allow published posts back to draft status
        // postModel.IsPublished = request.Request.IsPublished;
        // Edit draft -> save and publish, ignore false case because #221
        bool isNewPublish = false;

        if (request.Payload.IsPublished && !post.IsPublished)
        {
            post.IsPublished = true;
            post.PubDateUtc  = DateTime.UtcNow;

            isNewPublish = true;
        }

        // #325: Allow changing publish date for published posts
        if (request.Payload.PublishDate is not null && post.PubDateUtc.HasValue)
        {
            var tod          = post.PubDateUtc.Value.TimeOfDay;
            var adjustedDate = request.Payload.PublishDate.Value;
            post.PubDateUtc = adjustedDate.AddTicks(tod.Ticks);
        }

        post.Author              = request.Payload.Author?.Trim();
        post.Slug                = request.Payload.Slug.ToLower().Trim();
        post.Title               = request.Payload.Title.Trim();
        post.LastModifiedUtc     = DateTime.UtcNow;
        post.IsFeedIncluded      = request.Payload.FeedIncluded;
        post.ContentLanguageCode = request.Payload.LanguageCode;
        post.IsFeatured          = request.Payload.Featured;
        post.IsOriginal          = request.Payload.IsOriginal;
        post.OriginLink          = string.IsNullOrWhiteSpace(request.Payload.OriginLink) ? null : Helper.SterilizeLink(request.Payload.OriginLink);
        post.HeroImageUrl        = string.IsNullOrWhiteSpace(request.Payload.HeroImageUrl) ? null : Helper.SterilizeLink(request.Payload.HeroImageUrl);
        post.InlineCss           = request.Payload.InlineCss;

        // compute hash
        var input    = $"{post.Slug}#{post.PubDateUtc.GetValueOrDefault():yyyyMMdd}";
        var checkSum = Helper.ComputeCheckSum(input);

        post.HashCheckSum = checkSum;

        // 1. Add new tags to tag lib
        var tags = string.IsNullOrWhiteSpace(request.Payload.Tags) ?
                   Array.Empty <string>() :
                   request.Payload.Tags.Split(',').ToArray();

        foreach (var item in tags.Where(item => !_tagRepo.Any(p => p.DisplayName == item)))
        {
            await _tagRepo.AddAsync(new()
            {
                DisplayName    = item,
                NormalizedName = Tag.NormalizeName(item, _tagNormalizationDictionary)
            });
        }

        // 2. update tags
        post.Tags.Clear();
        if (tags.Any())
        {
            foreach (var tagName in tags)
            {
                if (!Tag.ValidateName(tagName))
                {
                    continue;
                }

                var tag = await _tagRepo.GetAsync(t => t.DisplayName == tagName);

                if (tag is not null)
                {
                    post.Tags.Add(tag);
                }
            }
        }

        // 3. update categories
        var catIds = request.Payload.CategoryList.Where(p => p.IsChecked).Select(p => p.Id).ToArray();

        post.PostCategory.Clear();

        if (catIds is { Length : > 0 })