mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-20 02:37:55 +08:00
Adding display links to feed. Deduplicating some worker code into an inspector class.
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045457
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
|
using System.Web.Routing;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using Autofac.Builder;
|
using Autofac.Builder;
|
||||||
using Autofac.Modules;
|
using Autofac.Modules;
|
||||||
@@ -165,6 +166,7 @@ namespace Orchard.Core.Tests.Feeds.Controllers {
|
|||||||
var builder = new ContainerBuilder();
|
var builder = new ContainerBuilder();
|
||||||
builder.RegisterModule(new ImplicitCollectionSupportModule());
|
builder.RegisterModule(new ImplicitCollectionSupportModule());
|
||||||
builder.Register<FeedController>();
|
builder.Register<FeedController>();
|
||||||
|
builder.Register(new RouteCollection());
|
||||||
builder.Register(mockContentManager.Object).As<IContentManager>();
|
builder.Register(mockContentManager.Object).As<IContentManager>();
|
||||||
builder.Register<RssFeedFormatProvider>().As<IFeedFormatterProvider>();
|
builder.Register<RssFeedFormatProvider>().As<IFeedFormatterProvider>();
|
||||||
builder.Register<CorePartsFeedItemBuilder>().As<IFeedItemBuilder>();
|
builder.Register<CorePartsFeedItemBuilder>().As<IFeedItemBuilder>();
|
||||||
|
@@ -64,6 +64,9 @@
|
|||||||
<SpecificVersion>False</SpecificVersion>
|
<SpecificVersion>False</SpecificVersion>
|
||||||
<HintPath>..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
|
<HintPath>..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="System.Web.Routing">
|
||||||
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System.Xml.Linq">
|
<Reference Include="System.Xml.Linq">
|
||||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
@@ -51,6 +51,12 @@ namespace Orchard.Core.Feeds.Controllers {
|
|||||||
return context.FeedFormatter.Process(context, () => {
|
return context.FeedFormatter.Process(context, () => {
|
||||||
bestQueryMatch.FeedQuery.Execute(context);
|
bestQueryMatch.FeedQuery.Execute(context);
|
||||||
_feedItemBuilders.Invoke(x => x.Populate(context), Logger);
|
_feedItemBuilders.Invoke(x => x.Populate(context), Logger);
|
||||||
|
foreach (var contextualizer in context.Response.Contextualizers) {
|
||||||
|
if (ControllerContext != null &&
|
||||||
|
ControllerContext.RequestContext != null) {
|
||||||
|
contextualizer(ControllerContext.RequestContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,15 +1,22 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
|
using System.Web.Routing;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
|
||||||
namespace Orchard.Core.Feeds.Models {
|
namespace Orchard.Core.Feeds.Models {
|
||||||
public class FeedResponse {
|
public class FeedResponse {
|
||||||
public FeedResponse() {
|
public FeedResponse() {
|
||||||
Items = new List<FeedItem>();
|
Items = new List<FeedItem>();
|
||||||
|
Contextualizers = new List<Action<RequestContext>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IList<FeedItem> Items { get; set; }
|
public IList<FeedItem> Items { get; set; }
|
||||||
public XElement Element { get; set; }
|
public XElement Element { get; set; }
|
||||||
public ActionResult Result { get; set; }
|
public IList<Action<RequestContext>> Contextualizers { get; set; }
|
||||||
|
|
||||||
|
public void Contextualize(Action<RequestContext> contextualizer) {
|
||||||
|
Contextualizers.Add(contextualizer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,61 +1,60 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Web.Mvc;
|
||||||
|
using System.Web.Routing;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.ContentManagement.Aspects;
|
|
||||||
using Orchard.Core.Common.Models;
|
|
||||||
using Orchard.Core.Feeds.Models;
|
using Orchard.Core.Feeds.Models;
|
||||||
|
|
||||||
namespace Orchard.Core.Feeds.Services {
|
namespace Orchard.Core.Feeds.Services {
|
||||||
public class CorePartsFeedItemBuilder : IFeedItemBuilder {
|
public class CorePartsFeedItemBuilder : IFeedItemBuilder {
|
||||||
private readonly IContentManager _contentManager;
|
private readonly IContentManager _contentManager;
|
||||||
|
private readonly RouteCollection _routes;
|
||||||
|
|
||||||
public CorePartsFeedItemBuilder(IContentManager contentManager) {
|
public CorePartsFeedItemBuilder(IContentManager contentManager, RouteCollection routes) {
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
|
_routes = routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Populate(FeedContext context) {
|
public void Populate(FeedContext context) {
|
||||||
foreach (var feedItem in context.Response.Items) {
|
foreach (var feedItem in context.Response.Items) {
|
||||||
// locate parts
|
// locate parts
|
||||||
var contentItem = feedItem.ContentItem;
|
var inspector = new ItemInspector(
|
||||||
var metadata = _contentManager.GetItemMetadata(contentItem);
|
feedItem.ContentItem,
|
||||||
var common = contentItem.Get<ICommonAspect>();
|
_contentManager.GetItemMetadata(feedItem.ContentItem));
|
||||||
var routable = contentItem.Get<RoutableAspect>();
|
|
||||||
var body = contentItem.Get<BodyAspect>();
|
|
||||||
|
|
||||||
// standard fields
|
|
||||||
var link = "/todo";// metadata.DisplayRouteValues();
|
|
||||||
|
|
||||||
var title = metadata.DisplayText;
|
|
||||||
if (string.IsNullOrEmpty(title) && routable != null)
|
|
||||||
title = routable.Title;
|
|
||||||
|
|
||||||
var contentText = title;
|
|
||||||
if (body != null && !string.IsNullOrEmpty(body.Text))
|
|
||||||
contentText = body.Text;
|
|
||||||
|
|
||||||
DateTime? publishedDate = null;
|
|
||||||
if (common != null)
|
|
||||||
publishedDate = common.PublishedUtc ?? common.ModifiedUtc;
|
|
||||||
|
|
||||||
// TODO: author
|
// TODO: author
|
||||||
|
|
||||||
|
|
||||||
// add to known formats
|
// add to known formats
|
||||||
if (context.Format == "rss") {
|
if (context.Format == "rss") {
|
||||||
feedItem.Element.SetElementValue("title", title);
|
var link = new XElement("link");
|
||||||
feedItem.Element.SetElementValue("link", link);
|
var guid = new XElement("guid", new XAttribute("isPermaLink", "true"));
|
||||||
feedItem.Element.SetElementValue("description", contentText);
|
|
||||||
if (publishedDate != null)
|
feedItem.Element.SetElementValue("title", inspector.Title);
|
||||||
feedItem.Element.SetElementValue("pubDate", publishedDate);//TODO: format
|
feedItem.Element.Add(link);
|
||||||
//feedItem.Data.SetElementValue("description", contentText);
|
feedItem.Element.SetElementValue("description", inspector.Description);
|
||||||
feedItem.Element.Add(new XElement("guid", new XAttribute("isPermaLink", "true"), new XText(link)));
|
if (inspector.PublishedUtc != null)
|
||||||
|
feedItem.Element.SetElementValue("pubDate", inspector.PublishedUtc);//TODO: format
|
||||||
|
feedItem.Element.Add(guid);
|
||||||
|
|
||||||
|
context.Response.Contextualize(requestContext => {
|
||||||
|
var urlHelper = new UrlHelper(requestContext, _routes);
|
||||||
|
link.Add(urlHelper.RouteUrl(inspector.Link));
|
||||||
|
guid.Add(urlHelper.RouteUrl(inspector.Link));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
context.FeedFormatter.AddProperty(context, feedItem, "link", link);
|
var feedItem1 = feedItem;
|
||||||
context.FeedFormatter.AddProperty(context, feedItem, "title", title);
|
context.Response.Contextualize(requestContext => {
|
||||||
context.FeedFormatter.AddProperty(context, feedItem, "description", contentText);
|
var urlHelper = new UrlHelper(requestContext, _routes);
|
||||||
if (publishedDate != null)
|
context.FeedFormatter.AddProperty(context, feedItem1, "published-date", urlHelper.RouteUrl(inspector.Link));
|
||||||
context.FeedFormatter.AddProperty(context, feedItem, "published-date", Convert.ToString(publishedDate)); // format? cvt to generic T?
|
});
|
||||||
|
context.FeedFormatter.AddProperty(context, feedItem, "title", inspector.Title);
|
||||||
|
context.FeedFormatter.AddProperty(context, feedItem, "description", inspector.Description);
|
||||||
|
|
||||||
|
if (inspector.PublishedUtc != null)
|
||||||
|
context.FeedFormatter.AddProperty(context, feedItem, "published-date", Convert.ToString(inspector.PublishedUtc)); // format? cvt to generic T?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
59
src/Orchard.Web/Core/Feeds/StandardBuilders/ItemInspector.cs
Normal file
59
src/Orchard.Web/Core/Feeds/StandardBuilders/ItemInspector.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Web.Routing;
|
||||||
|
using Orchard.ContentManagement;
|
||||||
|
using Orchard.ContentManagement.Aspects;
|
||||||
|
using Orchard.Core.Common.Models;
|
||||||
|
|
||||||
|
namespace Orchard.Core.Feeds.Services {
|
||||||
|
public class ItemInspector {
|
||||||
|
private readonly IContent _item;
|
||||||
|
private readonly ContentItemMetadata _metadata;
|
||||||
|
private readonly ICommonAspect _common;
|
||||||
|
private readonly RoutableAspect _routable;
|
||||||
|
private readonly BodyAspect _body;
|
||||||
|
|
||||||
|
public ItemInspector(IContent item, ContentItemMetadata metadata) {
|
||||||
|
_item = item;
|
||||||
|
_metadata = metadata;
|
||||||
|
_common = item.Get<ICommonAspect>();
|
||||||
|
_routable = item.Get<RoutableAspect>();
|
||||||
|
_body = item.Get<BodyAspect>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Title {
|
||||||
|
get {
|
||||||
|
if (_metadata != null && !string.IsNullOrEmpty(_metadata.DisplayText))
|
||||||
|
return _metadata.DisplayText;
|
||||||
|
if (_routable != null && !string.IsNullOrEmpty(_routable.Title))
|
||||||
|
return _routable.Title;
|
||||||
|
return _item.ContentItem.ContentType + " #" + _item.ContentItem.Id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RouteValueDictionary Link {
|
||||||
|
get {
|
||||||
|
if (_metadata != null) {
|
||||||
|
return _metadata.DisplayRouteValues;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Description {
|
||||||
|
get {
|
||||||
|
if (_body != null && !string.IsNullOrEmpty(_body.Text)) {
|
||||||
|
return _body.Text;
|
||||||
|
}
|
||||||
|
return Title;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTime? PublishedUtc {
|
||||||
|
get {
|
||||||
|
if (_common != null && _common.PublishedUtc != null)
|
||||||
|
return _common.PublishedUtc;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,8 +1,11 @@
|
|||||||
using JetBrains.Annotations;
|
using System.Web.Mvc;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.Core.Common.Models;
|
using Orchard.Core.Common.Models;
|
||||||
using Orchard.Core.Common.Records;
|
using Orchard.Core.Common.Records;
|
||||||
using Orchard.Core.Feeds.Models;
|
using Orchard.Core.Feeds.Models;
|
||||||
|
using Orchard.Core.Feeds.Services;
|
||||||
|
|
||||||
namespace Orchard.Core.Feeds.StandardQueries {
|
namespace Orchard.Core.Feeds.StandardQueries {
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
@@ -34,17 +37,25 @@ namespace Orchard.Core.Feeds.StandardQueries {
|
|||||||
var containerId = (int)containerIdValue.ConvertTo(typeof(int));
|
var containerId = (int)containerIdValue.ConvertTo(typeof(int));
|
||||||
var container = _contentManager.Get(containerId);
|
var container = _contentManager.Get(containerId);
|
||||||
|
|
||||||
var containerRoutable = container.As<RoutableAspect>();
|
var inspector = new ItemInspector(container, _contentManager.GetItemMetadata(container));
|
||||||
var containerBody = container.As<BodyAspect>();
|
if (context.Format == "rss") {
|
||||||
if (containerRoutable != null) {
|
var link = new XElement("link");
|
||||||
context.FeedFormatter.AddProperty(context, null, "title", containerRoutable.Title);
|
context.Response.Element.SetElementValue("title", inspector.Title);
|
||||||
context.FeedFormatter.AddProperty(context, null, "link", "/" + containerRoutable.Slug);
|
context.Response.Element.Add(link);
|
||||||
|
context.Response.Element.SetElementValue("description", inspector.Description);
|
||||||
|
|
||||||
|
context.Response.Contextualize(requestContext => {
|
||||||
|
var urlHelper = new UrlHelper(requestContext);
|
||||||
|
link.Add(urlHelper.RouteUrl(inspector.Link));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (containerBody != null) {
|
else {
|
||||||
context.FeedFormatter.AddProperty(context, null, "description", containerBody.Text);
|
context.FeedFormatter.AddProperty(context, null, "title", inspector.Title);
|
||||||
}
|
context.FeedFormatter.AddProperty(context, null, "description", inspector.Description);
|
||||||
else if (containerRoutable != null) {
|
context.Response.Contextualize(requestContext => {
|
||||||
context.FeedFormatter.AddProperty(context, null, "description", containerRoutable.Title);
|
var urlHelper = new UrlHelper(requestContext);
|
||||||
|
context.FeedFormatter.AddProperty(context, null, "link", urlHelper.RouteUrl(inspector.Link));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var items = _contentManager.Query()
|
var items = _contentManager.Query()
|
||||||
|
@@ -83,6 +83,7 @@
|
|||||||
<Compile Include="Common\ViewModels\OwnerEditorViewModel.cs" />
|
<Compile Include="Common\ViewModels\OwnerEditorViewModel.cs" />
|
||||||
<Compile Include="Feeds\Controllers\FeedController.cs" />
|
<Compile Include="Feeds\Controllers\FeedController.cs" />
|
||||||
<Compile Include="Feeds\Rss\Routes.cs" />
|
<Compile Include="Feeds\Rss\Routes.cs" />
|
||||||
|
<Compile Include="Feeds\StandardBuilders\ItemInspector.cs" />
|
||||||
<Compile Include="Feeds\StandardQueries\ContainerFeedQuery.cs" />
|
<Compile Include="Feeds\StandardQueries\ContainerFeedQuery.cs" />
|
||||||
<Compile Include="Feeds\StandardBuilders\CorePartsFeedItemBuilder.cs" />
|
<Compile Include="Feeds\StandardBuilders\CorePartsFeedItemBuilder.cs" />
|
||||||
<Compile Include="Feeds\IFeedFormatter.cs" />
|
<Compile Include="Feeds\IFeedFormatter.cs" />
|
||||||
|
Reference in New Issue
Block a user