public async Task <RssChannel> GetChannel(CancellationToken cancellationToken = default(CancellationToken))
        {
            var project = await projectService.GetCurrentProjectSettings();

            if (project == null)
            {
                return(null);
            }
            var posts = await blogService.GetRecentPosts(maxFeedItems);

            if (posts == null)
            {
                return(null);
            }

            var channel = new RssChannel();

            channel.Title = project.Title;
            if (!string.IsNullOrEmpty(project.Description))
            {
                channel.Description = project.Description;
            }
            else
            {
                // prevent error, channel desc cannot be empty
                channel.Description = "Welcome to my blog";
            }

            channel.Copyright = project.CopyrightNotice;
            if (!string.IsNullOrEmpty(project.ChannelCategoriesCsv))
            {
                var channelCats = project.ChannelCategoriesCsv.Split(',');
                foreach (var cat in channelCats)
                {
                    channel.Categories.Add(new RssCategory(cat));
                }
            }

            channel.Generator     = Name;
            channel.RemoteFeedUrl = project.RemoteFeedUrl;
            channel.RemoteFeedProcessorUseAgentFragment = project.RemoteFeedProcessorUseAgentFragment;


            var urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccesor.ActionContext);

            if (!string.IsNullOrEmpty(project.Image))
            {
                channel.Image.Url = new Uri(urlHelper.Content(project.Image));
            }
            if (!string.IsNullOrEmpty(project.LanguageCode))
            {
                channel.Language = new CultureInfo(project.LanguageCode);
            }

            var baseUrl = string.Concat(
                contextAccessor.HttpContext.Request.Scheme,
                "://",
                contextAccessor.HttpContext.Request.Host.ToUriComponent()
                );

            // asp.net bug? the comments for this method say it returns an absolute fully qualified url but it returns relative
            // looking at latest code seems ok so maybe just a bug in rc1
            //https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/UrlHelperExtensions.cs
            //https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/Routing/UrlHelper.cs

            var indexUrl = urlHelper.RouteUrl(blogRoutes.BlogIndexRouteName);


            if (indexUrl.StartsWith("/"))
            {
                indexUrl = string.Concat(baseUrl, indexUrl);
            }
            channel.Link = new Uri(indexUrl);
            if (!string.IsNullOrEmpty(project.ManagingEditorEmail))
            {
                channel.ManagingEditor = project.ManagingEditorEmail;
            }

            if (!string.IsNullOrEmpty(project.ChannelRating))
            {
                channel.Rating = project.ChannelRating;
            }


            var feedUrl = string.Concat(
                contextAccessor.HttpContext.Request.Scheme,
                "://",
                contextAccessor.HttpContext.Request.Host.ToUriComponent(),
                contextAccessor.HttpContext.Request.PathBase.ToUriComponent(),
                contextAccessor.HttpContext.Request.Path.ToUriComponent(),
                contextAccessor.HttpContext.Request.QueryString.ToUriComponent());

            channel.SelfLink = new Uri(feedUrl);
            //channel.TextInput =
            channel.TimeToLive = project.ChannelTimeToLive;
            if (!string.IsNullOrEmpty(project.WebmasterEmail))
            {
                channel.Webmaster = project.WebmasterEmail;
            }

            DateTime mostRecentPubDate = DateTime.MinValue;
            var      items             = new List <RssItem>();

            foreach (var post in posts)
            {
                if (post.PubDate > mostRecentPubDate)
                {
                    mostRecentPubDate = post.PubDate;
                }
                var rssItem = new RssItem();
                rssItem.Author = post.Author;

                if (post.Categories.Count > 0)
                {
                    foreach (var c in post.Categories)
                    {
                        rssItem.Categories.Add(new RssCategory(c));
                    }
                }


                //rssItem.Comments
                if (project.UseMetaDescriptionInFeed)
                {
                    rssItem.Description = post.MetaDescription;
                }
                else
                {
                    // change relative urls in content to absolute
                    rssItem.Description = htmlProcessor.ConvertUrlsToAbsolute(baseUrl, post.Content);
                }

                //rssItem.Enclosures

                var postUrl = await blogService.ResolvePostUrl(post);

                if (string.IsNullOrEmpty(postUrl))
                {
                    //TODO: log
                    continue;
                }


                if (postUrl.StartsWith("/"))
                {
                    //postUrl = urlHelper.Content(postUrl);
                    postUrl = string.Concat(
                        contextAccessor.HttpContext.Request.Scheme,
                        "://",
                        contextAccessor.HttpContext.Request.Host.ToUriComponent(),
                        postUrl);
                }

                rssItem.Guid            = new RssGuid(postUrl, true);
                rssItem.Link            = new Uri(postUrl);
                rssItem.PublicationDate = post.PubDate;
                //rssItem.Source
                rssItem.Title = post.Title;

                items.Add(rssItem);
            }

            channel.PublicationDate = mostRecentPubDate;
            channel.Items           = items;

            return(channel);
        }
示例#2
0
        public async Task <RssChannel> GetChannel(CancellationToken cancellationToken = default(CancellationToken))
        {
            var blog  = _blogService.GetBlogSettings();
            var posts = _blogService.GetPostList(new PostListRequest
            {
                PageIndex = 0,
                PageSize  = maxFeedItems
            }).Posts;

            var categories = _blogService.GetCategories();

            var channel = new RssChannel();

            channel.Title       = blog.BlogName;
            channel.Description = blog.Description;
            channel.Generator   = Name;

            foreach (var cat in categories)
            {
                channel.Categories.Add(new RssCategory(cat.Title));
            }

            var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccesor.ActionContext);
            var baseUrl   = string.Concat(
                _contextAccessor.HttpContext.Request.Scheme,
                "://",
                _contextAccessor.HttpContext.Request.Host.ToUriComponent()
                );

            var indexUrl = urlHelper.RouteUrl("spa-fallback");

            if (indexUrl.StartsWith("/"))
            {
                indexUrl = string.Concat(baseUrl, indexUrl);
            }
            channel.Link = new Uri(indexUrl);

            channel.TimeToLive = 60;

            var items = new List <RssItem>();

            foreach (var post in posts)
            {
                var rssItem = new RssItem();
                rssItem.Author = post.UserDisplayName;

                foreach (var c in post.Categories)
                {
                    rssItem.Categories.Add(new RssCategory(c.Title));
                }

                rssItem.Description = _htmlProcessor.ConvertUrlsToAbsolute(baseUrl, post.Content);

                var postUrl = $"{baseUrl}/post/{post.Slug}";
                rssItem.Link = new Uri(postUrl);

                rssItem.Guid            = new RssGuid(postUrl);
                rssItem.PublicationDate = DateTime.Parse(post.PublishDate);
                rssItem.Title           = post.Title;
                items.Add(rssItem);
            }

            channel.PublicationDate = DateTime.Parse(posts.First().PublishDate);
            channel.Items           = items;

            return(channel);
        }